Command
A searchable list of actions for command palettes and quick switchers.
Anatomy
<Command.Root>
<Command.Label />
<Command.Control>
<Command.Input />
</Command.Control>
<Command.List>
<Command.ItemGroup>
<Command.ItemGroupLabel />
<Command.Item>
<Command.ItemText />
</Command.Item>
</Command.ItemGroup>
</Command.List>
<Command.Empty />
</Command.Root>
Examples
Basic
Pass a collection to Command.Root and render the filtered items from Command.Context. Typing filters the items.
Arrow keys, Home and End move the highlight while focus stays in the input. Enter or a click calls onSelect.
Dialog
Put the command in a Dialog for a command palette. Open it with a useHotkey shortcut and give the dialog content an
aria-label.
Groups
Use groupBy on the collection and render collection.group() with ItemGroup and ItemGroupLabel. Groups with no
matching items are not rendered. Disabled items are skipped by the keyboard.
Empty State
Command.Empty renders its children when no item matches the input.
Keywords
Items are fuzzy-matched on their text and an optional keywords array, best match first.
{ value: 'settings', label: 'Open settings', keywords: ['preferences', 'config'] }
Pages
Use createPageListCollection and give an item a parent to put it on that item's page. Selecting the parent opens the
page, Backspace in an empty input goes up, and typing also searches nested pages. Pass page to start on one.
const collection = createPageListCollection({
items: [
{ value: 'theme', label: 'Change theme' },
{ value: 'dark', label: 'Dark', parent: 'theme' },
{ value: 'light', label: 'Light', parent: 'theme' },
],
page: 'theme',
})
Render breadcrumbs with path and a result's location with getAncestors. To make Escape go back before the dialog
closes, call back() from onEscapeKeyDown:
const command = useCommand({ collection })
<Dialog.Root
onEscapeKeyDown={(event) => {
if (command.canGoBack) {
event.preventDefault()
command.back()
}
}}
>
{/* ... */}
<Command.RootProvider value={command}>{/* ... */}</Command.RootProvider>
</Dialog.Root>
Async
Load items with useAsyncList, call setFilter as the input changes, and pass isLoading to loading. Pass a
filter that returns every item, since the server already filtered them.
Root Provider
Use RootProvider with the useCommand hook to control the command from outside, for example to clear the input.
Guides
Custom filtering
Pass filter to replace the fuzzy search. It returns the matching items in the order to show them.
<Command.Root
collection={collection}
filter={(items, query) => items.filter((item) => item.label.toLowerCase().startsWith(query.toLowerCase()))}
/>
Accessibility
The input is a combobox that controls the listbox and points to the highlighted option with aria-activedescendant,
so focus never leaves the input. Result counts are announced to screen readers. Label the command with Command.Label,
and label the dialog when you render it in one.
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
collection | ListCollection<T, never> | PageListCollection<T>The collection of items. Use `createPageListCollection` for nested pages, where an item with a `parent` value is shown on the page of that item. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
autoFocus | booleanWhether to autofocus the input on mount | |
defaultHighlightedValue | stringThe initial highlighted value of the combobox when rendered. Use when you don't need to control the highlighted value of the combobox. | |
defaultInputValue | '' | stringThe initial value of the combobox's input when rendered. Use when you don't need to control the value of the combobox's input. |
disabled | booleanWhether the combobox is disabled | |
filter | CommandFilter<T>Returns the items that match the query, best match first. Defaults to a fuzzy search of the item text and its `keywords`. | |
highlightedValue | stringThe controlled highlighted value of the combobox | |
id | stringThe unique identifier of the command | |
ids | Partial<{
root: string
label: string
control: string
input: string
content: string
list: string
trigger: string
clearTrigger: string
item: (id: string, index?: number | undefined) => string
positioner: string
itemGroup: (id: string | number) => string
itemGroupLabel: (id: string | number) => string
}>The ids of the elements in the combobox. Useful for composition. | |
inputValue | stringThe controlled value of the combobox's input | |
loading | booleanWhether the results are being loaded (e.g. fetched from a server). While loading, the listbox is marked with `aria-busy`, the loading message is announced instead of the result count, and "no results" is never announced. | |
loopFocus | true | booleanWhether to loop the keyboard navigation through the items |
navigate | (details: NavigateDetails) => voidFunction to navigate to the selected item | |
onHighlightChange | (details: HighlightChangeDetails<T>) => voidFunction called when an item is highlighted using the pointer or keyboard navigation. | |
onInputValueChange | (details: InputValueChangeDetails) => voidFunction called when the input's value changes | |
onPageChange | (details: CommandPageChangeDetails<T>) => voidFunction called when the page changes | |
onSelect | (details: SelectionDetails) => voidFunction called when an item is selected | |
placeholder | stringThe placeholder text of the combobox's input | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
scrollToIndexFn | (details: ScrollToIndexDetails) => voidFunction to scroll to a specific index | |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states |
Control
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Empty
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Input
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
ItemGroupLabel
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
ItemGroup
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Item
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
item | anyThe item to render | |
persistFocus | booleanWhether hovering outside should clear the highlighted state | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CommandItemState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
ItemText
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Label
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
List
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseCommandReturn<T> | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |