Combobox
Data inputA text input with a list of options, filtered as the user types: <Combobox.Root> (type="single" or "multiple", bind:value, items, name) holds a Combobox.Input (styled like Input, with a button that opens the list) and a Combobox.Content. Given items, <Combobox.Content /> draws those whose label contains the typed text, or emptyText when none do. For your own filtering (a search on the server, say) or markup, give the content Combobox.Items (value, label), optionally in Combobox.Groups with a Combobox.GroupHeading, and read the typed text from the input's oninput. Arrow keys move, Enter chooses, Escape closes. Inside a Field the input is labelled, described and marked invalid by the Field.
import { Combobox } from '@limerdev/zest'; Select. Running commands: use Command.Examples
Basic
Given items, the content draws those whose label contains the typed text.
Chosen: none
<script lang="ts">
import { Combobox, Field, type ComboboxOption } from '@limerdev/zest';
const customers: ComboboxOption[] = [
{ value: 'acme', label: 'Acme AB' },
{ value: 'globex', label: 'Globex' },
{ value: 'initech', label: 'Initech' },
{ value: 'umbrella', label: 'Umbrella Corporation' },
{ value: 'wayne', label: 'Wayne Enterprises' },
{ value: 'wonka', label: 'Wonka Industries' }
];
let customer = $state('');
</script>
<Field.Root class="w-72">
<Field.Label>Customer</Field.Label>
<Combobox.Root type="single" bind:value={customer} items={customers}>
<Combobox.Input placeholder="Search customers" />
<Combobox.Content emptyText="No customers found." />
</Combobox.Root>
<Field.Description>Chosen: {customer || 'none'}</Field.Description>
</Field.Root> Your own filtering
Your own filtering (prefix match here; a server search works the same): keep the typed text in state and give the content the matching Combobox.Items.
Chosen: none
<script lang="ts">
import { Combobox, Field, type ComboboxOption } from '@limerdev/zest';
const customers: ComboboxOption[] = [
{ value: 'acme', label: 'Acme AB' },
{ value: 'globex', label: 'Globex' },
{ value: 'initech', label: 'Initech' },
{ value: 'umbrella', label: 'Umbrella Corporation' },
{ value: 'wayne', label: 'Wayne Enterprises' },
{ value: 'wonka', label: 'Wonka Industries' }
];
let search = $state('');
let customer = $state('');
const shown = $derived(
customers.filter((option) => option.label.toLowerCase().startsWith(search.toLowerCase()))
);
</script>
<Field.Root class="w-72">
<Field.Label>Customer</Field.Label>
<Combobox.Root
type="single"
bind:value={customer}
items={customers}
onOpenChange={(open) => {
if (!open) search = '';
}}
>
<Combobox.Input
placeholder="Search customers"
oninput={(event) => (search = event.currentTarget.value)}
/>
<Combobox.Content>
{#each shown as option (option.value)}
<Combobox.Item value={option.value} label={option.label} />
{:else}
<p class="px-2 py-1.5 text-sm text-muted">No customers found.</p>
{/each}
</Combobox.Content>
</Combobox.Root>
<Field.Description>Chosen: {customer || 'none'}</Field.Description>
</Field.Root> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
Combobox.Root
| Prop | Type | Default | Description |
|---|---|---|---|
| allowDeselect | boolean | – | Whether to allow the user to deselect an item by clicking on an already selected item. This is only applicable to type="single" selects/comboboxes. |
| disabled | boolean | false | Whether the combobox is disabled. |
| inputValue | string | – | A read-only value that can be used to programmatically update the input value. This is useful for updating the displayed label/input when the value changes outside of Bits UI. |
| items | { value: string; label: string; disabled?: boolean | undefined; }[] | – | Optionally provide an array of value and label pairs that will be used to match and trigger selection when the trigger is focused and a key is pressed while the content is closed. It's also used to handle form autofill. By providing this value, you enable selecting a value when the trigger is focused and a key is pressed without the content being open, similar to how a native <select> works. For this to work, you must The label is what the user will potentially search for via typeahead, and the value is what is set as the selected value when a typeahead match is found. We can't rely on the individual Item components to do this because they may not ever be mounted to do the DOM if using a conditional block with a Svelte transition or certain animation libraries. IMPORTANT: This functionality is only available for single-select listboxes. |
| loop | boolean | false | Whether or not the combobox menu should loop through the items when navigating with the keyboard. |
| name | string | – | The name to apply to the hidden input element for form submission. If not provided, a hidden input will not be rendered and the combobox will not be part of a form. |
| onOpenChange | OnChangeFn<boolean> | – | A callback function called when the open state changes. |
| onOpenChangeComplete | OnChangeFn<boolean> | – | A callback function called when the open state changes and the animation is complete. |
| onValueChange | OnChangeFn<string> | OnChangeFn<string[]> | – | A callback function called when the value changes. |
| open bindable | boolean | false | Whether the combobox popover is open. |
| required | boolean | false | Whether the combobox is required (for form submission). |
| scrollAlignment | "center" | "nearest" | "nearest" | How to scroll the combobox items into view when navigating with the keyboard. |
| type required | "multiple" | "single" | – | The type of combobox. |
| value bindable | string | string[] | – | The value of the selected combobox item. |
Combobox.Input
| Prop | Type | Default | Description |
|---|---|---|---|
| clearOnDeselect | boolean | false | Whether to clear the input when the last item is deselected. |
| defaultValue | string | – | The default value of the input. This is not a reactive prop and is only used to populate the input when the combobox is first mounted if there is already a value set. |
| triggerLabel | string | the Config's `strings.showOptions`, else 'Show options' | The name of the button that opens the list. |
Combobox.Trigger
No props of its own beyond the common ones.
Combobox.Content
| Prop | Type | Default | Description |
|---|---|---|---|
| align | "start" | "center" | "end" | – | The preferred alignment of the anchor to render against when open. This may change when collisions occur. |
| alignOffset | number | – | An offset in pixels from the "start" or "end" alignment options. |
| arrowPadding | number | – | This describes the padding between the arrow and the edges of the floating element. If your floating element has border-radius, this will prevent it from overflowing the corners. |
| avoidCollisions | boolean | true | When true, overrides the side and align options to prevent collisions with the boundary edges. |
| collisionBoundary | Arrayable<Boundary> | – | A boundary element or array of elements to check for collisions against. |
| collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 | The amount in pixels of virtual padding around the viewport edges to check for overflow which will cause a collision. |
| customAnchor | string | HTMLElement | Measurable | null | – | Use an element other than the trigger to anchor the content to. If provided, the content will be anchored to the provided element instead of the trigger. You can pass a selector string or an HTMLElement. |
| dir | "ltr" | "rtl" | – | The text direction of the content. |
| emptyText | string | the Config's `strings.noResults`, else 'No results found.' | Shown when no item matches the typed text, when the content has no children. |
| escapeKeydownBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Escape behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to its parent component that has an escape keydown handler. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press. |
| forceMount | boolean | – | Whether to force mount the component. |
| hideWhenDetached | boolean | – | |
| interactOutsideBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Interact outside behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to the parent element. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an outside interaction. |
| loop | boolean | false | Whether or not to loop through the items when navigating with the keyboard. |
| onEscapeKeydown | ((e: KeyboardEvent) => void) | – | Callback fired when escape is pressed. |
| onFocusOutside | ((event: FocusEvent) => void) | – | Callback fired when focus leaves the dismissible layer. |
| onInteractOutside | InteractOutsideEventHandler | – | Callback fired when an outside interaction event completes, which is either a pointerup, mouseup, or touchend event, depending on the user's input device. |
| portalProps | PortalProps | – | Props for the portal the content renders in: to (the element to render in, the body by default) and disabled (render in place). |
| preventOverflowTextSelection | boolean | true | Passing true will prevent the overflow of text selection outside the element, provided the element is the top layer. |
| preventScroll | boolean | true | Whether to prevent scrolling the body when the content is open. Whether to prevent body scrolling when the content is open. |
| side | "top" | "right" | "bottom" | "left" | – | The preferred side of the anchor to render against when open. Will be reversed when collisions occur. |
| sideOffset | number | 4 | The distance in pixels from the anchor to the floating element. |
| sticky | "partial" | "always" | – | |
| strategy | "absolute" | "fixed" | – | The positioning strategy to use for the floating element. |
| updatePositionStrategy | "always" | "optimized" | "optimized" | "optimized" will only update the position when necessary, while "always" will update the position on each animation frame, which is useful if the floating content is following something like a mouse cursor. |
Combobox.Item
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Whether the item is disabled. |
| label | string | – | The label of the item. If provided, this is the item that users will search for. If not provided, the value will be used as the label. |
| onHighlight | (() => void) | – | A callback function called when the item is highlighted. This can be used as a replacement for onfocus since we don't actually focus the item and instead rely on the aria-activedescendant attribute to indicate the highlighted item. |
| onUnhighlight | (() => void) | – | A callback function called when the item is unhighlighted. This can be used as a replacement for onblur since we don't actually focus the item and instead rely on the aria-activedescendant attribute to indicate the highlighted item. |
| value required | string | – | The value of the item. |
Combobox.Group
No props of its own beyond the common ones.
Combobox.GroupHeading
No props of its own beyond the common ones.
Combobox.Portal
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Disable portalling and render the component inline |
| to | PortalTarget | document.body | Where to portal the content to. |