import * as React from "react"; import { Command } from "cmdk"; import { Search } from "lucide-react"; import { cn } from "../lib/cn"; export interface CommandAction { id: string; label: string; group?: string; icon?: React.ReactNode; onSelect: () => void; } /** ⌘K palette. Opens on Cmd/Ctrl+K by default; big perceived-quality boost on any tool. */ export function CommandPalette({ actions, placeholder = "Type a command or search…", }: { actions: CommandAction[]; placeholder?: string; }) { const [open, setOpen] = React.useState(false); React.useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "k" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); setOpen((o) => !o); } }; document.addEventListener("keydown", onKey); return () => document.removeEventListener("keydown", onKey); }, []); const groups = React.useMemo(() => { const map = new Map(); for (const a of actions) { const g = a.group ?? "Actions"; map.set(g, [...(map.get(g) ?? []), a]); } return [...map.entries()]; }, [actions]); return (
No results. {groups.map(([group, items]) => ( {items.map((a) => ( { a.onSelect(); setOpen(false); }} className="flex cursor-pointer items-center gap-2 rounded px-2 py-2 text-sm text-text aria-selected:bg-surface-2 [&_svg]:size-4" > {a.icon} {a.label} ))} ))}
); }