Overlays
Command Palette
⌘K modal search over grouped commands with shortcuts and keyboard hints.
react-aria-componentslucide-react
Preview
Or press ⌘K / Ctrl+K.
"use client";
import { Building2, List, Plus, Send, Settings, Sparkles, Upload, User } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { CommandPalette } from "@/components/ui/command-palette";
import { Kbd } from "@/components/ui/kbd";
export default function Example() {
const [open, setOpen] = useState(false);
const [ran, setRan] = useState<string | null>(null);
return (
<div className="flex flex-col items-center gap-3">
<Button onPress={() => setOpen(true)} trailingIcon={<Kbd className="ml-1">⌘K</Kbd>}>
Open command palette
</Button>
<p className="text-sm text-fg-muted">{ran ? `Ran: ${ran}` : "Or press ⌘K / Ctrl+K."}</p>
<CommandPalette
isOpen={open}
onOpenChange={setOpen}
onAction={setRan}
groups={[
{
title: "Actions",
items: [
{ id: "find", label: "Find prospects with AI", icon: <Sparkles />, meta: "⌘F" },
{ id: "new-seq", label: "Create sequence", icon: <Plus />, meta: "⌘N" },
{ id: "import", label: "Import CSV", icon: <Upload /> },
],
},
{
title: "Go to",
items: [
{ id: "sarah", label: "Sarah Chen", icon: <User />, meta: "Prospect", keywords: "ledgerly vp sales" },
{ id: "ledgerly", label: "Ledgerly", icon: <Building2 />, meta: "Company" },
{ id: "list", label: "Series B — VP Sales", icon: <List />, meta: "List" },
{ id: "seqs", label: "Sequences", icon: <Send /> },
{ id: "settings", label: "Settings", icon: <Settings />, meta: "⌘," },
],
},
]}
/>
</div>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-components lucide-reactCreate a
command-palette.tsxfile and paste the following code into it.src/components/ui/command-palette.tsx"use client"; import { CornerDownLeft, Search } from "lucide-react"; import { type ReactNode, useEffect } from "react"; import { Autocomplete, Dialog, Header, Input, Menu, MenuItem, MenuSection, Modal, ModalOverlay, SearchField, useFilter } from "react-aria-components"; import { Kbd } from "./kbd"; export interface CommandItem { id: string; label: string; icon?: ReactNode; /** Right-aligned hint: a shortcut or a type label. */ meta?: ReactNode; /** Extra words to match while filtering. */ keywords?: string; } export interface CommandGroup { title: string; items: CommandItem[]; } export interface CommandPaletteProps { isOpen: boolean; onOpenChange: (open: boolean) => void; groups: CommandGroup[]; onAction: (id: string) => void; placeholder?: string; /** Bind ⌘K / Ctrl+K to toggle. Default true. */ shortcut?: boolean; } /** ⌘K palette: modal search over grouped commands with keyboard navigation. */ export function CommandPalette({ isOpen, onOpenChange, groups, onAction, placeholder = "Type a command or search…", shortcut = true }: CommandPaletteProps) { const { contains } = useFilter({ sensitivity: "base" }); useEffect(() => { if (!shortcut) return; const onKey = (e: globalThis.KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") { e.preventDefault(); onOpenChange(!isOpen); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [isOpen, onOpenChange, shortcut]); return ( <ModalOverlay isOpen={isOpen} onOpenChange={onOpenChange} isDismissable className="fixed inset-0 z-50 flex items-start justify-center bg-inverse/30 p-4 pt-[12vh] backdrop-blur-[1px]"> <Modal className="w-full max-w-[560px] outline-none"> <Dialog aria-label="Command palette" className="overflow-hidden rounded-lg bg-surface shadow-popover outline-none"> <Autocomplete filter={contains}> <SearchField aria-label="Search commands" autoFocus className="flex h-12 items-center gap-2.5 border-b border-line px-4"> <Search aria-hidden className="size-4 text-fg-muted" /> <Input placeholder={placeholder} className="h-full flex-1 bg-transparent text-base outline-none placeholder:text-fg-muted [&::-webkit-search-cancel-button]:hidden" /> <Kbd>esc</Kbd> </SearchField> <Menu aria-label="Commands" onAction={(key) => { onOpenChange(false); onAction(String(key)); }} renderEmptyState={() => <div className="px-4 py-10 text-center text-base text-fg-muted">No results.</div>} className="max-h-[min(60vh,400px)] overflow-auto p-1.5 outline-none" > {groups.map((g) => ( <MenuSection key={g.title} className="mb-1 last:mb-0"> <Header className="px-2.5 pb-1 pt-2 text-label text-[10px] text-fg-muted">{g.title}</Header> {g.items.map((it) => ( <MenuItem key={it.id} id={it.id} textValue={`${it.label} ${it.keywords ?? ""}`} className="group flex h-9 cursor-default items-center gap-2.5 rounded-sm px-2.5 text-base text-fg outline-none data-focused:bg-muted [&_svg]:size-4 [&_svg]:text-fg-secondary" > {it.icon} <span className="flex-1 truncate">{it.label}</span> {it.meta && <span className="text-sm text-fg-muted group-data-focused:hidden">{it.meta}</span>} <CornerDownLeft className="hidden !size-3.5 group-data-focused:block" aria-hidden /> </MenuItem> ))} </MenuSection> ))} </Menu> </Autocomplete> <footer className="flex items-center gap-4 border-t border-line bg-subtle px-4 py-2.5 text-sm text-fg-muted"> <span className="inline-flex items-center gap-1.5"> <Kbd>↑↓</Kbd> Navigate </span> <span className="inline-flex items-center gap-1.5"> <Kbd>↵</Kbd> Run </span> <span className="inline-flex items-center gap-1.5"> <Kbd>esc</Kbd> Close </span> </footer> </Dialog> </Modal> </ModalOverlay> ); }Create a
kbd.tsxfile and paste the following code into it.src/components/ui/kbd.tsximport { cx } from "@/utils/cx"; /** Marks keyboard shortcuts. */ export function Kbd({ className, children }: { className?: string; children: React.ReactNode }) { return ( <kbd className={cx( "inline-flex h-5 min-w-5 items-center justify-center rounded-[6px] border border-line bg-surface px-1.5 font-sans text-xs font-medium text-fg-muted", className, )} > {children} </kbd> ); }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.