Skip to content

React Command Palette (⌘K)

A glass React command palette that opens with ⌘K or Ctrl+K, with fuzzy search, recent picks first and nested pages.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/command.json

Workspace

Jump anywhere without the mouse.

  • CommandPalette
  • useCommandShortcut
  • highlightMatch
  • defaultCommandPaletteLabels
  • +2 more

⌘K anywhere

Try "dsh" for Dashboard, "prefs" (a hidden keyword) for Settings, or "dark" to reach a theme inside the "Change theme…" page straight from the top. Enter runs a command; Escape or Backspace in an empty field steps back out of a page. Your last picks show under Recent next time.

Files to copy

9 folders · 18 files · 4 packages
From theme/src/
  • components/command/
  • components/dialog/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/spinner/
  • hooks/{use-controllable.ts, use-latest.ts, use-media-query.ts, use-preferences.ts, use-virtual-window.ts}
  • motion/{presets.ts, tokens.ts}
  • lib/{cn.ts, form.ts, group-segments.ts, ime.ts, modal-flag.ts, modal-tab-trap.ts, page-size.ts, popup-motion.ts, state-light.tsx, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/command.jsonRegistry item: command. The styles need the Tailwind v4 glass UI setup.
The examples also use
  • components/button/
  • hooks/use-magnetic.ts
  • motion/gate.ts
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Thousands of commands

Long lists are windowed on their own (virtualized defaults to auto, above 200 results), so the palette stays fast with a big command list. This one lists 3,000 commands and sets maxResults high enough to search them all: arrow down, press Page Down, or type a number.

Your own results

filter={false} shows the items you pass, as they are: search a server in onQueryChange and set loading meanwhile. Matches are still highlighted. This one has no global shortcut (shortcut={false}).

API

CommandPalette

PropTypeDefaultDescription
items (required)readonly CommandItem[]NoneThe commands. Order is kept when the search field is empty.
openbooleanNoneWhether the palette is open (controlled).
defaultOpenbooleanfalseWhether it starts open (uncontrolled).
onOpenChange((open: boolean) => void)NoneCalled with the new state when the palette opens or closes.
modalbooleantrueWhether the rest of the page is blocked while it is open (Base UI dialog semantics). false leaves the page interactive, drops the scrim and does not trap focus: for previews and demos that open on their own.
initialFocusboolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | HTMLElement | null | void)the search fieldWhere focus lands when it opens: an element, a ref, or a function (Base UI's initialFocus). false moves focus nowhere, so a palette that opens by itself never steals it.
containerHTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | nullthe end of document.bodyWhere the palette is portalled. Pass an element to keep it inside a region instead of the end of the body.
triggerReactElement<unknown, string | JSXElementConstructor<any>>NoneRendered as the button that opens the palette (focus returns to it on close).
shortcutstring | false"mod+k"Global shortcut that toggles the palette. false to register none.
onSelect((item: CommandItem) => void)NoneCalled after any command runs (besides its own onSelect).
querystringNoneSets the search text when controlled.
defaultQuerystring""The search text on mount (uncontrolled).
onQueryChange((query: string) => void)NoneCalled with the search text as it changes (fetch your own results here).
filterfalse | CommandFilterdefaultFilterHow results are matched. Default: fuzzy match on label, description and keywords. Pass a function to match your way, or false to show items as given (e.g. results you fetched yourself in onQueryChange); matches are still highlighted.
deepSearchbooleantrueFrom the top level, also search commands inside nested pages.
maxResultsnumber100Most results listed while searching (the best ones). Keeps typing quick with thousands of commands.
virtualizedboolean | "auto""auto"Windows the result list so only the rows near the viewport are in the DOM: true always, false never, "auto" only above 200 results (a long list with no query, or a large maxResults). The active command, the keyboard keys (aria-activedescendant stays on the search field), aria-setsize / aria-posinset and the scroll height work across every result. Windowed rows are a fixed height (a description is cut to fit) and the group headings are visual only.
recentreadonly string[]NoneLists the ids of recent commands, newest first, when controlled.
onRecentChange((ids: string[]) => void)NoneCalled with the new list of recent ids after a command runs.
recentStorageKeystringNoneRemember recents in localStorage under this key (uncontrolled).
maxRecentnumber5How many recents to keep. 0 turns the Recent group off.
loadingbooleanfalseShows a spinner in the field (and "Searching…" while there are no results yet).
emptyStateReactNode | ((query: string) => ReactNode)NoneReplaces the empty state. Gets the query.
footerReactNodeNoneExtra content at the end of the key-hint footer, or false to hide the footer. Hidden on touch screens.
labelsPartial<CommandPaletteLabels>defaultCommandPaletteLabelsEvery other string, for translation: labels={{ title: "…" }}; omitted keys keep the English of defaultCommandPaletteLabels. All 17 keys of CommandPaletteLabels are listed in the key table under the props table.
shortcutLabelsPartial<KbdLabels>defaultKbdLabelsKey names in the shortcut hints (i18n). Every other string, for translation: shortcutLabels={{ mac: "…" }}; omitted keys keep the English of defaultKbdLabels. All 4 keys of KbdLabels are listed in the key table under the props table.
classNamestringNoneAdds extra classes to the dialog panel.
CommandPaletteLabels keys and English defaults (17)
Pass any key in labels; the rest keep the defaults of defaultCommandPaletteLabels.
KeyEnglish default
titleCommand palette
inputLabelSearch commands
pageInputLabel(page, inputLabel) => `${inputLabel}: ${page}`
placeholderSearch or jump to…
recentRecent
noResults(query) => `No results for “${query}”`
noResultsHintTry a shorter word or check the spelling.
emptyListNo commands yet.
loadingSearching…
results(count) => (count === 1 ? "1 result" : `${count} results`)
backBack
backButton(page, back) => `${back}: ${page}`
closeClose
navigateNavigate
selectSelect
opensPage(label) => `${label}, opens more options`
instructionsType to search. Use the up and down arrow keys to move through results, Enter to run one, Escape to go back or close.
KbdLabels keys and English defaults (4)
Pass any key in shortcutLabels; the rest keep the defaults of defaultKbdLabels.
KeyEnglish default
mac{ mod: "⌘", meta: "⌘", ctrl: "⌃", alt: "⌥", shift: "⇧", enter: "↵", escape: "Esc", backspace: "⌫", delete: ...
other{ mod: "Ctrl", meta: "Win", ctrl: "Ctrl", alt: "Alt", shift: "Shift", enter: "Enter", escape: "Esc", backsp...
macSpoken{ mod: "Command", meta: "Command", ctrl: "Control", alt: "Option", shift: "Shift", enter: "Return", escape:...
otherSpoken{ mod: "Control", meta: "Windows", ctrl: "Control", alt: "Alt", shift: "Shift", enter: "Enter", escape: "Es...

CommandItem

PropTypeDefaultDescription
idstringNoneUnique across the tree (recents and keys use it).
label / descriptionstringNoneWhat is shown and searched.
keywordsstring[]NoneExtra words that find it but aren't shown.
groupstringNoneHeading it is listed under.
icon / hint / shortcutReactNode / ReactNode / stringNoneIcon tile, trailing text or badge (shown on every screen) and a shortcut hint (display only; hidden on phones).
onSelect(item) => voidNoneRuns when chosen. The palette then closes unless keepOpen.
items / placeholderCommandItem[] / stringNoneTurns the command into a nested page, with its own placeholder.
disabled / keepOpen / recordRecentbooleanfalse / false / trueSkip it, stay open after running, list it under Recent.

useCommandShortcut(callback, options)

PropTypeDefaultDescription
keysstring | string[]"mod+k"Shortcut such as "mod+k", "/" or "shift+mod+p".
enabledbooleantrueKeeps listening only while true.
allowInInputsbooleantrueFire while typing in a field. Turn off for bare keys like "/".
preventDefaultbooleantrueStop the browser's own action for the shortcut.

Keyboard

KeysAction
Cmd/Ctrl + KOpen or close the palette from anywhere.
Arrow Down/Arrow Up (or Ctrl + N/P)Move through the results.
Page Up/Page DownJump through the results.
EnterRun the highlighted command.
EscapeGo back one page, then close.
Backspace in an empty fieldGo back one page.

Accessibility

  • The field is an ARIA combobox that owns a listbox (aria-activedescendant), so focus never leaves it.
  • The result count is announced politely and usage instructions are read on open; the dialog is named by labels.title.
  • Shortcut hints expose aria-keyshortcuts. Focus returns to the trigger on close. Every string is translatable.

All exports on this page

  • CommandPalette — Command palette (Cmd/Ctrl+K): a glass dialog with a fuzzy search field over grouped commands.
  • defaultCommandPaletteLabels — The English strings of CommandPalette.
  • highlightMatch — Wrap the matched characters in <mark>.
  • useCommandShortcut — Global keyboard shortcut, ⌘K / Ctrl+K by default.

When to use the command palette

  • Use it as a Cmd+K menu, a global Cmd+K or Ctrl+K search in a dashboard, where people jump to pages and run actions without leaving the keyboard, with fuzzy matching on label, description and hidden keywords. As a cmdk alternative it is a styled command palette with a search field, groups and keyboard shortcuts built in.
  • Use it for an app with many settings, where commands with nested items open a page such as Change theme, and deepSearch finds those inner commands straight from the top level. Use it for Spotlight search in React: press the shortcut, type, and pick a command.
  • Use it for async results by passing filter={false} and fetching in onQueryChange, so you can show server search results while the palette still highlights the matched characters.
  • Skip it when the user simply picks a value for a form field; Combobox or Select belong inside the form, and a Dropdown menu fits a short fixed list of actions behind one button.
  • Pair it with KbdShortcut to display the key beside the trigger button, the useCommandShortcut hook for your own bindings, and Button as the trigger element that opens it.

SheenUI Command Palette or cmdk?

cmdk is a popular unstyled command menu for React that you compose and style yourself. SheenUI's CommandPalette is styled glass and ships the behaviors most apps then build: groups, nested pages, fuzzy search with highlighted matches, recent commands and a Cmd or Ctrl+K shortcut. Pick cmdk when you want full control of the markup and styling; pick this one when you want those behaviors ready to use and the source to copy.

Frequently asked questions

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .