This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Combobox

An input combined with a list of predefined items to select.

Examples#

Value selection with IDs#

When your app stores IDs rather than labels, give each ComboboxItem the ID as its value, and the label as its content. With selectionMode="single", the input shows the label, and the form gets the ID.

The label is also used for filtering and typeahead.

Multiple select#

The combobox can allow multiple selections by adding the multiple prop to Combobox. Selection chips are rendered with ComboboxChip inside the input that can be removed. Render a chip with the value of each item: the browser keeps the selection, and shows the chips of the chosen items.

In order for screen readers to announce how to reach and remove the chips, add aria-description to ComboboxChip and selectedDescription to ComboboxInput, alongside the aria-label on ComboboxChips and ComboboxChipRemove. Translate them together with the rest of your interface.

Grouped#

Organize related options with ComboboxGroup and ComboboxGroupLabel to add section headings inside the popup.

Input inside popup#

ComboboxInput can be rendered inside ComboboxPopup to create a searchable select popup. The trigger shows the value with ComboboxValue.

Async search (single)#

Load items from the server on input changes: the onChange handler of ComboboxInput calls a repository service, and the state keeps the results. With phem:bind on the root, the browser keeps the selection and the selected item in the list. ComboboxStatus shows the texts of the states of the search: pending with a spinner while it runs, error when the handler throws, idle before the user types, and empty when nothing matches. This pattern avoids needing to load items upfront.

The onClick handler of an item assigns the person. The selection shows at once, and the clear button shows a spinner until the server answers. When the handler fails, the button shows a warning, and after one second the selection goes. Choose Marvin McKinney to see a failure.

This example sends events to the server: PHP runs in your browser.

Async search (multiple)#

Load items from the server on input changes while supporting multiple selections. Give each ComboboxChip the value of its item: the browser keeps the chosen items in the list while new matches come in. The handler of an item adds or removes the person, and a choice with a text closes the popup and clears the text. This pattern avoids needing to load items upfront.

A choice shows at once: the browser adds a chip from ComboboxChipTemplate, where {label} is the label of the item, and its remove button shows a spinner until the server answers. When the handler fails, the button shows a warning, and after one second the chip goes. Choose Marvin McKinney to see a failure.

This example sends events to the server: PHP runs in your browser.

Async search in a form#

Load items from the server on input changes, and send the choices with the form. Give the root a name and a selectionMode (single, or multiple with multiple), and the items no onClick handler: the browser keeps the selection, and a choice sends no event. With multiple, the browser adds a chip from ComboboxChipTemplate for each choice, and each chip has a hidden input with the value of its item. The onSubmit handler of the form gets the values.

Use this pattern when the app saves all the data of a form on submit. Use the async search examples above when each choice saves at once.

This example sends events to the server: PHP runs in your browser.

Creatable#

Create a new item when the filter matches no items, opening a creation Dialog.

This example sends events to the server: PHP runs in your browser.

Large datasets#

For a large dataset, render only the first matches in the popup, and show the total in the status. The server searches the dataset at each change of the input.

This example sends events to the server: PHP runs in your browser.

API reference#

Import the components of the family:

use Phem\Ui\Combobox;
use Phem\Ui\ComboboxChipTemplate;
use function Phem\Ui\{ComboboxLabel, ComboboxInputGroup, ComboboxInput, ComboboxActions, ComboboxTrigger, ComboboxValue, ComboboxIcon, ComboboxClear, ComboboxSpinner, ComboboxPopup, ComboboxList, ComboboxRow, ComboboxItem, ComboboxItemIndicator, ComboboxItemLabel, ComboboxHighlight, ComboboxEmpty, ComboboxStatus, ComboboxGroup, ComboboxGroupLabel, ComboboxSeparator, ComboboxChips, ComboboxChip, ComboboxChipRemove};

Combobox#

A text field with a list of suggestions: Base UI Combobox.Root, on the phem-combobox controller.

The browser filters a list that the server renders one time, as in Base UI: set filter to contains, starts-with or fuzzy (the characters of the text in the same order, as match-sorter). A choice writes the label of the item in the input. With multiple, the browser also keeps the chosen items: render a ComboboxChip with a value for each item, and the chips of the chosen items show. For a long list, the server filters: bind the input to a state (phem:bind), filter the items in the render, and choose an item in the handler of its onClick. The server marks the chosen items with selected. For the Async search of Base UI, search in the onChange handler of the input, and bind the root to a state with phem:bind: the browser selects one item (selectionMode is single), and keeps the selected item in the list. With multiple, the browser keeps the items of the chips (a ComboboxChip with a value). A ComboboxStatus shows the state of the search. An item with an onClick handler shows its choice before the server answers: with multiple, a ComboboxChipTemplate gives the chip. While the handler runs, the ComboboxChipRemove of the chip (or the ComboboxClear) shows a spinner. When the handler fails, it shows a warning, and after one second the choice goes. In a form that sends the choice on submit, give the root a name and a selectionMode (single, or multiple with multiple), and the items no handler: the browser keeps the selection, also with a list of the server. With multiple, a ComboboxChipTemplate gives the chips of the choices. Give an id: the ids of the input and the items start with it, so the highlight stays on its item through the morph.

mode is the aria-autocomplete of the input (list, both, inline, none).

autoHighlight gives the highlight to the first item when the user types; 'always' gives an open list a highlight at all times. keepHighlight keeps the highlight when the pointer leaves the items, and highlightItemOnHover: false stops the highlight on hover. With no ComboboxPopup, the ComboboxList is inline and always open (a command palette). grid makes the list a grid: put the items in ComboboxRow parts. With filter, the visible items fill the rows again after each filter, and a row with no visible item hides.

The props of the Base UI Positioner are props of the root. The popup goes in the top layer (popover="manual"), so it needs no Portal.

PropTypeDefault
id (required)string—
multipleboolfalse
filter?stringnull
mode?stringnull
limit?intnull
locale?stringnull
autoHighlightbool|stringfalse
keepHighlightboolfalse
highlightItemOnHoverbooltrue
gridboolfalse
insertInto?stringnull
loopFocusbooltrue
side'top' | 'bottom' | 'left' | 'right' | 'inline-start' | 'inline-end'Side::Bottom
align'start' | 'center' | 'end'Align::Start
sideOffsetint4
alignOffsetint0
collisionPaddingint5
name?stringnull
value?Boundnull
defaultValuestring|array|nullnull
defaultInputValue?stringnull
selectionMode?stringnull
openOnInputClick?boolnull
children?Htmlnull
...attrsarray[]

ComboboxLabel#

The label of the input.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxInput#

The text field. Search in its onChange handler: the event gives the text of the user in value (no text while the input shows the label of the selection). Or bind it to a state of the app with phem:bind.

The input has its own border, also in a ComboboxInputGroup. In the ComboboxChips, the chips are the box: put the input directly in the ComboboxChips, and it has no border.

With multiple and a list of the browser, the placeholder shows only when no item is chosen, and selectedDescription is the aria-description while an item is chosen: {count} is the number of the chosen items.

PropTypeDefault
value?Boundnull
selectedDescription?stringnull
...restmixed—

ComboboxPopup#

The popup of the list. In an Autocomplete it has no animation, as in Base UI.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxItem#

A suggestion. value gives its id, and the value of the selection.

With a list of the browser (filter on the root), a click selects the item in the browser (with multiple, it adds or removes the item): it needs no handler, and the items with the defaultValue of the root are selected at first. With a list of the server, a click runs its onClick handler, which chooses it, and the server gives selected to the chosen items. With a list of the server and the single selection mode, a click selects the item in the browser, and the item with the bound value of the root is selected. label is the text of the item for the input and the filter, when it is not the text of the item. keywords are more texts that the filter of the browser finds the item with, for example ['citrus', 'sour']: each keyword matches alone, and the label still fills the input.

PropTypeDefault
value (required)string—
label?stringnull
keywordsarray[]
selectedboolfalse
disabledboolfalse
children?Htmlnull
...restmixed—

ComboboxChip#

A chosen value, for a combobox with multiple. Give it an aria-label.

With a list of the browser, render a chip for each item, with the value of its item: the browser shows the chips of the chosen items, in the order of the choices. With a name on the root, the chip has a hidden input with its value, so a form sends the chosen values. The chip of a ComboboxChipTemplate also has it: its input gets the value of the item. A chip with no value out of a template has no input.

