{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "combobox",
  "type": "registry:ui",
  "title": "Combobox",
  "description": "A select with a search field: a button that shows the choice and opens a list that narrows as one types.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn",
    "@rivier-ui/command",
    "@rivier-ui/popover",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/combobox.tsx",
      "type": "registry:ui",
      "target": "components/ui/combobox.tsx",
      "content": "\"use client\";\n/**\n * @name combobox\n * @category ui\n * @description A select with a search field: a button that shows the choice and opens a list that narrows as one types.\n * @when Choosing one value from a list too long to scan: a country, a city, a person, a product. Under about eight options use select (or radio-group, or segmented-control for two to five); for running actions rather than choosing a value use command; for several values use chip or checkboxes. Name the trigger with a Label (`htmlFor` its `id`) or `aria-label`. Each item's `value` is what is stored; what is typed is matched against the item's text.\n * @example\n * const CITIES = [{ value: \"lis\", label: \"Lisbon\" }, { value: \"opo\", label: \"Porto\" }];\n * const [city, setCity] = useState(\"\");\n *\n * <Combobox value={city} onValueChange={setCity} name=\"city\">\n *   <ComboboxTrigger aria-label=\"City\" className=\"w-56\">\n *     <ComboboxValue placeholder=\"Choose a city\">{CITIES.find((item) => item.value === city)?.label}</ComboboxValue>\n *   </ComboboxTrigger>\n *   <ComboboxContent aria-label=\"Cities\">\n *     <ComboboxInput placeholder=\"Search cities\" />\n *     <ComboboxList>\n *       <ComboboxEmpty>No city found.</ComboboxEmpty>\n *       {CITIES.map((item) => (\n *         <ComboboxItem key={item.value} value={item.value}>{item.label}</ComboboxItem>\n *       ))}\n *     </ComboboxList>\n *   </ComboboxContent>\n * </Combobox>\n */\nimport { Check, ChevronsUpDown } from \"lucide-react\";\nimport { createContext, use, useId, type ComponentProps, type ReactNode } from \"react\";\nimport { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator } from \"@/components/ui/command\";\nimport { Popover, PopoverContent, PopoverTrigger } from \"@/components/ui/popover\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\ntype ComboboxContextValue = {\n  value: string;\n  /** Chooses a value and closes the list. */\n  choose: (value: string) => void;\n  open: boolean;\n  disabled: boolean;\n  selectedLabel: string;\n  /** The id of the floating panel, which the trigger says it controls. */\n  contentId: string;\n};\n\nconst ComboboxContext = createContext<ComboboxContextValue | null>(null);\n\nfunction useCombobox(part: string): ComboboxContextValue {\n  const context = use(ComboboxContext);\n  if (!context) throw new Error(`<${part}> must be used inside <Combobox>.`);\n  return context;\n}\n\nexport type ComboboxProps = {\n  /** The chosen item's value, controlled. \"\" means nothing is chosen. */\n  value?: string;\n  /** The starting value when uncontrolled. */\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  /** Whether the list is open, controlled. */\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  /** The field's name in a form: the value is submitted with it. */\n  name?: string;\n  disabled?: boolean;\n  /** Choosing the chosen item again clears the choice. Default false: a choice stays until another is made. */\n  clearable?: boolean;\n  /** Read after the chosen item's text in the list (\"Lisbon, selected\"). Translate it with the interface. */\n  selectedLabel?: string;\n  children?: ReactNode;\n};\n\n/** Holds the value and whether the list is open; both work controlled or not. */\nexport function Combobox({ value, defaultValue = \"\", onValueChange, open, defaultOpen = false, onOpenChange, name, disabled = false, clearable = false, selectedLabel = \"selected\", children }: ComboboxProps) {\n  const [current, setCurrent] = useControllableState({ value, defaultValue, onChange: onValueChange });\n  const [isOpen, setIsOpen] = useControllableState({ value: open, defaultValue: defaultOpen, onChange: onOpenChange });\n  const contentId = useId();\n\n  const choose = (next: string): void => {\n    setCurrent(next === current ? (clearable ? \"\" : current) : next);\n    setIsOpen(false);\n  };\n\n  return (\n    <ComboboxContext value={{ value: current, choose, open: isOpen, disabled, selectedLabel, contentId }}>\n      <Popover open={isOpen} onOpenChange={(next) => setIsOpen(disabled ? false : next)}>\n        {children}\n      </Popover>\n      {name ? <input type=\"hidden\" name={name} value={current} disabled={disabled} /> : null}\n    </ComboboxContext>\n  );\n}\n\nexport type ComboboxTriggerProps = ComponentProps<\"button\">;\n\n/**\n * The button that shows the choice. It is a `combobox` that says whether its list is open and which element\n * it controls; Enter, Space or a press opens the list, and focus comes back here when the list closes.\n */\nexport function ComboboxTrigger({ className, children, type = \"button\", disabled, ...props }: ComboboxTriggerProps) {\n  const context = useCombobox(\"ComboboxTrigger\");\n  return (\n    <PopoverTrigger asChild>\n      <button\n        type={type}\n        role=\"combobox\"\n        // Stated here, not left to the popover to add: a combobox must say whether it is open and what it controls.\n        aria-expanded={context.open}\n        aria-controls={context.open ? context.contentId : undefined}\n        aria-haspopup=\"dialog\"\n        data-slot=\"combobox-trigger\"\n        data-placeholder={context.value === \"\" ? \"\" : undefined}\n        disabled={disabled ?? context.disabled}\n        className={cn(\n          \"inline-flex h-control w-full max-w-full min-w-0 items-center justify-between gap-2 rounded-control border border-input bg-background px-3 text-start text-sm text-foreground transition-[background-color,border-color] duration-instant ease-out hover:bg-accent disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive motion-reduce:transition-none\",\n          className,\n        )}\n        {...props}\n      >\n        {children}\n        <ChevronsUpDown aria-hidden=\"true\" strokeWidth={1.5} className=\"size-4 shrink-0 text-muted-foreground\" />\n      </button>\n    </PopoverTrigger>\n  );\n}\n\nexport type ComboboxValueProps = Omit<ComponentProps<\"span\">, \"children\"> & {\n  /** Shown while nothing is chosen. */\n  placeholder?: ReactNode;\n  /** The chosen item's text, or a function that is given the value and returns it (for an uncontrolled combobox). */\n  children?: ReactNode | ((value: string) => ReactNode);\n};\n\n/** The chosen item's text inside the trigger (its children), or the placeholder while nothing is chosen. */\nexport function ComboboxValue({ className, placeholder, children, ...props }: ComboboxValueProps) {\n  const { value } = useCombobox(\"ComboboxValue\");\n  const content = typeof children === \"function\" ? children(value) : children;\n  const empty = value === \"\" || content === undefined || content === null;\n  return (\n    <span data-slot=\"combobox-value\" className={cn(\"min-w-0 flex-1 truncate\", empty && \"text-muted-foreground\", className)} {...props}>\n      {empty ? placeholder : content}\n    </span>\n  );\n}\n\nexport type ComboboxContentProps = ComponentProps<typeof PopoverContent> & {\n  /** Props for the list engine inside: `filter`, `shouldFilter` (false when the options come filtered from a server), `loop`. */\n  commandProps?: ComponentProps<typeof Command>;\n};\n\n/**\n * The floating panel: as wide as the trigger, never wider than the screen. It is a small dialog, so give it\n * a name with `aria-label` (\"Cities\"). The chosen item is the one marked when it opens.\n */\nexport function ComboboxContent({ className, children, align = \"start\", commandProps, \"aria-label\": ariaLabel = \"Options\", ...props }: ComboboxContentProps) {\n  const { value, contentId } = useCombobox(\"ComboboxContent\");\n  return (\n    <PopoverContent id={contentId} data-slot=\"combobox-content\" align={align} aria-label={ariaLabel} className={cn(\"w-(--radix-popover-trigger-width) min-w-56 overflow-hidden p-0\", className)} {...props}>\n      <Command label={ariaLabel} defaultValue={value || undefined} {...commandProps}>\n        {children}\n      </Command>\n    </PopoverContent>\n  );\n}\n\n/** The search field. Focus stays here while the arrow keys move through the list. Give it a `placeholder`. */\nexport function ComboboxInput(props: ComponentProps<typeof CommandInput>) {\n  return <CommandInput data-slot=\"combobox-input\" {...props} />;\n}\n\n/** The list (a `listbox`). Its `label` names it for screen readers: translate it with the interface. */\nexport function ComboboxList({ className, ...props }: ComponentProps<typeof CommandList>) {\n  return <CommandList data-slot=\"combobox-list\" className={cn(\"max-h-64\", className)} {...props} />;\n}\n\n/** Shown when nothing matches what was typed. */\nexport function ComboboxEmpty(props: ComponentProps<typeof CommandEmpty>) {\n  return <CommandEmpty data-slot=\"combobox-empty\" {...props} />;\n}\n\n/** A titled group of items: pass `heading`. */\nexport function ComboboxGroup(props: ComponentProps<typeof CommandGroup>) {\n  return <CommandGroup data-slot=\"combobox-group\" {...props} />;\n}\n\nexport function ComboboxSeparator(props: ComponentProps<typeof CommandSeparator>) {\n  return <CommandSeparator data-slot=\"combobox-separator\" {...props} />;\n}\n\nexport type ComboboxItemProps = Omit<ComponentProps<typeof CommandItem>, \"value\" | \"onSelect\"> & {\n  /** What is stored when this item is chosen. */\n  value: string;\n  /** More words to match typing against, besides the item's own text (which is matched when it is a plain string). */\n  keywords?: string[];\n  /** Called with the value as the item is chosen, before the list closes. */\n  onSelect?: (value: string) => void;\n};\n\n/** One option. The chosen one carries a tick, and \", selected\" for screen readers. */\nexport function ComboboxItem({ className, value, keywords, onSelect, children, ...props }: ComboboxItemProps) {\n  const context = useCombobox(\"ComboboxItem\");\n  const chosen = context.value === value;\n  const words = [...(typeof children === \"string\" ? [children] : []), ...(keywords ?? [])];\n  return (\n    <CommandItem\n      data-slot=\"combobox-item\"\n      data-chosen={chosen ? \"\" : undefined}\n      value={value}\n      keywords={words}\n      onSelect={() => {\n        onSelect?.(value);\n        context.choose(value);\n      }}\n      className={cn(chosen && \"font-medium\", className)}\n      {...props}\n    >\n      <span className=\"min-w-0 flex-1 truncate\">{children}</span>\n      {chosen ? (\n        <>\n          <Check aria-hidden=\"true\" strokeWidth={2} className=\"size-4 shrink-0\" />\n          <span className=\"sr-only\">, {context.selectedLabel}</span>\n        </>\n      ) : null}\n    </CommandItem>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Choosing one value from a list too long to scan: a country, a city, a person, a product. Under about eight options use select (or radio-group, or segmented-control for two to five); for running actions rather than choosing a value use command; for several values use chip or checkboxes. Name the trigger with a Label (`htmlFor` its `id`) or `aria-label`. Each item's `value` is what is stored; what is typed is matched against the item's text.",
    "example": "const CITIES = [{ value: \"lis\", label: \"Lisbon\" }, { value: \"opo\", label: \"Porto\" }];\nconst [city, setCity] = useState(\"\");\n\n<Combobox value={city} onValueChange={setCity} name=\"city\">\n  <ComboboxTrigger aria-label=\"City\" className=\"w-56\">\n    <ComboboxValue placeholder=\"Choose a city\">{CITIES.find((item) => item.value === city)?.label}</ComboboxValue>\n  </ComboboxTrigger>\n  <ComboboxContent aria-label=\"Cities\">\n    <ComboboxInput placeholder=\"Search cities\" />\n    <ComboboxList>\n      <ComboboxEmpty>No city found.</ComboboxEmpty>\n      {CITIES.map((item) => (\n        <ComboboxItem key={item.value} value={item.value}>{item.label}</ComboboxItem>\n      ))}\n    </ComboboxList>\n  </ComboboxContent>\n</Combobox>",
    "client": true
  }
}
