Primitives
Combobox
A combobox pairs a Popover with a Command menu to build an autocomplete input, letting users search and select from a list of suggestions. Supports single select, multi-select, and form-field usage.
Combobox Examples
Searchable Combobox
Multi-select Combobox
Next.js
Form Field
This is used to tailor the docs shown to you.
Installation
# The combobox is composed from existing primitives, already installed at:
# src/components/ui/shadcn/popover.tsx
# src/components/ui/shadcn/command.tsx
# src/components/ui/shadcn/button.tsx
Usage
'use client'
import * as React from 'react'
import { CheckIcon, ChevronsUpDownIcon } from 'lucide-react'
import { Button } from '@/components/ui/shadcn/button'
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '@/components/ui/shadcn/command'
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/shadcn/popover'
import { cn } from '@/lib/utils'
const frameworks = [
{ value: 'next.js', label: 'Next.js' },
{ value: 'sveltekit', label: 'SvelteKit' },
]
export function ComboboxDemo() {
const [open, setOpen] = React.useState(false)
const [value, setValue] = React.useState('')
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button variant="outline" role="combobox" aria-expanded={open}>
{value || 'Select framework...'}
<ChevronsUpDownIcon className="opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="p-0">
<Command>
<CommandInput placeholder="Search framework..." />
<CommandList>
<CommandEmpty>No framework found.</CommandEmpty>
<CommandGroup>
{frameworks.map((framework) => (
<CommandItem
key={framework.value}
value={framework.value}
onSelect={(currentValue) => {
setValue(currentValue)
setOpen(false)
}}
>
<CheckIcon
className={cn(value === framework.value ? 'opacity-100' : 'opacity-0')}
/>
{framework.label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}
Components
| Component | Description |
|---|---|
| Popover | Positions the dropdown relative to the trigger button |
| PopoverTrigger | The button that opens the combobox |
| PopoverContent | Wraps the command menu |
| Command | The searchable list container |
| CommandInput | The search input |
| CommandList | Scrollable list of results |
| CommandEmpty | Shown when no items match |
| CommandGroup | Groups related items |
| CommandItem | An individual selectable option |