# Combobox
A text input that filters a list, searched and picked by a person or an agent.
Source page: https://dinachi.dev/docs/components/combobox

## Installation
```bash
npx @dinachi/cli@latest add combobox
```
Adds to your project:
- `components/ui/combobox/combobox.tsx`
- `components/ui/combobox/index.ts`
Installs:
- `@base-ui/react` ^1.9.0
- `lucide-react` ^0.552.0

## Usage
```tsx
import {
  Combobox,
  ComboboxContent,
  ComboboxEmpty,
  ComboboxInput,
  ComboboxInputGroup,
  ComboboxItem,
  ComboboxList,
  ComboboxTrigger,
} from "@/components/ui/combobox"

const fruits = ["Apple", "Banana", "Cherry", "Mango"]

export function FruitPicker() {
  return (
    <Combobox items={fruits}>
      <label htmlFor="fruit" className="text-sm font-medium">Fruit</label>
      <ComboboxInputGroup className="flex w-64 items-center rounded-md border">
        <ComboboxInput id="fruit" placeholder="Search fruit" />
        <ComboboxTrigger />
      </ComboboxInputGroup>
      <ComboboxContent>
        <ComboboxEmpty>No fruit found.</ComboboxEmpty>
        <ComboboxList>
          {(fruit: string) => (
            <ComboboxItem key={fruit} value={fruit}>
              {fruit}
            </ComboboxItem>
          )}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  )
}
```

## Anatomy
```tsx
<Combobox>
  <ComboboxLabel />
  <ComboboxInputGroup>
    <ComboboxInput />
    <ComboboxClear />
    <ComboboxTrigger />
  </ComboboxInputGroup>
  <ComboboxContent>
    <ComboboxEmpty />
    <ComboboxList>
      <ComboboxGroup>
        <ComboboxGroupLabel />
        <ComboboxItem />
      </ComboboxGroup>
      <ComboboxSeparator />
    </ComboboxList>
    <ComboboxStatus />
  </ComboboxContent>
</Combobox>
```
- `Combobox`: Holds value, input text and open state. No element.
- `ComboboxInputGroup`: Wraps the input and buttons; aligns the popup.
- `ComboboxInput`: The text field. Typing filters the list.
- `ComboboxTrigger`: The button that opens the list.
- `ComboboxClear`: A button that clears the value.
- `ComboboxLabel`: A visible label, associated with the trigger.
- `ComboboxContent`: The positioned popup, 4px from the field.
- `ComboboxList`: The scrolling list of options.
- `ComboboxItem`: One option, with a check while chosen.
- `ComboboxGroup`: Groups items under a `ComboboxGroupLabel`.
- `ComboboxGroupLabel`: The heading of a group.
- `ComboboxEmpty`: Shown when nothing matches. Needs root `items`.
- `ComboboxStatus`: Hidden polite announcement, such as loading status.
- `ComboboxSeparator`: A line between groups.
- `ComboboxValue`: Renders the current value. No element.
- `ComboboxCollection`: Renders a group's filtered items. No element.
- `ComboboxPortal`: Base UI's portal, for building a custom popup.
- `createComboboxItems`: Builds an item collection from your objects.

## Examples

### Default
Type to filter a list, then pick one.
```tsx
import {
  Combobox,
  ComboboxInputGroup,
  ComboboxInput,
  ComboboxTrigger,
  ComboboxContent,
  ComboboxList,
  ComboboxItem,
  ComboboxEmpty,
} from '@/components/ui/combobox';

const frameworks = ["React", "Vue", "Angular", "Svelte", "Solid", "Next.js", "Nuxt", "Remix"];

export function Example() {
  return (
    <Combobox items={frameworks} openOnInputClick>
      <div className="space-y-2">
        <label htmlFor="combobox-framework" className="block text-sm font-medium leading-none">Framework</label>
        <ComboboxInputGroup className="flex w-[280px] items-center gap-1 rounded-md border border-input focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
          <ComboboxInput id="combobox-framework" placeholder="Search frameworks..." />
          <ComboboxTrigger />
        </ComboboxInputGroup>
      </div>
      <ComboboxContent>
        <ComboboxEmpty>No frameworks found.</ComboboxEmpty>
        <ComboboxList>
          {(fw: string) => (
            <ComboboxItem key={fw} value={fw}>
              {fw}
            </ComboboxItem>
          )}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  );
}
```

