{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "multi-select",
  "title": "Multi Select",
  "description": "A searchable multi-select that shows the chosen options as removable chips, with checkbox rows, select all, a selection limit, a +N overflow chip, an empty state and full keyboard navigation.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/multi-select/multi-select.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 interface MultiSelectOption {\n  value: string;\n  label: string;\n  description?: string;\n  disabled?: boolean;\n}\n\nexport type MultiSelectSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface MultiSelectProps {\n  options?: MultiSelectOption[];\n  value?: string[];\n  defaultValue?: string[];\n  onValueChange?: (value: string[]) => void;\n  placeholder?: string;\n  label?: string;\n  helperText?: string;\n  emptyText?: string;\n  maxSelected?: number;\n  maxVisibleTags?: number;\n  showSelectAll?: boolean;\n  clearable?: boolean;\n  size?: MultiSelectSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  maxMenuHeight?: number;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst DEFAULT_OPTIONS: MultiSelectOption[] = [\n  { value: \"design\", label: \"Design\", description: \"Interfaces, systems and motion\" },\n  { value: \"engineering\", label: \"Engineering\", description: \"Frontend, backend and infra\" },\n  { value: \"product\", label: \"Product\", description: \"Strategy, research and roadmaps\" },\n  { value: \"marketing\", label: \"Marketing\", description: \"Growth, content and brand\" },\n  { value: \"sales\", label: \"Sales\", description: \"Pipeline and partnerships\" },\n  { value: \"support\", label: \"Support\", description: \"Help center and success\" },\n  { value: \"legal\", label: \"Legal\", description: \"Contracts and compliance\", disabled: true },\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)\", box: \"rgba(255,255,255,0.28)\", boxOn: \"#F5F4F1\", boxOnText: \"#0A0A0A\", 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)\", box: \"rgba(10,10,10,0.3)\", boxOn: \"#0A0A0A\", boxOnText: \"#FFFFFF\", shadow: \"0 20px 50px rgba(0,0,0,0.16)\" },\n};\n\nconst SIZES: Record<MultiSelectSize, { 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\nfunction highlightMatch(text: string, query: string) {\n  const q = query.trim();\n  if (!q) return text;\n  const i = text.toLowerCase().indexOf(q.toLowerCase());\n  if (i < 0) return text;\n  return (\n    <>\n      {text.slice(0, i)}\n      <span style={{ fontWeight: 700, textDecoration: \"underline\", textUnderlineOffset: 3, textDecorationThickness: 1.5 }}>{text.slice(i, i + q.length)}</span>\n      {text.slice(i + q.length)}\n    </>\n  );\n}\n\nexport function MultiSelect({\n  options = DEFAULT_OPTIONS,\n  value,\n  defaultValue = [],\n  onValueChange,\n  placeholder = \"Select teams...\",\n  label,\n  helperText,\n  emptyText = \"No results found\",\n  maxSelected,\n  maxVisibleTags = 3,\n  showSelectAll = true,\n  clearable = true,\n  size = \"md\",\n  theme = \"dark\",\n  width = 420,\n  maxMenuHeight = 300,\n  disabled = false,\n  className,\n}: MultiSelectProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const listRef = React.useRef<HTMLUListElement>(null);\n  const [internal, setInternal] = React.useState<string[]>(defaultValue);\n  const [query, setQuery] = React.useState(\"\");\n  const [open, setOpen] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const [activeIndex, setActiveIndex] = React.useState(-1);\n  const isControlled = value !== undefined;\n  const selected = isControlled ? value : internal;\n  const atLimit = maxSelected !== undefined && selected.length >= maxSelected;\n\n  const filtered = React.useMemo(() => {\n    const q = query.trim().toLowerCase();\n    return q ? options.filter((o) => o.label.toLowerCase().includes(q)) : options;\n  }, [options, query]);\n\n  const enabledFiltered = filtered.filter((o) => !o.disabled);\n  const allSelected = enabledFiltered.length > 0 && enabledFiltered.every((o) => selected.includes(o.value));\n\n  const commit = (next: string[]) => {\n    if (!isControlled) setInternal(next);\n    onValueChange?.(next);\n  };\n\n  const toggle = (opt: MultiSelectOption) => {\n    if (opt.disabled) return;\n    if (selected.includes(opt.value)) commit(selected.filter((v) => v !== opt.value));\n    else if (!atLimit) commit([...selected, opt.value]);\n    setQuery(\"\");\n    inputRef.current?.focus();\n  };\n\n  const toggleAll = () => {\n    if (allSelected) commit(selected.filter((v) => !enabledFiltered.some((o) => o.value === v)));\n    else {\n      const add = enabledFiltered.map((o) => o.value).filter((v) => !selected.includes(v));\n      const room = maxSelected !== undefined ? Math.max(0, maxSelected - selected.length) : add.length;\n      commit([...selected, ...add.slice(0, room)]);\n    }\n    inputRef.current?.focus();\n  };\n\n  const closeMenu = () => {\n    setOpen(false);\n    setQuery(\"\");\n    setActiveIndex(-1);\n  };\n\n  const openMenu = () => {\n    if (disabled) return;\n    setOpen(true);\n    setActiveIndex((i) => (i >= 0 ? i : filtered.findIndex((o) => !o.disabled)));\n  };\n\n  const move = (dir: 1 | -1) => {\n    const enabled = filtered.map((o, i) => (o.disabled ? -1 : i)).filter((i) => i >= 0);\n    if (enabled.length === 0) return;\n    const pos = enabled.indexOf(activeIndex);\n    const next = pos === -1 ? (dir === 1 ? 0 : enabled.length - 1) : (pos + dir + enabled.length) % enabled.length;\n    setActiveIndex(enabled[next]);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"ArrowDown\" || e.key === \"ArrowUp\") {\n      e.preventDefault();\n      if (!open) openMenu();\n      else move(e.key === \"ArrowDown\" ? 1 : -1);\n    } else if (e.key === \"Enter\") {\n      if (open && activeIndex >= 0 && filtered[activeIndex]) {\n        e.preventDefault();\n        toggle(filtered[activeIndex]);\n      }\n    } else if (e.key === \"Backspace\" && !query && selected.length > 0) {\n      commit(selected.slice(0, -1));\n    } else if (e.key === \"Escape\") {\n      if (open) {\n        e.preventDefault();\n        closeMenu();\n      }\n    } else if (e.key === \"Tab\") {\n      closeMenu();\n    }\n  };\n\n  React.useEffect(() => {\n    if (!open) return;\n    const onDown = (e: MouseEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) {\n        setOpen(false);\n        setQuery(\"\");\n        setActiveIndex(-1);\n      }\n    };\n    document.addEventListener(\"mousedown\", onDown);\n    return () => document.removeEventListener(\"mousedown\", onDown);\n  }, [open]);\n\n  React.useEffect(() => {\n    if (!open || activeIndex < 0) return;\n    listRef.current?.querySelector(`[data-index=\"${activeIndex}\"]`)?.scrollIntoView({ block: \"nearest\" });\n  }, [activeIndex, open]);\n\n  const chips = selected.map((v) => options.find((o) => o.value === v)).filter((o): o is MultiSelectOption => Boolean(o));\n  const visibleChips = open ? chips : chips.slice(0, maxVisibleTags);\n  const hiddenCount = chips.length - visibleChips.length;\n  const borderColor = open ? p.focus : hovered ? p.borderHover : p.border;\n\n  return (\n    <div ref={rootRef} 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          onMouseEnter={() => setHovered(true)}\n          onMouseLeave={() => setHovered(false)}\n          onClick={() => {\n            inputRef.current?.focus();\n            if (!open) openMenu();\n          }}\n          className=\"flex items-center\"\n          style={{ gap: 8, padding: `${s.padY}px ${s.padX}px`, minHeight: s.chipH + s.padY * 2, borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: open ? `0 0 0 3px 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          <div className=\"flex min-w-0 flex-1 flex-wrap items-center\" style={{ gap: s.gap }}>\n            <AnimatePresence initial={false}>\n              {visibleChips.map((opt) => (\n                <motion.span\n                  key={opt.value}\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                  {opt.label}\n                  <button\n                    type=\"button\"\n                    aria-label={`Remove ${opt.label}`}\n                    disabled={disabled}\n                    onMouseDown={(e) => e.preventDefault()}\n                    onClick={(e) => {\n                      e.stopPropagation();\n                      commit(selected.filter((v) => v !== opt.value));\n                    }}\n                    className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-full border-none bg-transparent p-0 opacity-60 outline-none transition-opacity 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              {hiddenCount > 0 && (\n                <motion.span key=\"more\" layout className=\"inline-flex items-center font-semibold\" style={{ height: s.chipH, padding: `0 ${s.padX - 2}px`, borderRadius: 999, color: p.muted, fontSize: s.font - 1 }} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>\n                  +{hiddenCount} more\n                </motion.span>\n              )}\n            </AnimatePresence>\n            <input\n              ref={inputRef}\n              id={`${uid}-input`}\n              type=\"text\"\n              role=\"combobox\"\n              aria-expanded={open}\n              aria-controls={`${uid}-list`}\n              aria-autocomplete=\"list\"\n              aria-activedescendant={open && activeIndex >= 0 ? `${uid}-opt-${activeIndex}` : undefined}\n              autoComplete=\"off\"\n              spellCheck={false}\n              disabled={disabled}\n              value={query}\n              placeholder={selected.length === 0 ? placeholder : \"\"}\n              onChange={(e) => {\n                setQuery(e.target.value);\n                setOpen(true);\n                setActiveIndex(0);\n              }}\n              onFocus={() => !open && openMenu()}\n              onKeyDown={onKeyDown}\n              className=\"min-w-[60px] flex-1 border-none bg-transparent p-0 outline-none placeholder:text-[color:var(--ms-placeholder)]\"\n              style={{ color: p.text, fontSize: s.font, height: s.chipH, [\"--ms-placeholder\" as string]: p.muted }}\n            />\n          </div>\n\n          {clearable && !disabled && selected.length > 0 && (\n            <button\n              type=\"button\"\n              aria-label=\"Clear all\"\n              onMouseDown={(e) => e.preventDefault()}\n              onClick={(e) => {\n                e.stopPropagation();\n                commit([]);\n                inputRef.current?.focus();\n              }}\n              className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-full border-none p-0 outline-none\"\n              style={{ width: 20, height: 20, background: p.chip, color: p.muted }}\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          )}\n          <motion.span aria-hidden=\"true\" className=\"flex shrink-0\" animate={{ rotate: open ? 180 : 0 }} transition={{ type: \"spring\", stiffness: 420, damping: 28 }} style={{ color: open ? p.text : p.muted }}>\n            <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n              <path d=\"M4 6.2L8 10L12 6.2\" />\n            </svg>\n          </motion.span>\n        </div>\n\n        <AnimatePresence>\n          {open && (\n            <motion.ul\n              ref={listRef}\n              id={`${uid}-list`}\n              role=\"listbox\"\n              aria-multiselectable=\"true\"\n              className=\"absolute left-0 z-50 m-0 w-full list-none overflow-auto p-1.5\"\n              style={{ top: \"calc(100% + 8px)\", maxHeight: maxMenuHeight, borderRadius: s.radius + 2, 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              {(showSelectAll || maxSelected !== undefined) && filtered.length > 0 && (\n                <li role=\"presentation\" className=\"flex items-center justify-between\" style={{ padding: `${s.padY - 2}px ${s.padX - 4}px ${s.padY}px`, color: p.muted, fontSize: s.font - 1.5 }}>\n                  <span className=\"tabular-nums\">{maxSelected !== undefined ? `${selected.length}/${maxSelected} selected` : `${selected.length} selected`}</span>\n                  {showSelectAll && (\n                    <button type=\"button\" onMouseDown={(e) => e.preventDefault()} onClick={toggleAll} className=\"cursor-pointer border-none bg-transparent p-0 font-semibold outline-none hover:underline\" style={{ color: p.text, fontSize: \"inherit\" }}>\n                      {allSelected ? \"Clear\" : \"Select all\"}\n                    </button>\n                  )}\n                </li>\n              )}\n              {filtered.length === 0 ? (\n                <li role=\"presentation\" className=\"text-center\" style={{ padding: \"18px 12px\", color: p.muted, fontSize: s.font }}>\n                  {emptyText}\n                </li>\n              ) : (\n                filtered.map((opt, i) => {\n                  const on = selected.includes(opt.value);\n                  const blocked = opt.disabled || (!on && atLimit);\n                  return (\n                    <li\n                      key={opt.value}\n                      id={`${uid}-opt-${i}`}\n                      data-index={i}\n                      role=\"option\"\n                      aria-selected={on}\n                      aria-disabled={blocked || undefined}\n                      onMouseEnter={() => !blocked && setActiveIndex(i)}\n                      onMouseDown={(e) => e.preventDefault()}\n                      onClick={() => !blocked && toggle(opt)}\n                      className=\"flex items-center\"\n                      style={{ gap: 12, padding: `${s.padY + 1}px ${s.padX - 4}px`, borderRadius: s.radius - 4, background: i === activeIndex ? p.highlight : \"transparent\", opacity: blocked ? 0.4 : 1, cursor: blocked ? \"default\" : \"pointer\", color: p.text, transition: \"background 0.12s ease\" }}\n                    >\n                      <span className=\"flex shrink-0 items-center justify-center\" style={{ width: 18, height: 18, borderRadius: 5, border: `1.5px solid ${on ? p.boxOn : p.box}`, background: on ? p.boxOn : \"transparent\", transition: \"background 0.15s ease, border-color 0.15s ease\" }}>\n                        <svg width=\"11\" height=\"11\" viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n                          <motion.path d=\"M3 8.2L6.4 11.6L13 4.4\" stroke={p.boxOnText} strokeWidth=\"2.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" initial={false} animate={{ pathLength: on ? 1 : 0, opacity: on ? 1 : 0 }} transition={{ duration: 0.2 }} />\n                        </svg>\n                      </span>\n                      <div className=\"flex min-w-0 flex-col\" style={{ gap: 2 }}>\n                        <span className=\"truncate\" style={{ fontSize: s.font, fontWeight: 500 }}>\n                          {highlightMatch(opt.label, query)}\n                        </span>\n                        {opt.description && (\n                          <span className=\"truncate\" style={{ fontSize: s.font * 0.85, color: p.muted }}>\n                            {opt.description}\n                          </span>\n                        )}\n                      </div>\n                    </li>\n                  );\n                })\n              )}\n            </motion.ul>\n          )}\n        </AnimatePresence>\n      </div>\n\n      {helperText && <span style={{ color: p.muted, fontSize: s.font - 1.5, lineHeight: 1.4 }}>{helperText}</span>}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}