Back to cookbook

AI Prompt to Build a React Command Palette (Cmd+K) Component

0 views Updated

Make this prompt yours

Share

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

Make this prompt yours

prompt-template
352 tokens
ROLE: You are a senior frontend engineer who writes accessible, reusable React components with TypeScript. TASK: Build a command palette (Cmd+K / Ctrl+K) component for a React application with the following behavior: 1. A global keyboard shortcut ([SHORTCUT, E.G. CMD+K]) opens the palette from anywhere in the app. 2. The palette renders as a centered modal overlay with a search input focused automatically on open. 3. Below the input, show a filtered, scrollable list of commands based on a prop-driven array of {id, label, icon, action, group} items. 4. Support fuzzy or substring matching on the label as the user types, updating the list in real time. 5. Support arrow-up/arrow-down to move the active selection and Enter to run the selected command's action and close the palette. 6. Escape closes the palette and returns focus to whatever element triggered it. 7. Group commands by their 'group' field with a small header label per group in the results list. CONSTRAINTS: - Use React hooks only, no class components. - Trap focus inside the palette while it is open. - Use ARIA roles appropriate for a combobox/listbox pattern. - Style with [STYLING APPROACH, E.G. TAILWIND CSS / CSS MODULES]. OUTPUT FORMAT: A single CommandPalette.tsx component file plus a short usage example showing how to pass the command list and mount the component, with brief inline comments explaining the keyboard and focus logic. APP CONTEXT: [BRIEF DESCRIPTION OF THE APP AND EXAMPLE COMMANDS, E.G. 'PROJECT MANAGEMENT TOOL WITH COMMANDS LIKE NEW TASK, GO TO SETTINGS, INVITE TEAMMATE']

Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.

Example input

example-input
99 tokens
ROLE: You are a senior frontend engineer who writes accessible, reusable React components with TypeScript. TASK: Build a command palette (Cmd+K / Ctrl+K) component with the behavior described above. APP CONTEXT: A project management tool. Example commands: 'Create new task', 'Go to Settings', 'Invite teammate', 'Switch to Dark Mode', 'Go to Dashboard'. Commands should be grouped as 'Actions' and 'Navigation'. STYLING APPROACH: Tailwind CSS

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.

Found this prompt useful? Share it.

Share

More in React Prompt Recipes

React Prompt Recipes

AI Prompt to Build a React Infinite Scroll List With Virtualization

This prompt is for developers who need a React infinite scroll list that stays fast even with thousands of rows, by combining lazy loaded pa…

ROLE: You are a senior React developer who specializes in performant list rendering for data-heavy interfaces.

CONTEXT:
Framework: [REACT VERSION, e.g. React 18 with hooks]
Data source: [DESCRIBE THE API OR DATA SOURCE, e.g. a paginated REST endpoint returning 50 items per page]
List item shape: [DESCRIBE THE FIELDS EACH ROW NEEDS TO DISPLAY]
Expected list size: [APPROXIMATE TOTAL ROWS, e.g. up to 10,000]

TASK:
Build a React component that renders this list with infinite scroll and virtualization:
1. A custom hook that manages paginated fetching, tracks loading and error state, and triggers the next page fetch when the user nears the bottom of the list.
2. A virtualized list renderer that only mounts rows near the current viewport, using a windowing technique, with a small buffer of extra rows above and below the visible area.
3. Loading placeholders for in-flight pages and a clear end-of-list indicator when there is no more data.
4. Basic error handling that lets the user retry a failed page fetch.

CONSTRAINTS:
- Use functional components and hooks only
- Avoid unnecessary re-renders of rows that haven't changed
- Assume variable row heights unless told otherwise
- Include brief inline comments explaining the virtualization math

OUTPUT FORMAT:
1. A short explanation of the approach
2. The custom data-fetching hook as a complete code block
3. The virtualized list component as a complete code block
4. Notes on any assumptions made about row height or page size

Make this prompt yours

React Prompt Recipes

AI Prompt to Turn a Design Screenshot into a React Component

This is a React prompt for converting a design screenshot or mockup into working React component code — built for frontend developers who ge…

ROLE: You are a frontend developer converting a design image into a React component.

CONTEXT: I'm attaching a design screenshot/mockup. Tech stack: [REACT VERSION, STYLING APPROACH e.g. Tailwind/CSS Modules/styled-components, COMPONENT LIBRARY IF ANY]

TASK:
1. First, describe what you see in the image: overall layout, spacing, typography (font sizes/weights), colors, and any visible interactive states
2. Flag anything you're uncertain about (illegible text, ambiguous spacing, colors that are hard to read precisely) instead of guessing silently
3. Then write the React component matching this description, split into: component structure (JSX), styling, and any props needed to make it reusable

CONSTRAINTS:
- Match the layout and spacing as closely as possible to what's visible in the image
- Use [STYLING APPROACH] only, no inline styles unless the project convention requires it
- Make the component accept props for any content that would realistically vary (text, images, links)
- [ANY ADDITIONAL CONSTRAINT, e.g. must be accessible, must support dark mode]

OUTPUT FORMAT:
1. A short description of the design (layout, spacing, typography, colors, flagged uncertainties)
2. The complete component code in a fenced code block
3. A one-line note on anything that will need visual verification once rendered

Make this prompt yours

React Prompt Recipes

ChatGPT Prompt to Build a React Drag-and-Drop File Upload Component With Progress Bar

This React drag and drop file upload component prompt is built for developers who need a working uploader with a drop zone, file previews, a…

ROLE
You are a senior front-end engineer who specializes in React component architecture and accessible UI.

CONTEXT
I'm building a [FORM / DASHBOARD / ADMIN PANEL] in React using [REACT VERSION, e.g. React 18 with function components] and [STYLING APPROACH, e.g. Tailwind CSS / CSS Modules / styled-components]. I need a file upload component that supports drag-and-drop in addition to a standard click-to-browse input.

REQUIREMENTS
- Accept these file types: [ALLOWED FILE TYPES, e.g. .png, .jpg, .pdf]
- Enforce a max file size of [MAX FILE SIZE] and a max file count of [MAX FILE COUNT]
- Show a visually distinct drop zone state when a file is dragged over it
- Display a progress bar per file while it uploads, using [UPLOAD METHOD, e.g. an onUploadProgress callback from axios, or XMLHttpRequest progress events]
- Show clear success and error states per file, including a retry option on failure
- Accept the actual upload function as a prop so the component is backend-agnostic
- Be keyboard accessible: the file input must be triggerable without a mouse, and status changes must be announced via an ARIA live region

CONSTRAINTS
- Use only [ALLOWED LIBRARIES, e.g. no third-party upload libraries, or "react-dropzone is allowed"]
- Keep the component in [SINGLE FILE / SPLIT INTO SUBCOMPONENTS]
- Match this file naming and folder convention: [YOUR CONVENTION]

OUTPUT FORMAT
1. The complete component code in a single fenced code block, with inline comments explaining the drag event handlers and progress logic
2. A short list of the props the component expects and what each one does
3. One sentence noting any edge case the implementation does not handle, if any

Make this prompt yours