Command

A searchable list of actions for command palettes and quick switchers.

Loading...

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.

AttributeDefaultType
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
boolean

Use 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
boolean

Whether to autofocus the input on mount

defaultHighlightedValue
string

The initial highlighted value of the combobox when rendered. Use when you don't need to control the highlighted value of the combobox.

defaultInputValue''
string

The 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
boolean

Whether 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
string

The controlled highlighted value of the combobox

id
string

The 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
string

The controlled value of the combobox's input

loading
boolean

Whether 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.

loopFocustrue
boolean

Whether to loop the keyboard navigation through the items

navigate
(details: NavigateDetails) => void

Function to navigate to the selected item

onHighlightChange
(details: HighlightChangeDetails<T>) => void

Function called when an item is highlighted using the pointer or keyboard navigation.

onInputValueChange
(details: InputValueChangeDetails) => void

Function called when the input's value changes

onPageChange
(details: CommandPageChangeDetails<T>) => void

Function called when the page changes

onSelect
(details: SelectionDetails) => void

Function called when an item is selected

placeholder
string

The 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) => void

Function to scroll to a specific index

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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
any

The item to render

persistFocus
boolean

Whether 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
value
UseCommandReturn<T>

asChild
boolean

Use 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.

Context