Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Combobox for Astro

A searchable Astro choice control with data-slot selection behavior.

API Reference

Installation

bunx --bun shadcn@latest add @gnostic/combobox

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

PropType
defaultValue?string

Initial selected value

Typestring
defaultOpen?boolean

Initial open state

Typeboolean
placeholder?string

Placeholder text for the input

Typestring
disabled?boolean

Disable interaction

Typeboolean
required?boolean

Form validation required

Typeboolean
name?string

Form field name (auto-creates hidden input)

Typestring
openOnFocus?boolean

Open popup when input receives focus

Typeboolean
Defaulttrue
autoHighlight?boolean

Auto-highlight first visible item when filtering

Typeboolean
Defaultfalse
side?Side
TypeSide
Default"bottom"
align?Align
TypeAlign
Default"start"
sideOffset?number
Typenumber
Default4
alignOffset?number
Typenumber
Default0
avoidCollisions?boolean
Typeboolean
Defaulttrue
collisionPadding?number
Typenumber
Default8

See 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.