Tags Input
A component that allows users to add tags to an input field.
Anatomy
<TagsInput.Root>
<TagsInput.Label />
<TagsInput.Control>
<TagsInput.Item>
<TagsInput.ItemPreview>
<TagsInput.ItemText />
<TagsInput.ItemDeleteTrigger />
</TagsInput.ItemPreview>
<TagsInput.ItemInput />
</TagsInput.Item>
<TagsInput.Input />
<TagsInput.ClearTrigger />
</TagsInput.Control>
<TagsInput.HiddenInput />
</TagsInput.Root>
Examples
Controlled
Pass value and update it in onValueChange to manage the tags array yourself.
Controlled Input Value
Use inputValue and onInputValueChange to control the text field on its own, for example to clear or pre-fill it.
Root Provider
Use RootProvider with the useTagsInput hook to access the state and methods from outside the component.
Field
Wrap the tags input in Field to wire up the label, helper text, error text and ARIA attributes.
Max Tags
Set max to limit the number of tags (default Infinity). Once the limit is reached, new tags are rejected unless
allowOverflow is true.
Custom Delimiter
Pass a regex to delimiter to split tags on several characters. By default, only Enter creates a tag.
Disabled
Set disabled to block adding, removing and editing tags.
Invalid
Set invalid to mark the tags input as invalid.
Max Length
Set maxLength to cap the characters per tag.
Read-only
Set readOnly to show tags without allowing changes.
Validation
validate runs before a tag is added and decides whether to accept it. Use it to prevent duplicates or check the data
type.
Blur behavior
blurBehavior sets what happens when the input loses focus:
add: Adds the tag to the list of tags.clear: Clears the tags input value.
Paste behavior
Set addOnPaste to add tags from pasted text. The pasted value is:
- checked against
validate - split by
delimiter
Disable Editing
Tags are editable by default: double-click a tag, or focus it and press Enter. Pass editable={false} to
turn this off.
Programmatic Control
Use useTagsInput with RootProvider to call API methods like addValue(), setValue() and clearValue().
Sanitize Value
sanitizeValue normalizes every new tag before it's added, for example to trim whitespace or lowercase it.
Combobox
Combine TagsInput with Combobox for autocomplete. Share IDs between both components and compose the inputs with
asChild.
Guides
Navigation
When the input is empty or the caret is at the start, use the left and right arrow keys to move through tags. When a tag has visual focus:
- Enter or double-click puts it in edit mode. Press Enter again to commit.
- Delete or Backspace deletes it.
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
addOnPaste | false | booleanWhether to add a tag when you paste values into the tag input |
allowDuplicates | false | booleanWhether to allow duplicate tags. |
allowOverflow | booleanWhether to allow tags to exceed max. In this case, we'll attach `data-invalid` to the root | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
autoFocus | booleanWhether the input should be auto-focused | |
blurBehavior | 'clear' | 'add'The behavior of the tags input when the input is blurred - `"add"`: add the input value as a new tag - `"clear"`: clear the input value | |
defaultInputValue | stringThe initial tag input value when rendered. Use when you don't need to control the tag input value. | |
defaultValue | string[]The initial tag value when rendered. Use when you don't need to control the tag value. | |
delimiter | ',' | string | RegExpThe character that serves has: - event key to trigger the addition of a new tag - character used to split tags when pasting into the input |
disabled | booleanWhether the tags input should be disabled | |
editable | false | booleanWhether a tag can be edited after creation, by pressing `Enter` or double clicking. |
form | stringThe associate form of the underlying input element. | |
id | stringThe unique identifier of the machine. | |
ids | ElementIdsThe ids of the elements in the tags input. Useful for composition. | |
inputValue | stringThe controlled tag input's value | |
invalid | booleanWhether the tags input is invalid | |
max | Infinity | numberThe max number of tags |
maxLength | numberThe max length of the input. | |
name | stringThe name attribute for the input. Useful for form submissions | |
onFocusOutside | (event: FocusOutsideEvent) => voidFunction called when the focus is moved outside the component | |
onHighlightChange | (details: HighlightChangeDetails) => voidCallback fired when a tag is highlighted by pointer or keyboard navigation | |
onInputValueChange | (details: InputValueChangeDetails) => voidCallback fired when the input value is updated | |
onInteractOutside | (event: InteractOutsideEvent) => voidFunction called when an interaction happens outside the component | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => voidFunction called when the pointer is pressed down outside the component | |
onValueChange | (details: ValueChangeDetails) => voidCallback fired when the tag values is updated | |
onValueInvalid | (details: ValidityChangeDetails) => voidCallback fired when the max tag count is reached or the `validateTag` function returns `false` | |
placeholder | stringThe placeholder text for the input | |
readOnly | booleanWhether the tags input should be read-only | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<TagsInputRootState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
required | booleanWhether the tags input is required | |
sanitizeValue | (value) => value.trim() | (value: string) => stringFunction to sanitize the tag value before adding. Useful for trimming whitespace, normalizing case, or stripping special characters. |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
validate | (details: ValidateArgs) => booleanReturns a boolean that determines whether a tag can be added. Useful for preventing duplicates or invalid tag values. | |
value | string[]The controlled tag value |
| Attribute | Description |
|---|---|
[data-tags-input-root] | <uid> |
[data-invalid] | Present when invalid |
[data-readonly] | Present when read-only |
[data-disabled] | Present when disabled |
[data-focus] | Present when focused |
[data-empty] | Present when the content is empty |
ClearTrigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-clear-trigger] | <uid> |
[data-readonly] | Present when read-only |
Control
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-control] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
HiddenInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Input
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-input] | <uid> |
[data-invalid] | Present when invalid |
[data-readonly] | Present when read-only |
[data-empty] | Present when the content is empty |
ItemDeleteTrigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-item-delete-trigger] | <uid> |
[data-disabled] | Present when disabled |
[data-highlighted] | Present when highlighted |
ItemInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
ItemPreview
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-item-preview] | <uid> |
[data-value] | The value of the item |
[data-disabled] | Present when disabled |
[data-highlighted] | Present when highlighted |
Item
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
index | string | number | |
value | string | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
disabled | boolean | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<TagsInputItemState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-item] | <uid> |
[data-value] | The value of the item |
[data-disabled] | Present when disabled |
ItemText
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-item-text] | <uid> |
[data-disabled] | Present when disabled |
[data-highlighted] | Present when highlighted |
Label
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-tags-input-label] | <uid> |
[data-disabled] | Present when disabled |
[data-invalid] | Present when invalid |
[data-readonly] | Present when read-only |
[data-required] | Present when required |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseTagsInputReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<TagsInputRootProviderState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Context
API
| Property | Type |
|---|---|
empty | booleanWhether the tags are empty |
inputValue | stringThe value of the tags entry input. |
value | string[]The value of the tags as an array of strings. |
valueAsString | stringThe value of the tags as a string. |
count | numberThe number of the tags. |
atMax | booleanWhether the tags have reached the max limit. |
highlightedValue | string | nullThe value of the highlighted tag. |
setValue | (value: string[]) => voidFunction to set the value of the tags. |
clearValue | (id?: string) => voidFunction to clear the value of the tags. |
addValue | (value: string) => voidFunction to add a tag to the tags. |
setValueAtIndex | (index: number, value: string) => voidFunction to set the value of a tag at the given index. |
setInputValue | (value: string) => voidFunction to set the value of the tags entry input. |
clearInputValue | VoidFunctionFunction to clear the value of the tags entry input. |
focus | VoidFunctionFunction to focus the tags entry input. |
getItemState | (props: ItemProps) => ItemStateReturns the state of a tag |
getRootState | () => RootStateReturns the state of the root |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
ArrowLeft | Moves focus to the previous tag item |
ArrowRight | Moves focus to the next tag item |
Backspace | Deletes the tag item that has visual focus or the last tag item |
Enter | When a tag item has visual focus, it puts the tag in edit mode. When the input has focus, it adds the value to the list of tags |
Delete | Deletes the tag item that has visual focus |
Control + V | When `addOnPaste` is set. Adds the pasted value as a tags |