Build faster with Premium Chakra UI Components šŸ’Ž

Learn more
Skip to Content
DocsShowcaseBlogGuides
Sponsor

Command

Used to build searchable command menus, search dialogs, and pickers.

SourceStorybookRecipe
AI TipWant to skip the docs? Use our Agent Skills

Usage

import { Command, Portal, useCommandCollection } from "@chakra-ui/react"
<Command.Provider>
  <Command.Dialog>
    <Command.DialogTrigger />
    <Portal>
      <Command.DialogBackdrop />
      <Command.DialogContent>
        <Command.Root>
          <Command.Label />
          <Command.Control>
            <Command.Indicator />
            <Command.Input />
            <Command.ClearTrigger />
          </Command.Control>
          <Command.List>
            <Command.Loading />
            <Command.ItemGroup>
              <Command.ItemGroupLabel />
              <Command.Item>
                <Command.ItemText />
                <Command.ItemCommand />
              </Command.Item>
            </Command.ItemGroup>
            <Command.Separator />
            <Command.Empty />
          </Command.List>
          <Command.Footer />
        </Command.Root>
      </Command.DialogContent>
    </Portal>
  </Command.Dialog>
</Command.Provider>

Items are data. useCommandCollection ranks them as you type, and Command.Provider shares the result with the dialog and the root.

const command = useCommandCollection({ initialItems: commands })

<Command.Provider value={command}>
  <Command.Dialog>
    <Portal>
      <Command.DialogBackdrop />
      <Command.DialogContent>
        <Command.Root>{/* ... */}</Command.Root>
      </Command.DialogContent>
    </Portal>
  </Command.Dialog>
</Command.Provider>
  • Selecting an item runs its onSelect and closes the dialog. Set closeOnSelect: false on the item, or on Root, to keep it open.
  • Holding ⌘, Ctrl, Shift, or Alt while selecting keeps the dialog open, e.g. to open several links in new tabs. onSelect receives them as modifiers.
  • The dialog clears the query after it closes.
  • Command.DialogBackdrop is optional.
  • Set disabled: true on an item to show it without letting it run.
  • There's no built-in shortcut to open the dialog. Bind ⌘K with the tool your app already uses, as in Hotkeys.

Command runs actions and keeps no selected value. For a picker, use Combobox or Listbox.

Coming from cmdk

In cmdk, items are elements that cmdk filters in the DOM. Here, items are data that useCommandCollection ranks.

cmdkChakra UI
<Command><Command.Root> with useCommandCollection
<Command.Dialog><Command.Dialog> + <Command.DialogContent>
<Command.Input><Command.Input>
<Command.List><Command.List>
<Command.Item onSelect={fn}>{ label, value, onSelect: fn } in initialItems
<Command.Group heading="..."><Command.ItemGroup> + <Command.ItemGroupLabel>
<Command.Empty><Command.Empty>
shouldFilter={false}filter: null

Examples

Inline

Render Command.Root without a dialog and pass the collection with the command prop. Add a Command.Label to name the search, visually hidden if needed. In a dialog, the hidden title from Command.DialogContent names it instead.

Calendar
Search emoji
Calculator
Profile⌘P
Billing⌘B
Settings⌘,

Groups

Pass groupBy to useCommandCollection. Items are ranked within each group, and the group order stays fixed while you type.

Keywords

Add keywords to an item so other words find it, like "preferences" for Settings. Keyword matches rank just below label matches.

Highlight Matches

Set highlight on ItemText to bold the matched characters, including fuzzy and camelCase matches.

<Command.ItemText highlight>{item.label}</Command.ItemText>

Pass query and onQueryChange to keep the query in your own state, for example to sync it with the URL.

Try

Query: empty

Type >, @, or # first to search only commands, people, or channels. Backspace on an empty input clears the scope.

Tabs

Filter the items by the active tab.

Async Content

Pass load to fetch items as you type. Stale requests are cancelled, and Loading and Empty never show at the same time.

Load More

Return { items, cursor } from load to page through results. Call command.loadMore() when the user reaches the end of the list and command.hasMore is true.

Bring Your Own Data

To fetch with your own data layer, such as TanStack Query, control the query and pass the results in. Set filter: null so server results show as returned.

