Combobox
A searchable select with chips for multiple values, on Base UI Combobox.
With a label
"use client"
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@/components/ui/combobox"
import { Label } from "@/components/ui/label"
const projects = [
"Atlas onboarding",
"Billing migration",
"Customer portal",
"Design tokens",
"Mobile release",
"Quarterly report",
]
export default function ComboboxWithALabel() {
return (
<div className="grid w-full max-w-xs gap-2">
<Label htmlFor="combobox-project">Project</Label>
<Combobox items={projects}>
<ComboboxInput id="combobox-project" placeholder="Search projects" />
<ComboboxContent>
<ComboboxEmpty>No project matches.</ComboboxEmpty>
<ComboboxList>
{(project: string) => (
<ComboboxItem key={project} value={project}>
{project}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
)
}
With a clear button
"use client"
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@/components/ui/combobox"
import { Label } from "@/components/ui/label"
const projects = [
"Atlas onboarding",
"Billing migration",
"Customer portal",
"Design tokens",
"Mobile release",
"Quarterly report",
]
export default function ComboboxWithAClearButton() {
return (
<div className="grid w-full max-w-xs gap-2">
<Label htmlFor="combobox-clearable">Project</Label>
<Combobox items={projects} defaultValue="Billing migration">
<ComboboxInput id="combobox-clearable" placeholder="Search projects" showClear />
<ComboboxContent>
<ComboboxEmpty>No project matches.</ComboboxEmpty>
<ComboboxList>
{(project: string) => (
<ComboboxItem key={project} value={project}>
{project}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
)
}
With groups
"use client"
import {
Combobox,
ComboboxCollection,
ComboboxContent,
ComboboxEmpty,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxLabel,
ComboboxList,
ComboboxSeparator,
} from "@/components/ui/combobox"
import { Label } from "@/components/ui/label"
const assignees = [
{ value: "Teams", items: ["Design", "Engineering", "Finance"] },
{ value: "Roles", items: ["Project owner", "Reviewer", "Approver"] },
]
export default function ComboboxWithGroups() {
return (
<div className="grid w-full max-w-xs gap-2">
<Label htmlFor="combobox-assignee">Assign to</Label>
<Combobox items={assignees}>
<ComboboxInput id="combobox-assignee" placeholder="Search teams and roles" />
<ComboboxContent>
<ComboboxEmpty>No team or role matches.</ComboboxEmpty>
<ComboboxList>
{(group: (typeof assignees)[number], index: number) => (
<ComboboxGroup key={group.value} items={group.items}>
{index > 0 ? <ComboboxSeparator /> : null}
<ComboboxLabel>{group.value}</ComboboxLabel>
<ComboboxCollection>
{(assignee: string) => (
<ComboboxItem key={assignee} value={assignee}>
{assignee}
</ComboboxItem>
)}
</ComboboxCollection>
</ComboboxGroup>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
)
}
Several values
Design
Feature
"use client"
import {
Combobox,
ComboboxChip,
ComboboxChips,
ComboboxChipsInput,
ComboboxContent,
ComboboxEmpty,
ComboboxItem,
ComboboxList,
ComboboxValue,
useComboboxAnchor,
} from "@/components/ui/combobox"
import { Label } from "@/components/ui/label"
const labels = ["Bug", "Design", "Documentation", "Feature", "Performance", "Security"]
export default function ComboboxSeveralValues() {
const anchor = useComboboxAnchor()
return (
<div className="grid w-full max-w-xs gap-2">
<Label htmlFor="combobox-labels">Labels</Label>
<Combobox multiple items={labels} defaultValue={["Design", "Feature"]}>
<ComboboxChips ref={anchor}>
<ComboboxValue>
{(selected: string[]) => (
<>
{selected.map((label) => (
<ComboboxChip key={label}>{label}</ComboboxChip>
))}
<ComboboxChipsInput id="combobox-labels" />
</>
)}
</ComboboxValue>
</ComboboxChips>
<ComboboxContent anchor={anchor}>
<ComboboxEmpty>No label matches.</ComboboxEmpty>
<ComboboxList>
{(label: string) => (
<ComboboxItem key={label} value={label}>
{label}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
)
}
Disabled
"use client"
import { Combobox, ComboboxContent, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox"
import { Label } from "@/components/ui/label"
const projects = [
"Atlas onboarding",
"Billing migration",
"Customer portal",
"Design tokens",
"Mobile release",
"Quarterly report",
]
export default function ComboboxDisabled() {
return (
<div className="group grid w-full max-w-xs gap-2" data-disabled="true">
<Label htmlFor="combobox-locked">Project</Label>
<Combobox items={projects} defaultValue="Customer portal" disabled>
<ComboboxInput id="combobox-locked" disabled />
<ComboboxContent>
<ComboboxList>
{(project: string) => (
<ComboboxItem key={project} value={project}>
{project}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</div>
)
}
Invalid
Choose the project this invoice belongs to.
"use client"
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@/components/ui/combobox"
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
const projects = [
"Atlas onboarding",
"Billing migration",
"Customer portal",
"Design tokens",
"Mobile release",
"Quarterly report",
]
export default function ComboboxInvalid() {
return (
<Field data-invalid="true" className="max-w-xs">
<FieldLabel htmlFor="combobox-required">Project</FieldLabel>
<Combobox items={projects} required>
<ComboboxInput
id="combobox-required"
placeholder="Search projects"
aria-invalid
aria-describedby="combobox-required-error"
/>
<ComboboxContent>
<ComboboxEmpty>No project matches.</ComboboxEmpty>
<ComboboxList>
{(project: string) => (
<ComboboxItem key={project} value={project}>
{project}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
<FieldError id="combobox-required-error">Choose the project this invoice belongs to.</FieldError>
</Field>
)
}
Installation
npx shadcn@latest add @khaeli/comboboxbunx --bun shadcn@latest add @khaeli/comboboxInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Keyboard
A to Z- Typing opens the list and narrows it. Nothing is highlighted until an arrow key is pressed.
ArrowDownArrowUp- Open the list and move the highlight. Focus stays in the input.
Enter- Chooses the highlighted option and closes the list. With nothing highlighted it closes the list and puts back the value there was.
Escape- Closes the list. Pressed again, clears the value.
Tab- Moves from the input to the button that opens the list, then on.
ArrowLeftArrowRight- With several values, move between the chips and the input.
Backspace- With several values, removes the focused chip, or the last chip when the input is empty.
Accessible name
- The input has
role="combobox". GiveComboboxInput(orComboboxChipsInput) anidand point a @khaeli/label at it withhtmlFor, or give it anaria-label. - The button that opens the list is named "Show options" and the clear button "Clear": change them with
triggerLabelandclearLabelonComboboxInput. A chip's remove button is named "Remove" and the chip's text; change it withremoveLabelonComboboxChip. ComboboxLabelnames aComboboxGroup.ComboboxEmptyis a polite live region: it is read when nothing matches.- For an invalid combobox, put
aria-invalidandaria-describedbyonComboboxInput.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/combobox.json