---
title: Combobox for Astro
description: "A searchable Astro choice control with data-slot selection behavior."
seo:
    title: Combobox for Astro | Shadcn & Tailwind CSS
related:
    - /components/select
    - /components/native-select
    - /components/command
sidebar:
    label: "Combobox"
---

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

<Combobox class="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>
```

## Installation

### Command

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

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/combobox
```

#### src/components/ui/combobox/combobox-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import type {
  Align as ComboboxAlign,
  Side as ComboboxSide,
} from "@data-slot/combobox"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  side?: ComboboxSide
  align?: ComboboxAlign
  sideOffset?: number
  alignOffset?: number
  avoidCollisions?: boolean
  collisionPadding?: number
}

const {
  class: className,
  hidden = true,
  side,
  align,
  sideOffset,
  alignOffset,
  avoidCollisions,
  collisionPadding,
  ...props
} = Astro.props
---

<div data-slot="combobox-portal">
  <div
    data-slot="combobox-positioner"
    data-side={side}
    data-align={align}
    data-side-offset={sideOffset}
    data-align-offset={alignOffset}
    data-avoid-collisions={avoidCollisions}
    data-collision-padding={collisionPadding}
    class="isolate z-50"
  >
    <div
      data-slot="combobox-content"
      data-side={side}
      data-align={align}
      data-side-offset={sideOffset}
      data-align-offset={alignOffset}
      data-avoid-collisions={avoidCollisions}
      data-collision-padding={collisionPadding}
      hidden={hidden}
      class={cn("relative flex flex-col min-w-(--anchor-width) max-w-(--available-width) origin-(--transform-origin) rounded-lg border bg-popover not-dark:bg-clip-padding shadow-lg/5 before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] dark:before:shadow-[0_-1px_--theme(--color-white/6%)] max-h-[min(var(--available-height),23rem)] overflow-y-auto text-foreground transition-[scale,opacity] duration-200 data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 group/combobox-content", className)}
      {...props}
    >
      <slot />
    </div>
  </div>
</div>
```

#### src/components/ui/combobox/combobox-empty.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, hidden = true, ...props } = Astro.props
---

<div
  data-slot="combobox-empty"
  hidden={hidden}
  class={cn("not-empty:p-2 text-center text-base text-muted-foreground sm:text-sm", className)}
  {...props}
>
  <slot>No results found.</slot>
</div>
```

#### src/components/ui/combobox/combobox-group.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div data-slot="combobox-group" class={cn(className)} {...props}>
  <slot />
</div>
```

#### src/components/ui/combobox/combobox-input.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import XIcon from "@lucide/astro/icons/x"

import { cn } from "cn"
import InputGroupAddon from "../input-group/input-group-addon.astro"
import InputGroupInput from "../input-group/input-group-input.astro"
import InputGroup from "../input-group/input-group.astro"

import ComboboxTrigger from "./combobox-trigger.astro"

type Props = HTMLAttributes<"input"> & {
  showTrigger?: boolean
  showClear?: boolean
}

const {
  class: className,
  type = "text",
  disabled = false,
  showTrigger = true,
  showClear = false,
  ...props
} = Astro.props
---

<InputGroup class={cn("w-auto", className)}>
  <InputGroupInput
    type={type}
    data-slot="combobox-input"
    disabled={disabled}
    {...props}
  />
  <InputGroupAddon align="inline-end">
    {showTrigger && (
      <ComboboxTrigger
        class="group-has-data-[slot=combobox-clear]/input-group:hidden data-pressed:bg-transparent"
        disabled={disabled}
      />
    )}
    {showClear && (
      <button
        type="button"
        data-slot="combobox-clear"
        class="inline-flex size-6 shrink-0 items-center justify-center rounded-[calc(var(--radius)-5px)] p-0 text-sm shadow-none outline-none disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
        disabled={disabled}
      >
        <XIcon class="pointer-events-none" />
      </button>
    )}
  </InputGroupAddon>
  <slot />
</InputGroup>
```

#### src/components/ui/combobox/combobox-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import CheckIcon from "@lucide/astro/icons/check"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  value?: string
  disabled?: boolean
  showIndicator?: boolean
}

const {
  class: className,
  value,
  disabled,
  showIndicator = true,
  ...props
} = Astro.props
---

<div
  data-slot="combobox-item"
  data-value={value}
  data-disabled={disabled ? "" : undefined}
  class={cn("flex min-h-8 in-data-[side=none]:min-w-[calc(var(--anchor-width)+1.25rem)] cursor-default items-center gap-2 rounded-sm py-1 ps-2 text-base outline-none data-disabled:pointer-events-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:opacity-64 sm:min-h-7 sm:text-sm [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0 relative w-full pe-8 group/combobox-item", className)}
  {...props}
