Command menu
command-menuA command palette opened with ⌘K or Ctrl K, with a search field, grouped items and matched letters highlighted as you type. Ranking prefers whole-word matches, and cmdk's list runs inside a Base UI dialog for the focus trap.
"use client";
import { CreditCard, FileText, LayoutGrid, LifeBuoy, Search, Settings, User } from "lucide-react";
import { useState, useSyncExternalStore } from "react";
import { Button } from "@/components/forme/button/button";
import {
CommandMenu,
CommandMenuEmpty,
CommandMenuGroup,
CommandMenuInput,
CommandMenuItem,
CommandMenuList,
CommandMenuSeparator,
} from "@/components/forme/command-menu/command-menu";
import { KbdGroup } from "@/components/forme/kbd/kbd";
/** Nothing to subscribe to: the platform cannot change without a reload. */
const subscribeNothing = () => () => {};
const applePlatform = () => /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);
const alwaysFalse = () => false;
export function CommandMenuDemo() {
const [open, setOpen] = useState(false);
const [last, setLast] = useState<string | null>(null);
const apple = useSyncExternalStore(subscribeNothing, applePlatform, alwaysFalse);
// cmdk hands the item's `value` to `onSelect`, so one handler names the
// command and closes the palette, the way a native command palette does.
function run(value: string) {
setLast(value);
setOpen(false);
}
return (
<div className="flex flex-col gap-8">
<div className="flex flex-wrap items-center gap-3">
{/* Labelled at every width: this is a page, not a crowded header, so
there is no reason to collapse it to a bare icon. The shortcut and
its hint only show where there is a keyboard to press them on. */}
<Button
variant="outline"
leadingIcon={Search}
aria-label="Search commands"
onClick={() => setOpen(true)}
className="group w-full justify-start pr-1.25 pl-3 sm:w-60 [&>span]:flex-1"
>
<span className="flex-1 truncate text-left">Search</span>
<KbdGroup
variant="pill"
keys={[apple ? "⌘" : "Ctrl", "K"]}
className="pointer-coarse:hidden"
/>
</Button>
{last ? (
<p className="text-body-regular text-muted-foreground">
Ran <span className="text-foreground">{last}</span>
</p>
) : (
<p className="text-body-regular text-muted-foreground pointer-coarse:hidden">
Press ⌘K, or Ctrl K on Windows and Linux.
</p>
)}
</div>
<CommandMenu label="Command menu" open={open} onOpenChange={setOpen}>
<CommandMenuInput placeholder="Type a command or search..." />
<CommandMenuList>
<CommandMenuEmpty>No commands found.</CommandMenuEmpty>
<CommandMenuGroup heading="Go to">
<CommandMenuItem
value="overview"
keywords={["home", "dashboard", "start"]}
description="Everything at a glance"
onSelect={run}
>
<LayoutGrid />
Overview
</CommandMenuItem>
<CommandMenuItem
value="deployments"
keywords={["releases", "production"]}
description="Releases and environments"
onSelect={run}
>
<FileText />
Deployments
</CommandMenuItem>
<CommandMenuItem
value="settings"
keywords={["preferences", "config"]}
description="Preferences and keyboard"
shortcut={["⌘", ","]}
onSelect={run}
>
<Settings />
Settings
</CommandMenuItem>
</CommandMenuGroup>
<CommandMenuSeparator />
<CommandMenuGroup heading="Account">
<CommandMenuItem
value="profile"
keywords={["name", "email"]}
description="Name, email and avatar"
onSelect={run}
>
<User />
Profile
</CommandMenuItem>
<CommandMenuItem
value="billing"
keywords={["invoice", "plan", "payment"]}
description="Invoices and plan"
shortcut={["⌘", "B"]}
onSelect={run}
>
<CreditCard />
Billing
</CommandMenuItem>
<CommandMenuItem
value="support"
keywords={["help", "contact"]}
description="We reply within a day"
onSelect={run}
>
<LifeBuoy />
Contact support
</CommandMenuItem>
</CommandMenuGroup>
</CommandMenuList>
</CommandMenu>
</div>
);
}Install
Adds the component, its registry dependencies, and the token stylesheet you import once.
Usage
import {
CommandMenu, CommandMenuEmpty, CommandMenuGroup, CommandMenuInput, CommandMenuItem, CommandMenuList,
} from "@/components/forme/command-menu/command-menu";
<CommandMenu open={open} onOpenChange={setOpen}>
<CommandMenuInput placeholder="Search…" />
<CommandMenuList>
<CommandMenuEmpty>Nothing found.</CommandMenuEmpty>
<CommandMenuGroup heading="Projects">
<CommandMenuItem description="Design system" onSelect={() => go("/forme-web")}>
forme-web
</CommandMenuItem>
</CommandMenuGroup>
</CommandMenuList>
</CommandMenu>When to use
Reach for it
- Finding a page or running a command by name, keyword or a few letters in order, with the matching letters highlighted as you type.
Use something else
- A handful of actions on one object; a
Dropdownis closer to hand. - The only way to reach something. Everything in the palette should also be reachable without it.
API reference
Read from the source, so it is the API of the file you install. A prop marked * is required.
CommandMenu
| Prop | Type | Default | Description |
|---|---|---|---|
children* | ReactNode | — | |
label | string | "Command menu" | Names the palette for screen readers. |
open | boolean | — | Pass these when the items need to close the palette on select. Left out, the menu keeps its own state and closes on Esc or a click outside. |
onOpenChange | (open: boolean) => void | — | |
shouldFilter | boolean | true | Leave on to let the palette filter and rank items as you type. Turn off when you filter yourself — a search index with its own scoring — and render only the results, in the order you want them. |
filter | CommandMenuFilter | commandMenuFilter | Scores one item against the query. Return 0 to hide it. Defaults to word-start matching with an ordered-subsequence fallback, where every word of the query has to match. |
CommandMenuItem
| Prop | Type | Default | Description |
|---|---|---|---|
description | string | — | A short note after the label. Searched like a keyword. |
shortcut | readonly string[] | — | Keycaps drawn at the end of the row. Labels only: bind the keys yourself. |
Also takes every prop of ComponentProps<typeof CommandItem>.
The other parts
CommandMenuInputTakesComponentProps<typeof CommandInput>CommandMenuListTakesComponentProps<typeof CommandList>CommandMenuEmptyTakesComponentProps<typeof CommandEmpty>CommandMenuGroupTakesComponentProps<typeof CommandGroup>CommandMenuSeparatorTakesComponentProps<typeof CommandSeparator>
Accessibility
- Focus stays in the input while the arrow keys move the selection and Enter runs it.
- Escape clears the query first and closes on the second press; the result count is announced as it changes.
labelnames the palette and the search field for screen readers.
Motion
- The panel condenses in — fade, 96% scale, a slight blur — over 150ms and leaves on the exit curve; the selection tint glides between rows in 150ms. Reduced motion collapses both to an instant change.
Related


Breadcrumb
Where a page sits in the hierarchy, as a labelled nav with an ordered list and the current page marked. Separators are drawn in CSS, so a screen reader hears the path and not the slashes.
navigation

Dock
A macOS-style row of icons that swell under the pointer on a spring, with their neighbours growing too. Labels show on hover and focus, open apps get a dot, and reduced motion keeps every icon at its resting size.
navigation

Pagination
Page links rendered on the server, with the ends always reachable and a gap only where it hides more than one page. Previous and next hold their place on the first and last page, so nothing shifts as you click through.
navigation