### Groups
Options under labelled groups.
```tsx
import {
  Combobox,
  ComboboxInput,
  ComboboxTrigger,
  ComboboxContent,
  ComboboxList,
  ComboboxItem,
  ComboboxEmpty,
  ComboboxGroup,
  ComboboxGroupLabel,
} from '@/components/ui/combobox';

const languages = [
  { value: "Frontend", items: ["JavaScript", "TypeScript", "HTML", "CSS"] },
  { value: "Backend", items: ["Python", "Go", "Rust", "Java"] },
];

export function Example() {
  return (
    <Combobox items={languages} openOnInputClick>
      <div className="flex w-[280px] items-center gap-1 rounded-md border border-input focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
        <ComboboxInput placeholder="Search languages..." />
        <ComboboxTrigger />
      </div>
      <ComboboxContent>
        <ComboboxEmpty>No results found.</ComboboxEmpty>
        <ComboboxList>
          {(group: { value: string; items: string[] }) => (
            <ComboboxGroup key={group.value}>
              <ComboboxGroupLabel>{group.value}</ComboboxGroupLabel>
              {group.items.map((item) => (
                <ComboboxItem key={item} value={item}>
                  {item}
                </ComboboxItem>
              ))}
            </ComboboxGroup>
          )}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  );
}
```

### With clear
A clear button resets the value.
```tsx
import {
  Combobox,
  ComboboxInput,
  ComboboxTrigger,
  ComboboxClear,
  ComboboxContent,
  ComboboxList,
  ComboboxItem,
  ComboboxEmpty,
} from '@/components/ui/combobox';

const countries = ["United States", "United Kingdom", "Canada", "Australia", "Germany", "France", "Japan"];

export function Example() {
  return (
    <Combobox items={countries} openOnInputClick>
      <div className="flex w-[280px] items-center gap-1 rounded-md border border-input focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
        <ComboboxInput placeholder="Select a country..." />
        <ComboboxClear />
        <ComboboxTrigger />
      </div>
      <ComboboxContent>
        <ComboboxEmpty>No countries found.</ComboboxEmpty>
        <ComboboxList>
          {(country: string) => (
            <ComboboxItem key={country} value={country}>
              {country}
            </ComboboxItem>
          )}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  );
}
```

### Selecting by ID
Items built from objects, with the ID as the value.
```tsx
import {
  Combobox,
  ComboboxInput,
  ComboboxClear,
  ComboboxTrigger,
  ComboboxContent,
  ComboboxList,
  ComboboxItem,
  ComboboxEmpty,
  createComboboxItems,
} from '@/components/ui/combobox';
import { useState } from 'react';

type User = { id: string; name: string; email: string };

const users: User[] = [
  { id: "u1", name: "Ada Lovelace", email: "ada@example.com" },
  { id: "u2", name: "Grace Hopper", email: "grace@example.com" },
  { id: "u3", name: "Katherine Johnson", email: "katherine@example.com" },
  { id: "u4", name: "Linus Torvalds", email: "linus@example.com" },
];

const userItems = createComboboxItems(users, {
  getValue: (user) => user.id,
  getLabel: (user) => user.name,
});

export function Example() {
  const [value, setValue] = useState<string | null>(null);

  return (
    <div className="space-y-3">
      <Combobox items={userItems} value={value} onValueChange={setValue} openOnInputClick>
        <div className="flex w-[280px] items-center gap-1 rounded-md border border-input focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
          <ComboboxInput placeholder="Select a user..." />
          <ComboboxClear />
          <ComboboxTrigger />
        </div>
        <ComboboxContent>
          <ComboboxEmpty>No users found.</ComboboxEmpty>
          <ComboboxList>
            {(user: User) => (
              <ComboboxItem key={user.id} value={user.id}>
                {user.name}
              </ComboboxItem>
            )}
          </ComboboxList>
        </ComboboxContent>
      </Combobox>
      <p className="text-sm text-muted-foreground">Selected ID: {value ?? "none"}</p>
    </div>
  );
}
```

