{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "multi-select-tag-input",
  "type": "registry:ui",
  "title": "Multi-Select Tag Input",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Interactive tag and chip input with live autocomplete suggestions, custom tag creation via Enter or comma, Backspace removal, and max-tag limits.",
  "dependencies": [
    "framer-motion",
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/multi-select-tag-input.tsx",
      "content": "\"use client\";\n\nimport React, { useState, useRef, useEffect, useId, useMemo } from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"framer-motion\";\nimport { X, Plus, Tag, Check } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface TagSuggestion {\n  value: string;\n  label: string;\n  category?: string;\n}\n\nexport interface MultiSelectTagInputProps {\n  /** Suggested tags shown in the dropdown */\n  suggestions?: TagSuggestion[];\n  /** Controlled array of selected tag values */\n  value?: string[];\n  /** Initial selected tag values for uncontrolled mode */\n  defaultValue?: string[];\n  /** Callback fired whenever selected tags change */\n  onChange?: (tags: string[]) => void;\n  /** Input placeholder when empty */\n  placeholder?: string;\n  /** Optional field label */\n  label?: string;\n  /** Maximum number of tags allowed. Default: 8 */\n  maxTags?: number;\n  /** Allow creating custom tags not in suggestions by pressing Enter or comma. Default: true */\n  allowCustomTags?: boolean;\n  /** Disable input and tag removal */\n  disabled?: boolean;\n  className?: string;\n}\n\nconst DEFAULT_SUGGESTIONS: TagSuggestion[] = [\n  { value: \"react\", label: \"React\", category: \"Frontend\" },\n  { value: \"typescript\", label: \"TypeScript\", category: \"Language\" },\n  { value: \"tailwind\", label: \"Tailwind CSS\", category: \"Styling\" },\n  { value: \"framer-motion\", label: \"Framer Motion\", category: \"Animation\" },\n  { value: \"nextjs\", label: \"Next.js\", category: \"Framework\" },\n  { value: \"accessibility\", label: \"Accessibility (WAI-ARIA)\", category: \"UX\" },\n  { value: \"design-systems\", label: \"Design Systems\", category: \"UI\" },\n  { value: \"graphql\", label: \"GraphQL\", category: \"API\" },\n];\n\nexport function MultiSelectTagInput({\n  suggestions = DEFAULT_SUGGESTIONS,\n  value,\n  defaultValue = [\"react\", \"typescript\", \"framer-motion\"],\n  onChange,\n  placeholder = \"Add tag or select...\",\n  label = \"Tech Stack Tags\",\n  maxTags = 8,\n  allowCustomTags = true,\n  disabled = false,\n  className,\n}: MultiSelectTagInputProps) {\n  const shouldReduceMotion = useReducedMotion() ?? false;\n  const listboxId = useId();\n  const isControlled = value !== undefined;\n\n  const [internalTags, setInternalTags] = useState<string[]>(defaultValue);\n  const selectedTags = isControlled ? value : internalTags;\n\n  const [inputValue, setInputValue] = useState(\"\");\n  const [open, setOpen] = useState(false);\n  const [activeIndex, setActiveIndex] = useState(0);\n\n  const containerRef = useRef<HTMLDivElement>(null);\n  const inputRef = useRef<HTMLInputElement>(null);\n  const listRef = useRef<HTMLDivElement>(null);\n\n  const updateTags = (next: string[]) => {\n    if (!isControlled) {\n      setInternalTags(next);\n    }\n    onChange?.(next);\n  };\n\n  const getTagLabel = (val: string) => {\n    const match = suggestions.find(\n      (s) => s.value.toLowerCase() === val.toLowerCase()\n    );\n    return match ? match.label : val;\n  };\n\n  const filteredSuggestions = useMemo(() => {\n    const q = inputValue.trim().toLowerCase();\n    return suggestions.filter((item) => {\n      const alreadySelected = selectedTags.some(\n        (t) => t.toLowerCase() === item.value.toLowerCase()\n      );\n      if (alreadySelected) return false;\n      if (!q) return true;\n      return (\n        item.label.toLowerCase().includes(q) ||\n        item.value.toLowerCase().includes(q) ||\n        (item.category?.toLowerCase().includes(q) ?? false)\n      );\n    });\n  }, [suggestions, selectedTags, inputValue]);\n\n  const isAtLimit = selectedTags.length >= maxTags;\n\n  const addTag = (rawVal: string) => {\n    if (disabled || isAtLimit) return;\n    const trimmed = rawVal.trim().replace(/,+$/, \"\");\n    if (!trimmed) return;\n\n    // Check if matches existing suggestion value or label\n    const existingSuggestion = suggestions.find(\n      (s) =>\n        s.value.toLowerCase() === trimmed.toLowerCase() ||\n        s.label.toLowerCase() === trimmed.toLowerCase()\n    );\n\n    const normalizedValue = existingSuggestion\n      ? existingSuggestion.value\n      : trimmed;\n\n    if (!existingSuggestion && !allowCustomTags) return;\n\n    const isDuplicate = selectedTags.some(\n      (t) => t.toLowerCase() === normalizedValue.toLowerCase()\n    );\n    if (isDuplicate) {\n      setInputValue(\"\");\n      return;\n    }\n\n    updateTags([...selectedTags, normalizedValue]);\n    setInputValue(\"\");\n    setActiveIndex(0);\n    inputRef.current?.focus();\n  };\n\n  const removeTag = (tagToRemove: string) => {\n    if (disabled) return;\n    updateTags(selectedTags.filter((t) => t !== tagToRemove));\n  };\n\n  useEffect(() => {\n    setActiveIndex(0);\n  }, [inputValue]);\n\n  useEffect(() => {\n    if (!open || !listRef.current) return;\n    const activeEl = listRef.current.querySelector<HTMLElement>(\n      `[data-index=\"${activeIndex}\"]`\n    );\n    activeEl?.scrollIntoView({ block: \"nearest\" });\n  }, [activeIndex, open]);\n\n  useEffect(() => {\n    const handleOutside = (e: MouseEvent) => {\n      if (\n        containerRef.current &&\n        !containerRef.current.contains(e.target as Node)\n      ) {\n        setOpen(false);\n      }\n    };\n    document.addEventListener(\"mousedown\", handleOutside);\n    return () => document.removeEventListener(\"mousedown\", handleOutside);\n  }, []);\n\n  const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n    if (disabled) return;\n\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      if (!open) {\n        setOpen(true);\n        return;\n      }\n      if (filteredSuggestions.length > 0) {\n        setActiveIndex((prev) => (prev + 1) % filteredSuggestions.length);\n      }\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      if (filteredSuggestions.length > 0) {\n        setActiveIndex(\n          (prev) =>\n            (prev - 1 + filteredSuggestions.length) %\n            filteredSuggestions.length\n        );\n      }\n    } else if (e.key === \"Enter\" || e.key === \",\") {\n      if (e.key === \",\" && !inputValue.trim()) {\n        e.preventDefault();\n        return;\n      }\n      if (open && filteredSuggestions[activeIndex] && !inputValue.endsWith(\",\")) {\n        e.preventDefault();\n        addTag(filteredSuggestions[activeIndex].value);\n      } else if (inputValue.trim()) {\n        e.preventDefault();\n        addTag(inputValue);\n      }\n    } else if (\n      e.key === \"Backspace\" &&\n      inputValue === \"\" &&\n      selectedTags.length > 0\n    ) {\n      e.preventDefault();\n      removeTag(selectedTags[selectedTags.length - 1]);\n    } else if (e.key === \"Escape\") {\n      setOpen(false);\n    }\n  };\n\n  return (\n    <div ref={containerRef} className={cn(\"relative w-full max-w-md\", className)}>\n      {(label || maxTags) && (\n        <div className=\"mb-1.5 flex items-center justify-between\">\n          {label && (\n            <label\n              onClick={() => inputRef.current?.focus()}\n              className=\"cursor-pointer text-xs font-medium text-neutral-600 dark:text-neutral-400\"\n            >\n              {label}\n            </label>\n          )}\n          <div className=\"flex items-center gap-2\">\n            {selectedTags.length > 0 && !disabled && (\n              <button\n                type=\"button\"\n                onClick={() => updateTags([])}\n                className=\"text-[11px] font-medium text-neutral-400 transition-colors hover:text-black dark:hover:text-white\"\n              >\n                Clear all\n              </button>\n            )}\n            <span\n              className={cn(\n                \"font-mono text-[11px]\",\n                isAtLimit\n                  ? \"font-semibold text-amber-600 dark:text-amber-400\"\n                  : \"text-neutral-400 dark:text-neutral-500\"\n              )}\n            >\n              {selectedTags.length}/{maxTags}\n            </span>\n          </div>\n        </div>\n      )}\n\n      {/* Input & Tag Container */}\n      <div\n        onClick={() => {\n          if (!disabled) {\n            inputRef.current?.focus();\n            setOpen(true);\n          }\n        }}\n        className={cn(\n          \"flex min-h-[44px] w-full flex-wrap items-center gap-1.5 rounded-xl border bg-white p-2 transition-colors dark:bg-[#0A0A0A]\",\n          open\n            ? \"border-black ring-2 ring-black/10 dark:border-white dark:ring-white/10\"\n            : \"border-neutral-200 hover:border-neutral-300 dark:border-[#262626] dark:hover:border-neutral-700\",\n          disabled && \"cursor-not-allowed opacity-50\"\n        )}\n      >\n        <AnimatePresence mode=\"popLayout\" initial={false}>\n          {selectedTags.map((tag) => (\n            <motion.span\n              key={tag}\n              layout={!shouldReduceMotion}\n              initial={\n                shouldReduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.85 }\n              }\n              animate={{ opacity: 1, scale: 1 }}\n              exit={\n                shouldReduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.85 }\n              }\n              transition={{ type: \"spring\", stiffness: 500, damping: 30 }}\n              className=\"inline-flex items-center gap-1 rounded-lg border border-neutral-200 bg-neutral-100 px-2.5 py-1 text-xs font-medium text-black dark:border-[#262626] dark:bg-[#161616] dark:text-white\"\n            >\n              <Tag className=\"h-3 w-3 text-neutral-400 dark:text-neutral-500\" />\n              <span>{getTagLabel(tag)}</span>\n              {!disabled && (\n                <button\n                  type=\"button\"\n                  aria-label={`Remove ${getTagLabel(tag)}`}\n                  onClick={(e) => {\n                    e.stopPropagation();\n                    removeTag(tag);\n                  }}\n                  className=\"ml-0.5 rounded p-0.5 text-neutral-400 transition-colors hover:bg-neutral-200 hover:text-black dark:hover:bg-[#262626] dark:hover:text-white\"\n                >\n                  <X className=\"h-3 w-3\" />\n                </button>\n              )}\n            </motion.span>\n          ))}\n        </AnimatePresence>\n\n        <input\n          ref={inputRef}\n          type=\"text\"\n          role=\"combobox\"\n          aria-expanded={open}\n          aria-controls={listboxId}\n          aria-autocomplete=\"list\"\n          disabled={disabled || isAtLimit}\n          value={inputValue}\n          onFocus={() => setOpen(true)}\n          onChange={(e) => {\n            const val = e.target.value;\n            if (val.endsWith(\",\")) {\n              addTag(val);\n            } else {\n              setInputValue(val);\n              setOpen(true);\n            }\n          }}\n          onKeyDown={handleKeyDown}\n          placeholder={\n            isAtLimit\n              ? \"Maximum tags reached\"\n              : selectedTags.length === 0\n                ? placeholder\n                : \"Add more...\"\n          }\n          className=\"min-w-[110px] flex-1 bg-transparent px-1.5 py-0.5 text-sm text-black placeholder:text-neutral-400 focus:outline-none disabled:cursor-not-allowed dark:text-white dark:placeholder:text-neutral-600\"\n        />\n      </div>\n\n      {/* Helper hint */}\n      <p className=\"mt-1.5 text-[11px] text-neutral-400 dark:text-neutral-500\">\n        Press <kbd className=\"rounded border border-neutral-200 bg-neutral-100 px-1 py-0.5 font-mono text-[10px] dark:border-neutral-800 dark:bg-neutral-900\">Enter</kbd> or <kbd className=\"rounded border border-neutral-200 bg-neutral-100 px-1 py-0.5 font-mono text-[10px] dark:border-neutral-800 dark:bg-neutral-900\">,</kbd> to add · <kbd className=\"rounded border border-neutral-200 bg-neutral-100 px-1 py-0.5 font-mono text-[10px] dark:border-neutral-800 dark:bg-neutral-900\">Backspace</kbd> to remove\n      </p>\n\n      {/* Suggestions Dropdown */}\n      <AnimatePresence>\n        {open && !disabled && !isAtLimit && (\n          <motion.div\n            id={listboxId}\n            ref={listRef}\n            role=\"listbox\"\n            initial={\n              shouldReduceMotion ? { opacity: 0 } : { opacity: 0, y: -6, scale: 0.98 }\n            }\n            animate={{ opacity: 1, y: 0, scale: 1 }}\n            exit={\n              shouldReduceMotion ? { opacity: 0 } : { opacity: 0, y: -4, scale: 0.98 }\n            }\n            transition={{ type: \"spring\", stiffness: 420, damping: 28 }}\n            className=\"absolute left-0 right-0 z-50 mt-1 max-h-52 overflow-y-auto rounded-xl border border-neutral-200 bg-white p-1.5 shadow-xl dark:border-[#262626] dark:bg-[#0A0A0A]\"\n          >\n            {filteredSuggestions.length === 0 ? (\n              allowCustomTags && inputValue.trim() ? (\n                <button\n                  type=\"button\"\n                  onMouseDown={(e) => e.preventDefault()}\n                  onClick={() => addTag(inputValue)}\n                  className=\"flex w-full items-center justify-between rounded-lg bg-neutral-100 px-3 py-2 text-left text-xs font-medium text-black dark:bg-[#161616] dark:text-white\"\n                >\n                  <span className=\"flex items-center gap-2\">\n                    <Plus className=\"h-3.5 w-3.5\" />\n                    Create tag &ldquo;{inputValue.trim()}&rdquo;\n                  </span>\n                  <span className=\"font-mono text-[10px] text-neutral-400\">\n                    Enter\n                  </span>\n                </button>\n              ) : (\n                <div className=\"px-3 py-3 text-center text-xs text-neutral-400\">\n                  No matching suggestions available\n                </div>\n              )\n            ) : (\n              filteredSuggestions.map((item, idx) => {\n                const isActive = idx === activeIndex;\n                return (\n                  <button\n                    key={item.value}\n                    type=\"button\"\n                    role=\"option\"\n                    data-index={idx}\n                    aria-selected={isActive}\n                    onMouseDown={(e) => e.preventDefault()}\n                    onMouseEnter={() => setActiveIndex(idx)}\n                    onClick={() => addTag(item.value)}\n                    className={cn(\n                      \"flex w-full items-center justify-between rounded-lg px-3 py-2 text-left text-sm transition-colors\",\n                      isActive\n                        ? \"bg-neutral-100 text-black dark:bg-[#161616] dark:text-white\"\n                        : \"text-neutral-600 hover:bg-neutral-50 dark:text-neutral-400 dark:hover:bg-[#121212]\"\n                    )}\n                  >\n                    <span className=\"flex items-center gap-2 font-medium\">\n                      <Plus className=\"h-3.5 w-3.5 text-neutral-400\" />\n                      {item.label}\n                    </span>\n                    {item.category && (\n                      <span className=\"rounded-md border border-neutral-200 px-1.5 py-0.5 font-mono text-[10px] text-neutral-500 dark:border-neutral-800 dark:text-neutral-400\">\n                        {item.category}\n                      </span>\n                    )}\n                  </button>\n                );\n              })\n            )}\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n\nexport default MultiSelectTagInput;\n",
      "type": "registry:ui",
      "target": "components/ui/multi-select-tag-input.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
