AI Prompt to Build a React Command Palette (Cmd+K) Component
This AI prompt generates a complete React command palette component, the kind of Cmd+K (or Ctrl+K) search-and-action overlay popularized by apps like Linear and Raycast. It's built for frontend developers who want a fuzzy-searchable list of commands and navigation shortcuts without hand-coding keyboard handling, focus trapping, and filtering logic from scratch every time they add one to a project.
A command palette looks simple but has several fiddly requirements that are easy to get wrong in a first pass: a global keyboard shortcut to open it, arrow-key navigation through filtered results, fuzzy or substring matching against a command list, and proper focus management so keyboard users aren't trapped or dropped out of the overlay. The prompt below asks the model for all of these explicitly, plus accessibility attributes, rather than leaving them to be discovered through follow-up bug reports.
If the command palette needs to match an existing app's exact look and spacing rather than a generic style, it helps to start from a measurement of the real page instead of a text description alone. Site to Prompt reads a rendered webpage's actual layout and turns it into a prompt, which is a useful starting point when you're replicating a specific product's command palette design.
Prompt template
Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.
Example input
When to use it
- You're adding global keyboard-driven navigation or a quick-actions menu to a React app
- Users have asked for a faster way to jump between pages or trigger actions than clicking through menus
- You're building an admin panel, dashboard, or developer tool where power users expect Cmd+K search
- You want consistent keyboard and focus behavior across the app instead of one-off dropdown menus
Best practices
- Specify the exact keyboard shortcut to open the palette (Cmd+K on Mac, Ctrl+K on Windows) and require it to work from anywhere in the app, not just one page
- Ask for debounced or client-side fuzzy filtering so results update smoothly as the user types, especially with larger command lists
- Require focus to move into the palette on open and return to the triggering element on close, with Escape always closing it
- If you're matching a specific existing app's command palette pixel-for-pixel, generate a starting prompt from the live page with Site to Prompt before refining the component details
Common mistakes
- Forgetting to trap focus inside the open palette, so Tab key presses leak out to the page behind it
- Hardcoding the command list instead of asking for a prop-driven list, which makes the component unreusable across pages
- Skipping ARIA roles (combobox, listbox, option) so screen reader users can't tell what's selected
- Not handling the case where the filtered results list is empty, leaving a blank overlay with no feedback
FAQs
How do I add a Cmd+K command palette to a React app?
Listen for the Cmd+K (or Ctrl+K) keydown event at the window level to toggle the palette's open state, then render a filtered, keyboard-navigable list of commands inside a focus-trapped overlay.
What's the difference between a command palette and a regular search dropdown?
A command palette is global (opened from anywhere via a keyboard shortcut) and typically runs an action directly on selection, while a search dropdown is usually scoped to one page and just filters visible data.
Do I need a library like cmdk or kbar, or can I build this from scratch?
You can build a basic version from scratch with the pattern in this prompt, but a library like cmdk handles edge cases like nested command groups and virtualization if the command list grows large.
How do I make a React command palette accessible?
Use combobox/listbox ARIA roles on the input and results, trap focus while it's open, support arrow-key navigation, and return focus to the triggering element when it closes.
Which Cuelara tool should I use if I'm copying an existing app's command palette design?
Site to Prompt — measures a live, rendered page and turns its actual layout into a prompt, which is the faster starting point when you're replicating a specific product's command palette rather than designing one from scratch.