{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tags-input",
  "type": "registry:ui",
  "title": "Tags Input",
  "description": "A field that collects several short values as removable tags: typed and added with Enter or a comma, pasted as a list, picked from suggestions, removed by their button or with Backspace, each change said aloud.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/announce",
    "@rivier-ui/cn",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/tags-input.tsx",
      "type": "registry:ui",
      "target": "components/ui/tags-input.tsx",
      "content": "\"use client\";\n/**\n * @name tags-input\n * @category ui\n * @description A field that collects several short values as removable tags: typed and added with Enter or a comma, pasted as a list, picked from suggestions, removed by their button or with Backspace, each change said aloud.\n * @when Free-form lists of words: labels on a note, email addresses to invite, skills. For choosing from a fixed list use combobox or chips; for one value use input.\n * @example\n * <TagsInput aria-label=\"Labels\" defaultValue={[\"urgent\"]} suggestions={[\"design\", \"bug\", \"later\"]} name=\"labels\" />\n */\nimport { X } from \"lucide-react\";\nimport { useId, useRef, useState, type ComponentProps, type KeyboardEvent } from \"react\";\nimport { announce } from \"@/lib/announce\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\nexport type TagsInputLabels = {\n  remove: (tag: string) => string;\n  added: (tag: string) => string;\n  removed: (tag: string) => string;\n  rejected: (tag: string) => string;\n  full: string;\n  suggestions: string;\n};\n\nconst LABELS: TagsInputLabels = {\n  remove: (tag) => `Remove ${tag}`,\n  added: (tag) => `${tag} added`,\n  removed: (tag) => `${tag} removed`,\n  rejected: (tag) => `${tag} cannot be added`,\n  full: \"No more can be added\",\n  suggestions: \"Suggestions\",\n};\n\nexport type TagsInputProps = Omit<ComponentProps<\"input\">, \"value\" | \"defaultValue\" | \"onChange\" | \"size\"> & {\n  value?: readonly string[];\n  defaultValue?: readonly string[];\n  onValueChange?: (value: readonly string[]) => void;\n  /** Values offered as the person types; any other value can still be typed. */\n  suggestions?: readonly string[];\n  /** The most tags. */\n  max?: number;\n  /** Whether a typed value may be added: return false to refuse it (it is said as refused). */\n  validate?: (tag: string) => boolean;\n  /** Characters that end a tag as it is typed, besides Enter. Default [\",\"]. */\n  separators?: readonly string[];\n  labels?: Partial<TagsInputLabels>;\n};\n\nexport function TagsInput({\n  value,\n  defaultValue = [],\n  onValueChange,\n  suggestions = [],\n  max,\n  validate,\n  separators = [\",\"],\n  labels: given,\n  name,\n  className,\n  disabled,\n  id,\n  \"aria-label\": ariaLabel,\n  \"aria-labelledby\": ariaLabelledBy,\n  ...props\n}: TagsInputProps) {\n  const labels = { ...LABELS, ...given };\n  const [tags, setTags] = useControllableState<readonly string[]>({ value, defaultValue, onChange: onValueChange });\n  const [draft, setDraft] = useState(\"\");\n  const [active, setActive] = useState(-1);\n  const inputRef = useRef<HTMLInputElement>(null);\n  const listId = useId();\n  const ownId = useId();\n  const inputId = id ?? ownId;\n  const open = draft.trim().length > 0;\n  const matches = open ? suggestions.filter((s) => s.toLocaleLowerCase().includes(draft.trim().toLocaleLowerCase()) && !tags.includes(s)).slice(0, 8) : [];\n\n  const add = (raw: string): void => {\n    const parts = raw\n      .split(new RegExp(`[${[\"\\n\", ...separators].map((s) => s.replace(/[-\\\\^\\]]/g, \"\\\\$&\")).join(\"\")}]`))\n      .map((part) => part.trim())\n      .filter(Boolean);\n    let next = [...tags];\n    for (const part of parts) {\n      if (next.includes(part)) continue;\n      if (max !== undefined && next.length >= max) {\n        announce(labels.full);\n        break;\n      }\n      if (validate && !validate(part)) {\n        announce(labels.rejected(part), \"assertive\");\n        continue;\n      }\n      next = [...next, part];\n      announce(labels.added(part));\n    }\n    setTags(next);\n    setDraft(\"\");\n    setActive(-1);\n  };\n  const remove = (tag: string): void => {\n    setTags(tags.filter((t) => t !== tag));\n    announce(labels.removed(tag));\n    inputRef.current?.focus();\n  };\n  const onKeyDown = (event: KeyboardEvent<HTMLInputElement>): void => {\n    if (event.key === \"ArrowDown\" && matches.length > 0) {\n      event.preventDefault();\n      setActive((a) => (a + 1) % matches.length);\n    } else if (event.key === \"ArrowUp\" && matches.length > 0) {\n      event.preventDefault();\n      setActive((a) => (a <= 0 ? matches.length - 1 : a - 1));\n    } else if (event.key === \"Enter\") {\n      if (!draft.trim() && active < 0) return;\n      event.preventDefault();\n      add(active >= 0 ? (matches[active] ?? draft) : draft);\n    } else if (event.key === \"Escape\" && open) {\n      event.preventDefault();\n      setDraft(\"\");\n      setActive(-1);\n    } else if (event.key === \"Backspace\" && draft === \"\" && tags.length > 0) {\n      event.preventDefault();\n      const last = tags[tags.length - 1];\n      if (last) remove(last);\n    }\n  };\n\n  return (\n    <div data-slot=\"tags-input\" className={cn(\"relative w-full min-w-0\", className)}>\n      <div\n        className={cn(\n          \"flex min-h-(--control-h) w-full flex-wrap items-center gap-1.5 rounded-control border border-input bg-background px-2 py-1.5 text-sm focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-ring\",\n          disabled && \"pointer-events-none opacity-50\",\n        )}\n      >\n        <ul className=\"contents\" aria-label={ariaLabel}>\n          {tags.map((tag) => (\n            <li key={tag} data-slot=\"tags-input-tag\" className=\"inline-flex max-w-full items-center gap-1 rounded-control bg-muted ps-2 text-sm\">\n              <span className=\"truncate\" dir=\"auto\">\n                {tag}\n              </span>\n              <button\n                type=\"button\"\n                aria-label={labels.remove(tag)}\n                disabled={disabled}\n                onClick={(event) => {\n                  event.stopPropagation();\n                  remove(tag);\n                }}\n                className=\"flex size-7 items-center justify-center rounded-control text-muted-foreground hover:bg-accent hover:text-foreground pointer-coarse:size-9\"\n              >\n                <X aria-hidden=\"true\" className=\"size-3.5\" />\n              </button>\n            </li>\n          ))}\n        </ul>\n        <input\n          ref={inputRef}\n          id={inputId}\n          role=\"combobox\"\n          aria-expanded={matches.length > 0}\n          aria-controls={listId}\n          aria-autocomplete=\"list\"\n          aria-activedescendant={active >= 0 ? `${listId}-${active}` : undefined}\n          aria-label={ariaLabel}\n          aria-labelledby={ariaLabelledBy}\n          value={draft}\n          disabled={disabled}\n          onChange={(event) => {\n            const text = event.target.value;\n            if (separators.some((s) => text.endsWith(s))) add(text);\n            else {\n              setDraft(text);\n              setActive(-1);\n            }\n          }}\n          onPaste={(event) => {\n            const text = event.clipboardData.getData(\"text\");\n            if (/[\\n,]/.test(text)) {\n              event.preventDefault();\n              add(text);\n            }\n          }}\n          onBlur={() => {\n            if (draft.trim()) add(draft);\n          }}\n          onKeyDown={onKeyDown}\n          className=\"h-7 min-w-24 flex-1 bg-transparent outline-none placeholder:text-muted-foreground\"\n          {...props}\n        />\n      </div>\n      <ul\n        id={listId}\n        role=\"listbox\"\n        aria-label={labels.suggestions}\n        hidden={matches.length === 0}\n        className=\"absolute inset-x-0 top-full z-50 mt-1 max-h-60 overflow-auto rounded-surface border bg-popover p-1 text-sm\"\n      >\n        {matches.map((match, index) => (\n          <li\n            key={match}\n            id={`${listId}-${index}`}\n            role=\"option\"\n            aria-selected={index === active}\n            onPointerDown={(event) => {\n              event.preventDefault();\n              add(match);\n            }}\n            className={cn(\"cursor-pointer rounded-control px-2 py-2\", index === active && \"bg-accent\")}\n          >\n            {match}\n          </li>\n        ))}\n      </ul>\n      {name ? tags.map((tag) => <input key={tag} type=\"hidden\" name={name} value={tag} />) : null}\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Free-form lists of words: labels on a note, email addresses to invite, skills. For choosing from a fixed list use combobox or chips; for one value use input.",
    "example": "<TagsInput aria-label=\"Labels\" defaultValue={[\"urgent\"]} suggestions={[\"design\", \"bug\", \"later\"]} name=\"labels\" />",
    "client": true
  }
}
