GooseUI

Components

Tag Input

New

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 available
2 features
Details
field-sizing

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.

Your browser:
:has()

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.

Your browser:

Preview

ReactTypeScript

Type to filter, or press ArrowDown to see all. Enter picks the highlighted option.

Installation

npx shadcn@latest add @gooseui/tag-input
pnpm dlx shadcn@latest add @gooseui/tag-input
yarn dlx shadcn@latest add @gooseui/tag-input
bunx --bun shadcn@latest add @gooseui/tag-input

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.

maria.lopez@northwind.io

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.

cssweb platform
<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.

billingpriority: high
<TagInput
  aria-label="Labels"
  defaultValue={["billing", "priority: high"]}
  disabled
/>

Props

PropTypeDefaultDescription
valuestring[]-Controlled list of tags
defaultValuestring[][]Initial tags when uncontrolled
onValueChange(value: string[]) => void-Called with the new list after a tag is added or removed
delimitersstring[]["Enter", ",", "Tab"]Keys that add a tag. Single characters also split pasted text (comma, semicolon, new line and tab always do)
maxTagsnumber-Maximum number of tags
validate(tag: string) => string | true-Return true to accept a tag or a message to reject it
caseSensitivebooleanfalseTreat “React” and “react” as different tags
suggestionsstring[]-Options shown under the field while typing
namestring-Name for the hidden inputs, one per tag
placeholderstring-Shown while there are no tags
idstring-Id of the text input, for <label htmlFor>
disabledbooleanfalseDisables the field and hides the remove buttons
labelsPartial<TagInputLabels>EnglishTexts for the errors, the live region and the remove buttons, to translate them
classNamestring-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.
Keyboard
Enter , TabAdds the typed text, or the highlighted suggestion
BackspaceIn an empty field selects the last tag, on a tag removes it
DeleteRemoves the selected tag
← → Home EndMove between tags and back to the field
↓ ↑ EscOpen, move through and close the suggestions