Combobox
An input that filters a list as the reader types, for one value or several as chips.
A combobox lets the reader type to narrow a list, then choose. Use it when the list is too long to
scan, or when the reader knows what to look for: a city, a currency, a colleague. It filters on the
client; for a list that comes from a server as the reader types, use Async. For a ready
search picker with its trigger and messages, see the composed Search.
Value: none
1 chosen
// apps/docs/examples/ui/combobox.tsx'use client';
import {useState} from 'react';
import { Combobox, ComboboxChip, ComboboxChips, ComboboxChipsInput, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList, ComboboxValue, useComboboxAnchor,} from '@workspace/active-ui/components/primitives/combobox';import { Field, FieldDescription, FieldLabel,} from '@workspace/active-ui/components/primitives/field';
// A fixed list; a list from a server belongs in AsyncCombobox.const CITIES = [ 'Amsterdam', 'Antwerp', 'Berlin', 'Brussels', 'Copenhagen', 'Dublin', 'Lisbon', 'Madrid', 'Paris', 'Rotterdam', 'Utrecht', 'Vienna',];
/** * Type to filter a list, then choose: one value in an input with a clear * button, and several values as chips in one field. `items` is the full * list; the combobox filters it as you type and renders the matches through * the function child of `ComboboxList`. */export default function ComboboxExample() { const [city, setCity] = useState<string | null>(null); const [offices, setOffices] = useState<string[]>(['Utrecht']); const anchor = useComboboxAnchor(); return ( <div className="grid w-full max-w-sm gap-8"> <Field> <FieldLabel htmlFor="combobox-city">Head office</FieldLabel> <Combobox items={CITIES} value={city} onValueChange={setCity}> <ComboboxInput id="combobox-city" placeholder="Search a city" triggerLabel="Show cities" showClear clearLabel="Clear head office" /> <ComboboxContent> <ComboboxEmpty>No city matches.</ComboboxEmpty> <ComboboxList> {(item: string) => ( <ComboboxItem key={item} value={item}> {item} </ComboboxItem> )} </ComboboxList> </ComboboxContent> </Combobox> <FieldDescription>Value: {city ?? 'none'}</FieldDescription> </Field>
<Field> <FieldLabel htmlFor="combobox-offices">Branch offices</FieldLabel> <Combobox items={CITIES} multiple value={offices} onValueChange={setOffices} > <ComboboxChips ref={anchor}> <ComboboxValue> {(values: string[]) => values.map((value) => ( <ComboboxChip key={value} removeLabel={`Remove ${value}`}> {value} </ComboboxChip> )) } </ComboboxValue> <ComboboxChipsInput id="combobox-offices" placeholder={offices.length ? '' : 'Add a city'} /> </ComboboxChips> <ComboboxContent anchor={anchor}> <ComboboxEmpty>No city matches.</ComboboxEmpty> <ComboboxList> {(item: string) => ( <ComboboxItem key={item} value={item}> {item} </ComboboxItem> )} </ComboboxList> </ComboboxContent> </Combobox> <FieldDescription>{offices.length} chosen</FieldDescription> </Field> </div> );}Usage#
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList,} from '@workspace/active-ui/components/primitives/combobox';
<Combobox items={cities} value={city} onValueChange={setCity}> <ComboboxInput placeholder="Search a city" aria-label="City" /> <ComboboxContent> <ComboboxEmpty>No city matches.</ComboboxEmpty> <ComboboxList> {(item: string) => ( <ComboboxItem key={item} value={item}> {item} </ComboboxItem> )} </ComboboxList> </ComboboxContent></Combobox>;How filtering works#
itemsis the whole list. The combobox filters it against the typed text and passes each match to the function child ofCombobox, which renders it. WithoutList items, there is nothing to filter.For objects, the text of an item is its
label, or whatitemreturns; compare objects withTo String Label is.Item Equal To Value The default filter matches case-insensitively with the browser's collator. Pass
filterfor a rule of your own, or build one withComboboxfrom Base UI with the reader'sPrimitive. use Filter locale.Comboboxshows its children only when nothing matches, and is a polite live region.Empty
Types#
Combobox is generic: Combobox<Value, Multiple = false, Item = Value>. items is a list of
Item; with one value, value is a Value or null and on receives the same; with
multiple, both are a Value[]. The function child of Combobox receives an Item.
TypeScript infers the type from items and value, or you name it: <Combobox<Airport> โฆ>. The
API list below keeps these type parameters: value is Combobox, filter
receives an Item. Where it shows any, the type comes that way from Base UI: items also accepts
readonly any[] and groups of any, and the value of a Combobox and the items of a
Combobox are any. Those places are not checked against your type; the example shows the
contract with a real one.
Nothing chosen.
// apps/docs/examples/ui/combobox-objects.tsx'use client';
import {useState} from 'react';
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList,} from '@workspace/active-ui/components/primitives/combobox';import { Field, FieldDescription, FieldLabel,} from '@workspace/active-ui/components/primitives/field';
interface Airport { code: string; label: string;}
const AIRPORTS: readonly Airport[] = [ {code: 'AMS', label: 'Amsterdam Schiphol'}, {code: 'BRU', label: 'Brussels'}, {code: 'CDG', label: 'Paris Charles de Gaulle'}, {code: 'FRA', label: 'Frankfurt'}, {code: 'LIS', label: 'Lisbon'},];
/** * The generic contract with objects: `Combobox<Airport>` ties `items`, the * `value`, the argument of `onValueChange` and the function child of * `ComboboxList` to one type. The text shown and filtered is the `label`; * `isItemEqualToValue` compares by code, so a value from elsewhere (a form, * the server) matches its item. */export default function ComboboxObjectsExample() { const [airport, setAirport] = useState<Airport | null>(null); return ( <Field className="w-full max-w-sm"> <FieldLabel htmlFor="combobox-airport">Departure</FieldLabel> <Combobox<Airport> items={AIRPORTS} value={airport} onValueChange={(next) => setAirport(next)} isItemEqualToValue={(item, value) => item.code === value.code} > <ComboboxInput id="combobox-airport" placeholder="Search an airport" triggerLabel="Show airports" /> <ComboboxContent> <ComboboxEmpty>No airport matches.</ComboboxEmpty> <ComboboxList> {(item: Airport) => ( <ComboboxItem key={item.code} value={item}> {item.label} ({item.code}) </ComboboxItem> )} </ComboboxList> </ComboboxContent> </Combobox> <FieldDescription> {airport ? `Chosen: ${airport.code}` : 'Nothing chosen.'} </FieldDescription> </Field> );}One value or several#
One value:
Comboboxis the field.Input showadds a clear button, which needs aClear clear.Label show(defaultTrigger true) shows the chevron that opens the list: give it atriggerto make it a named button for the keyboard too; without one it serves the pointer only, and the input opens the list withLabel โ.Several values: pass
multiple.Comboboxis the field, holding aChips Comboboxper value (throughChip Combobox, which hands you the values) and aValue Comboboxto type in. Anchor the popup to the chips withChips Input use: pass the ref toCombobox Anchor() Comboboxand toChips anchorofCombobox. A chip has a remove button unlessContent show; it needs aRemove={false} remove.Label ComboboxwithGroup Combobox,Label ComboboxandCollection Comboboxgroup the items.Separator
What your application provides#
A label for the input (
idwith aField, orLabel aria-), and the accessible names of the chevron, clear and remove buttons; the package has no English defaults.label The list, its labels and the value.
The text for no matches.
Accessibility and keyboard#
The input has
role="combobox"; typing opens the list.โandโmove through the matches,Enterchooses andEscapecloses the list; the clear button empties the value.In multiple mode,
Backspacein an empty input removes the last chip.The matches are announced as you move; the chosen item carries
aria-.selected
API#
Combobox#
Import from @workspace/
Generic over Value, Multiple extends boolean | undefined = false, Item = Value; the props below name them.
All props come from the Base UI part this component is built on.
Props
- Type
stringnumberreadonly string[]
The uncontrolled input value when initially rendered.
To render a controlled input, use the input prop instead.
- Type
boolean
Whether the popup is currently open. Use when controlled.
- Type
boolean- Default
false
Whether the popup is initially open.
To render a controlled popup, use the open prop instead.
- Type
ComboboxInput Value<โฆ> null
The uncontrolled selected value of the combobox when it's initially rendered.
To render a controlled combobox, use the value prop instead.
- Type
string
Provides a hint to the browser for autofill.
- Type
boolean- Default
false
Whether the first matching item is highlighted automatically while filtering.
- Type
ReactNode
The content of this part, in the forms its type allows.
- Type
boolean- Default
false
Whether the component should ignore user interaction.
- Type
(item, query, itemTo String?) => boolean null
Filter function used to match items vs input query. Receives the source item, which is the derived value's item when items is a create collection, and the item itself otherwise.
- Type
readonly Item[]readonly Group<Item>[]
Filtered items to display in the list. When provided, the list uses these items instead of filtering the items prop internally. When items is also provided, this array must preserve its flat or grouped structure. With a create collection, pass source items rather than derived values. Nullish entries are not supported, as in items. Use when you want to control filtering logic externally with the use hook.
- Type
string
Identifies the form that owns the internal input. Useful when the combobox is rendered outside the form.
- Type
boolean- Default
false
Whether list items are presented in a grid layout. When enabled, arrow keys navigate across rows and columns inferred from DOM rows.
- Type
boolean- Default
true
Whether moving the pointer over items should highlight them. Disabling this prop allows CSS :hover to be differentiated from the :focus (data-) state.
- Type
string
The id of the component.
- Type
boolean- Default
false
Whether the list is rendered inline without using the component's own popup.
Specify open unconditionally in conjunction with this prop so the list is considered visible: <Combobox.
In a Combobox. > Dialog. composition, bind the Combobox's open and on props to the Dialog's open and on state instead so the component resets its transient state (filter query, highlighted item, and input value) when the dialog closes.
- Type
(itemValue, value) => boolean
Custom comparison logic used to determine if a combobox item value matches the current selected value. Useful when item values are objects without matching referentially. With a create collection, both arguments are derived values. Defaults to Object. comparison.
- Type
ComboboxItem Collection<โฆ> readonly any[]readonly Group<any>[]
The items to be displayed in the list. Can be a flat array of items, an array of groups with items, or a collection created by the create function, which derives each item's selection value and label. Nullish entries are not supported: remove them from the data before passing it.
- Type
(itemValue: Value) => string
When the item values are objects (<Combobox.), this function converts the object value to a string representation for display in the input. If the shape of the object is { value, label }, the label will be used automatically without needing to specify this prop. With a create collection, this receives the derived value, and the collection's get takes precedence for values it can resolve.
- Type
(itemValue: Value) => string
When the item values are objects (<Combobox.), this function converts the object value to a string representation for form submission. If the shape of the object is { value, label }, the value will be used automatically without needing to specify this prop. With a create collection, this receives the derived value.
- Type
number- Default
-1
The maximum number of items to display in the list.
- Type
Intl.Locales Argument
The locale to use for string comparison. Defaults to the user's runtime locale.
- Type
boolean- Default
true
Whether to loop keyboard focus back to the input when the end of the list is reached while using the arrow keys. The first item can then be reached by pressing <kbd>ArrowDown</kbd> again from the input, or the last item can be reached by pressing <kbd>ArrowUp</kbd> from the input. The input is always included in the focus loop per ARIA Authoring Practices. When disabled, focus does not move when on the last element and the user presses <kbd>ArrowDown</kbd>, or when on the first element and the user presses <kbd>ArrowUp</kbd>.
- Type
boolean- Default
false
Determines if the popup enters a modal state when open.
true: user interaction is limited to the popup: document page scroll is locked and pointer interactions on outside elements are disabled.false: user interaction with the rest of the document is allowed.
On touch devices, a true modal blocks outside taps but leaves the page scrollable unless the popup spans nearly the full viewport width, matching native iOS behavior.
- Type
Multiple- Default
false
Whether multiple items can be selected.
- Type
string
Identifies the field when a form is submitted.
- Type
boolean- Default
true
Whether the popup opens when clicking the input.
- Type
boolean- Default
false
Whether the user should be unable to choose a different option from the popup.
- Type
boolean- Default
false
Whether the user must choose a value before submitting a form.
- Type
boolean- Default
false
Whether the items are being externally virtualized.
- Type
RefObject<โฆ>
A ref to imperative actions.
unmount: Ends the closing phase of the combobox after an externally controlled closing animation finishes. CallpreventinUnmount On Close() onfirst, otherwise the combobox completes closing on its own. Whether it leaves the DOM is decided byOpen Change keepon the portal.Mounted close: Closes the combobox imperatively when called.
- Type
Ref<HTMLInputElement>
A ref to the hidden input element.
Events
Called by the component when something happens.
- Type
(inputValue, event Details) => void
Event handler called when the input value changes.
- Type
(open, eventDetails) => void
Event handler called when the popup is opened or closed.
- Type
(value, eventDetails) => void
Event handler called when the selected value of the combobox changes.
- Type
(highlightedValue, event Details) => void
Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item value (or undefined if no item is highlighted) and event details with a reason property describing why the highlight changed. The reason can be:
'keyboard': the highlight changed due to keyboard navigation.'pointer': the highlight changed due to pointer hovering. The event may be aMouserather than aEvent Pointer.Event 'none': the highlight changed for another reason, such as typing,auto, the item list changing, or the popup opening or closing.Highlight
- Type
(open: boolean) => void
Event handler called after any animations complete when the popup is opened or closed.
Takes only the props listed.
ComboboxInput#
Import from @workspace/
Props marked Base UI are behaviour of the Base UI part this component is built on.
Props
- Type
string
The accessible name of the clear button; required with it.
- Type
boolean- Default
false
Whether the component should ignore user interaction.
- Type
boolean
Whether a button that clears the value is shown.
- Type
boolean- Default
true
Whether the chevron button that opens the list is shown.
- Type
string
The accessible name of the chevron button, such as "Show cities". With it the button is a named tab stop; without it the button is for the pointer only: out of the tab order and hidden from assistive technology, since the input itself opens the list with the arrow keys.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
Other parts#
ComboboxContent
All props come from the Base UI part this component is built on.
Props
- Type
Align- Default
'center'
How to align the popup relative to the specified side.
- Type
number | OffsetFunction - Default
0
Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties:
data.: the dimensions of the anchor element with propertiesanchor widthandheight.data.: the dimensions of the positioner element with propertiespositioner widthandheight.data.: which side of the anchor element the positioner is aligned against.side data.: how the positioner is aligned relative to the specified side.align
- Type
ElementRefObject<Element | null> VirtualElement () => โฆnull
An element to position the popup against. By default, the popup will be positioned against the trigger.
- Type
booleanRefObject<HTMLElement | null> (closeType) => โฆ
Determines the element to focus when the popup is closed.
false: Do not move focus.true: Move focus based on the default behavior (trigger or previously focused element).Ref: Move focus to the ref element.Object function: Called with the interaction type (mouse,touch,pen, orkeyboard). Return an element to focus,trueto use the default behavior, orfalse/undefinedto do nothing.
- Type
booleanRefObject<HTMLElement | null> (openType) => โฆ
Determines the element to focus when the popup is opened.
false: Do not move focus.true: Move focus based on the default behavior (first tabbable element or popup).Ref: Move focus to the ref element.Object function: Called with the interaction type (mouse,touch,pen, orkeyboard). Return an element to focus,trueto use the default behavior, orfalse/undefinedto do nothing.
- Type
Side- Default
'bottom'
Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
- Type
number | OffsetFunction - Default
0
Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties:
data.: the dimensions of the anchor element with propertiesanchor widthandheight.data.: the dimensions of the positioner element with propertiespositioner widthandheight.data.: which side of the anchor element the positioner is aligned against.side data.: how the positioner is aligned relative to the specified side.align
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxList
All props come from the Base UI part this component is built on.
Props
- Type
(item, index) => ReactNode ReactNode
The content of this part, in the forms its type allows.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxItem
All props come from the Base UI part this component is built on.
Props
- Type
ReactNode
The content of this part, in the forms its type allows.
- Type
boolean- Default
false
Whether the component should ignore user interaction.
- Type
number
The index of the item in the list. Improves performance when specified by avoiding the need to calculate the index automatically from the DOM.
- Type
any- Default
null
A unique value that identifies this item.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Events
Called by the component when something happens.
- Type
(event) => void
An optional click handler for the item when selected. It fires when clicking the item with the pointer, as well as when pressing Enter with the keyboard if the item is highlighted when the Input or List element has focus.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxGroup
All props come from the Base UI part this component is built on.
Props
- Type
readonly any[]
Items to be rendered within this group. When provided, child Collection components will use these items.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxCollection
ComboboxSeparator
All props come from the Base UI part this component is built on.
Props
- Type
Orientation- Default
'horizontal'
The orientation of the separator.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxChip
Props marked Base UI are behaviour of the Base UI part this component is built on.
Props
- Type
string
The accessible name of that button, such as "Remove Utrecht".
- Type
boolean- Default
true
Whether the chip has a button that removes its value.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxChipsInput
All props come from the Base UI part this component is built on.
Props
- Type
boolean- Default
false
Whether the component should ignore user interaction.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxTrigger
All props come from the Base UI part this component is built on.
Props
- Type
boolean- Default
false
Whether the component should ignore user interaction.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
ComboboxValue
All props come from the Base UI part this component is built on.
Props
- Type
(selectedValue) => React Node ReactNode
Accepts a function that returns a React to format the selected value. Treat the value as read-only: in multiple mode it may be a shared frozen array when nothing is selected.
- Type
ReactNode
The placeholder value to display when no value is selected. This is overridden by children if specified, or by a null item's label in items.
Takes only the props listed.
Parts that take only their elementโs attributes
Combobox: the attributes of its element, with className and ref, and Base UI'sLabel renderto use your own element.Combobox: the attributes of its element, with className and ref, and Base UI'sEmpty renderto use your own element.Combobox: the attributes of its element, with className and ref, and Base UI'sChips renderto use your own element.
Functions, hooks and constants
- Type
() => RefObject<HTMLDiv Element | null>
A ref for the element the popup is anchored to. In multiple mode, pass it to Combobox and to anchor of Combobox, so the list opens below the whole field of chips instead of below the input.
Returns
An object with these fields.
- Type
HTMLDivElement | null
The current value of the ref.
This page adapts the Combobox documentation of shadcn/ui (MIT licence) to this package; see Third-party notices.