const [query, setQuery] = useState("")
const people = useQuery({
  queryKey: ["people", query],
  queryFn: () => searchPeople(query),
})

const command = useCommandCollection({
  initialItems: people.data ?? [],
  query,
  onQueryChange: setQuery,
  filter: null,
})

<Command.Root command={command} loading={people.isFetching}>
  {/* ... */}
</Command.Root>

Nested

Give an item a parent to put it on that item's page. Nothing else changes in the markup.

const command = useCommandCollection({
  initialItems: [
    { label: "Change status...", value: "status" },
    { label: "Done", value: "status:done", parent: "status" },
  ],
  deepSearch: true,
})
  • Selecting an item with children opens its page.
  • Escape goes back to the previous page, then closes the dialog.
  • Backspace on an empty input goes up a page.
  • Going back restores the query and highlights the item you came from.
  • command.pages is the trail of open pages, for a breadcrumb.
  • With deepSearch, a query also matches items in nested pages. command.getPath(item) returns where a match lives.
  • To open a page directly, such as from a shortcut, call command.reset("status") before opening the dialog. Escape then closes it.
Status
In progress
Priority
High
Assignee
OM
Olivia Martin
Project
Auth revamp
Labels
Bug

Shortcuts

S

Change status...

P

Change priority...

A

Assign to...

L

Add labels...

ShiftP

Move to project...

Ctrl.

Copy issue ID

CtrlShift,

Copy issue URL

Async Pages

Set page: true on an item to open a page that loads its own items. load receives the current page and reloads when it changes.

Input Pages

Build a page's items from the query, such as "Rename to …" or "Invite by email".

Fix login redirect loop on Safari

No invites yet

Hotkeys

Register the same items with useHotkeys so their shortcuts work while the menu is closed. Format them per platform with useFormatHotkey.

Try G then I, or C, with the menu closed

Store

Use useCommand with Command.RootProvider to work with the command's state from outside, such as controlling the dialog from a ⌘K shortcut.

Last search: None

Render items as anchors with asChild, so links open with a click, a middle-click, or Enter. ⌘+Enter or Ctrl+Enter opens the link in a new tab, like ⌘+click.

Recent Items

Store recently used commands and show them in their own group.

Toggle Items

Set closeOnSelect={false} and toggle values in onSelect to pick several items without closing.

BugAccessibility

Show shortcut hints or actions below the list with Footer.

Empty State

Customize what Empty shows when nothing matches.

Virtualized

Virtualize the list for large datasets. Pass a deferred copy of the query as filterQuery so typing stays responsive. Enter still runs the top match for what you typed.

const [query, setQuery] = useState("")
const command = useCommandCollection({
  initialItems,
  query,
  onQueryChange: setQuery,
  filterQuery: useDeferredValue(query),
})

Sizes

size on Command.Root sets the density. size on Command.Dialog sets the width.

Props

Root

PropDefaultType
hotkeys ['mod+k']
string[] | undefined

Keyboard shortcuts that toggle the palette. `mod` resolves to Command on macOS and Control elsewhere.

selectionMode 'none'
'none' | 'single' | 'multiple' | undefined

How selection behaves. The default `none` runs commands without persisting a selected state; use `onSelect` to execute them. Pass `single` or `multiple` to build pickers where selection persists.

size 'md'
'sm' | 'md' | 'lg'

The size of the component

loading false
boolean | undefined

Whether the command palette is loading results. Shows the `Spinner` and `Loading` parts, and is reflected on the panel and indicator parts.

hotkeyOptions
CommandHotkeyOptions | undefined

Options for the keyboard shortcuts.

closeOnSelect
boolean | undefined

Whether selecting an item closes the palette. Defaults to `true` in action mode and `false` in single and multiple selection modes.

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

RootProvider

Label

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Input

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Control

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Indicator

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

List

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Loading

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Item

PropDefaultType
onSelect
((details

Runs when the item is selected, before the Root `onSelect` and resulting `onOpenChange` callbacks.

closeOnSelect
boolean | undefined

Overrides `Root` close behavior for this item.

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

ItemText

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

ItemGroup

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

ItemGroupLabel

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

ItemCommand

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Empty

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Separator

PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
PropDefaultType
as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.

Previous

Combobox

Next

Listbox