shadcn.io is an independent project built on shadcn/ui
Components
Free
Command for UI SDK
A command palette for React on UI SDK: a search field that narrows a list of actions as you type, with groups, shortcut hints and a dialog.
On this page9 sections
Command puts a search field above a list of actions. People type a few letters, the list narrows to the rows that match, and Enter runs the highlighted one. Open it in a dialog on Cmd+K as an app-wide command palette, or keep it inline at the top of a sidebar or a menu.
When the pick is a value a form keeps, such as a country or an assignee, use a Combobox. A short menu with nothing to search reads better as a Dropdown Menu.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
Focus stays in the search field while the arrow keys move a highlight through the list, and the field points at the highlighted row with aria-activedescendant, so a screen reader reads each row as it's reached. The field has the combobox role, and the list is a listbox of options that takes its name from the field. Once typing pauses, a screen reader hears how many rows match. CommandInput takes its accessible name from its placeholder, or "Search" without one. CommandDialog gives the dialog a title only screen readers hear, "Command Palette" unless you set title. The icons are decoration, so keep them aria-hidden as the demos do.
KeyAction
Arrow Down
Highlights the next item, skipping disabled ones, and stays on the last one unless you add loop to Command.
Arrow Up
Highlights the previous item, skipping disabled ones, and stays on the first one unless you add loop to Command.
⌘Command+Arrow Down
Highlights the last item. Cmd + ArrowDown on a Mac.
⌘Command+Arrow Up
Highlights the first item. Cmd + ArrowUp on a Mac.
Page Down
Moves the highlight ten items down.
Page Up
Moves the highlight ten items up.
Ctrl+N
On a Mac, highlights the next item, as ArrowDown does.
Ctrl+P
On a Mac, highlights the previous item, as ArrowUp does.
Enter
Runs the highlighted item's onSelect.
EscEscape
Clears the search field. When it's already empty, closes CommandDialog.
Demos
Basic
An Open Menu button opens a dialog with three suggestions, Calendar, Search Emoji and Calculator, under a field that narrows them as you type. A short message takes the rows' place when nothing matches. Start here for a palette over a few actions, or leave out the dialog to keep the list on the page, like the menu at the top.
Shortcuts
Profile, Billing and Settings, each with an icon and a CommandShortcut at the end of the row showing ⌘P, ⌘B or ⌘S. The hint is text, and nothing in this demo listens for those keys. Show hints when the actions already have shortcuts elsewhere in your app, so the palette teaches them, and bind the keys yourself.
Groups
Suggestions and Settings sit under their own headings with a separator between them, an icon on every row and shortcut hints on the settings rows. Each section is a CommandGroup with a heading, written out by hand. Group the commands once they fall into more than one kind, such as places to go and things to do.
Scrollable
Twenty-three commands in five groups, from Navigation and Actions down to Account and Tools, more than the dialog can show at once. The list stops at 18rem and scrolls with its scrollbar hidden, while the search field stays at the top. At this length a few typed letters beat scrolling, and the headings help the people who scroll anyway.
Palette
Cmd+K on a Mac or Ctrl+K elsewhere toggles the palette from anywhere on the page, and a Search button opens it for pointer and touch users. Pages and Actions are grouped, and a footer names the keys for moving, picking and closing. Picking a row writes its name under the button and closes the dialog, which is the wiring an app-wide palette needs.
RTL
The inline menu from the top of the page in Arabic, with dir set to rtl on both Command and CommandInput. Headings, labels, the placeholder and the empty message are translated, the calculator row stays disabled, and the shortcut hints move to the inline end of each row, on the left. Use it to check the filter and the separators in a right-to-left layout.
FAQ
How do I open the Command palette with Cmd+K in React?
Listen for the shortcut on document yourself, because the component binds no keys of its own. In the keydown handler, check that the key is k and that metaKey or ctrlKey is down, call preventDefault() so the browser doesn't act on the keys too, and flip the open state you pass to CommandDialog.
Add the listener when the component mounts and remove it when it unmounts. The Palette demo does exactly this, and keeps a Search button on the page for phones, where there is no shortcut to press.
How do I run an action when a Command item is selected?
Give each CommandItem an onSelect handler. It runs on a click, or on Enter while the row is highlighted, and receives the item's value: its value prop if you set one, otherwise its text, which the Palette demo reads to show which row was picked. A Promise it returns keeps the row pending, with data-pending, until it settles.
Navigating works the same way: go to the route in that handler, then close the dialog so the page underneath is what people see.
Should I use a Command or a Combobox?
Use a Command when picking a row does something: it opens a page, runs an action or flips a setting, the way a command palette or a searchable action menu works. The search is a way to reach the action, and nothing needs to remember which row was chosen.
Use a Combobox when the pick is a value that stays in a form field afterwards, like a country or an assignee, and goes out with the form when it's submitted.
How do I change how the Command filters and ranks items?
UI SDK scores every item against the search with a fuzzy filter, which keeps the rows that hold the typed letters in order, ignoring case and accents, and hides the rest. As in cmdk, it matches each item's value, or its text when there is none, plus the words in its keywords array, so a Settings row with the keyword preferences turns up for that word as well. The rows keep the order you wrote them in, and the first match is highlighted.
Set filter on Command to contains or startsWith to match the value and keywords that way instead, or pass a function of the item's value, the search and its keywords that returns a score, where 0 hides the item.
How do I load Command results from an API as the user types?
Set shouldFilter to false on Command, so it stops filtering and shows every item you render. Hold the search in state through value and onValueChange on CommandInput, fetch with it after a short debounce, and map the response to CommandItem rows.
While a request is in flight, pass status="loading" to Command, which marks the list busy for screen readers, and have CommandEmpty say so, or it reports no results before any have arrived.
Why doesn't pressing a Command shortcut do anything?
CommandShortcut is a label and nothing more. It prints the keys at the end of the row, but no code listens for them, in the palette or anywhere else.
Bind each one yourself with a keydown listener on document, the way the Palette demo binds Cmd+K, and have it call the same function the row runs when it's picked. Keeping both in one place stops a hint from naming keys that do something else. Or give the CommandItem a shortcut and UI SDK runs the row on those keys, with an empty CommandShortcut showing them, though only while the list is on the page.
How do I show a message when no Command results match?
Put a CommandEmpty inside CommandList with your message. UI SDK keeps it in the page as a live region and fills it only while the search leaves no item to show, so a screen reader hears the message when it appears, once, in place of UI SDK's own "No results". It has no text of its own, so write something useful, such as a hint to try another word or a link to the full search.
How do I clear the Command search when the palette closes?
It clears itself. Closing CommandDialog unmounts everything inside it, so the next time it opens the field is empty and the whole list is back.
If you hold the search text in your own state, for example to fetch results, reset that state in onOpenChange when the dialog closes, or the old search comes back with it.
API Reference
Command
<div>data-slot="command"
PropType
pages?string[]
The stack of open pages, from the first page opened to the current one. Pass it to control it.
Runs when the highlight moves: arrow keys, the pointer, typing, a page change or a controller. details.index says where the command is, for virtualized lists.
What screen readers hear after typing pauses, when no CommandStatus is rendered. Return null for nothing.
Type
(change: ListAnnouncement) → string | null
Default
"3 results", "No results" or "Loading"
Parameters
change – ListAnnouncement
Returns
string | null
autoHighlight?boolean
Whether the first command is highlighted while nothing else is, and the best match as the user types, so Enter runs it.
Default
true
controller?CommandController
Reads and drives the list from anywhere. Create one with createCommandController().
debug?boolean
Logs every state change with its reason to the console, in development builds.
emptyItems?CommandData
Commands shown instead of items on the first page while the search is empty, like recent or suggested ones. Groups work, like { type: "group", label: "Suggestions", items }. Needs items.
How typing narrows the list: "fuzzy", "contains", "startsWith", or
cmdk's function of an item's value, the search and its keywords, which
returns a score (0 hides the item).
Runs when a command runs, with its value: Enter, a click, or its shortcut. Return a Promise to mark the command pending until it settles: it gets data-pending, aria-busy and aria-disabled, and running it again waits. A rejection puts the reason in its error state until the next run.
Runs when the highlight or scrolling reaches the last command, so the next page can load, like createAsyncOptions's loadMore. Not again for the same commands, unless the last load failed.
Returns
void
pendingAnnouncement?string
What screen readers hear once a command has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
Default
"Loading"
shouldFilter?boolean
Whether typing filters the items. Turn it off for results you filter
yourself, like a server's.
Default
true
status?AsyncOptionsStatus
Whether results are loading or failed, for the empty and status parts. Sets aria-busy on the list while loading.
Whether pressing outside the dialog, like on its backdrop, closes it. The press counts once it's released outside too, so dragging a text selection out of a field doesn't close it.
Default
true, or false when `modal` is false
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
Logs every state change with its reason to the console, in development builds.
description?string
The dialog's description, for screen readers.
Default
"Search for a command to run..."
dismissible?boolean
Whether Escape, an outside press and Android's back gesture can close the dialog. Close parts, triggers and the controller always can. Pass false while a form has unsaved changes, and ask in onDismissAttempt.
Default
true
modal?"trap-focus" | boolean
true: the rest of the page is inert and doesn't scroll, focus is trapped, and an outside press only closes the dialog. "trap-focus": traps focus, and leaves the page scrollable and clickable. false: a dialog beside the page, which Tab can leave and outside presses don't close.
Runs when a dismissal was refused, because dismissible is false, an outside press doesn't close it, or a Close part's action is running. Ask "Discard changes?" here.
Type
(details: DialogDismissAttemptDetails) → void
Parameters
details – DialogDismissAttemptDetails
Returns
void
onOpenChangeComplete?(open: boolean) → void
Runs once the dialog has finished opening or closing, after any animation.
Runs when the payload the content shows changes: a trigger with another payload opens the dialog, or takes a non-modal dialog over while it's open, which fires no onOpenChange, and so does a controller's open({ payload }) on the open dialog. details.cancel() keeps the previous payload.
What screen readers hear once a Close part's action has been running for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
Elements that stay usable while the dialog makes the page inert, like a chat launcher or a password manager's button. Elements, CSS selectors, or a function that returns elements, read when the dialog opens. Presses on them never close it.
Type
(string | Element)[] | (() → Element[])
Parameters
string | Element)[] | ((
Returns
Element[])
presentation?PresentationSetting<"dialog">
How the dialog presents. "sheet" slides it up from the bottom of the screen, where it can be swiped away, and "auto" does that on small touch screens. Decided each time it opens; the server renders "dialog" for "auto".
Type
PresentationSetting<"dialog">
Default
"dialog"
showCloseButton?boolean
Shows the close button in the corner.
Default
false
title?string
The dialog's title, for screen readers.
Default
"Command Palette"
className?string
Merged with the part's classes.
CommandInput
<div>data-slot="command-input-wrapper"
PropType
value?string
The search, to control it from the field (cmdk's API).