## Notes

### Selecting by ID
`createComboboxItems` derives each item's value and label, so `value` and `onValueChange` use IDs while the list receives the full object. Create the collection at module scope, or memoize it for runtime data:

```tsx
const userItems = React.useMemo(
  () => createComboboxItems(users, { getValue: (user) => user.id, getLabel: (user) => user.name }),
  [users]
)
```

### Naming the input
`ComboboxLabel` names the trigger. Name the input with a `<label htmlFor>` on its id, or `aria-label`, as in the default example. The agent reads the input's name.

### Rendering in place
`ComboboxContent` portals by default. Pass `portal={false}` to render inside its parent and inherit its stacking.

## API reference
Props marked **agent** change what an agent reads or may do.

### Combobox
Holds value, input text and open state. No element.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` **agent** | `Item[] \| Group[] \| ItemCollection` | - | The options. Needed for filtering; lets the agent pick while closed. |
| `value` | `Value \| Value[] \| null` | - | Controlled value. |
| `defaultValue` | `Value \| Value[] \| null` | `null, or [] when multiple` | Starting value when uncontrolled. |
| `onValueChange` **agent** | `(value, details) => void` | - | Runs for people and agents. `details.cancel()` refuses the change. |
| `onInputValueChange` **agent** | `(text, details) => void` | - | Runs as the input text changes, including when an agent searches. |
| `open` | `boolean` | - | Controlled open state. |
| `defaultOpen` | `boolean` | `false` | Starting open state when uncontrolled. |
| `onOpenChange` **agent** | `(open, details) => void` | - | Runs when the list opens or closes, from either side. |
| `multiple` **agent** | `boolean` | `false` | Holds an array of values. Agents also get `setValue`. |
| `openOnInputClick` | `boolean` | `true` | Opens the list when the input is clicked. |
| `autoHighlight` | `boolean` | `false` | Highlights the first match while filtering. |
| `itemToStringLabel` **agent** | `(value) => string` | - | Turns an object value into its label. The agent matches by it too. |
| `filter` | `(item, query) => boolean \| null` | - | Replaces the built-in filter. `null` turns filtering off. |
| `disabled` **agent** | `boolean` | `false` | Blocks people and agents. |
| `readOnly` **agent** | `boolean` | `false` | Refuses any change, from a person or an agent. |
| `name` | `string` | - | Submits the value with a form. |
| `required` | `boolean` | `false` | Must have a value before the form submits. |

### ComboboxInputGroup
Wraps the input and buttons; aligns the popup.

Takes the props of the element it renders.

### ComboboxInput
The text field. Typing filters the list.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `id` **agent** | `string` | - | Point a `<label htmlFor>` at it. The agent reads that label. |
| `placeholder` | `string` | - | Shown while empty. The agent's fallback name for the control. |

### ComboboxTrigger
The button that opens the list.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | `<ChevronDown />` | Replaces the icon. |

### ComboboxClear
A button that clears the value.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | `<X />` | Replaces the icon. |

### ComboboxLabel
A visible label, associated with the trigger.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | - | The label text. |

### ComboboxContent
The positioned popup, 4px from the field.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `portal` | `boolean` | `true` | Renders into the body. Pass false to render in place. |

### ComboboxList
The scrolling list of options.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode \| (item, index) => ReactNode` | - | A function renders each filtered item. |

