{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "combobox",
  "title": "Combobox",
  "description": "A shadcn-style combobox component built with Ark UI primitives.",
  "dependencies": ["@ark-ui/react", "lucide-react"],
  "registryDependencies": ["button", "input-group"],
  "files": [
    {
      "path": "src/components/ui/combobox.tsx",
      "content": "\"use client\";\n\nimport {\n  type CollectionItem,\n  type ComboboxInputValueChangeDetails,\n  Combobox as ComboboxPrimitive,\n  type ListCollection,\n  type UseComboboxContext,\n  useComboboxContext,\n  useListCollection,\n} from \"@ark-ui/react/combobox\";\nimport { useFilter } from \"@ark-ui/react/locale\";\nimport { Portal } from \"@ark-ui/react/portal\";\nimport { CheckIcon, ChevronsUpDownIcon, XIcon } from \"lucide-react\";\nimport {\n  type ComponentProps,\n  Fragment,\n  type ReactNode,\n  useCallback,\n  useEffect,\n  useMemo,\n} from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport {\n  InputGroup,\n  InputGroupAddon,\n  InputGroupInput,\n} from \"@/components/ui/input-group\";\nimport { cn } from \"@/lib/utils\";\n\n/** Default item shape when you omit an explicit generic on list helpers. */\nexport type DefaultComboboxItem = { label: string; value: string };\n\n/**\n * Narrows `collection` from context. Prefer passing `collection` from\n * `Combobox`’s `renderChildren` so `T` is inferred from `ListCollection<T>`.\n */\nexport const useComboboxListCollection = <\n  T extends CollectionItem = CollectionItem,\n>() => {\n  const { collection } = useComboboxContext();\n  return collection as ListCollection<T>;\n};\n\nexport type ComboboxProps<T extends CollectionItem = CollectionItem> = Omit<\n  ComboboxPrimitive.RootProps<T>,\n  \"collection\" | \"children\"\n> & { children: ReactNode | ((api: UseComboboxContext<T>) => ReactNode) } & (\n    | {\n        items: readonly T[];\n        groupBy?: (item: T, index: number) => string;\n        groupSort?:\n          | string[]\n          | \"asc\"\n          | \"desc\"\n          | ((a: string, b: string) => number);\n        limit?: number;\n        itemToString?: (item: T) => string;\n        itemToValue?: (item: T) => string;\n        isItemDisabled?: (item: T) => boolean;\n\n        filterType?: \"contains\" | \"endsWith\" | \"startsWith\";\n      }\n    | { collection: ListCollection<T> }\n  );\n\n/** Optional keys for `items`/`collection` branches; used only to destructure off `ComboboxPrimitive.Root` props. */\ntype ComboboxListCollectionKeys<T extends CollectionItem> = {\n  items?: readonly T[];\n  groupBy?: (item: T, index: number) => string;\n  groupSort?: string[] | \"asc\" | \"desc\" | ((a: string, b: string) => number);\n  limit?: number;\n  itemToString?: (item: T) => string;\n  itemToValue?: (item: T) => string;\n  isItemDisabled?: (item: T) => boolean;\n  filterType?: \"contains\" | \"endsWith\" | \"startsWith\";\n  collection?: ListCollection<T>;\n};\n\nexport const Combobox = <T extends CollectionItem = CollectionItem>(\n  props: ComboboxProps<T>,\n) => {\n  const {\n    onInputValueChange,\n    positioning,\n    children,\n    items: itemsFromProps,\n    groupBy,\n    groupSort,\n    limit,\n    itemToString,\n    itemToValue,\n    isItemDisabled,\n    filterType = \"contains\",\n    collection: collectionFromProps,\n    ...rootProps\n  } = props as ComboboxProps<T> & ComboboxListCollectionKeys<T>;\n  const items = useMemo(\n    () => (itemsFromProps !== undefined ? itemsFromProps : []),\n    [itemsFromProps],\n  );\n\n  const { contains, endsWith, startsWith } = useFilter({ sensitivity: \"base\" });\n\n  const {\n    collection: defaultCollection,\n    filter,\n    set,\n  } = useListCollection<T>({\n    initialItems: items,\n    filter:\n      filterType === \"contains\"\n        ? contains\n        : filterType === \"endsWith\"\n          ? endsWith\n          : startsWith,\n    groupBy,\n    groupSort,\n    limit,\n    itemToString,\n    itemToValue,\n    isItemDisabled,\n  });\n\n  const itemsKey = useMemo(\n    () =>\n      items\n        .map((item: T) =>\n          String(\n            itemToValue?.(item) ?? (item as { value?: string }).value ?? \"\",\n          ),\n        )\n        .join(\"\\uffff\"),\n    [items, itemToValue],\n  );\n\n  useEffect(() => {\n    set([...items]);\n  }, [itemsKey]);\n\n  const handleInputValueChange = useCallback(\n    (details: ComboboxInputValueChangeDetails) => {\n      filter(details.inputValue);\n      onInputValueChange?.(details);\n    },\n    [filter, onInputValueChange],\n  );\n\n  return (\n    <ComboboxPrimitive.Root\n      collection={collectionFromProps ?? defaultCollection}\n      onInputValueChange={\n        collectionFromProps ? onInputValueChange : handleInputValueChange\n      }\n      positioning={\n        positioning ?? { placement: \"bottom-start\", sameWidth: true }\n      }\n      {...rootProps}\n    >\n      <ComboboxPrimitive.Context>\n        {(api) => {\n          return typeof children === \"function\"\n            ? // Render-prop `api` is typed wider than our generic `T`; safe at runtime when `items`/`collection` match `T`.\n              children(api as UseComboboxContext<T>)\n            : children;\n        }}\n      </ComboboxPrimitive.Context>\n    </ComboboxPrimitive.Root>\n  );\n};\n\nexport const ComboboxControl = ({\n  ...props\n}: ComboboxPrimitive.ControlProps) => {\n  return <ComboboxPrimitive.Control data-slot=\"combobox-control\" {...props} />;\n};\n\nexport type ComboboxInputProps = Omit<\n  ComboboxPrimitive.InputProps,\n  \"size\" | \"asChild\"\n> & {\n  size?: \"default\" | \"lg\" | \"sm\";\n  startAddon?: ReactNode;\n  endAddon?: ReactNode;\n  showClear?: boolean;\n  showIndicator?: boolean;\n};\n\nexport const ComboboxInput = ({\n  startAddon,\n  endAddon,\n  showClear = true,\n  showIndicator = true,\n  size = \"default\",\n  ...props\n}: ComboboxInputProps) => {\n  return (\n    <ComboboxPrimitive.Control asChild {...props}>\n      <InputGroup>\n        {startAddon && (\n          <InputGroupAddon align={\"inline-start\"}>{startAddon}</InputGroupAddon>\n        )}\n        <ComboboxPrimitive.Input data-slot=\"combobox-input\" {...props} asChild>\n          <InputGroupInput size={size} />\n        </ComboboxPrimitive.Input>\n\n        {(endAddon || showClear || showIndicator) && (\n          <InputGroupAddon align={\"inline-end\"}>\n            {endAddon}\n            {showClear && (\n              <ComboboxClearTrigger asChild>\n                <Button variant=\"ghost\" size={\"icon-sm\"}>\n                  <XIcon />\n                </Button>\n              </ComboboxClearTrigger>\n            )}\n            {showIndicator && (\n              <ComboboxTrigger asChild>\n                <Button variant=\"ghost\" size={\"icon-sm\"}>\n                  <ChevronsUpDownIcon />\n                </Button>\n              </ComboboxTrigger>\n            )}\n          </InputGroupAddon>\n        )}\n      </InputGroup>\n    </ComboboxPrimitive.Control>\n  );\n};\n\nexport const ComboboxPopup = ({\n  className,\n  disablePortal,\n  ...props\n}: ComboboxPrimitive.ContentProps & { disablePortal?: boolean }) => {\n  const inner = (\n    <ComboboxPrimitive.Content\n      className={cn(\n        \"flex max-h-[min(var(--available-height,20rem),20rem)] flex-col overflow-y-auto overscroll-contain rounded-lg border border-border bg-popover p-0.5 text-popover-foreground shadow-md focus-visible:outline-none\",\n        !disablePortal && \"z-50\",\n        className,\n      )}\n      data-slot=\"combobox-content\"\n      {...props}\n    />\n  );\n  return disablePortal ? (\n    inner\n  ) : (\n    <Portal>\n      <ComboboxPrimitive.Positioner>{inner} </ComboboxPrimitive.Positioner>\n    </Portal>\n  );\n};\n\nexport type ComboboxListProps<T extends CollectionItem = CollectionItem> = {\n  className?: string;\n  items: readonly T[];\n  children: (item: T) => ReactNode;\n};\n\nexport const ComboboxList = <T extends CollectionItem = CollectionItem>({\n  items,\n  className,\n  children,\n}: ComboboxListProps<T>) => {\n  const { collection } = useComboboxContext();\n\n  return (\n    <ComboboxPrimitive.List\n      className={cn(\"flex flex-col gap-0.5 p-0.5\", className)}\n      data-slot=\"combobox-list\"\n    >\n      {collection.items.map((item: (typeof items)[number]) => {\n        const key = collection.getItemValue(item) ?? \"\";\n        return <Fragment key={key}>{children(item)}</Fragment>;\n      })}\n    </ComboboxPrimitive.List>\n  );\n};\n\nexport type ComboboxGroupedListProps<\n  T extends CollectionItem = CollectionItem,\n> = {\n  className?: string;\n  items: readonly T[];\n  children: (group: readonly [string, T[]]) => ReactNode;\n};\n\nexport const ComboboxGroupedList = <T extends CollectionItem = CollectionItem>({\n  items,\n  className,\n  children,\n}: ComboboxGroupedListProps<T>) => {\n  const { collection } = useComboboxContext();\n  const groups = collection.group();\n  return (\n    <div\n      className={cn(\"flex flex-col gap-1 p-0.5\", className)}\n      data-slot=\"combobox-grouped-list\"\n    >\n      {groups.map((tuple: readonly [string, (typeof items)[number][]]) => (\n        <Fragment key={tuple[0]}>{children(tuple)}</Fragment>\n      ))}\n    </div>\n  );\n};\n\nexport const ComboboxEmpty = ({\n  className,\n  ...props\n}: ComboboxPrimitive.EmptyProps) => (\n  <ComboboxPrimitive.Empty\n    className={cn(\n      \"rounded-md px-2 py-3 text-center text-muted-foreground text-sm\",\n      className,\n    )}\n    data-slot=\"combobox-empty\"\n    {...props}\n  />\n);\n\nexport const ComboboxItem = ({\n  className,\n  ...props\n}: ComboboxPrimitive.ItemProps) => (\n  <ComboboxPrimitive.Item\n    className={cn(\n      \"relative flex cursor-pointer select-none items-center gap-2 rounded-md px-2 py-1.5 text-sm outline-none data-disabled:pointer-events-none data-disabled:opacity-50 data-highlighted:bg-accent data-highlighted:text-accent-foreground\",\n      className,\n    )}\n    data-slot=\"combobox-item\"\n    {...props}\n  />\n);\n\nexport const ComboboxItemText = ({\n  className,\n  ...props\n}: ComboboxPrimitive.ItemTextProps) => (\n  <ComboboxPrimitive.ItemText\n    className={cn(\"min-w-0 flex-1 truncate\", className)}\n    data-slot=\"combobox-item-text\"\n    {...props}\n  />\n);\n\nexport const ComboboxItemIndicator = ({\n  className,\n  children,\n  ...props\n}: ComboboxPrimitive.ItemIndicatorProps) => (\n  <ComboboxPrimitive.ItemIndicator\n    className={cn(\"text-primary\", className)}\n    data-slot=\"combobox-item-indicator\"\n    {...props}\n  >\n    {children ?? <CheckIcon className=\"size-4\" />}\n  </ComboboxPrimitive.ItemIndicator>\n);\n\nexport const ComboboxItemGroup = ({\n  className,\n  ...props\n}: ComboboxPrimitive.ItemGroupProps) => (\n  <ComboboxPrimitive.ItemGroup\n    className={cn(\"flex flex-col gap-0.5\", className)}\n    data-slot=\"combobox-item-group\"\n    {...props}\n  />\n);\n\nexport const ComboboxItemGroupLabel = ({\n  className,\n  ...props\n}: ComboboxPrimitive.ItemGroupLabelProps) => (\n  <ComboboxPrimitive.ItemGroupLabel\n    className={cn(\n      \"px-2 py-1 font-medium text-muted-foreground text-xs uppercase tracking-wide\",\n      className,\n    )}\n    data-slot=\"combobox-item-group-label\"\n    {...props}\n  />\n);\n\nexport const ComboboxSeparator = ({\n  className,\n  ...props\n}: ComponentProps<\"div\">) => (\n  <div\n    role=\"separator\"\n    className={cn(\"-mx-0.5 my-1 h-px bg-border\", className)}\n    data-slot=\"combobox-separator\"\n    {...props}\n  />\n);\n\nexport const ComboboxLabel = ({\n  className,\n  ...props\n}: ComboboxPrimitive.LabelProps) => (\n  <ComboboxPrimitive.Label\n    className={cn(\n      \"mb-1 block font-medium text-foreground text-sm select-none data-disabled:opacity-50\",\n      className,\n    )}\n    data-slot=\"combobox-label\"\n    {...props}\n  />\n);\n\nexport const ComboboxTrigger = ({\n  ...props\n}: ComboboxPrimitive.TriggerProps) => (\n  <ComboboxPrimitive.Trigger data-slot=\"combobox-trigger-raw\" {...props} />\n);\n\nexport const ComboboxClearTrigger = ({\n  ...props\n}: ComboboxPrimitive.ClearTriggerProps) => (\n  <ComboboxPrimitive.ClearTrigger\n    data-slot=\"combobox-clear-trigger-raw\"\n    {...props}\n  />\n);\n\nexport const ComboboxValue = ({\n  children,\n  placeholder,\n  className,\n}: {\n  children?: (api: ComboboxPrimitive.ContextProps<CollectionItem>) => ReactNode;\n  placeholder?: string;\n  className?: string;\n}) => (\n  <ComboboxPrimitive.Context>\n    {(api) => {\n      if (children) {\n        return (\n          <>\n            {/* `Context` callback api vs `ContextProps` differ in Ark's types; double assertion is intentional. */}\n            {children(\n              api as unknown as ComboboxPrimitive.ContextProps<CollectionItem>,\n            )}\n          </>\n        );\n      }\n      const text = api.valueAsString?.trim()\n        ? api.valueAsString\n        : api.collection.stringifyItems(api.selectedItems, \", \");\n      return (\n        <span\n          className={cn(\n            \"truncate\",\n            !text && \"text-muted-foreground\",\n            className,\n          )}\n          data-slot=\"combobox-value\"\n        >\n          {text || placeholder}\n        </span>\n      );\n    }}\n  </ComboboxPrimitive.Context>\n);\n\nexport const ComboboxChips = ({\n  className,\n  startAddon,\n  endAddon,\n  children,\n  ...props\n}: ComboboxPrimitive.ControlProps & {\n  className?: string;\n  startAddon?: ReactNode;\n  children?: ReactNode;\n  endAddon?: ReactNode;\n}) => {\n  return (\n    <ComboboxPrimitive.Control\n      className={cn(\n        \"relative inline-flex min-h-9 w-full flex-wrap gap-1 rounded-lg border border-input bg-background not-dark:bg-clip-padding p-[calc(--spacing(1)-1px)] text-base shadow-xs/5 outline-none ring-ring/24 transition-shadow *:min-h-7 before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] not-has-disabled:not-focus-within:not-data-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-within:border-ring focus-within:ring-[3px] has-disabled:pointer-events-none has-data-[size=lg]:min-h-10 has-data-[size=sm]:min-h-8 has-data-invalid:border-destructive/36 has-autofill:bg-foreground/4 has-disabled:opacity-64 has-[:disabled,:focus-within,[data-invalid]]:shadow-none focus-within:has-data-invalid:border-destructive/64 focus-within:has-data-invalid:ring-destructive/16 has-data-[size=lg]:*:min-h-8 has-data-[size=sm]:*:min-h-6 sm:min-h-8 sm:text-sm sm:has-data-[size=lg]:min-h-9 sm:has-data-[size=sm]:min-h-7 sm:*:min-h-6 sm:has-data-[size=lg]:*:min-h-7 sm:has-data-[size=sm]:*:min-h-5 dark:not-has-disabled:bg-input/32 dark:has-autofill:bg-foreground/8 dark:has-data-invalid:ring-destructive/24 dark:not-has-disabled:not-focus-within:not-data-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)]\",\n        className,\n      )}\n      data-slot=\"combobox-chips\"\n      {...props}\n    >\n      {startAddon && (\n        <div\n          aria-hidden=\"true\"\n          className=\"flex shrink-0 items-center ps-2 opacity-80 has-[~[data-size=sm]]:has-[+[data-slot=combobox-chip]]:pe-1.5 has-[~[data-size=sm]]:ps-1.5 has-[+[data-slot=combobox-chip]]:pe-2\"\n          data-slot=\"combobox-start-addon\"\n        >\n          {startAddon}\n        </div>\n      )}\n      {children}\n      {endAddon && (\n        <div\n          aria-hidden=\"true\"\n          className=\"flex shrink-0 items-center pe-2 opacity-80 has-[~[data-size=sm]]:has-[+[data-slot=combobox-chip]]:ps-1.5 has-[~[data-size=sm]]:pe-1.5 has-[+[data-slot=combobox-chip]]:ps-2\"\n          data-slot=\"combobox-end-addon\"\n        >\n          {endAddon}\n        </div>\n      )}\n    </ComboboxPrimitive.Control>\n  );\n};\n\nexport type ComboboxChipsInputProps = Omit<\n  ComponentProps<typeof ComboboxPrimitive.Input>,\n  \"size\"\n>;\n\nexport const ComboboxChipsInput = ({\n  className,\n  ...props\n}: ComboboxChipsInputProps) => (\n  <ComboboxPrimitive.Input\n    className={cn(\n      \"min-w-18 flex-1 border-0 bg-transparent py-0.5 outline-none placeholder:text-muted-foreground/72\",\n      className,\n    )}\n    data-slot=\"combobox-chips-input\"\n    {...props}\n  />\n);\n\nexport const ComboboxChip = ({\n  className,\n  value,\n  children,\n  removeProps,\n  \"aria-label\": ariaLabel,\n}: {\n  className?: string;\n  value: string;\n  children: ReactNode;\n  removeProps?: ComponentProps<\"button\">;\n  \"aria-label\"?: string;\n}) => {\n  const { clearValue, disabled } = useComboboxContext();\n  return (\n    <span\n      aria-label={ariaLabel}\n      className={cn(\n        \"inline-flex h-6 max-w-full items-center gap-1 rounded-md border border-border bg-muted/50 px-1.5 text-xs\",\n        className,\n      )}\n      data-slot=\"combobox-chip\"\n    >\n      <span className=\"truncate\">{children}</span>\n      {!disabled && (\n        <button\n          type=\"button\"\n          className=\"cursor-pointer inline-flex size-5 shrink-0 items-center justify-center rounded text-muted-foreground hover:bg-background hover:text-foreground p-0.5 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background focus-visible:outline-none\"\n          onClick={() => {\n            clearValue(value);\n          }}\n          {...removeProps}\n        >\n          <XIcon className=\"size-3\" />\n        </button>\n      )}\n    </span>\n  );\n};\n\nexport const ComboboxProvider = <T extends CollectionItem = CollectionItem>({\n  ...props\n}: ComboboxPrimitive.RootProviderProps<T>) => {\n  return <ComboboxPrimitive.RootProvider {...props} />;\n};\n\nexport const ComboboxContext = ComboboxPrimitive.Context;\n",
      "type": "registry:ui",
      "target": "src/components/ui/combobox.tsx"
    }
  ],
  "type": "registry:ui"
}