>
  <slot />
  {showIndicator && (
    <span
      data-slot="combobox-item-indicator"
      hidden={false}
      class="pointer-events-none absolute right-2 flex size-4 items-center justify-center opacity-0 group-data-selected/combobox-item:opacity-100"
    >
      <CheckIcon class="pointer-events-none" />
    </span>
  )}
</div>
```

#### src/components/ui/combobox/combobox-label.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="combobox-label"
  class={cn("text-muted-foreground px-2 py-1.5 text-xs", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/combobox/combobox-list.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="combobox-list"
  class={cn("not-empty:scroll-py-1 not-empty:px-1 not-empty:py-1 in-data-has-overflow-y:pe-3", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/combobox/combobox-separator.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="combobox-separator"
  class={cn("bg-border -mx-1 my-1 h-px", className)}
  {...props}
></div>
```

#### src/components/ui/combobox/combobox-trigger.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import ChevronDownIcon from "@lucide/astro/icons/chevron-down"

import { cn } from "cn"

type Props = HTMLAttributes<"button"> & {
  showIcon?: boolean
}

const {
  class: className,
  type = "button",
  showIcon = true,
  ...props
} = Astro.props
---

<button
  type={type}
  data-slot="combobox-trigger"
  class={cn(
    "[&_svg]:text-muted-foreground inline-flex size-6 shrink-0 items-center justify-center gap-2 rounded-[calc(var(--radius)-5px)] p-0 text-sm shadow-none outline-none disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
    className,
  )}
  {...props}
>
  <slot />
  {showIcon && (
    <ChevronDownIcon
      data-slot="combobox-trigger-icon"
      class="text-muted-foreground pointer-events-none size-4"
    />
  )}
</button>
```

#### src/components/ui/combobox/combobox-value.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  data-slot="combobox-value"
  class={cn(
    "data-[placeholder]:text-muted-foreground block flex-1 truncate text-left",
    className,
  )}
  {...props}
>
  <slot />
</span>
```

#### src/components/ui/combobox/combobox.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import type { ComboboxOptions } from "./types.ts"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & ComboboxOptions

const {
  class: className,
  defaultValue,
  defaultOpen,
  placeholder,
  disabled,
  required,
  name,
  openOnFocus,
  autoHighlight,
  side,
  align,
  sideOffset,
  alignOffset,
  avoidCollisions,
  collisionPadding,
  ...props
} = Astro.props
---

<div
  data-slot="combobox"
  data-default-value={defaultValue}
  data-default-open={defaultOpen}
  data-placeholder={placeholder}
  data-disabled={disabled}
  data-required={required}
  data-name={name}
  data-open-on-focus={openOnFocus}
  data-auto-highlight={autoHighlight}
  data-side={side}
  data-align={align}
  data-side-offset={sideOffset}
  data-align-offset={alignOffset}
  data-avoid-collisions={avoidCollisions}
  data-collision-padding={collisionPadding}
  class={cn("group/combobox relative", className)}
  {...props}
>
  <slot />
</div>

<script>
  import { createCombobox } from "@data-slot/combobox"

  const initCombobox = () => {
    document.querySelectorAll('[data-slot="combobox"]').forEach((root) => {
      if (
        !root.querySelector('[data-slot="combobox-input"]') ||
        !root.querySelector('[data-slot="combobox-content"]')
      ) {
        return
      }

      createCombobox(root as HTMLElement)
    })
  }

  initCombobox()

  document.addEventListener("astro:page-load", initCombobox)
</script>
```

#### src/components/ui/combobox/index.ts

```ts
export { default as Combobox } from './combobox.astro'
export { default as ComboboxContent } from './combobox-content.astro'
export { default as ComboboxEmpty } from './combobox-empty.astro'
export { default as ComboboxGroup } from './combobox-group.astro'
export { default as ComboboxInput } from './combobox-input.astro'
export { default as ComboboxItem } from './combobox-item.astro'
export { default as ComboboxLabel } from './combobox-label.astro'
export { default as ComboboxList } from './combobox-list.astro'
export { default as ComboboxSeparator } from './combobox-separator.astro'
export { default as ComboboxTrigger } from './combobox-trigger.astro'
export { default as ComboboxValue } from './combobox-value.astro'

export type { ComboboxOptions } from './types.ts'
```

#### src/components/ui/combobox/types.ts

```ts
import type { ComboboxOptions as ComboboxOptionsType } from '@data-slot/combobox'

/** Combobox configuration rendered as data attributes. */
export type ComboboxOptions = Pick<
    ComboboxOptionsType,
    | 'defaultValue'
    | 'defaultOpen'
    | 'placeholder'
    | 'disabled'
    | 'required'
    | 'name'
    | 'openOnFocus'
    | 'autoHighlight'
    | 'side'
    | 'align'
    | 'sideOffset'
    | 'alignOffset'
    | 'avoidCollisions'
    | 'collisionPadding'
