{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"combobox","type":"registry:ui","title":"Combobox","description":"A searchable select with grouped options and keywords.","dependencies":["clsx","lucide-react","tailwind-merge"],"registryDependencies":["https://caliperui.site/r/styles.json"],"files":[{"path":"ui/combobox.tsx","type":"registry:ui","target":"components/ui/combobox.tsx","content":"\"use client\";\n\nimport { Check, ChevronDown, Plus } from \"lucide-react\";\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { Label } from \"./label\";\nimport { cn } from \"./lib/cn\";\nimport \"./shared/input.css\";\nimport \"./shared/input-2.css\";\nimport \"./combobox.css\";\n\nexport type ComboboxOption = {\n  value: string;\n  label: string;\n  group?: string;\n  keywords?: readonly string[];\n  disabled?: boolean;\n};\n\nexport function Combobox({\n  label,\n  options,\n  value,\n  defaultValue = \"\",\n  onValueChange,\n  placeholder = \"Search\",\n  name,\n  disabled,\n  id,\n  className,\n  onCreate,\n  createLabel = (query: string) => `Add “${query}”`,\n}: {\n  label: string;\n  options: readonly ComboboxOption[];\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  placeholder?: string;\n  name?: string;\n  disabled?: boolean;\n  id?: string;\n  className?: string;\n  /** Offered when nothing matches, e.g. invite someone by the typed name. */\n  onCreate?: (query: string) => void;\n  createLabel?: (query: string) => string;\n}) {\n  const generated = useId();\n  const fieldId = id ?? generated;\n  const listId = `${fieldId}-list`;\n  const root = useRef<HTMLDivElement>(null);\n  const [local, setLocal] = useState(defaultValue);\n  const [query, setQuery] = useState(\"\");\n  const [open, setOpen] = useState(false);\n  const [active, setActive] = useState(0);\n  const current = value ?? local;\n  const selected = options.find((option) => option.value === current);\n  const needle = query.trim().toLowerCase();\n  const matches = options.filter((option) => {\n    if (!needle) return true;\n    return `${option.label} ${option.value} ${(option.keywords ?? []).join(\" \")}`.toLowerCase().includes(needle);\n  });\n  const enabled = matches.filter((option) => !option.disabled);\n\n  function highlight(text: string) {\n    if (!needle) return text;\n    const at = text.toLowerCase().indexOf(needle);\n    if (at < 0) return text;\n    return (\n      <>\n        {text.slice(0, at)}\n        <mark data-slot=\"combobox-match\">{text.slice(at, at + needle.length)}</mark>\n        {text.slice(at + needle.length)}\n      </>\n    );\n  }\n\n  function choose(next: string) {\n    if (value == null) setLocal(next);\n    onValueChange?.(next);\n    setQuery(\"\");\n    setOpen(false);\n  }\n\n  useEffect(() => {\n    if (!open) return;\n    const onPointer = (event: PointerEvent) => {\n      if (!root.current?.contains(event.target as Node)) setOpen(false);\n    };\n    document.addEventListener(\"pointerdown\", onPointer);\n    return () => document.removeEventListener(\"pointerdown\", onPointer);\n  }, [open]);\n\n  return (\n    <div ref={root} className={cn(\"grid max-w-sm gap-1.5\", className)}>\n      <Label htmlFor={fieldId}>{label}</Label>\n      <div className=\"relative\">\n        <input\n          id={fieldId}\n          data-slot=\"input\"\n          role=\"combobox\"\n          disabled={disabled}\n          aria-expanded={open}\n          aria-controls={listId}\n          aria-autocomplete=\"list\"\n          aria-activedescendant={open && matches[active] ? `${listId}-${matches[active].value}` : undefined}\n          placeholder={placeholder}\n          value={open ? query : (selected?.label ?? \"\")}\n          className=\"h-[var(--control-height)] w-full rounded-button pr-9 pl-3 text-base outline-none placeholder:text-muted-foreground md:text-sm\"\n          onFocus={() => {\n            setQuery(\"\");\n            setOpen(true);\n          }}\n          onChange={(event) => {\n            setQuery(event.currentTarget.value);\n            setOpen(true);\n            setActive(0);\n          }}\n          onKeyDown={(event) => {\n            if (event.key === \"ArrowDown\" || event.key === \"ArrowUp\") {\n              event.preventDefault();\n              setOpen(true);\n              setActive((index) => {\n                const step = event.key === \"ArrowDown\" ? 1 : -1;\n                let next = index;\n                for (let count = 0; count < matches.length; count += 1) {\n                  next = (next + step + matches.length) % matches.length;\n                  if (!matches[next]?.disabled) return next;\n                }\n                return index;\n              });\n            } else if (event.key === \"Enter\" && open && enabled[0]) {\n              event.preventDefault();\n              const pick = matches[active] && !matches[active].disabled ? matches[active] : enabled[0];\n              if (pick) choose(pick.value);\n            } else if (event.key === \"Escape\") {\n              setQuery(\"\");\n              setOpen(false);\n            }\n          }}\n        />\n        <ChevronDown\n          aria-hidden=\"true\"\n          className={cn(\n            \"pointer-events-none absolute top-1/2 right-3 size-4 -translate-y-1/2 text-muted-foreground transition-transform duration-[var(--duration-fast)] ease-out motion-reduce:transition-none\",\n            open && \"rotate-180\",\n            disabled && \"opacity-45\",\n          )}\n        />\n        {open && <div id={listId} role=\"listbox\" data-slot=\"combobox-menu\" data-open=\"true\" aria-label={label}>\n          <div className=\"p-1\">\n            {matches.length === 0 ? (\n              <div role=\"status\" className=\"grid gap-1 px-2 py-3\">\n                <p className=\"text-sm font-medium\">No matches for “{query.trim()}”</p>\n                <p className=\"text-xs text-muted-foreground\">Check the spelling{onCreate ? \", or add it.\" : \".\"}</p>\n                {onCreate && query.trim() && (\n                  <button\n                    type=\"button\"\n                    className=\"mt-2 flex h-[var(--control-height)] w-full items-center gap-2 rounded-control bg-muted px-2 text-left text-sm font-medium outline-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-ring\"\n                    onMouseDown={(event) => event.preventDefault()}\n                    onClick={() => {\n                      onCreate(query.trim());\n                      setQuery(\"\");\n                      setOpen(false);\n                    }}\n                  >\n                    <Plus aria-hidden=\"true\" className=\"size-4\" />\n                    {createLabel(query.trim())}\n                  </button>\n                )}\n              </div>\n            ) : (\n              matches.map((option, index) => (\n                <button\n                  key={option.value}\n                  id={`${listId}-${option.value}`}\n                  type=\"button\"\n                  role=\"option\"\n                  aria-selected={option.value === current}\n                  aria-disabled={option.disabled || undefined}\n                  data-highlighted={index === active || undefined}\n                  className=\"flex h-[var(--control-height)] w-full items-center justify-between rounded-surface px-2 text-left text-sm font-medium outline-none data-[highlighted]:bg-muted aria-disabled:opacity-45 aria-selected:font-semibold\"\n                  onMouseDown={(event) => event.preventDefault()}\n                  onMouseEnter={() => setActive(index)}\n                  onClick={() => {\n                    if (!option.disabled) choose(option.value);\n                  }}\n                >\n                  <span className=\"flex min-w-0 items-center gap-2\">\n                    <Check aria-hidden=\"true\" className={cn(\"size-3.5 shrink-0\", option.value !== current && \"opacity-0\")} />\n                    <span className=\"truncate\">{highlight(option.label)}</span>\n                  </span>\n                  {option.group && <span className=\"text-xs font-normal text-muted-foreground\">{option.group}</span>}\n                </button>\n              ))\n            )}\n          </div>\n        </div>}\n        {name ? <input type=\"hidden\" name={name} value={current} /> : null}\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {open && needle ? `${matches.length} ${matches.length === 1 ? \"result\" : \"results\"}` : \"\"}\n        </span>\n      </div>\n    </div>\n  );\n}\n"},{"path":"ui/label.tsx","type":"registry:ui","target":"components/ui/label.tsx","content":"import type { ComponentProps } from \"react\";\nimport { cn } from \"./lib/cn\";\n\nexport function Label({ className, ...props }: ComponentProps<\"label\">) {\n  return (\n    <label\n      data-slot=\"label\"\n      className={cn(\n        \"inline-block max-w-full text-sm leading-5 font-medium [overflow-wrap:anywhere]\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n"},{"path":"ui/lib/cn.ts","type":"registry:ui","target":"components/ui/lib/cn.ts","content":"import { clsx, type ClassValue } from \"clsx\";\nimport { extendTailwindMerge } from \"tailwind-merge\";\n\nconst twMerge = extendTailwindMerge({\n  extend: { theme: { radius: [\"control\", \"surface\", \"overlay\"], shadow: [\"overlay\"] } },\n});\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n"},{"path":"ui/shared/input.css","type":"registry:file","target":"components/ui/shared/input.css","content":"/* Shared by combobox.tsx, input.tsx; each imports it. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  [data-slot=\"input\"] {\n    transition-property: background-color, color, border-color;\n    transition-duration: var(--duration-fast);\n    transition-timing-function: var(--ease-out);\n  }\n\n  /* Recessed to fill. One ring for every field: 3:1 at rest, darker on\n     hover, a 2px ember ring with a soft halo on focus, danger when invalid.\n     Disabled and read-only drop into the sunken fill and stay legible. */\n  [data-slot=\"input\"] {\n    border: 0;\n    background-color: var(--field);\n    box-shadow: var(--shadow-field);\n    transition: box-shadow var(--duration-fast) var(--ease-out);\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"input\"] {\n      transition: none;\n    }\n  }\n}\n"},{"path":"ui/shared/input-2.css","type":"registry:file","target":"components/ui/shared/input-2.css","content":"/* Shared by combobox.tsx, input.tsx, select-menu.tsx, select.tsx, textarea.tsx; each imports it. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  @media (hover: hover) {\n    :is([data-slot=\"input\"], [data-slot=\"textarea\"], [data-slot=\"select\"]):hover:enabled:not(:focus-visible):not([aria-invalid=\"true\"]) {\n      box-shadow: var(--shadow-field-hover);\n    }\n  }\n  :is([data-slot=\"input\"], [data-slot=\"textarea\"], [data-slot=\"select\"]):focus-visible {\n    outline: none;\n    box-shadow: var(--shadow-field-focus);\n    transition: none;\n  }\n  :is([data-slot=\"input\"], [data-slot=\"textarea\"], [data-slot=\"select\"])[aria-invalid=\"true\"] {\n    box-shadow: var(--shadow-field-invalid);\n  }\n  :is([data-slot=\"input\"], [data-slot=\"textarea\"], [data-slot=\"select\"])[aria-invalid=\"true\"]:focus-visible {\n    box-shadow:\n      var(--shadow-field-invalid),\n      0 0 0 4px color-mix(in srgb, var(--danger) 18%, transparent);\n  }\n  :is([data-slot=\"input\"], [data-slot=\"textarea\"], [data-slot=\"select\"]):disabled {\n    background-color: var(--sunken);\n    box-shadow: var(--shadow-field-quiet);\n    color: var(--muted-foreground);\n  }\n  :is([data-slot=\"input\"], [data-slot=\"textarea\"])[readonly] {\n    background-color: var(--sunken);\n    box-shadow: var(--shadow-field-quiet);\n  }\n}\n"},{"path":"ui/combobox.css","type":"registry:file","target":"components/ui/combobox.css","content":"/* combobox.tsx's styles; it imports this file. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  [data-slot=\"combobox-match\"] {\n    background: none;\n    color: inherit;\n    font-weight: 600;\n    text-decoration: underline;\n    text-decoration-color: var(--ring);\n    text-decoration-thickness: 1.5px;\n    text-underline-offset: 3px;\n  }\n  [data-slot=\"combobox-menu\"] {\n    position: absolute;\n    top: calc(100% + 0.25rem);\n    left: 0;\n    z-index: 20;\n    display: grid;\n    width: 100%;\n    grid-template-rows: 0fr;\n    overflow: hidden;\n    border: 1px solid var(--border);\n    border-radius: var(--radius-overlay);\n    transition: grid-template-rows var(--duration-travel) var(--ease-showcase);\n  }\n  [data-slot=\"combobox-menu\"][data-open=\"true\"] {\n    grid-template-rows: 1fr;\n  }\n  [data-slot=\"combobox-menu\"] > div {\n    min-height: 0;\n    max-height: 15rem;\n    overflow: auto;\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"combobox-menu\"] {\n      transition: grid-template-rows 400ms var(--ease-showcase);\n    }\n  }\n}\n\n[data-slot=\"combobox-menu\"] {\n  background-color: var(--elevated-glass);\n  border-color: color-mix(in srgb, var(--edge-light) 45%, var(--border));\n  backdrop-filter: blur(var(--blur-overlay));\n  box-shadow:\n    var(--shadow-overlay),\n    0 0 40px var(--edge-glow),\n    inset 0 1px 0 var(--edge-light);\n}\n"}],"categories":["inputs"],"meta":{"tier":"free","license":"MIT","family":"combobox","docs":"/docs/components/combobox"}}