PropTypeDefault
value?stringnull
children?Htmlnull
...restmixed—

ComboboxInputGroup#

The place of the input, the clear button and the trigger: the buttons go over the end of the input. A press on the group out of the input and the buttons gives the focus to the input. The input fills the group: give the group its width. With multiple, put the ComboboxChips in it: the chips are the box.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxActions#

The buttons at the end of the input group: put a ComboboxClear and a ComboboxTrigger in it.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxTrigger#

The button that opens and closes the popup. Give it an aria-label.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxValue#

The label of the selected item, or the placeholder, in a trigger: for a combobox with its input in the popup.

PropTypeDefault
placeholder?stringnull
...restmixed—

ComboboxIcon#

The icon of a trigger that shows the value.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxClear#

The button that clears the input. Give it an aria-label. While the handler of the selected item runs, it shows a spinner (data-pending), and a warning when the handler fails (data-failed).

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxSpinner#

The spinner of a choice that waits for the server. It is hidden: a variant of its button shows it.

PropTypeDefault
...restmixed—

ComboboxList#

The list of the items in the popup (listbox). The browser filters its ComboboxItem parts while the user types.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxRow#

A row of items in a list with grid. Put the ComboboxItem parts directly in the row.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxItemIndicator#

The check of a chosen item.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxItemLabel#

The label of an item, in the column after the indicator.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxHighlight#

The text of an item, with the text of the user in a mark element, as the highlightText of the Base UI demos. The browser marks the parts of the text that are the text of the input (the case and the accents do not count). markClassName is the class of the mark elements.

PropTypeDefault
markClassNamestring'bg-transparent font-bold text-match'
children?Htmlnull
...restmixed—

ComboboxEmpty#

The text that shows when the list has no item.

text is a template that the browser fills at each render: {query} is the text of the input, for example No results found for "{query}".

PropTypeDefault
text?stringnull
children?Htmlnull
...restmixed—

ComboboxStatus#

A text for screen readers, such as the number of results (role="status").

For a search of the server, give the texts of its states: pending shows with a spinner while the search runs, error when the search fails (the handler of the input throws), idle when the input has no text and the list has no item, empty when no item matches the text, and results when items show (the server renders the text, for example "3 results found"). The browser shows one of them, and the content always shows. With no text to show, the status hides, and it does not open the popup.

more shows with results when the limit of the root hides matches of the filter of the browser. It is a template: {count} is the number of hidden matches, for example Hiding {count} results (type a more specific query to narrow results).

PropTypeDefault
idle?stringnull
pending?stringnull
empty?stringnull
error?stringnull
results?stringnull
more?stringnull
children?Htmlnull
...restmixed—

ComboboxGroup#

A group of items in the list, with a ComboboxGroupLabel.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxGroupLabel#

The label of a ComboboxGroup.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxSeparator#

A line between two items or two groups of the list.

PropTypeDefault
...restmixed—

ComboboxChips#

The box of the chips and the input, for a combobox with multiple: it grows with the rows of the chips. Put it in a ComboboxInputGroup, and put the ComboboxInput directly in it. It gets role="toolbar" while a chip shows. With a list of the browser, its aria-label shows only when an item is chosen.

PropTypeDefault
children?Htmlnull
...restmixed—

ComboboxChipTemplate#

The chip that the browser shows at once for a choice of the server, before the handler of the item answers, or for a choice that the browser keeps (the multiple selection mode with a list of the server): put a ComboboxChip with no value in it. {label} in its text and its attributes is the label of the item, and {value} in its attributes the value of the item. Put it after the chips of the server, in the ComboboxChips.

PropTypeDefault
children?Htmlnull

ComboboxChipRemove#

The button that removes a chip. Give it an aria-label. With a list of the browser, a click removes the chosen item. Else give it an onClick handler. On the chip of a ComboboxChipTemplate, it shows a spinner while the handler of the item runs, and a warning when the handler fails.

PropTypeDefault
children?Htmlnull
...restmixed—

The source of the examples: examples/ui/Combobox.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close