Combobox for Astro
A searchable Astro choice control with data-slot selection behavior.
Installation
bunx --bun shadcn@latest add @gnostic/comboboxnpx shadcn@latest add @gnostic/comboboxpnpm dlx shadcn@latest add @gnostic/comboboxInstall component packages
bun add @data-slot/comboboxnpm install @data-slot/comboboxpnpm add @data-slot/comboboxCopy the files
src/components/ui/combobox/combobox-content.astrosrc/components/ui/combobox/combobox-empty.astrosrc/components/ui/combobox/combobox-group.astrosrc/components/ui/combobox/combobox-input.astrosrc/components/ui/combobox/combobox-item.astrosrc/components/ui/combobox/combobox-label.astrosrc/components/ui/combobox/combobox-list.astrosrc/components/ui/combobox/combobox-separator.astrosrc/components/ui/combobox/combobox-trigger.astrosrc/components/ui/combobox/combobox-value.astrosrc/components/ui/combobox/combobox.astrosrc/components/ui/combobox/index.tssrc/components/ui/combobox/types.ts
Usage
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxLabel,
ComboboxList,
ComboboxSeparator,
ComboboxTrigger,
ComboboxValue,
} from "@/components/ui/combobox"
<Combobox class="w-full max-w-sm">
<ComboboxInput placeholder="Choose a project" />
<ComboboxContent>
<ComboboxList>
<ComboboxEmpty>No matching project.</ComboboxEmpty>
<ComboboxItem value="reading-room">Reading room</ComboboxItem>
<ComboboxItem value="studio-bookings">Studio bookings</ComboboxItem>
<ComboboxItem value="autumn-catalog">Autumn catalog</ComboboxItem>
<ComboboxItem value="weekly-review">Weekly review</ComboboxItem>
<ComboboxItem value="field-notes">Field notes</ComboboxItem>
</ComboboxList>
</ComboboxContent>
</Combobox>
Composition
Keep the no-results message in ComboboxList by using ComboboxEmpty. The controller expects the empty state in that list.
Use this composition for a single-line input and a flat list:
Combobox
├── ComboboxInput
└── ComboboxContent
└── ComboboxList
├── ComboboxEmpty
├── ComboboxItem
└── ComboboxItem
Use ComboboxGroup, ComboboxLabel, and ComboboxSeparator to organize items:
Combobox
├── ComboboxInput
└── ComboboxContent
└── ComboboxList
├── ComboboxEmpty
├── ComboboxGroup
│ ├── ComboboxLabel
│ ├── ComboboxItem
│ └── ComboboxItem
├── ComboboxSeparator
└── ComboboxGroup
├── ComboboxLabel
├── ComboboxItem
└── ComboboxItem
Use ComboboxTrigger and ComboboxValue when composing a custom trigger. Use showClear on ComboboxInput when the input needs a clear control.
Examples
Auto Highlight
Use autoHighlight to automatically highlight the first visible item after a
non-whitespace query.
Combobox in Popup
Trigger the combobox from a button and move the search input inside the popup.
Disabled
Use disabled on Combobox to disable interaction.
API Reference
ComboboxOptions
defaultValue?string
Initial selected value
stringdefaultOpen?boolean
Initial open state
booleanplaceholder?string
Placeholder text for the input
stringdisabled?boolean
Disable interaction
booleanrequired?boolean
Form validation required
booleanname?string
Form field name (auto-creates hidden input)
stringopenOnFocus?boolean
Open popup when input receives focus
booleantrueautoHighlight?boolean
Auto-highlight first visible item when filtering
booleanfalseside?Side
Side"bottom"align?Align
Align"start"sideOffset?number
number4alignOffset?number
number0avoidCollisions?boolean
booleantruecollisionPadding?number
number8See the data-slot docs for more information on interactivity.
Events
Dispatch these events on the combobox root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
combobox:change |
{ value: string | null } |
combobox:open-change |
{ open: boolean } |
combobox:input-change |
{ inputValue: string } |
Inbound events
| Event | Detail |
|---|---|
combobox:set |
{ value?: string | null, open?: boolean, inputValue?: string, itemToStringValue?: ((item: HTMLElement | null, value: string | null) => string) | null } |
Read the installed source for the complete prop types.
When to use Combobox
Compose the input, popup, list, items, and empty result under Combobox. Give each item a stable value. Use this control when a list is long enough that typing a search is useful.
Keyboard and behavior
The existing controller handles the query and keyboard selection. Keep the input labelled and provide useful empty-result text. This port does not add Coss multi-value chips or its React value callbacks. Confirm the installed controller’s DOM event and form behavior before wiring saved data.
Theme and layout
The input group and popup use the same surfaces as other controls. Constrain a long results list and check that item text wraps without moving the selection marker. Remote data loading and request cancellation are application concerns.