Command
A searchable list of commands that ranks as you type, inline on a page or as a palette opened with a shortcut.
Usage
import {
CommandDialog,
CommandDialogContent,
CommandEmpty,
CommandInput,
CommandItem,
CommandList,
} from 'nyctoui/command'
<CommandDialog shortcut="mod+k">
<CommandDialogContent>
<CommandInput placeholder="Type a command…" />
<CommandEmpty>No commands found.</CommandEmpty>
<CommandList>
<CommandItem onSelect={createIssue}>New issue</CommandItem>
<CommandItem onSelect={openSettings}>Settings</CommandItem>
</CommandList>
</CommandDialogContent>
</CommandDialog>An item’s text is what the search matches, so most items need no props beyond onSelect, which runs on a click and on Enter. Choosing an item closes the palette. mod in a shortcut is Command on Apple platforms and Ctrl elsewhere.
Good for
- A ⌘ K palette in a project tool for jumping to any issue, page or setting.
- Quick actions in a code editor or a design tool, searchable by name.
- A “Move to” or “Assign to” picker inside a menu, filtering a long list of people or folders.
- An admin console’s search across users, teams and billing accounts.
Anatomy
<CommandDialog>
<CommandDialogTrigger />
<CommandDialogContent> {/* or <Command> on its own */}
<CommandInput />
<CommandLoading />
<CommandEmpty />
<CommandList>
<CommandPage> {/* optional, for nested pages */}
<CommandGroup>
<CommandLabel />
<CommandItem>
<CommandShortcut>
<Kbd />
</CommandShortcut>
</CommandItem>
</CommandGroup>
<CommandSeparator />
</CommandPage>
</CommandList>
<CommandFooter>
<CommandHint />
</CommandFooter>
</CommandDialogContent>
</CommandDialog>Searching
Every word of the search has to match, in any order, and the best matches rise to the top. An exact match ranks first, then a prefix, then the start of a later word, then a match inside a word, then the letters in order with gaps, so gtst still finds “Go to settings”. Shorter values win ties, accents are ignored, and keywords rank just below the item’s own text. Keywords match only as unbroken runs, never as scattered letters, so a long description cannot match everything. Items written out in a list keep their order and only hide; items passed as data rank by score. Groups with no match hide, and so do separators while searching.
Pass filter to match another way, such as ui-sdk’s 'fuzzy', or wrap defaultFilter to boost recent items.
Keyboard
Focus stays in the input, so typing and moving never fight. UpDown move the highlight and wrap at the ends. Enter runs the item, or follows it when it is a link. Esc clears the search, then goes back a page; Backspace in an empty search goes back a page too.
Examples
Inline
Command on its own draws the same surface as a popup, for a picker set into a page or a popover. Disabled items show but are skipped.
Links
Pass render={<Link to="…" />} for an item that navigates. Enter follows the link the way a click does. These items find pages by their descriptions too, through keywords.
Nested pages
Put each step in a CommandPage and give the item that opens it a page, such as picking a person after “Assign to…”. The search clears as a page opens and comes back when you go back. In a dialog, an item with a page keeps the palette open.
Server search
With filter={false}, the palette shows items as you give them. Pass status="loading" while the request is out, and a CommandLoading shows its spinner.
Thousands of items
For data rather than a written-out list, such as files, people or records, pass them to Command as items and use CommandVirtualList in place of CommandList, with a function that renders one row. The search covers every item and only the rows in view render, so 50,000 items cost about as much to show as 50. The arrow keys, Enter and the highlight work across the whole list, scrolled into view or not. The list is flat.
Reach for it past a few hundred items, or whenever the count has no limit. Below that, CommandList is the better fit: it takes groups, separators and pages.
Best practices
- Show useful items before anything is typed, such as recent pages or common actions.
- Group results under labels like Pages, Actions and People so they scan quickly.
- Add
keywordsto items people might search for by another name, such as “billing” for Payment methods. - Show each command’s shortcut with
CommandShortcut, so people learn to skip the palette next time.
- Use a command palette for a short fixed list. A dropdown or a select is easier to find.
- Make it the only way to reach an action. People who don’t know the shortcut need a visible route.
- Put buttons or links inside an item. Each row is one option.
API reference
Command
The palette on its own: input, list and keyboard handling on one surface. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
onActionRuns when any item runs, with its value. Return a promise to keep the item pending until it settles. | (value, details) => unknown | — |
query / defaultQuery / onQueryChangeControl the text in the input, for a server search. | string · (query, details) => void | '' |
value / defaultValue / onValueChangeThe value of the highlighted item. Without it, the first match is highlighted. | string | null · (value, details) => void | — |
pages / defaultPages / onPagesChangeThe stack of open CommandPages, first to current. | string[] · (pages, details) => void | — |
itemsItems as data, for a CommandVirtualList. | Array<{ value, label, keywords?, disabled? }> | — |
filterReturn 0 or false to hide an item. false shows the items as given, for results a server filtered. | (item, query) => number | boolean | 'fuzzy' | 'contains' | false | word filter |
statusWhether results are loading, for CommandLoading and the list’s aria-busy. | 'idle' | 'loading' | 'error' | 'idle' |
loopArrow keys wrap from the last item to the first. | boolean | true |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandDialog
Wraps Dialog and accepts its props. Renders no element.
| Prop | Type | Default |
|---|---|---|
shortcutOpens and closes the palette from anywhere, e.g. mod+k or mod+shift+p. | string | — |
controllerLets triggers outside the dialog open it, such as a sidebar button and a mobile top-bar button. Pass the same controller to each CommandDialogTrigger. | ReturnType<typeof createCommandDialogController> | — |
open / defaultOpen / onOpenChangeControl whether the palette is open. | boolean · (open, details) => void | — |
CommandDialogTrigger
A DialogTrigger. Renders a plain <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the <button>, e.g. render={<Button />}. | ReactElement | function | — |
controllerThe dialog to open, for a trigger placed outside its CommandDialog. | ReturnType<typeof createCommandDialogController> | — |
CommandDialogContent
The palette window: a Command in a dialog popup, 560px wide. Takes the Command props. The search starts empty each time it opens.
| Prop | Type | Default |
|---|---|---|
aria-labelNames the dialog for screen readers. | string | 'Command menu' |
onActionRuns when any item runs, with its value. Return a promise to keep the item pending until it settles. | (value, details) => unknown | — |
query / defaultQuery / onQueryChangeControl the text in the input, for a server search. | string · (query, details) => void | '' |
value / defaultValue / onValueChangeThe value of the highlighted item. Without it, the first match is highlighted. | string | null · (value, details) => void | — |
pages / defaultPages / onPagesChangeThe stack of open CommandPages, first to current. | string[] · (pages, details) => void | — |
itemsItems as data, for a CommandVirtualList. | Array<{ value, label, keywords?, disabled? }> | — |
filterReturn 0 or false to hide an item. false shows the items as given, for results a server filtered. | (item, query) => number | boolean | 'fuzzy' | 'contains' | false | word filter |
statusWhether results are loading, for CommandLoading and the list’s aria-busy. | 'idle' | 'loading' | 'error' | 'idle' |
loopArrow keys wrap from the last item to the first. | boolean | true |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandInput
The search field. Renders an <input> in a header row; other props go to the <input>.
| Prop | Type | Default |
|---|---|---|
placeholderHint shown while empty; also the label unless aria-label is set. | string | 'Search…' |
iconShown before the field. null drops it. | ReactNode | search icon |
childrenShown after the field, such as an Esc keycap. | ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandList
The scrolling listbox. Labelled “Suggestions” unless you pass aria-label.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandVirtualList
A CommandList for the Command’s items, rendering only the rows in view with TanStack Virtual. Renders the same listbox; other props go to it. Flat: no groups or separators.
| Prop | Type | Default |
|---|---|---|
childrenRenders one row of the matching items, usually a CommandItem, which takes its value and label from the item. | (item, index) => ReactNode | — |
estimateSizeRow height before a row is measured. | number | 32 |
overscanRows rendered past each edge of the view. | number | 8 |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandItem
One command. A <div> by default.
| Prop | Type | Default |
|---|---|---|
onSelectRuns when the item is clicked or chosen with Enter. preventDefault() keeps a palette open; a promise keeps the item pending. | (event) => unknown | — |
valueWhat onAction receives and the highlight refers to. | string | — |
labelWhat the search matches. Shortcuts are left out. | string | the item’s text |
pageThe CommandPage choosing this item opens. | string | — |
keywordsOther words that find the item. | string[] | — |
iconIcon before the label, tinted quieter than the label. | ReactNode | — |
disabledShown, but skipped by the arrow keys and never chosen. | boolean | false |
closeOnSelectIn a CommandDialogContent, close the palette once chosen. | boolean | true |
renderReplaces the <div>, e.g. render={<Link to="/" />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandGroup
Items under a CommandLabel. Hidden while none of them match.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandEmpty
Shown when nothing matches, and announced. Put it beside the CommandList.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandLoading
A row with a spinner, shown while Command’s status is loading. Put it beside the CommandList.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandPage
Items shown while this page is on top. Renders no element.
| Prop | Type | Default |
|---|---|---|
valueThe name an item’s page opens. Leave it out for the first page. | string | — |
CommandStatus
A live region for results. Pass a function of { count, loading } to show the count.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CommandLabel, CommandSeparator, CommandShortcut, CommandFooter, CommandHint
A group heading, a hairline that hides while searching, a hint aligned to the end of an item, a row of key hints under the list (hidden on touch screens), and one hint in it. Each takes sx.