Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Tags Input for Svelte UI SDK

Type a word, press Enter, and it becomes a removable chip. A tags input for Svelte on UI SDK, with paste splitting, a tag limit and validation.

On this page9 sections

A tags input turns each entry someone types into a chip they can remove, and holds the chips as an array of strings. Enter or a comma adds the typed text as a tag. Reach for it when the values are free text with no fixed list behind them: labels on an issue, skills on a profile, the people on an invite.

When the values come from a list you already have, such as team members or countries, a Combobox with several selections lets people search that list instead.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Tags Input anatomy: label, item, item text, item delete trigger, input, clear trigger

Usage

<script lang="ts">
import { Field, FieldLabel } from "@/components/ui/field"
import {
  TagsInput, TagsInputContext, TagsInputItem,
} from "@/components/ui/tags-input"
const defaultValue = ["React", "Solid", "Vue", "Svelte"]
const id = $props.id()
</script>
<Field class="w-full max-w-sm"><FieldLabel for={id}>Frameworks</FieldLabel><TagsInput class="w-full" defaultValue={defaultValue} id={id} placeholder="Add framework"><TagsInputContext>{#snippet render(__api1)}{@const { value } = __api1()}{#each value as tag, index (tag)}<TagsInputItem index={index} value={tag}>{tag}</TagsInputItem>{/each}{/snippet}</TagsInputContext></TagsInput></Field>

Accessibility

UI SDK has no tags input, so this one is built on its token input: the tags are chips inside one editable text box, which has the textbox role, and the caret steps over a whole chip in one press. Focus never leaves the text box, and adding or removing a tag is announced. Each remove button is hidden from screen readers and takes no focus, since Backspace does its job. A <label> can't name an editable element on its own, so set an id on TagsInput and point the FieldLabel at it, as every demo on this page does: the component then names the text box after the label.

Arrow Left
Moves the caret back one character, or past a whole tag in one press. Text typed between two tags becomes a tag in that spot.
Arrow Right
Moves the caret on one character, or past a whole tag in one press. Right to left, the two arrows swap.
Backspace
With the caret right after a tag, highlights it, and the next press deletes it. Otherwise deletes the character before the caret.
Delete
With the caret right before a tag, highlights it, and the next press deletes it.
Enter
Adds the typed text as a tag, where it was typed, without submitting the form. When a tag is highlighted and editable is set, puts that tag in edit mode instead, and in edit mode, saves the change.
Escape
While a tag is being edited, cancels the edit and keeps its old text.
Ctrl+V
Pastes into the text box. With addOnPaste set, splits the pasted text on the delimiter and adds each piece as a tag.
Ctrl+Z
Undoes the last change, such as a tag deleted by mistake.
Tab
Moves focus from the text box to the clear button, then on to the next control.
Space
When focus is on the clear button, removes every tag and moves focus back to the text box.

Demos

Blur behavior

With blurBehavior set to add, text left in the box becomes a tag when focus moves away, so someone who types a framework and tabs to the next field doesn't lose it. Without the prop that text stays in the box but isn't part of the value, and clear empties the box instead. Pick add for forms people tab through.

Preview width

Combobox

A search field under the tags input opens a list of eight frameworks. Typing there filters it, picking a suggestion adds it as a tag through addValue, and frameworks already chosen drop out of the list. The tags input's own text box holds the tags, so it can't search, but it still takes any text. Use this when most tags are known in advance but people may still invent one.

Preview width

Controlled

The tags, starting at React and Solid, are held in the page's own state, passed in as value and replaced from onValueChange each time one is added or removed. Control the list when something besides the field changes it, such as a reset button, a saved draft or a URL parameter. When nothing else touches it, defaultValue is enough.

Preview width

Controlled input value

Two buttons above the field write React into the text box or empty it, through inputValue and onInputValueChange, while the tags stay on defaultValue. Hold the typed text in state when something outside the box fills it, such as a suggested tag or a search query. React is already a tag here, so Enter after the first button adds nothing.

Preview width

Custom delimiter

Here delimiter is a regular expression that matches a comma or any whitespace, so a space after a word ends the tag just as a comma would. Use it for one-word tags such as hashtags or package names, where people expect the space bar to commit. Keep the default comma when a tag can hold a space, like a city or a full name.

Preview width

Disable editing

editable is set to false here, which is also what TagsInput does when you leave the prop out, so a tag can be removed but never rewritten in place. useTagsInput defaults to true, where a double click or Enter on a highlighted tag turns it back into text. Pass editable when people should fix a typo without deleting the tag, as in Editable.

Preview width

Disabled

disabled on TagsInput dims the box and turns off the text field, the remove buttons and the clear button, so the three frameworks can be read but not changed. The Field around it is marked disabled as well. Use it when a setting is locked by plan or by permission, and say why next to the field.

Preview width

Editable

editable turns on what TagsInput leaves off by default: a double click on a tag, or Enter on a highlighted one, turns it back into text. Enter saves the change and Escape cancels it. The hint under the field says so, since nothing on a tag shows it can be edited. Use it where typos are likely, such as skills or keywords typed in a hurry.

Preview width

Field

The box sits under a FieldLabel, with a FieldDescription below it that says to press Enter, a hint that stays on screen while someone types, unlike the placeholder, which goes away once there are tags. Use this layout when the tags are one question in a longer form.

Preview width

Form

The tags input sits in a form under the name labels, with Save and Reset buttons. Save reads the form's data, where the hidden input holds the tags as one string, and shows it under the buttons as "bug, frontend". Reset puts back the tags the field started with. Use this when the tags go out with an ordinary form post.

Preview width

Invalid

invalid on TagsInput and on its Field sets aria-invalid on the text field, and the border turns to the error color. Set it after a failed submit, when the list as a whole breaks a rule the field can't check, such as needing at least one tag. Put the reason in words under the field, because a red border doesn't say what to fix.

Preview width

Max length

maxLength is 10 here, so the text box stops taking characters at the tenth and cuts a longer paste to fit, and the label states the limit up front. Use it for short tokens such as codes or slugs, where a pasted sentence should never become one tag. It limits the length of each tag, while max limits how many there are.

Preview width

Max tags

max is 3 and two frameworks are already in, so one more is accepted, and after that Enter does nothing and the extra text waits in the box. Use a hard cap when the meaning is up to some number, such as three reviewers or five labels. The field shows no message at the limit, so state it in the label, as this one does.

Preview width

Max with overflow

The cap is 3 again and the field starts full, but allowOverflow lets a fourth tag in. Past the cap, the root gets data-invalid, the text field gets aria-invalid and the error border, and onValueInvalid fires. Prefer this over a hard cap when people should see what they added and choose which tag to drop.

Preview width

Paste behavior

addOnPaste is on and the field starts empty, so pasting the line React, Vue, Svelte creates three tags, split on the comma that delimiter names. Turn it on when values arrive in bulk, such as a list of invitees. Leave it off when a comma can belong inside one tag, because a paste would break that tag apart.

Preview width

Sanitize value

sanitizeValue trims and lowercases the text before it becomes a tag, so typing React or REACT adds nothing, since both turn into the react already in the list. Use a sanitizer for emails, slugs and handles, where case and stray spaces shouldn't make two tags. Passing one replaces the default, which only trims, so keep the trim in yours.

Preview width

Size lg

With size set to lg, the tags grow to 28 pixels with larger text and the box to at least 36 pixels. Use it when the tags input is the main control on a screen, such as the recipients line of a message composer. Adding, editing and removing tags work the same at every size.

Preview width

Size md

md is the default, so this field looks the same as one with no size: 24-pixel tags in a box at least 32 pixels tall. It is the size for an ordinary form, beside inputs and selects at their own default size. Spell it out only when the size comes from a variable, such as a density setting.

Preview width

Size sm

With size set to sm, the tags shrink to 20 pixels and the box to a 28-pixel minimum, for a filter bar or a table toolbar where vertical room is tight. Put it beside other small controls, because a small tags field next to default-height buttons looks out of line.

Preview width

Validation

validate gets the tags so far and the text about to become a tag, and this one accepts three or more letters, digits or hyphens that aren't in the list yet. A rejected entry is cleared from the box, so onValueInvalid puts the reason under the field in a FieldError and marks it invalid until the next keystroke. Pair every validator with a message like this one.

Preview width

FAQ

When should I use a Tags Input instead of a multi-select Combobox?

Use a tags input when people type values that don't exist anywhere yet: labels on an issue, keywords, email addresses. Anything typed can become a tag, subject to your validate function.

A Combobox that allows several selections fits when the values come from a list you already have, such as team members or countries, because people search it and can't pick something that isn't there. When most values are known but some will be new, the Combobox demo puts suggestions on a tags input.

How do people add and remove tags in a Tags Input?

Enter adds the typed text as a tag, and so does typing the delimiter, a comma unless you change delimiter. Each tag has its own remove button, and the clear button at the end of the box removes them all.

From the keyboard, Backspace right after a tag highlights it, and the next press deletes it, so holding the key empties the list from the end. ArrowLeft and ArrowRight move the caret past a whole tag in one press, and text typed between two tags becomes a tag in that spot.

How do I control the Tags Input value in Svelte?

Keep the tags in a $state array and bind it with bind:value, or pass value and update it in onValueChange.

The change hands over the new array first and its details second. The Controlled demo passes value and updates it this way.

defaultValue is enough when nothing but the field changes the list. Control it when a reset button, a saved draft or another field has to change the tags.

How do I limit the number of tags or stop duplicates in a Tags Input?

Set max to the most tags the list may hold. At the cap, Enter and the delimiter do nothing and the typed text waits in the box, as in Max tags, or add allowOverflow to let more in and mark the field invalid instead.

Duplicates are refused with no prop at all: a tag already in the list is dropped and the box is cleared. Set allowDuplicates if the same text may appear twice. maxLength caps the characters in each tag, not the number of tags.

How do I paste a comma-separated list into a Tags Input?

Add addOnPaste to TagsInput. A pasted line is split on the delimiter, each piece goes through sanitizeValue, and every piece becomes a tag, as the Paste behavior demo shows. Without the prop, the whole paste lands in the box and turns into one tag on Enter.

Your validate function runs once, on the whole pasted text rather than on each piece, and if it refuses, nothing from the paste is added. A piece already in the list is dropped, and a paste isn't held to max: it can push the list past the cap, which marks the field invalid. Without addOnPaste, a paste longer than maxLength is cut to fit.

How does a Tags Input submit its tags with a form?

Give TagsInput a name, and a hidden input submits the tags as one string joined by a comma and a space, so React and Vue arrive as "React, Vue", as the Form demo shows. Split it on the server, or, if a tag can contain a comma, keep the array in state and send it as JSON yourself.

The hidden input also follows the form: resetting the form puts back the tags the field started with. Set form to the form's id when the field sits outside the form element it belongs to.

How do I add autocomplete suggestions to a Tags Input?

Put an Autocomplete beside it, as the Combobox demo does, since the tags input's text box is an editor that holds the tags and can't also be the autocomplete's input. Create the tags input with useTagsInput and render it through TagsInputRootProvider. When the autocomplete's value changes, pass the picked item to addValue and keep the autocomplete's own value empty, so each pick becomes a tag.

Filter the chosen tags out of the suggestions, as the demo does, so nobody picks the same one twice. When every value must come from the list, a Combobox with several selections is simpler.

Why can't I edit a tag after adding it to the Tags Input?

TagsInput in components/ui/tags-input sets editable to false unless you pass it, although useTagsInput leaves it on. Set editable to true, and a double click on a tag or Enter on a highlighted one turns it back into text, as in the Editable demo.

Enter saves the change, and Escape or moving focus away cancels it. Saving an empty tag deletes it, and an edit that matches another tag leaves the old text in place. useTagsInput, which the Combobox demo uses, defaults editable to true, so its tags are editable already.

API Reference

TagsInput

data-slot="tags-input"

PropType
inputValue?string

The text being typed, to control it. Two-way with bind:inputValue.

defaultInputValue?string

The initial tag input value when rendered. Use when you don't need to control the tag input value.

Default
""
onInputValueChange?(details: TagsInputInputValueChangeDetails) → void

Callback fired when the input value is updated.

Type
(details: TagsInputInputValueChangeDetails) → void
Parameters

details – { inputValue: string }

Returns
void
value?string[]

The tags, to control them. Two-way with bind:value.

defaultValue?string[]

The tags at first, when uncontrolled. A form reset returns to them.

onValueChange?(details: TagsInputValueChangeDetails) → void

Called when the value changes.

Type
(details: TagsInputValueChangeDetails) → void
Parameters

details – { value: string[] }

Returns
void
addOnPaste?boolean

Whether pasted text becomes tags, split on the delimiter.

Default
false
allowDuplicates?boolean

Whether the same tag can be added twice.

Default
false
allowOverflow?boolean

Whether tags can go past max, which marks the input invalid.

autoFocus?boolean

Whether the input should be auto-focused.

blurBehavior?"add" | "clear"

What leaving the field does with the typed text: add it, or clear it.

Type
"add" | "clear"
children?Snippet

The tags, usually from TagsInputContext.

delimiter?string | RegExp

The key that adds the typed tag, and what a paste splits on.

Type
string | RegExp
Default
","
disabled?boolean

Whether it's disabled.

Default
false
editable?boolean

Whether double-clicking a tag, or Enter on a selected one, edits it.

Default
true
form?string

The id of a form to belong to, when the input isn't inside it.

id?string

The id the parts' ids start from.

ids?object

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

Type
{ clearBtn?: string; control?: string; hiddenInput?: string; input?: string; root?: string }
invalid?boolean

Whether it's invalid.

max?number

The most tags. A paste with addOnPaste can go past it, as on Ark, which marks the input invalid. The type's default: Infinity.

Default
Number.POSITIVE_INFINITY
maxLength?number

The longest text the field takes.

name?string

The name the tags submit under, joined with ", ".

onHighlightChange?(details: TagsInputHighlightChangeDetails) → void

Runs when Backspace or Delete selects a tag, before removing it.

Type
(details: TagsInputHighlightChangeDetails) → void
Parameters

details – { highlightedValue: string | null }

Returns
void
onValueInvalid?(details: TagsInputValidityChangeDetails) → void

Runs when a tag is refused: past max, or by validate.

Type
(details: TagsInputValidityChangeDetails) → void
Parameters

details – { reason: "rangeOverflow" | "invalidTag" }

Returns
void
placeholder?string

Shown while there are no tags and no text.

readOnly?boolean

Whether the tags input should be read-only.

Default
false
required?boolean

Whether a value is required to submit its form.

sanitizeValue?(value: string) → string

Cleans a tag before it's added.

Default
(value) => value.trim()
Parameters

value – string

Returns
string
showClear?boolean

Whether to show the clear button.

Default
true
size?TagsInputSize

The size of the control.

Default
"md"
translations?object

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

Type
{ clearTriggerLabel?: string }
validate?(details: TagsInputValidateArgs) → boolean

Whether a tag can be added.

Type
(details: TagsInputValidateArgs) → boolean
Parameters

details – { inputValue: string; value: string[] }

Returns
boolean

TagsInputRootProvider

data-slot="tags-input-root-provider"

PropType
valueobject

The api from useTagsInput.

Type
{ addValue: (value: string) → void; atMax: boolean; clearInputValue: () → void; clearValue: (value?: string) → void; count: number; empty: boolean; focus: () → void; getItemState: (props: { index: number | string; value: string; disabled?: boolean; }) → TagsInputItemState; inputValue: string; setInputValue: (value: string) → void; setValue: (value: string[]) → void; setValueAtIndex: (index: number, value: string) → void; value: string[]; valueAsString: string }
showClear?boolean

Whether to show the clear button.

Default
true
size?TagsInputSize

The size of the control.

Default
"md"

TagsInputControl

<div> data-slot="token-input-editor"

PropType
showClear?boolean

Whether to show the clear button.

Default
true
size?TagsInputSize

The size of the control.

Default
"md"
class?string

Merged with the part's classes.

AttributeType
data-disabled?string
data-invalid?string
data-readonly?string
data-size?string

Mirrors size.

TagsInputItem

<span> data-slot="token-input-token"

PropType
indexnumber | string

The tag's position, as the other bases take it.

Type
number | string
valuestring

The tag it shows.

disabled?boolean

Whether the item is disabled.

showDelete?boolean

Whether to show the delete button.

Default
true
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the input is disabled, by its own prop, a field or a <fieldset>.

data-pending?string

Present while the token loads. Also on its host.

data-readonly?string

Present when the input is read-only.

data-selected?string

Present while the token is in the selection, or selected for removal. Also on its host.

data-token-type"file"

The token's type, like file or mention. Also on its host.

data-unresolved?string

Present while the token needs the user, like an ambiguous mention. Also on its host.

TagsInputItemPreview

<span> data-slot="tags-input-item-preview"

PropType
class?string

Merged with the part's classes.

TagsInputItemText

<span> data-slot="tags-input-item-text"

PropType
class?string

Merged with the part's classes.

TagsInputItemDeleteTrigger

<span> data-slot="token-input-token-remove"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the input is disabled, by its own prop, a field or a <fieldset>.

data-readonly?string

Present when the input is read-only.

data-size"icon-xs"
Default
"icon-xs"
data-variant"ghost"
Default
"ghost"

TagsInputItemInput

<input> data-slot="tags-input-item-input"

PropType
class?string

Merged with the part's classes.

TagsInputInput

<div> data-slot="token-input-editor"

PropType
placeholder?string

Text shown while the input is empty, through aria-placeholder. Show it with data-empty:before:content-[attr(aria-placeholder)].

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the input is disabled, by its own prop, a field or a <fieldset>.

data-empty?string

Present while the input holds nothing. Show the placeholder with it.

data-readonly?string

Present when the input is read-only.

TagsInputClearTrigger

PropType
clickEffect?boolean

Apply a click effect to the button.

Default
true
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.

Default
false
isLoading?boolean

Show a loading indicator.

Default
false
onclick?( event: MouseEvent, details?: ButtonActionDetails ) → unknown

Runs when the button is pressed. Return a Promise to keep it pending (data-pending) until it settles. details.source says how it was pressed; a wrapper passing the click on may leave it out.

Type
( event: MouseEvent, details?: ButtonActionDetails ) → unknown
Parameters

event – React.MouseEvent<HTMLButtonElement>

details? – ButtonActionDetails

Returns
unknown
pendingAnnouncement?string

What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Default
"Loading"
pill?boolean

Fully rounded corners.

Default
false
size?"icon-sm" | "icon-xs" | "sm" | "xs"

The size.

Type
"icon-sm" | "icon-xs" | "sm" | "xs"
Default
"xs"
type?"button" | "submit" | "reset"

The button's type: button by default, so it never submits its form.

Type
"button" | "submit" | "reset"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

The visual style.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"default"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

useTagsInput

PropType
inputValue?string

The text being typed, to control it. Two-way with bind:inputValue.

defaultInputValue?string
onInputValueChange?(details: TagsInputInputValueChangeDetails) → void
Type
(details: TagsInputInputValueChangeDetails) → void
Parameters

details – { inputValue: string }

Returns
void
value?string[]

The tags, to control them. Two-way with bind:value.

defaultValue?string[]

The tags at first, when uncontrolled. A form reset returns to them.

onValueChange?(details: TagsInputValueChangeDetails) → void

Called when the value changes.

Type
(details: TagsInputValueChangeDetails) → void
Parameters

details – { value: string[] }

Returns
void
addOnPaste?boolean

Whether pasted text becomes tags, split on the delimiter.

Default
false
allowDuplicates?boolean

Whether the same tag can be added twice.

Default
false
allowOverflow?boolean

Whether tags can go past max, which marks the input invalid.

autoFocus?boolean
blurBehavior?"add" | "clear"

What leaving the field does with the typed text: add it, or clear it.

Type
"add" | "clear"
delimiter?string | RegExp

The key that adds the typed tag, and what a paste splits on.

Type
string | RegExp
Default
","
disabled?boolean

Whether the put is disabled.

editable?boolean

Whether double-clicking a tag, or Enter on a selected one, edits it.

Default
true
form?string

The id of a form to belong to, when the input isn't inside it.

id?string

The id the parts' ids start from.

ids?object
Type
{ clearBtn?: string; control?: string; hiddenInput?: string; input?: string; root?: string }
invalid?boolean

Whether the put is invalid.

max?number

The most tags. A paste with addOnPaste can go past it, as on Ark, which marks the input invalid.

Default
Infinity
maxLength?number

The longest text the field takes.

name?string

The name the tags submit under, joined with ", ".

onHighlightChange?(details: TagsInputHighlightChangeDetails) → void

Runs when Backspace or Delete selects a tag, before removing it.

Type
(details: TagsInputHighlightChangeDetails) → void
Parameters

details – { highlightedValue: string | null }

Returns
void
onValueInvalid?(details: TagsInputValidityChangeDetails) → void

Runs when a tag is refused: past max, or by validate.

Type
(details: TagsInputValidityChangeDetails) → void
Parameters

details – { reason: "rangeOverflow" | "invalidTag" }

Returns
void
placeholder?string

Shown while there are no tags and no text.

readOnly?boolean
required?boolean

Whether a value is required to submit its form.

sanitizeValue?(value: string) → string

Cleans a tag before it's added.

Default
(value) => value.trim()
Parameters

value – string

Returns
string
translations?object
Type
{ clearTriggerLabel?: string }
validate?(details: TagsInputValidateArgs) → boolean

Whether a tag can be added.

Type
(details: TagsInputValidateArgs) → boolean
Parameters

details – { inputValue: string; value: string[] }

Returns
boolean

TagsInputContext