Command palette
Also called command menu or Cmd+K menu
A search box that opens with a shortcut like ⌘K and lets you find and run any command or page by typing a few letters.
import { useEffect, useState } from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { Dialog } from '@base-ui/react/dialog';
type Command = { label: string; shortcut?: string; run: () => void };
const POPUP =
'fixed top-24 left-1/2 w-140 max-w-[calc(100vw-2rem)] -translate-x-1/2 overflow-hidden rounded-xl border bg-background shadow-2xl transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-starting-style:scale-[0.98] data-starting-style:opacity-0';
export function CommandPalette({ commands }: { commands: Command[] }) {
const [open, setOpen] = useState(false);
useEffect(() => {
function onKeyDown(event: KeyboardEvent) {
if (event.key !== 'k' || !(event.metaKey || event.ctrlKey)) return;
event.preventDefault();
setOpen((value) => !value);
}
document.addEventListener('keydown', onKeyDown);
return () => document.removeEventListener('keydown', onKeyDown);
}, []);
return (
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger className='flex gap-6 rounded-lg border px-3 py-1.5 text-sm text-muted-foreground'>
Search <kbd>⌘K</kbd>
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Backdrop className='fixed inset-0 bg-black/40 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0' />
<Dialog.Popup aria-label='Command palette' className={POPUP}>
{/* Inline and always open, since the dialog is the popup. */}
<Autocomplete.Root open inline items={commands} autoHighlight>
<Autocomplete.Input
aria-label='Search commands'
placeholder='Type a command or search…'
className='h-12 w-full border-b bg-transparent px-4 text-sm outline-none'
/>
<Autocomplete.Empty className='p-4 text-sm text-muted-foreground empty:p-0'>
No results.
</Autocomplete.Empty>
<Autocomplete.List className='max-h-80 overflow-y-auto p-2 data-empty:p-0'>
{(command: Command) => (
<Autocomplete.Item
key={command.label}
value={command}
onClick={() => {
command.run();
setOpen(false);
}}
className='flex cursor-default justify-between rounded-lg px-3 py-2 text-sm outline-none select-none data-highlighted:bg-muted'
>
{command.label}
<kbd className='text-xs opacity-60'>{command.shortcut}</kbd>
</Autocomplete.Item>
)}
</Autocomplete.List>
</Autocomplete.Root>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>
);
}Add a ⌘K / Ctrl+K command palette: a 560px-wide panel near the top of the screen over a dimmed backdrop, with a search field and commands grouped under Navigation, Projects, and Settings that filter as you type with fuzzy matching. Support Up, Down, Enter, and Escape, show each command's shortcut on the right, open it with a 150ms fade and scale from 0.98, and add a 'Search ⌘K' button to the header for mouse users.
Paste into Layerwise or any AI coding tool.
What is Command palette?
A command palette is a keyboard-first launcher. Press ⌘K (Ctrl+K on Windows), a search field opens over the app, and the list of commands narrows as you type. Arrow keys move the highlight and Enter runs the command, so people can do almost anything without reaching for the mouse.
Add one when your app has more actions and pages than a toolbar can hold, as in editors, dashboards, and developer tools. It works alongside menus and buttons, which newer users still rely on, and showing each command's shortcut in the list helps people learn it.
Match loosely so 'new p' finds New project, rank recent commands first, and update the list on every keystroke. Pick a shortcut that does not clash with ones your app already uses, and add a visible Search button that shows the shortcut for people who don't know it.
Often confused with
- Combobox
- A combobox picks a value for one form field. A command palette searches and runs actions across the whole app.
Related terms
All termsKeyboard shortcut
A key combination that runs an action directly, like ⌘S to save or ⌘K to open search, so frequent users can skip the mouse.
Combobox
A text field with a dropdown list that filters as you type, so people can pick one option from a long list quickly.
Modal
A window that opens over the page, dims everything behind it, and blocks the rest of the interface until you finish or dismiss it.
Dropdown menu
A list of actions that drops down from a button when you click it, then closes as soon as you pick one.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.