Combobox
Visually the same control as Select, plus an input that filters the collection as you type. @zag-js/combobox handles filtering, keyboard highlight, and open/close behaviour. The trigger has no data-state of its own, so it is styled through the control using Tailwind’s group modifier.
Interactive Preview
Section titled “Interactive Preview”React Comboboxlive
Svelte Comboboxlive
Asia/Jakarta (WIB)
Asia/Singapore (SGT)
Asia/Tokyo (JST)
Europe/London (GMT)
America/New_York (EST)
Solid Comboboxlive
Asia/Jakarta (WIB)
Asia/Singapore (SGT)
Asia/Tokyo (JST)
Europe/London (GMT)
America/New_York (EST)
import { Combobox } from "@/components/ui/combobox";
const timezones = [ { label: "Asia/Jakarta (WIB)", value: "wib" }, { label: "Asia/Singapore (SGT)", value: "sgt" },];
<Combobox label="Timezone" items={timezones} placeholder="Search timezone" /><script lang="ts"> import Combobox from "@/components/ui/combobox.svelte";
const timezones = [ { label: "Asia/Jakarta (WIB)", value: "wib" }, { label: "Asia/Singapore (SGT)", value: "sgt" }, ];</script>
<Combobox label="Timezone" items={timezones} placeholder="Search timezone" />import { Combobox } from "@/components/ui/combobox";
const timezones = [ { label: "Asia/Jakarta (WIB)", value: "wib" }, { label: "Asia/Singapore (SGT)", value: "sgt" },];
<Combobox label="Timezone" items={timezones} placeholder="Search timezone" />Installation
Section titled “Installation”npx @glaceon/cli add combobox --framework react # or svelte, solidbun add @zag-js/combobox @zag-js/collection @zag-js/react clsx tailwind-merge# replace @zag-js/react with @zag-js/svelte or @zag-js/solid for other frameworksThen copy the source for your framework:
import { useId, useMemo } from "react"import { normalizeProps, useMachine } from "@zag-js/react"import * as zagCombobox from "@zag-js/combobox"import { ListCollection } from "@zag-js/collection"import { cn } from "@/lib/cn"
export interface ComboboxItem { label: string value: string}
export interface ComboboxProps { items: ComboboxItem[] placeholder?: string label?: string name?: string disabled?: boolean className?: string}
const rootClass = "flex w-full flex-col gap-1.5"const labelClass = "text-xs font-bold text-muted"const controlClass = "group flex h-11 w-full items-center gap-2 rounded-md border border-line bg-surface px-3 transition-colors data-[state=open]:border-green focus-within:ring-2 focus-within:ring-action focus-within:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"const inputClass = "min-w-0 flex-1 border-0 bg-transparent text-sm text-ink outline-none placeholder:text-muted"const triggerClass = "shrink-0 text-muted transition-transform group-data-[state=open]:rotate-180"const positionerClass = "z-50 w-[var(--reference-width)]"const contentClass = "z-50 overflow-hidden rounded-md border border-line bg-surface shadow-sm"const listClass = "max-h-[220px] overflow-auto py-1"const itemClass = "flex cursor-pointer items-center justify-between gap-2 px-2 py-1.5 text-sm text-ink data-[highlighted]:bg-surface-2 data-[state=checked]:font-bold data-[state=checked]:text-green-deep"
export function Combobox({ items, placeholder = "Search", label, name, disabled, className,}: ComboboxProps) { const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "") const collection = useMemo(() => new ListCollection<ComboboxItem>({ items }), [items]) const service = useMachine(zagCombobox.machine, { id: uid, collection, name, disabled }) const api = zagCombobox.connect(service, normalizeProps)
return ( <div {...api.getRootProps()} className={cn(rootClass, className)}> {label ? ( <label {...api.getLabelProps()} className={labelClass}> {label} </label> ) : null} <div {...api.getControlProps()} className={controlClass}> <input {...api.getInputProps()} className={inputClass} placeholder={placeholder} /> <button {...api.getTriggerProps()} className={triggerClass} aria-label="Open list"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" width="16" height="16" aria-hidden="true"> <path d="m6 9 6 6 6-6" /> </svg> </button> </div> <div {...api.getPositionerProps()} className={positionerClass}> <div {...api.getContentProps()} className={contentClass}> <div {...api.getListProps()} className={listClass}> {items.map((item) => ( <div key={item.value} {...api.getItemProps({ item })} className={itemClass}> <span {...api.getItemTextProps({ item })}>{item.label}</span> <span {...api.getItemIndicatorProps({ item })} className="text-green"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" width="14" height="14" aria-hidden="true"> <path d="m5 13 4 4L19 7" /> </svg> </span> </div> ))} </div> </div> </div> </div> )}<script lang="ts"> import { normalizeProps, useMachine } from "@zag-js/svelte" import * as zagCombobox from "@zag-js/combobox" import { ListCollection } from "@zag-js/collection" import { cn } from "@/lib/cn"
interface Item { label: string value: string } interface Props { items?: Item[] placeholder?: string label?: string name?: string disabled?: boolean class?: string }
const { items = [], placeholder = "Search", label = undefined, name = undefined, disabled = false, class: className, }: Props = $props()
const rootClass = "flex w-full flex-col gap-1.5" const labelClass = "text-xs font-bold text-muted" const controlClass = "group flex h-11 w-full items-center gap-2 rounded-md border border-line bg-surface px-3 transition-colors data-[state=open]:border-green focus-within:ring-2 focus-within:ring-action focus-within:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50" const inputClass = "min-w-0 flex-1 border-0 bg-transparent text-sm text-ink outline-none placeholder:text-muted" const triggerClass = "shrink-0 text-muted transition-transform group-data-[state=open]:rotate-180" const positionerClass = "z-50 w-[var(--reference-width)]" const contentClass = "z-50 overflow-hidden rounded-md border border-line bg-surface shadow-sm" const listClass = "max-h-[220px] overflow-auto py-1" const itemClass = "flex cursor-pointer items-center justify-between gap-2 px-2 py-1.5 text-sm text-ink data-[highlighted]:bg-surface-2 data-[state=checked]:font-bold data-[state=checked]:text-green-deep"
const uid = $props.id() const collection = $derived(new ListCollection<Item>({ items })) const service = useMachine(zagCombobox.machine, () => ({ id: uid, collection, name, disabled })) const api = $derived(zagCombobox.connect(service, normalizeProps))</script>
<div {...api.getRootProps()} class={cn(rootClass, className)}> {#if label} <label {...api.getLabelProps()} class={labelClass}>{label}</label> {/if} <div {...api.getControlProps()} class={controlClass}> <input {...api.getInputProps()} class={inputClass} {placeholder} /> <button {...api.getTriggerProps()} class={triggerClass} aria-label="Open list"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="16" height="16" aria-hidden="true"> <path d="m6 9 6 6 6-6" /> </svg> </button> </div> <div {...api.getPositionerProps()} class={positionerClass}> <div {...api.getContentProps()} class={contentClass}> <div {...api.getListProps()} class={listClass}> {#each items as item (item.value)} <div {...api.getItemProps({ item })} class={itemClass}> <span {...api.getItemTextProps({ item })}>{item.label}</span> <span {...api.getItemIndicatorProps({ item })} class="text-green"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" width="14" height="14" aria-hidden="true"> <path d="m5 13 4 4L19 7" /> </svg> </span> </div> {/each} </div> </div> </div></div>import { createMemo, createUniqueId, For, Show } from "solid-js"import { normalizeProps, useMachine } from "@zag-js/solid"import * as zagCombobox from "@zag-js/combobox"import { ListCollection } from "@zag-js/collection"import { cn } from "@/lib/cn"
export interface ComboboxItem { label: string value: string}
export interface ComboboxProps { items: ComboboxItem[] placeholder?: string label?: string name?: string disabled?: boolean class?: string}
const rootClass = "flex w-full flex-col gap-1.5"const labelClass = "text-xs font-bold text-muted"const controlClass = "group flex h-11 w-full items-center gap-2 rounded-md border border-line bg-surface px-3 transition-colors data-[state=open]:border-green focus-within:ring-2 focus-within:ring-action focus-within:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"const inputClass = "min-w-0 flex-1 border-0 bg-transparent text-sm text-ink outline-none placeholder:text-muted"const triggerClass = "shrink-0 text-muted transition-transform group-data-[state=open]:rotate-180"const positionerClass = "z-50 w-[var(--reference-width)]"const contentClass = "z-50 overflow-hidden rounded-md border border-line bg-surface shadow-sm"const listClass = "max-h-[220px] overflow-auto py-1"const itemClass = "flex cursor-pointer items-center justify-between gap-2 px-2 py-1.5 text-sm text-ink data-[highlighted]:bg-surface-2 data-[state=checked]:font-bold data-[state=checked]:text-green-deep"
export function Combobox(props: ComboboxProps) { const uid = createUniqueId() const collection = createMemo(() => new ListCollection<ComboboxItem>({ items: props.items })) const service = useMachine(zagCombobox.machine, { id: uid, collection: collection(), name: props.name, disabled: props.disabled, }) const api = createMemo(() => zagCombobox.connect(service, normalizeProps))
return ( <div {...api().getRootProps()} class={cn(rootClass, props.class)}> <Show when={props.label}> <label {...api().getLabelProps()} class={labelClass}> {props.label} </label> </Show> <div {...api().getControlProps()} class={controlClass}> <input {...api().getInputProps()} class={inputClass} placeholder={props.placeholder ?? "Search"} /> <button {...api().getTriggerProps()} class={triggerClass} aria-label="Open list"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="16" height="16" aria-hidden="true"> <path d="m6 9 6 6 6-6" /> </svg> </button> </div> <div {...api().getPositionerProps()} class={positionerClass}> <div {...api().getContentProps()} class={contentClass}> <div {...api().getListProps()} class={listClass}> <For each={props.items}> {(item) => ( <div {...api().getItemProps({ item })} class={itemClass}> <span {...api().getItemTextProps({ item })}>{item.label}</span> <span {...api().getItemIndicatorProps({ item })} class="text-green"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" width="14" height="14" aria-hidden="true"> <path d="m5 13 4 4L19 7" /> </svg> </span> </div> )} </For> </div> </div> </div> </div> )}Data Attributes
Section titled “Data Attributes”| Element | Data Attribute | Value | Description |
|---|---|---|---|
control |
data-state |
"open" | "closed" |
Popover visibility. Drives the border and the indicator rotation. |
control |
data-focus |
"" |
The input holds focus. |
control |
data-disabled |
"" |
Present when disabled. |
input |
data-state |
"open" | "closed" |
Popover visibility. |
content |
data-state |
"open" | "closed" |
Popover visibility. |
content |
data-empty |
"" |
The filter matched nothing. |
item |
data-state |
"checked" | "unchecked" |
The selected item. |
item |
data-highlighted |
"" |
The keyboard-highlighted item. |
API Reference (Props)
Section titled “API Reference (Props)”| Prop | Type | Default | Description |
|---|---|---|---|
items |
{ label: string, value: string }[] |
required | Builds the Zag ListCollection. |
placeholder |
string |
"Search" |
Input placeholder text. |
label |
string |
undefined |
Renders a <label> wired to the input. |
name |
string |
undefined |
Name for form submission. |
disabled |
boolean |
false |
Disables the whole control. |
class / className |
string |
undefined |
Extra classes, appended last. |
Keyboard Interactions
Section titled “Keyboard Interactions”| Key | Description |
|---|---|
ArrowDown / ArrowUp |
Moves the highlight; opens the list when closed. |
Enter |
Selects the highlighted item. |
Esc |
Clears the input and closes the list. |
| Any character | Filters the collection. |