{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tag-input",
  "title": "Tag Input",
  "description": "A multi-value input that turns text into removable tags on Enter, comma or paste, with suggestions, duplicate and max limits, custom validation and an animated add and remove.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/tag-input/tag-input.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TagInputSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface TagInputProps {\n  value?: string[];\n  defaultValue?: string[];\n  onValueChange?: (value: string[]) => void;\n  placeholder?: string;\n  label?: string;\n  helperText?: string;\n  suggestions?: string[];\n  maxTags?: number;\n  allowDuplicates?: boolean;\n  addOnBlur?: boolean;\n  delimiters?: string[];\n  validate?: (tag: string) => string | null | undefined;\n  size?: TagInputSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", bg: \"rgba(255,255,255,0.03)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", chip: \"rgba(255,255,255,0.09)\", chipBorder: \"rgba(255,255,255,0.1)\", menuBg: \"#0E0E0E\", menuBorder: \"rgba(255,255,255,0.08)\", highlight: \"rgba(255,255,255,0.07)\", error: \"#FF7A6B\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", chip: \"rgba(10,10,10,0.06)\", chipBorder: \"rgba(10,10,10,0.1)\", menuBg: \"#FFFFFF\", menuBorder: \"rgba(10,10,10,0.12)\", highlight: \"rgba(10,10,10,0.05)\", error: \"#E5484D\", shadow: \"0 20px 50px rgba(0,0,0,0.16)\" },\n};\n\nconst SIZES: Record<TagInputSize, { font: number; chipH: number; padX: number; padY: number; gap: number; radius: number }> = {\n  sm: { font: 13, chipH: 24, padX: 10, padY: 6, gap: 6, radius: 12 },\n  md: { font: 14, chipH: 28, padX: 12, padY: 8, gap: 7, radius: 14 },\n  lg: { font: 15.5, chipH: 34, padX: 14, padY: 10, gap: 8, radius: 16 },\n};\n\nexport function TagInput({\n  value,\n  defaultValue = [],\n  onValueChange,\n  placeholder = \"Add a tag...\",\n  label,\n  helperText,\n  suggestions = [],\n  maxTags,\n  allowDuplicates = false,\n  addOnBlur = true,\n  delimiters = [\",\", \";\"],\n  validate,\n  size = \"md\",\n  theme = \"dark\",\n  width = 420,\n  disabled = false,\n  className,\n}: TagInputProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const [internal, setInternal] = React.useState<string[]>(defaultValue);\n  const [draft, setDraft] = React.useState(\"\");\n  const [focused, setFocused] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const [error, setError] = React.useState<string | null>(null);\n  const [activeIndex, setActiveIndex] = React.useState(-1);\n  const isControlled = value !== undefined;\n  const tags = isControlled ? value : internal;\n  const full = maxTags !== undefined && tags.length >= maxTags;\n  const tagKeys = React.useMemo(() => {\n    const seen = new Map<string, number>();\n    return tags.map((t) => {\n      const n = seen.get(t) ?? 0;\n      seen.set(t, n + 1);\n      return `${t}#${n}`;\n    });\n  }, [tags]);\n\n  const commit = (next: string[]) => {\n    if (!isControlled) setInternal(next);\n    onValueChange?.(next);\n  };\n\n  const addTags = (rawList: string[]) => {\n    const next = [...tags];\n    let problem: string | null = null;\n    for (const raw of rawList) {\n      const t = raw.trim();\n      if (!t) continue;\n      if (maxTags !== undefined && next.length >= maxTags) {\n        problem = `Up to ${maxTags} tags allowed`;\n        break;\n      }\n      if (!allowDuplicates && next.some((x) => x.toLowerCase() === t.toLowerCase())) {\n        problem = `\"${t}\" is already added`;\n        continue;\n      }\n      const invalid = validate?.(t);\n      if (invalid) {\n        problem = invalid;\n        continue;\n      }\n      next.push(t);\n    }\n    if (next.length !== tags.length) commit(next);\n    setError(problem);\n    return next.length !== tags.length || !problem;\n  };\n\n  const splitByDelimiters = (text: string) => text.split(new RegExp(`[${delimiters.map((d) => d.replace(/[\\]\\\\^-]/g, \"\\\\$&\")).join(\"\")}\\\\n]`));\n\n  const matches = React.useMemo(() => {\n    const q = draft.trim().toLowerCase();\n    return suggestions.filter((sug) => (allowDuplicates || !tags.some((t) => t.toLowerCase() === sug.toLowerCase())) && (!q || sug.toLowerCase().includes(q))).slice(0, 6);\n  }, [draft, suggestions, tags, allowDuplicates]);\n  const showMenu = focused && !disabled && !full && matches.length > 0;\n\n  const removeAt = (i: number) => {\n    commit(tags.filter((_, idx) => idx !== i));\n    setError(null);\n    inputRef.current?.focus();\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"Enter\") {\n      e.preventDefault();\n      if (showMenu && activeIndex >= 0) {\n        addTags([matches[activeIndex]]);\n        setDraft(\"\");\n        setActiveIndex(-1);\n      } else if (draft.trim()) {\n        if (addTags([draft])) setDraft(\"\");\n      }\n    } else if (delimiters.includes(e.key)) {\n      e.preventDefault();\n      if (draft.trim() && addTags([draft])) setDraft(\"\");\n    } else if (e.key === \"Backspace\" && !draft && tags.length > 0) {\n      removeAt(tags.length - 1);\n    } else if (e.key === \"ArrowDown\" || e.key === \"ArrowUp\") {\n      if (!showMenu) return;\n      e.preventDefault();\n      const dir = e.key === \"ArrowDown\" ? 1 : -1;\n      setActiveIndex((i) => (i === -1 ? (dir === 1 ? 0 : matches.length - 1) : (i + dir + matches.length) % matches.length));\n    } else if (e.key === \"Escape\") {\n      setActiveIndex(-1);\n      setError(null);\n    }\n  };\n\n  const onPaste = (e: React.ClipboardEvent<HTMLInputElement>) => {\n    const text = e.clipboardData.getData(\"text\");\n    if (!/[,;\\n]/.test(text) && delimiters.every((d) => !text.includes(d))) return;\n    e.preventDefault();\n    addTags(splitByDelimiters(text));\n    setDraft(\"\");\n  };\n\n  const borderColor = error ? p.error : focused ? p.focus : hovered ? p.borderHover : p.border;\n\n  return (\n    <div className={cn(\"relative inline-flex flex-col gap-2\", className)} style={{ width, maxWidth: \"100%\", fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      {label && (\n        <label htmlFor={`${uid}-input`} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font }}>\n          {label}\n        </label>\n      )}\n\n      <div className=\"relative\">\n      <div\n        onClick={() => !disabled && inputRef.current?.focus()}\n        onMouseEnter={() => setHovered(true)}\n        onMouseLeave={() => setHovered(false)}\n        className=\"flex flex-wrap items-center\"\n        style={{ gap: s.gap, padding: `${s.padY}px ${s.padX}px`, minHeight: s.chipH + s.padY * 2, borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: focused ? `0 0 0 3px ${error ? `color-mix(in srgb, ${p.error} 25%, transparent)` : `color-mix(in srgb, ${p.focus} 14%, transparent)`}` : \"0 0 0 0 transparent\", cursor: disabled ? \"default\" : \"text\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n      >\n        <AnimatePresence initial={false}>\n          {tags.map((tag, i) => (\n            <motion.span\n              key={tagKeys[i]}\n              layout\n              className=\"inline-flex items-center font-semibold whitespace-nowrap\"\n              style={{ height: s.chipH, gap: 6, paddingLeft: s.padX - 2, paddingRight: 6, borderRadius: 999, background: p.chip, border: `1px solid ${p.chipBorder}`, color: p.text, fontSize: s.font - 1 }}\n              initial={{ opacity: 0, scale: 0.8 }}\n              animate={{ opacity: 1, scale: 1 }}\n              exit={{ opacity: 0, scale: 0.8 }}\n              transition={{ type: \"spring\", stiffness: 500, damping: 32 }}\n            >\n              {tag}\n              <button\n                type=\"button\"\n                aria-label={`Remove ${tag}`}\n                disabled={disabled}\n                onClick={(e) => {\n                  e.stopPropagation();\n                  removeAt(i);\n                }}\n                className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-full border-none bg-transparent p-0 opacity-60 transition-opacity outline-none hover:opacity-100 focus-visible:opacity-100\"\n                style={{ width: s.chipH - 10, height: s.chipH - 10, color: p.text }}\n              >\n                <svg width=\"9\" height=\"9\" viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n                  <path d=\"M3 3L9 9M9 3L3 9\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" />\n                </svg>\n              </button>\n            </motion.span>\n          ))}\n        </AnimatePresence>\n\n        <input\n          ref={inputRef}\n          id={`${uid}-input`}\n          type=\"text\"\n          role={suggestions.length ? \"combobox\" : undefined}\n          aria-expanded={suggestions.length ? showMenu : undefined}\n          aria-controls={suggestions.length ? `${uid}-list` : undefined}\n          aria-activedescendant={showMenu && activeIndex >= 0 ? `${uid}-opt-${activeIndex}` : undefined}\n          aria-invalid={error ? true : undefined}\n          value={draft}\n          disabled={disabled || full}\n          placeholder={tags.length === 0 || !full ? placeholder : \"\"}\n          onChange={(e) => {\n            setDraft(e.target.value);\n            setActiveIndex(-1);\n            if (error) setError(null);\n          }}\n          onKeyDown={onKeyDown}\n          onPaste={onPaste}\n          onFocus={() => setFocused(true)}\n          onBlur={() => {\n            setFocused(false);\n            setActiveIndex(-1);\n            if (addOnBlur && draft.trim() && addTags([draft])) setDraft(\"\");\n          }}\n          className=\"min-w-[90px] flex-1 border-none bg-transparent p-0 outline-none placeholder:text-[color:var(--ti-placeholder)]\"\n          style={{ color: p.text, fontSize: s.font, height: s.chipH, [\"--ti-placeholder\" as string]: p.muted }}\n        />\n      </div>\n\n      <AnimatePresence>\n        {showMenu && (\n          <motion.ul\n            id={`${uid}-list`}\n            role=\"listbox\"\n            className=\"absolute left-0 z-50 m-0 w-full list-none p-1.5\"\n            style={{ top: \"calc(100% + 8px)\", borderRadius: s.radius, border: `1px solid ${p.menuBorder}`, background: p.menuBg, boxShadow: p.shadow, transformOrigin: \"top center\" }}\n            initial={{ opacity: 0, y: -6, scale: 0.98 }}\n            animate={{ opacity: 1, y: 0, scale: 1 }}\n            exit={{ opacity: 0, y: -6, scale: 0.98 }}\n            transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n          >\n            {matches.map((sug, i) => (\n              <li\n                key={sug}\n                id={`${uid}-opt-${i}`}\n                role=\"option\"\n                aria-selected={i === activeIndex}\n                onMouseEnter={() => setActiveIndex(i)}\n                onMouseDown={(e) => e.preventDefault()}\n                onClick={() => {\n                  addTags([sug]);\n                  setDraft(\"\");\n                  setActiveIndex(-1);\n                }}\n                className=\"cursor-pointer\"\n                style={{ padding: `${s.padY}px ${s.padX - 2}px`, borderRadius: s.radius - 5, background: i === activeIndex ? p.highlight : \"transparent\", color: p.text, fontSize: s.font, transition: \"background 0.12s ease\" }}\n              >\n                {sug}\n              </li>\n            ))}\n          </motion.ul>\n        )}\n      </AnimatePresence>\n      </div>\n\n      {(error || helperText || maxTags !== undefined) && (\n        <div className=\"flex items-start justify-between gap-3\" style={{ fontSize: s.font - 1.5, lineHeight: 1.4 }} role={error ? \"alert\" : undefined}>\n          <span style={{ color: error ? p.error : p.muted }}>{error ?? helperText}</span>\n          {maxTags !== undefined && (\n            <span className=\"shrink-0 tabular-nums\" style={{ color: p.muted }}>\n              {tags.length}/{maxTags}\n            </span>\n          )}\n        </div>\n      )}\n\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}