# Combobox

A field you type in to filter a list, boxed with the pen: pick one, or several as circled chips.

Docs: https://ballpoint.st9wd.com/docs/combobox

```bash
npx shadcn@latest add @ballpoint/combobox
```

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

const frameworks = ["Next.js", "SvelteKit", "Nuxt.js"]

<Combobox items={frameworks}>
  <ComboboxInput placeholder="Select a framework" />
  <ComboboxContent>
    <ComboboxEmpty>No framework found.</ComboboxEmpty>
    <ComboboxList>
      {(item) => <ComboboxItem key={item} value={item}>{item}</ComboboxItem>}
    </ComboboxList>
  </ComboboxContent>
</Combobox>
```

Props:

- `items`: `T[]`. What to choose from: strings, or objects with a value and label. Grouped items are { value, items }.
- `value / defaultValue`: `T`. The chosen item (an array with `multiple`), controlled or not.
- `multiple`: `boolean` (default `false`). Choose several; use ComboboxChips and ComboboxChip for the box.
- `ComboboxInput showTrigger / showClear`: `boolean` (default `true / false`). A chevron to open the list; a cross to empty the field.
- `ComboboxContent anchor`: `RefObject`. What the list lines up under. Use useComboboxAnchor() for the chips box.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `shadow`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Combobox: https://base-ui.com/react/components/combobox

## Example

```tsx
"use client";

import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox";

const frameworks = ["Next.js", "SvelteKit", "Nuxt.js", "Remix", "Astro"];

export default function ComboboxDemo() {
  return (
    <div className="w-full max-w-xs">
      <Combobox items={frameworks}>
        <ComboboxInput placeholder="Select a framework" aria-label="Framework" seed="cb-demo" />
        <ComboboxContent seed="cb-demo-list">
          <ComboboxEmpty>No framework found.</ComboboxEmpty>
          <ComboboxList>
            {(item: string) => (
              <ComboboxItem key={item} value={item}>
                {item}
              </ComboboxItem>
            )}
          </ComboboxList>
        </ComboboxContent>
      </Combobox>
    </div>
  );
}
```

## With a clear button

A cross that empties the field.

```tsx
"use client";

import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox";

const pens = ["Blue ballpoint", "Black gel", "Red felt tip", "Pencil", "Fountain pen"];

export default function ComboboxClear() {
  return (
    <div className="w-full max-w-xs">
      <Combobox items={pens} defaultValue={pens[0]}>
        <ComboboxInput placeholder="Choose a pen" aria-label="Pen" showClear seed="cbc" />
        <ComboboxContent seed="cbc-list">
          <ComboboxEmpty>No pen found.</ComboboxEmpty>
          <ComboboxList>
            {(item: string) => (
              <ComboboxItem key={item} value={item}>
                {item}
              </ComboboxItem>
            )}
          </ComboboxList>
        </ComboboxContent>
      </Combobox>
    </div>
  );
}
```

## Groups

Items under headings, ruled apart.

```tsx
"use client";

import {
  Combobox,
  ComboboxCollection,
  ComboboxContent,
  ComboboxEmpty,
  ComboboxGroup,
  ComboboxInput,
  ComboboxItem,
  ComboboxLabel,
  ComboboxList,
  ComboboxSeparator,
} from "@/components/ui/combobox";

const timezones = [
  { value: "Americas", items: ["(GMT-5) New York", "(GMT-8) Los Angeles", "(GMT-6) Chicago", "(GMT-3) São Paulo"] },
  { value: "Europe", items: ["(GMT+0) London", "(GMT+1) Paris", "(GMT+1) Berlin", "(GMT+2) Athens"] },
  { value: "Asia / Pacific", items: ["(GMT+9) Tokyo", "(GMT+8) Singapore", "(GMT+5:30) Mumbai", "(GMT+11) Sydney"] },
];

export default function ComboboxGroups() {
  return (
    <div className="w-full max-w-xs">
      <Combobox items={timezones}>
        <ComboboxInput placeholder="Select a timezone" aria-label="Timezone" seed="cbg" />
        <ComboboxContent seed="cbg-list">
          <ComboboxEmpty>No timezone found.</ComboboxEmpty>
          <ComboboxList>
            {(group: (typeof timezones)[number], index: number) => (
              <ComboboxGroup key={group.value} items={group.items}>
                <ComboboxLabel>{group.value}</ComboboxLabel>
                <ComboboxCollection>
                  {(item: string) => (
                    <ComboboxItem key={item} value={item}>
                      {item}
                    </ComboboxItem>
                  )}
                </ComboboxCollection>
                {index < timezones.length - 1 && <ComboboxSeparator />}
              </ComboboxGroup>
            )}
          </ComboboxList>
        </ComboboxContent>
      </Combobox>
    </div>
  );
}
```

## Multiple

Chosen items sit in the box as chips, with a cross each; the list opens under the box.

```tsx
"use client";

import {
  Combobox,
  ComboboxChip,
  ComboboxChips,
  ComboboxChipsInput,
  ComboboxContent,
  ComboboxEmpty,
  ComboboxItem,
  ComboboxList,
  ComboboxValue,
  useComboboxAnchor,
} from "@/components/ui/combobox";

const topics = ["Letters", "Sketches", "Lists", "Recipes", "Margins", "Maps"];

export default function ComboboxMultiple() {
  const anchor = useComboboxAnchor();
  return (
    <div className="w-full max-w-sm">
      <Combobox multiple items={topics} defaultValue={[topics[0], topics[1]]}>
        <ComboboxChips ref={anchor} seed="cbm">
          <ComboboxValue>
            {(values: string[]) => (
              <>
                {values.map((value) => (
                  <ComboboxChip key={value} seed={`cbm-${value}`}>
                    {value}
                  </ComboboxChip>
                ))}
                <ComboboxChipsInput aria-label="Topics" placeholder={values.length ? "" : "Pick some topics"} />
              </>
            )}
          </ComboboxValue>
        </ComboboxChips>
        <ComboboxContent anchor={anchor} seed="cbm-list">
          <ComboboxEmpty>No topics found.</ComboboxEmpty>
          <ComboboxList>
            {(item: string) => (
              <ComboboxItem key={item} value={item}>
                {item}
              </ComboboxItem>
            )}
          </ComboboxList>
        </ComboboxContent>
      </Combobox>
    </div>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `shadow`: `"hatch" | "solid" | "none"` (default `"hatch"`). What a lifted box leaves on the paper. "none" turns the lift off too.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
