Template Input

A textarea for templates with {{variables}}: typing {{ suggests variables, inserted ones are highlighted and unknown ones are flagged.

@drift-ui/template-inputRegistry JSONOpen in v0
On this page

Installation

The CLI also installs the npm packages and registry items it builds on, and reads the style in your components.json to pick the Base UI or Radix build.

Usage

template-input-usage.tsx
import { TemplateInput } from "@/components/ui/template-input"

const variables = [
  { value: "first_name", label: "First name" },
  { value: "order_id", label: "Order number" },
]

export function SubjectField() {
  return <TemplateInput aria-label="Subject" variables={variables} defaultValue="Hi {{first_name}}, order {{order_id}} shipped" />
}

Examples

Controlled

The value is the template string itself, so any template engine can render it. This one fills in sample values with a regular expression.

Preview: Noor, your Team plan renews on March 3

Unknown variables

Variables that are not in the list get a wavy underline. findUnknownVariables returns their names, so a form can block saving until they are fixed.

Unknown variable: repository

Unknown: repository

API reference

TemplateInput

A textarea that suggests variables after {{. Arrow keys move through the list, Enter or Tab inserts {{value}}, and Escape closes it. Known variables are highlighted and unknown ones get a wavy underline.

Prop
valuestringControlled template.
defaultValuestringDefault""Initial value when uncontrolled.
onValueChange(value: string) => voidCalled when the template changes.
variablesRequiredMentionOption[]The variables that can be inserted, each { value, label, description? }. Typing after {{ filters on value and label.
classNamestringApplied to the bordered wrapper, which also sets the font for the text and its highlights.

Other props go to the <textarea>.

findUnknownVariables

findUnknownVariables(text: string, variables: MentionOption[]): string[]

Names used as {{name}} in the text that are not in variables, without duplicates. Exported from the lib file.

Source

components/ui/template-input.tsx