### ComboboxItem
One option, with a check while chosen.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `Value` | - | What `onValueChange` receives. |
| `disabled` | `boolean` | `false` | Stays in the list but cannot be picked. |
| `inset` | `boolean` | `false` | Adds left padding for nested layouts. |

### ComboboxGroup
Groups items under a `ComboboxGroupLabel`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `Item[]` | - | The group's items, read by a `ComboboxCollection` inside it. |

### ComboboxGroupLabel
The heading of a group.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | - | Group name. Not selectable. |

### ComboboxEmpty
Shown when nothing matches. Needs root `items`.

Takes the props of the element it renders.

### ComboboxStatus
Hidden polite announcement, such as loading status.

Takes the props of the element it renders.

### ComboboxSeparator
A line between groups.

Takes the props of the element it renders.

### ComboboxValue
Renders the current value. No element.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `placeholder` | `ReactNode` | - | Shown while there is no value. |
| `children` | `ReactNode \| (value) => ReactNode` | - | Formats the value yourself. |

### ComboboxCollection
Renders a group's filtered items. No element.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `(item, index) => ReactNode` | - | Renders each item. |

### ComboboxPortal
Base UI's portal, for building a custom popup.

Takes the props of the element it renders.

### createComboboxItems
Builds an item collection from your objects.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `getValue` | `(item) => string \| number` | - | The value `onValueChange` receives, such as an id. |
| `getLabel` | `(item) => string` | - | The text shown and filtered on, and read by the agent. |

## Agent
Inside an `AgentProvider`, Combobox appears in `agent.snapshot()` as `combobox`.
Reads:
- `label`: The input's label, aria-label, or placeholder.
- `value`: The chosen label, a list of labels when multiple, or null.
- `open`: Whether the list is showing.
- `items`: Every option's label from items, or what the open list shows.
- `multiple`: Present when the combobox holds several values.
- `disabled`: True when the combobox is blocked.
Actions:
- `select("Svelte")`: Picks by label, then by value, open or closed. A miss lists the options.
- `search("sv")`: Types into the input and returns the labels that match.
- `open() / close()`: Whichever fits the current state.
- `setValue(["Vue", "Svelte"])`: Replaces the whole list. Only when multiple.
An agent's change runs through `onValueChange`, `onInputValueChange` and `onOpenChange` like anyone's. `isAgentChange(details)` is true for it, and `details.cancel()` refuses it; the agent is told the app refused. A disabled combobox refuses on its own, and `readOnly` refuses select, `setValue` and search.
```ts
await agent.act({
  steps: [{ ref: "Framework", action: "select", args: ["Svelte"] }],
})
```

## Accessibility
- The input has role="combobox" and controls the list popup.
- Give the input a name with a `<label htmlFor>` or aria-label. The agent reads the same name.
- `ComboboxEmpty` and `ComboboxStatus` announce changes politely.
- The list is a listbox; focus stays in the input as the highlight moves.
### Keyboard
| Keys | Does |
| --- | --- |
| `↓` / `↑` | Opens the list, then moves the highlight |
| `Enter` | Picks the highlighted item |
| `Esc` | Closes the list |

