Command
Used to build searchable command menus, search dialogs, and pickers.
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
onSelectand closes the dialog. SetcloseOnSelect: falseon the item, or onRoot, to keep it open. - Holding ā, Ctrl, Shift, or Alt while selecting keeps the dialog open, e.g. to
open several links in new tabs.
onSelectreceives them asmodifiers. - The dialog clears the query after it closes.
Command.DialogBackdropis optional.- Set
disabled: trueon an item to show it without letting it run. - There's no built-in shortcut to open the dialog. Bind
āKwith 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.
| cmdk | Chakra 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.
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>Controlled Search
Pass query and onQueryChange to keep the query in your own state, for
example to sync it with the URL.
Try
Query: empty
Prefix Search
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.pagesis 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
- OMOlivia Martin
- Project
- Auth revamp
- Labels
- Bug
Shortcuts
Change status...
Change priority...
Assign to...
Add labels...
Move to project...
Copy issue ID
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
Links
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.
Footer Actions
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
| Prop | Default | Type |
|---|---|---|
hotkeys | ['mod+k'] | string[] | undefinedKeyboard shortcuts that toggle the palette. `mod` resolves to Command on macOS and Control elsewhere. |
selectionMode | 'none' | 'none' | 'single' | 'multiple' | undefinedHow 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 | undefinedWhether the command palette is loading results. Shows the `Spinner` and `Loading` parts, and is reflected on the panel and indicator parts. |
hotkeyOptions | CommandHotkeyOptions | undefinedOptions for the keyboard shortcuts. | |
closeOnSelect | boolean | undefinedWhether selecting an item closes the palette. Defaults to `true` in action mode and `false` in single and multiple selection modes. | |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | booleanWhether to remove the component's style. |
RootProvider
Label
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Input
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Control
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Indicator
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
List
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Loading
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Item
| Prop | Default | Type |
|---|---|---|
onSelect | ((detailsRuns when the item is selected, before the Root `onSelect` and resulting `onOpenChange` callbacks. | |
closeOnSelect | boolean | undefinedOverrides `Root` close behavior for this item. | |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemText
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemGroup
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemGroupLabel
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemCommand
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Empty
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Separator
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Footer
| Prop | Default | Type |
|---|---|---|
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |