Tags Input

A component that allows users to add tags to an input field.

Loading...

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

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.

AttributeDefaultType
addOnPastefalse
boolean

Whether to add a tag when you paste values into the tag input

allowDuplicatesfalse
boolean

Whether to allow duplicate tags.

allowOverflow
boolean

Whether to allow tags to exceed max. In this case, we'll attach `data-invalid` to the root

asChild
boolean

Use 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
boolean

Whether 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
string

The 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 | RegExp

The 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
boolean

Whether the tags input should be disabled

editablefalse
boolean

Whether a tag can be edited after creation, by pressing `Enter` or double clicking.

form
string

The associate form of the underlying input element.

id
string

The unique identifier of the machine.

ids
ElementIds

The ids of the elements in the tags input. Useful for composition.

inputValue
string

The controlled tag input's value

invalid
boolean

Whether the tags input is invalid

maxInfinity
number

The max number of tags

maxLength
number

The max length of the input.

name
string

The name attribute for the input. Useful for form submissions

onFocusOutside
(event: FocusOutsideEvent) => void

Function called when the focus is moved outside the component

onHighlightChange
(details: HighlightChangeDetails) => void

Callback fired when a tag is highlighted by pointer or keyboard navigation

onInputValueChange
(details: InputValueChangeDetails) => void

Callback fired when the input value is updated

onInteractOutside
(event: InteractOutsideEvent) => void

Function called when an interaction happens outside the component

onPointerDownOutside
(event: PointerDownOutsideEvent) => void

Function called when the pointer is pressed down outside the component

onValueChange
(details: ValueChangeDetails) => void

Callback fired when the tag values is updated

onValueInvalid
(details: ValidityChangeDetails) => void

Callback fired when the max tag count is reached or the `validateTag` function returns `false`

placeholder
string

The placeholder text for the input

readOnly
boolean

Whether 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
boolean

Whether the tags input is required

sanitizeValue(value) => value.trim()
(value: string) => string

Function to sanitize the tag value before adding. Useful for trimming whitespace, normalizing case, or stripping special characters.

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

validate
(details: ValidateArgs) => boolean

Returns a boolean that determines whether a tag can be added. Useful for preventing duplicates or invalid tag values.

value
string[]

The controlled tag value

AttributeDescription
[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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-tags-input-clear-trigger]<uid>
[data-readonly]Present when read-only

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-tags-input-item-delete-trigger]<uid>
[data-disabled]Present when disabled
[data-highlighted]Present when highlighted

ItemInput

Defines the <input> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
index
string | number

value
string

asChild
boolean

Use 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.

AttributeDescription
[data-tags-input-item]<uid>
[data-value]The value of the item
[data-disabled]Present when disabled

ItemText

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-tags-input-item-text]<uid>
[data-disabled]Present when disabled
[data-highlighted]Present when highlighted

Label

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
value
UseTagsInputReturn

asChild
boolean

Use 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

PropertyType
empty
boolean

Whether the tags are empty

inputValue
string

The value of the tags entry input.

value
string[]

The value of the tags as an array of strings.

valueAsString
string

The value of the tags as a string.

count
number

The number of the tags.

atMax
boolean

Whether the tags have reached the max limit.

highlightedValue
string | null

The value of the highlighted tag.

setValue
(value: string[]) => void

Function to set the value of the tags.

clearValue
(id?: string) => void

Function to clear the value of the tags.

addValue
(value: string) => void

Function to add a tag to the tags.

setValueAtIndex
(index: number, value: string) => void

Function to set the value of a tag at the given index.

setInputValue
(value: string) => void

Function to set the value of the tags entry input.

clearInputValue
VoidFunction

Function to clear the value of the tags entry input.

focus
VoidFunction

Function to focus the tags entry input.

getItemState
(props: ItemProps) => ItemState

Returns the state of a tag

getRootState
() => RootState

Returns the state of the root

Accessibility

Keyboard Support

KeyDescription
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