## Source
`components/ui/combobox/combobox.tsx`
```tsx
"use client"

import * as React from "react"
import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox"
import { Check, ChevronDown, X } from "lucide-react"
import { cn } from "@/lib/utils"
import {
  agentNodeText,
  changeAsAgent,
  createAgentChangeDetails,
  matchAgentChoice,
  sameAgentValue,
  useAgentControl,
  useAgentState,
  useHasAgent,
  useMergedRef,
  waitForAgent,
  type AgentChoice,
  type AgentControl,
} from "@/components/ui/agent"

type ElementRef = React.MutableRefObject<HTMLElement | null>

interface ComboboxAgent {
  input: ElementRef
  list: ElementRef
  /** Items in the open list, by element. */
  items: Map<HTMLElement, unknown>
  /** Set while the agent types, so the callbacks it causes carry the agent's details. */
  typing?: boolean
  refused?: boolean
}

const ComboboxAgentContext = React.createContext<ComboboxAgent | null>(null)

type Choice = AgentChoice<string> & { item: unknown }
type ToLabel = ((item: never) => string) | undefined

type Item = { label?: unknown; value?: unknown } | null | undefined

/** Base UI's own label for an item: `itemToStringLabel`, then `label`, then `value`. */
function labelOf(item: unknown, toLabel?: ToLabel): string {
  const o = item as Item
  if (toLabel && o != null) return (toLabel as (i: unknown) => string)(o) ?? ""
  return String(o?.label ?? o?.value ?? o ?? "")
}

const choiceOf = (item: unknown, label: string): Choice => ({
  item,
  label,
  value: String((item as Item)?.value ?? item),
})

const leavesOf = (data: readonly unknown[] = []) =>
  data.flatMap((e) => ((e as { items?: unknown })?.items instanceof Array ? (e as { items: unknown[] }).items : [e]))

/** Every choice the `items` prop lists, filtered or not, so the agent can choose while closed. */
function catalogOf(items: unknown, toLabel: ToLabel): Choice[] {
  const c = (
    Array.isArray(items)
      ? {
          data: items,
          value: (i: unknown) => i,
          itemLabel: (i: unknown) => labelOf(i, toLabel),
        }
      : items
  ) as
    | {
        data?: unknown[]
        value(i: unknown): unknown
        itemLabel(i: unknown): string
      }
    | undefined
  return c ? leavesOf(c.data).map((i) => choiceOf(c.value(i), c.itemLabel(i))) : []
}

const liveOf = (items: Map<HTMLElement, unknown>): Choice[] =>
  Array.from(items, ([el, item]) => ({
    ...choiceOf(item, agentNodeText(el.textContent)),
    disabled: el.hasAttribute("data-disabled"),
  }))

/** Hands Base UI's callback the agent's details while the agent types, and its refusal back. */
function relay<V, D extends { cancel(): void }>(s: ComboboxAgent, handler?: (value: V, details: D) => void) {
  return (value: V, details: D) => {
    if (!s.typing) return handler?.(value, details)
    const mine = createAgentChangeDetails<D & { isCanceled: boolean }>()
    handler?.(value, mine)
    if (mine.isCanceled) {
      s.refused = true
      details.cancel()
    }
  }
}

/** Types into the input the way a person would, so Base UI filters exactly as it does for them. */
function typeInto(s: ComboboxAgent, input: HTMLInputElement, text: string) {
  s.typing = true
  s.refused = false
  try {
    Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set?.call(input, text)
    input.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }))
    return s.refused ? "The app refused the change." : undefined
  } finally {
    s.typing = false
  }
}

/** Waits a turn for React to filter, then for the list to be open with every item registered. */
async function settled({ list, items }: ComboboxAgent) {
  await waitForAgent(
    () => list.current?.isConnected && list.current.querySelectorAll('[role="option"]').length === items.size
  )
}

interface ComboboxState {
  s: ComboboxAgent
  catalog: Choice[]
  value: unknown
  open: boolean
  props: RootProps
  change: (value: unknown) => string | undefined
  setOpen: (open: boolean) => string | undefined
}

const READ_ONLY = "The combobox is read-only."

/** Searches as a person would by typing, and returns what the list then offers. */
async function search(c: ComboboxState, text: unknown) {
  const query = String(text ?? "")
  if (c.props.readOnly) return READ_ONLY
  const notOpened = c.open ? undefined : c.setOpen(true)
  if (notOpened) return notOpened
  const refused = typeInto(c.s, c.s.input.current as HTMLInputElement, query)
  if (refused) {
    // Leave the list as the agent found it.
    if (!c.open) c.setOpen(false)
    return refused
  }
  await settled(c.s)
  return { result: liveOf(c.s.items).map((i) => i.label) }
}

function comboboxActions(c: ComboboxState, choices: () => Choice[]): AgentControl["actions"] {
  // Without an `items` prop, the choices are only known once the list renders them.
  // Returns whether it opened the list, so the action closes it again.
  const load = async () => {
    const opening = !c.open && !choices().length
    if (opening) c.setOpen(true)
    await waitForAgent(() => choices().length)
    return opening
  }
  const find = (wanted: unknown) => matchAgentChoice(choices(), wanted) as Choice | string
  const current: unknown[] = Array.isArray(c.value) ? c.value : []
  return {
    select: async (wanted: unknown) => {
      const opened = await load()
      const match = find(wanted)
      const close = (result: string | undefined) => {
        if (opened || (c.open && !c.props.multiple)) c.setOpen(false)
        return result
      }
      if (typeof match === "string") return close(match)
      if (c.props.multiple)
        return close(current.some((v) => sameAgentValue(v, match.item)) ? undefined : c.change([...current, match.item]))
      return close(c.change(match.item))
    },
    ...(c.props.multiple && {
      setValue: async (wanted: unknown) => {
        const opened = await load()
        const found = [wanted].flat().map(find)
        const result = found.find((m) => typeof m === "string") ?? c.change(found.map((m) => (m as Choice).item))
        if (opened) c.setOpen(false)
        return result
      },
    }),
    search: (text: unknown) => search(c, text),
    [c.open ? "close" : "open"]: () => c.setOpen(!c.open),
  }
}

function comboboxControl(c: ComboboxState): AgentControl {
  const { multiple, disabled, itemToStringLabel } = c.props
  const choices = () => (c.catalog.length ? c.catalog : liveOf(c.s.items))
  const labelFor = (v: unknown) =>
    choices().find((i) => sameAgentValue(i.item, v))?.label ?? labelOf(v, itemToStringLabel)
  const items = choices().map((i) => i.label)
  return {
    kind: "combobox",
    element: c.s.input.current,
    value: multiple ? (c.value as unknown[]).map(labelFor) : labelFor(c.value),
    open: c.open,
    disabled,
    multiple,
    items: items.length ? items : undefined,
    actions: comboboxActions(c, choices),
  }
}

type RootProps = ComboboxPrimitive.Root.Props<unknown, boolean | undefined> & {
  itemToStringLabel?: ToLabel
}
type Details = ComboboxPrimitive.Root.ChangeEventDetails
type OpenDetails = ComboboxPrimitive.Root.OpenChangeEventDetails

/**
 * Base UI's root, holding its value and open state inside AgentProvider so an agent can
 * set them the way a parent would. The input text stays Base UI's: the agent types it, so
 * filtering and the selected label behave as they do for a person. Every change, the
 * agent's included, goes through the matching callback first, which can cancel it.
 */
function AgentCombobox({
  value: valueProp,
  defaultValue,
  onValueChange,
  onInputValueChange,
  open: openProp,
  defaultOpen = false,
  onOpenChange,
  ...props
}: RootProps) {
  const [value, changeValue] = useAgentState<unknown, Details>(
    valueProp,
    defaultValue ?? (props.multiple ? [] : null),
    onValueChange as (v: unknown, d: Details) => void
  )
  const [open, changeOpen] = useAgentState<boolean, OpenDetails>(openProp, defaultOpen, onOpenChange)
  const [s] = React.useState<ComboboxAgent>(() => ({
    input: { current: null },
    list: { current: null },
    items: new Map(),
  }))

  useAgentControl(() =>
    comboboxControl({
      s,
      catalog: catalogOf(props.items, props.itemToStringLabel),
      value,
      open,
      props,
      change: (next) => (props.readOnly ? READ_ONLY : changeAsAgent(changeValue, next)),
      setOpen: (next) => changeAsAgent(changeOpen, next),
    })
  )

  return (
    <ComboboxAgentContext.Provider value={s}>
      <ComboboxPrimitive.Root
        {...(props as ComboboxPrimitive.Root.Props<unknown>)}
        value={value}
        onValueChange={relay(s, changeValue)}
        onInputValueChange={relay(s, onInputValueChange)}
        open={open}
        onOpenChange={relay(s, changeOpen)}
      />
    </ComboboxAgentContext.Provider>
  )
}

/** Base UI's root as it is, until an AgentProvider is around it. */
function ComboboxRoot(props: RootProps) {
  if (!useHasAgent()) return <ComboboxPrimitive.Root {...(props as ComboboxPrimitive.Root.Props<unknown>)} />
  return <AgentCombobox {...props} />
}

const Combobox = ComboboxRoot as unknown as typeof ComboboxPrimitive.Root
const ComboboxValue = ComboboxPrimitive.Value
const ComboboxCollection = ComboboxPrimitive.Collection
const ComboboxPortal = ComboboxPrimitive.Portal
function ComboboxInputGroup(props: React.ComponentProps<typeof ComboboxPrimitive.InputGroup>) {
  return <ComboboxPrimitive.InputGroup data-slot="combobox-input-group" {...props} />
}
const createComboboxItems = ComboboxPrimitive.createItems

const ComboboxInput = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Input>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Input>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.Input
    data-slot="combobox-input"
    ref={useMergedRef(ref, React.useContext(ComboboxAgentContext)?.input)}
    className={cn(
      "flex h-10 w-full bg-transparent px-3 py-2 text-sm",
      "placeholder:text-muted-foreground",
      "focus:outline-none",
      "disabled:cursor-not-allowed disabled:opacity-50",
      className
    )}
    {...props}
  />
))
ComboboxInput.displayName = "ComboboxInput"

const ComboboxTrigger = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
  <ComboboxPrimitive.Trigger
    data-slot="combobox-trigger"
    ref={ref}
    className={cn(
      "inline-flex h-10 items-center justify-center px-3",
      "focus-visible:outline-none",
      "disabled:pointer-events-none disabled:opacity-50",
      className
    )}
    {...props}
  >
    {children ?? (
      <ComboboxPrimitive.Icon>
        <ChevronDown className="h-4 w-4 text-muted-foreground" />
      </ComboboxPrimitive.Icon>
    )}
  </ComboboxPrimitive.Trigger>
))
ComboboxTrigger.displayName = "ComboboxTrigger"

const ComboboxClear = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Clear>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Clear>
>(({ className, children, ...props }, ref) => (
  <ComboboxPrimitive.Clear
    data-slot="combobox-clear"
    ref={ref}
    className={cn(
      "inline-flex h-8 w-8 items-center justify-center rounded-sm text-muted-foreground",
      "hover:bg-accent hover:text-accent-foreground",
      className
    )}
    {...props}
  >
    {children ?? <X className="h-4 w-4" />}
  </ComboboxPrimitive.Clear>
))
ComboboxClear.displayName = "ComboboxClear"

const ComboboxContent = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Popup>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Popup> & {
    readonly portal?: boolean
  }
>(({ className, portal = true, ...props }, ref) => {
  const content = (
    <ComboboxPrimitive.Positioner sideOffset={4}>
      <ComboboxPrimitive.Popup
        data-slot="combobox-content"
        ref={ref}
        className={cn(
          "relative z-50 min-w-[var(--anchor-width)] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md",
          "origin-[var(--transform-origin)] outline-none",
          "transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
          "data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
          "data-[ending-style]:scale-95 data-[ending-style]:opacity-0",
          "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
          className
        )}
        {...props}
      />
    </ComboboxPrimitive.Positioner>
  )

  if (!portal) return content

  return <ComboboxPortal>{content}</ComboboxPortal>
})
ComboboxContent.displayName = "ComboboxContent"

const ComboboxList = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.List>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.List>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.List
    data-slot="combobox-list"
    ref={useMergedRef(ref, React.useContext(ComboboxAgentContext)?.list)}
    className={cn("max-h-80 overflow-y-auto p-1", className)}
    {...props}
  />
))
ComboboxList.displayName = "ComboboxList"

const ComboboxItem = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Item>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Item> & {
    inset?: boolean
  }
>(({ className, children, inset, ...props }, ref) => {
  const combobox = React.useContext(ComboboxAgentContext)
  const element = React.useRef<HTMLDivElement | null>(null)
  const { value } = props

  React.useEffect(() => {
    const el = element.current
    if (!combobox || !el) return
    combobox.items.set(el, value)
    return () => {
      combobox.items.delete(el)
    }
  }, [combobox, value])

  return (
    <ComboboxPrimitive.Item
      data-slot="combobox-item"
      ref={useMergedRef(ref, element)}
      className={cn(
        "relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none",
        "data-highlighted:bg-accent data-highlighted:text-accent-foreground",
        "data-disabled:pointer-events-none data-disabled:opacity-50",
        inset && "pl-10",
        className
      )}
      {...props}
    >
      <span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
        <ComboboxPrimitive.ItemIndicator>
          <Check className="h-4 w-4" />
        </ComboboxPrimitive.ItemIndicator>
      </span>
      {children}
    </ComboboxPrimitive.Item>
  )
})
ComboboxItem.displayName = "ComboboxItem"

const ComboboxGroup = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Group>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Group>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.Group data-slot="combobox-group" ref={ref} className={cn(className)} {...props} />
))
ComboboxGroup.displayName = "ComboboxGroup"

const ComboboxGroupLabel = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.GroupLabel>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.GroupLabel>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.GroupLabel
    data-slot="combobox-group-label"
    ref={ref}
    className={cn("px-2 py-1.5 text-xs font-medium text-muted-foreground", className)}
    {...props}
  />
))
ComboboxGroupLabel.displayName = "ComboboxGroupLabel"

const ComboboxEmpty = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Empty>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Empty>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.Empty
    data-slot="combobox-empty"
    ref={ref}
    className={cn("p-2 text-center text-sm text-muted-foreground", className)}
    {...props}
  />
))
ComboboxEmpty.displayName = "ComboboxEmpty"

const ComboboxStatus = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Status>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Status>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.Status data-slot="combobox-status" ref={ref} className={cn("sr-only", className)} {...props} />
))
ComboboxStatus.displayName = "ComboboxStatus"

const ComboboxLabel = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Label>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Label>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.Label
    data-slot="combobox-label"
    ref={ref}
    className={cn("block text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70", className)}
    {...props}
  />
))
ComboboxLabel.displayName = "ComboboxLabel"

const ComboboxSeparator = React.forwardRef<
  React.ComponentRef<typeof ComboboxPrimitive.Separator>,
  React.ComponentPropsWithoutRef<typeof ComboboxPrimitive.Separator>
>(({ className, ...props }, ref) => (
  <ComboboxPrimitive.Separator
    data-slot="combobox-separator"
    ref={ref}
    className={cn("-mx-1 my-1 h-px bg-border", className)}
    {...props}
  />
))
ComboboxSeparator.displayName = "ComboboxSeparator"

export {
  Combobox,
  ComboboxValue,
  ComboboxCollection,
  ComboboxPortal,
  ComboboxInputGroup,
  createComboboxItems,
  ComboboxInput,
  ComboboxTrigger,
  ComboboxClear,
  ComboboxLabel,
  ComboboxContent,
  ComboboxList,
  ComboboxItem,
  ComboboxGroup,
  ComboboxGroupLabel,
  ComboboxEmpty,
  ComboboxStatus,
  ComboboxSeparator,
}
```
