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
npx shadcn@latest add @gnostic/combobox
pnpm dlx shadcn@latest add @gnostic/combobox

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

Install component packages

bun add @data-slot/combobox

Copy the files

src/components/ui/combobox/combobox-content.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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
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
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'
>

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.