A text field that turns what you type into tags. Enter, comma or Tab adds a tag, pasting “a, b; c” adds several at once. Backspace in an empty field selects the last tag and a second Backspace removes it; the arrow keys, Home and End move between tags. Duplicates, a tag limit and your own validation show an error under the field, suggestions open as a listbox, and with name the tags are sent with an ordinary form.
Browser Support
Baseline Newly available2 featuresDetails
The text input grows with its text. Without it the input starts at 8ch and stretches to the end of the row, long text scrolls inside it.
Draws the focus ring around the whole box when the inner input has keyboard focus. Without it the border still changes colour through :focus-within.
Preview
Type to filter, or press ArrowDown to see all. Enter picks the highlighted option.
Installation
Usage
"use client"
import { useState } from "react"
import { Label } from "@/components/ui/label"
import { TagInput } from "@/components/ui/tag-input"
export function SkillsField() {
const [skills, setSkills] = useState(["React", "TypeScript"])
return (
<div className="grid gap-2">
<Label htmlFor="skills">Skills</Label>
<TagInput
id="skills"
value={skills}
onValueChange={setSkills}
suggestions={["Next.js", "Node.js", "PostgreSQL", "Figma"]}
placeholder="Add a skill"
/>
</div>
)
}Examples
Email recipients with validation
validate returns true or an error message. Rejected parts of a paste stay in the field so they can be fixed, valid ones become tags.
1 recipient. Try pasting “dev@acme.com; ops@acme.com, not-an-email”.
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/
<TagInput
id="recipients"
value={recipients}
onValueChange={setRecipients}
placeholder="name@company.com"
validate={(tag) =>
EMAIL.test(tag) ? true : `“${tag}” is not a valid email address`
}
/>Limit and form submission
maxTags stops adding at five. name renders one hidden input per tag, so FormData.getAll returns the list.
<form
onSubmit={(event) => {
event.preventDefault()
const topics = new FormData(event.currentTarget).getAll("topics")
}}
>
<TagInput
name="topics"
maxTags={5}
defaultValue={["css", "web platform"]}
placeholder="Add a topic"
/>
<Button type="submit">Save topics</Button>
</form>Disabled
Tags lose the remove buttons and the field cannot be focused.
<TagInput
aria-label="Labels"
defaultValue={["billing", "priority: high"]}
disabled
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string[] | - | Controlled list of tags |
| defaultValue | string[] | [] | Initial tags when uncontrolled |
| onValueChange | (value: string[]) => void | - | Called with the new list after a tag is added or removed |
| delimiters | string[] | ["Enter", ",", "Tab"] | Keys that add a tag. Single characters also split pasted text (comma, semicolon, new line and tab always do) |
| maxTags | number | - | Maximum number of tags |
| validate | (tag: string) => string | true | - | Return true to accept a tag or a message to reject it |
| caseSensitive | boolean | false | Treat “React” and “react” as different tags |
| suggestions | string[] | - | Options shown under the field while typing |
| name | string | - | Name for the hidden inputs, one per tag |
| placeholder | string | - | Shown while there are no tags |
| id | string | - | Id of the text input, for <label htmlFor> |
| disabled | boolean | false | Disables the field and hides the remove buttons |
| labels | Partial<TagInputLabels> | English | Texts for the errors, the live region and the remove buttons, to translate them |
| className | string | - | Additional CSS classes |
Features
- Screen readers. Added and removed tags are announced in a polite live region, the error is linked with aria-describedby, the suggestions are a combobox with aria-activedescendant.
- IME and mobile keyboards. Enter during composition does not add a tag, and a comma typed on a keyboard that reports “Unidentified” is caught in the change event.
- Roving focus. Only the text input is in the Tab order. Tags are reached with the arrows, so the field takes one Tab stop.
| Enter , Tab | Adds the typed text, or the highlighted suggestion |
| Backspace | In an empty field selects the last tag, on a tag removes it |
| Delete | Removes the selected tag |
| ← → Home End | Move between tags and back to the field |
| ↓ ↑ Esc | Open, move through and close the suggestions |