>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import {
    Combobox,
    ComboboxContent,
    ComboboxEmpty,
    ComboboxGroup,
    ComboboxInput,
    ComboboxItem,
    ComboboxLabel,
    ComboboxList,
    ComboboxSeparator,
    ComboboxTrigger,
    ComboboxValue,
} from "@/components/ui/combobox"
```

```astro
<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:

```text
Combobox
├── ComboboxInput
└── ComboboxContent
    └── ComboboxList
        ├── ComboboxEmpty
        ├── ComboboxItem
        └── ComboboxItem
```

Use `ComboboxGroup`, `ComboboxLabel`, and `ComboboxSeparator` to organize items:

```text
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

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

<Combobox
  class="relative w-full max-w-sm"
  placeholder="Choose a timezone..."
  defaultValue="cet"
>
  <ComboboxInput showClear />
  <ComboboxContent>
    <ComboboxList>
      <ComboboxEmpty>No matching time zone.</ComboboxEmpty>
      <ComboboxItem value="cet">Central European Time</ComboboxItem>
      <ComboboxItem value="est">Eastern Standard Time</ComboboxItem>
      <ComboboxItem value="pst">Pacific Standard Time</ComboboxItem>
    </ComboboxList>
  </ComboboxContent>
</Combobox>
```

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

<Combobox class="w-full max-w-sm" placeholder="Search project groups">
  <ComboboxInput />
  <ComboboxContent>
    <ComboboxList>
      <ComboboxEmpty>No matching project.</ComboboxEmpty>
      <ComboboxGroup>
        <ComboboxLabel>Writing</ComboboxLabel>
        <ComboboxItem value="field-notes">Field notes</ComboboxItem>
        <ComboboxItem value="reading-room">Reading room</ComboboxItem>
        <ComboboxItem value="studio-bookings">Studio bookings</ComboboxItem>
      </ComboboxGroup>
      <ComboboxSeparator />
      <ComboboxGroup>
        <ComboboxLabel>Operations</ComboboxLabel>
        <ComboboxItem value="express">Express</ComboboxItem>
        <ComboboxItem value="fastify">Fastify</ComboboxItem>
      </ComboboxGroup>
    </ComboboxList>
  </ComboboxContent>
</Combobox>
```

### Auto Highlight

Use `autoHighlight` to automatically highlight the first visible item after a
non-whitespace query.

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

<Combobox class="w-full max-w-sm" placeholder="Search projects" autoHighlight>
  <ComboboxInput />
  <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>
```

### Combobox in Popup

Trigger the combobox from a button and move the search input inside the popup.

```astro
---
import {
  Combobox,
  ComboboxContent,
  ComboboxEmpty,
  ComboboxInput,
  ComboboxItem,
  ComboboxList,
  ComboboxTrigger,
  ComboboxValue,
} from "@/components/ui/combobox"
---

<Combobox class="w-full max-w-sm" defaultValue="field-notes">
  <ComboboxTrigger class="border-input bg-background dark:bg-input/30 focus-visible:border-ring focus-visible:ring-ring/50 inline-flex h-9 w-full items-center justify-between gap-2 rounded-md border px-3 text-sm shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-3">
    <ComboboxValue>Choose a project</ComboboxValue>
  </ComboboxTrigger>
  <ComboboxContent>
    <ComboboxInput showTrigger={false} placeholder="Search projects" />
    <ComboboxList>
      <ComboboxEmpty>No matching project.</ComboboxEmpty>
      <ComboboxItem value="reading-room">Reading room</ComboboxItem>
      <ComboboxItem value="studio-bookings">Studio bookings</ComboboxItem>
      <ComboboxItem value="field-notes">Field notes</ComboboxItem>
    </ComboboxList>
  </ComboboxContent>
</Combobox>
```

### Disabled

Use `disabled` on `Combobox` to disable interaction.

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

<Combobox class="w-full max-w-sm" placeholder="Choose a project" disabled>
  <ComboboxInput />
  <ComboboxContent>
    <ComboboxList>
      <ComboboxItem value="reading-room">Reading room</ComboboxItem>
      <ComboboxItem value="studio-bookings">Studio bookings</ComboboxItem>
      <ComboboxItem value="field-notes">Field notes</ComboboxItem>
    </ComboboxList>
  </ComboboxContent>
</Combobox>
```

## API Reference

### ComboboxOptions

<AutoTypeTable
    path="../../packages/ui/src/components/ui/combobox/types.ts"
    name="ComboboxOptions"
/>

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/combobox/README.md) 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](/r/combobox.json) 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.
