Command Palette
One search for every page, action and record, opened with Cmd+K (Ctrl+K off a Mac) and run by a person or an agent.
labelrecentCommandPalettePreviewdisabledimport { Fragment } from "react"
import { BedDouble, Building2, FileText, Receipt, User, Zap } from "lucide-react"
import { CommandPalette, CommandPaletteBody, CommandPaletteContent, CommandPaletteInput, CommandPaletteList, CommandPalettePreview, CommandPaletteTrigger, type CommandItem } from "@/components/ui/command-palette"
const commands: CommandItem[] = [
{ id: "orders", label: "Orders", group: "Pages", kind: "Page", icon: <FileText />, shortcut: "G O", actions: [{ id: "open", label: "Open" }] },
{ id: "rooms", label: "Rooms", group: "Pages", kind: "Page", icon: <FileText />, shortcut: "G R", actions: [{ id: "open", label: "Open" }] },
{ id: "rates", label: "Rates", group: "Pages", kind: "Page", icon: <FileText />, shortcut: "G A", actions: [{ id: "open", label: "Open" }] },
{ id: "payouts", label: "Payouts", group: "Pages", kind: "Page", icon: <FileText />, actions: [{ id: "open", label: "Open" }] },
{ id: "settings", label: "Settings", group: "Pages", kind: "Page", icon: <FileText />, shortcut: "⌘ ,", actions: [{ id: "open", label: "Open" }] },
{ id: "new-booking", label: "New booking", group: "Actions", kind: "Action", icon: <Zap />, shortcut: "N", actions: [{ id: "run", label: "Run" }] },
{ id: "refund", label: "Refund an order", group: "Actions", kind: "Action", icon: <Zap />, actions: [{ id: "run", label: "Run" }] },
{ id: "block", label: "Block a room", group: "Actions", kind: "Action", icon: <Zap />, actions: [{ id: "run", label: "Run" }] },
{ id: "export", label: "Export orders", group: "Actions", kind: "Action", icon: <Zap />, actions: [{ id: "run", label: "Run" }] },
{ id: "property", label: "Switch property", group: "Actions", kind: "Action", icon: <Zap />, shortcut: "⌘ P", items: [
{ id: "saltmarsh", label: "Saltmarsh Hotel", description: "Whitstable", icon: <Building2 />, actions: [{ id: "open", label: "Open" }] },
{ id: "harbour", label: "Harbour House", description: "Deal", icon: <Building2 />, actions: [{ id: "open", label: "Open" }] },
{ id: "dunes", label: "The Dunes", description: "Camber", icon: <Building2 />, actions: [{ id: "open", label: "Open" }] },
] },
{ id: "order-4821", label: "Order #4821", description: "Ada Okafor", group: "Orders", kind: "Order", icon: <Receipt />, meta: "£204.00", actions: [{ id: "open", label: "Open order" }, { id: "refund", label: "Refund" }, { id: "email", label: "Email guest" }] },
{ id: "order-4820", label: "Order #4820", description: "Lukas Berg", group: "Orders", kind: "Order", icon: <Receipt />, meta: "£68.95", actions: [{ id: "open", label: "Open order" }, { id: "refund", label: "Refund" }, { id: "email", label: "Email guest" }] },
{ id: "order-4818", label: "Order #4818", description: "Leila Haddad", group: "Orders", kind: "Order", icon: <Receipt />, meta: "£155.00", actions: [{ id: "open", label: "Open order" }, { id: "refund", label: "Refund" }, { id: "email", label: "Email guest" }] },
{ id: "ada", label: "Ada Okafor", description: "3 stays · ada.okafor@fastmail.com", group: "Guests", kind: "Guest", icon: <User />, actions: [{ id: "open", label: "Open guest" }, { id: "book", label: "New booking" }, { id: "email", label: "Email" }] },
{ id: "tomas", label: "Tomás Herrera", description: "1 stay · arriving 14 Dec", group: "Guests", kind: "Guest", icon: <User />, actions: [{ id: "open", label: "Open guest" }, { id: "book", label: "New booking" }, { id: "email", label: "Email" }] },
{ id: "room-214", label: "Room 214", description: "Sea View Double · overbooked 14 Dec", group: "Rooms", kind: "Room", icon: <BedDouble />, actions: [{ id: "open", label: "Open room" }, { id: "block", label: "Block" }, { id: "move", label: "Move guest" }] },
{ id: "room-108", label: "Room 108", description: "Garden Single · free tonight", group: "Rooms", kind: "Room", icon: <BedDouble />, actions: [{ id: "open", label: "Open room" }, { id: "block", label: "Block" }, { id: "move", label: "Move guest" }] },
]
// Your own fields for a record. The palette lays out its name and actions.
const fields: Record<string, [string, string][]> = {
"order-4821": [["Status", "Pending"], ["Channel", "Android"], ["Placed", "17 Nov, 15:42"], ["Stay", "2 nights, Sea View Double"]],
"order-4820": [["Status", "Pending"], ["Channel", "Web"], ["Placed", "17 Nov, 12:55"], ["Stay", "1 night, Garden Single"]],
"order-4818": [["Status", "Paid"], ["Channel", "Android"], ["Placed", "17 Nov, 06:21"], ["Stay", "1 night, Harbour Twin"]],
}
function Fields(item: CommandItem) {
const rows = fields[item.id]
if (!rows) {
return <p className="text-[15px] text-muted-foreground sm:text-[12.5px]">{item.description ?? `Go to ${item.label}.`}</p>
}
return (
<dl className="grid grid-cols-[90px_1fr] gap-x-2 gap-y-2 text-[15px] sm:grid-cols-[80px_1fr] sm:gap-y-1.5 sm:text-[12.5px]">
{[["Amount", String(item.meta)], ...rows].map(([term, value]) => (
<Fragment key={term}>
<dt className="text-muted-foreground">{term}</dt>
<dd className="tabular-nums">{value}</dd>
</Fragment>
))}
</dl>
)
}
export function Search() {
return (
<CommandPalette
items={commands}
recent={["order-4821","rates","room-214"]}
suggested={["new-booking","refund","block","export"]}
label="Search Saltmarsh"
onRun={(item, action) => {
// run it
}}
>
<CommandPaletteTrigger />
<CommandPaletteContent>
<CommandPaletteInput placeholder="Search pages, orders, guests" />
<CommandPaletteBody>
<CommandPaletteList />
<CommandPalettePreview placeholder="Type to search pages, actions, orders, guests and rooms.">
{Fields}
</CommandPalettePreview>
</CommandPaletteBody>
</CommandPaletteContent>
</CommandPalette>
)
}Usage
import { FileText, Receipt } from "lucide-react"
import {
CommandPalette,
CommandPaletteBody,
CommandPaletteContent,
CommandPaletteInput,
CommandPaletteList,
CommandPalettePreview,
CommandPaletteTrigger,
type CommandItem,
} from "@/components/ui/command-palette"
const commands: CommandItem[] = [
{ id: "orders", label: "Orders", group: "Pages", kind: "Page", icon: <FileText />, shortcut: "G O" },
{
id: "order-4821",
label: "Order #4821",
description: "Ada Okafor",
group: "Orders",
kind: "Order",
icon: <Receipt />,
actions: [
{ id: "open", label: "Open order" },
{ id: "refund", label: "Refund" },
],
},
]
export function Search({ run }: { run: (id: string, action?: string) => void }) {
return (
<CommandPalette items={commands} recent={["order-4821"]} onRun={(item, action) => run(item.id, action?.id)}>
<CommandPaletteTrigger />
<CommandPaletteContent>
<CommandPaletteInput />
<CommandPaletteBody>
<CommandPaletteList />
<CommandPalettePreview placeholder="Type to search pages and orders." />
</CommandPaletteBody>
</CommandPaletteContent>
</CommandPalette>
)
}Examples
import { BedDouble, FileText, Receipt, Zap } from 'lucide-react';
import {
CommandPalette,
CommandPaletteBody,
CommandPaletteContent,
CommandPaletteInput,
CommandPaletteList,
CommandPalettePreview,
CommandPaletteTrigger,
type CommandItem,
} from '@/components/ui/command-palette';
const commands: CommandItem[] = [
{ id: 'orders', label: 'Orders', group: 'Pages', kind: 'Page', icon: <FileText />, shortcut: 'G O' },
{ id: 'refund', label: 'Refund an order', group: 'Actions', kind: 'Action', icon: <Zap /> },
{
id: 'property', label: 'Switch property', group: 'Actions', icon: <Zap />, shortcut: '⌘ P',
items: [
{ id: 'saltmarsh', label: 'Saltmarsh Hotel', description: 'Whitstable' },
{ id: 'harbour', label: 'Harbour House', description: 'Deal' },
],
},
{
id: 'order-4821', label: 'Order #4821', description: 'Ada Okafor', group: 'Orders', kind: 'Order',
icon: <Receipt />, meta: '£204.00',
actions: [
{ id: 'open', label: 'Open order' },
{ id: 'refund', label: 'Refund' },
{ id: 'email', label: 'Email guest' },
],
},
{ id: 'room-214', label: 'Room 214', description: 'Sea View Double', group: 'Rooms', kind: 'Room', icon: <BedDouble /> },
];
export function Search({ onRun }: { onRun: (item: CommandItem, actionId?: string) => void }) {
return (
<CommandPalette items={commands} recent={['order-4821']} onRun={(item, action) => onRun(item, action?.id)}>
<CommandPaletteTrigger />
<CommandPaletteContent>
<CommandPaletteInput />
<CommandPaletteBody>
<CommandPaletteList />
<CommandPalettePreview placeholder="Type to search pages, actions, orders and rooms.">
{(item) => <p className="text-muted-foreground">{item.description}</p>}
</CommandPalettePreview>
</CommandPaletteBody>
</CommandPaletteContent>
</CommandPalette>
);
}import {
CommandPalette,
CommandPaletteBody,
CommandPaletteContent,
CommandPaletteInput,
CommandPaletteList,
CommandPaletteTrigger,
type CommandItem,
} from '@/components/ui/command-palette';
const commands: CommandItem[] = [
{ id: 'inbox', label: 'Inbox', group: 'Go to', shortcut: 'G I' },
{ id: 'projects', label: 'Projects', group: 'Go to', shortcut: 'G P' },
{ id: 'new-issue', label: 'New issue', group: 'Create', shortcut: 'C' },
{ id: 'new-project', label: 'New project', group: 'Create' },
{ id: 'theme', label: 'Toggle theme', group: 'Preferences', keywords: ['dark', 'light'] },
];
export function Commands({ onRun }: { onRun: (id: string) => void }) {
return (
<CommandPalette items={commands} onRun={(item) => onRun(item.id)}>
<CommandPaletteTrigger>Commands</CommandPaletteTrigger>
<CommandPaletteContent className="sm:w-[520px]">
<CommandPaletteInput placeholder="Type a command" />
<CommandPaletteBody className="sm:h-auto sm:max-h-[320px]">
<CommandPaletteList empty={(q) => `No command for “${q}”`} />
</CommandPaletteBody>
</CommandPaletteContent>
</CommandPalette>
);
}Notes
The item shape
CommandItem, CommandAction, CommandRunDetails and filterCommands live in command-items, which dinachi add command-palette installs alongside, so other command surfaces can share them without the palette. An item has id, label, and optionally description, group, kind, icon, shortcut, meta, keywords, disabled, actions and items. All of it is plain data except icon and meta, which are React nodes: a list from a server or a model sends the rest and the app adds those two. The order of actions is part of the contract: actions[0] is what Enter runs, actions[1] what Mod+Enter runs, and the rest are reached from the preview. items makes it a sub-list that opens in place. Keep app logic in onRun rather than on the items.
What stays in your app
The palette lays out a record's name and actions. Its fields, such as an order's status and amount, come from CommandPalettePreview's children. What running does, and any toast after it, is onRun's job. shortcut on an item is a hint; binding it is up to you.
No motion
It is opened from the keyboard many times a day, so it never animates: no scale, fade or backdrop fade, and the highlight and sub-lists change at once. CommandPaletteContent uses Base UI's dialog parts directly, not the Dialog component, so it carries no transition. The phone screen appears at once too, as the keyboard already rises under it.
While the search is empty
With recent or suggested set, an empty search shows the recent items, then the suggested ones, up to idleLimit rows, rather than the whole list. Typing searches everything.
Long lists
Every search filters all the items, but at most limit rows render, 50 by default: about five screens of the list, and few enough that each keystroke stays quick with thousands of items. Past that, typing narrows faster than scrolling. The status says how many matched, such as 50 of 312 results, and the agent's search returns the same rows the list shows. Moving the highlight re-renders only the two rows it leaves and reaches, and the preview.
On a phone
Below 640px it fills the screen with the field at the bottom, above the keyboard, and the best match just above the field. The arrows move the way they point, so Up goes away from the field. A record with more than one action opens a detail screen with the keyboard down, focus on Back and the list hidden from assistive tech; Back returns focus to the field. Rows, the sub-list chip, Back and Cancel are 44px or taller.
In these docs
The site's search holds Cmd+K, so the demos here open from their button.
API
details.cancel() keeps the palette open.details.cancel() refuses the change.Takes the props of the element it renders.
backLabel in lower case.Takes the props of the element it renders.
Agent
Inside an AgentProvider, it appears in agent.snapshot() as combobox.
Reads
labelThe palette's label.
openTrue while the palette is showing.
valueThe search text, while open.
itemsEvery command on the current level while closed, or the matches while open.
disabledTrue when the palette is blocked.
Actions
open() / close()Opens or closes the palette.
search("refund")Opens it, types the text, returns the matches shown, up to limit, with their group and actions.
select("Order #4821", "Refund")Runs a command, with its first action or the one named, and closes if open. A sub-list opens and returns its commands.
back()Inside a sub-list, steps back out and returns the level above.
Opening and closing run through onOpenChange, the search through onValueChange and running through onRun. isAgentChange(details) is true for each; details.cancel() refuses it and the agent is told. disabled refuses everything. The agent sees a closed palette through its trigger, so render CommandPaletteTrigger for an agent to find it before it opens.
await agent.act({
steps: [
{ ref: "Command palette", action: "search", args: ["4821"] },
{ ref: "Command palette", action: "select", args: ["Order #4821", "Refund"] },
],
})Accessibility
- The palette is a modal dialog named by label; focus moves to the search and returns to the trigger on close.
- The search has role combobox and the results are a listbox of options; aria-activedescendant keeps focus in the field, and the highlighted option has aria-selected.
- The listbox is named after the label, and a polite status counts the matches as you type, as 50 of 312 results when limit cuts the list.
- Opening a sub-list names the search after it and announces it, as Switch property, 3 options.
- On a phone, the detail screen's Back is named Back to search. Back and Cancel mix the info colour with the text colour so they pass 4.5:1 in both themes.
- Groups are labelled by their headings. On a phone they are reversed visually only, so reading order stays best match first.
- Matched words are marked with mark, and shortcuts are kbd.