How to Build a Command Palette in React (Cmd+K)
Your app has dozens of actions spread across menus. Power users want one shortcut that gets them anywhere. A react command palette fixes that. Press Cmd+K, type two letters, press Enter. It looks like an input and a list. The hard part is everything around it: keyboard handling, focus, screen readers and an animation that never feels slow. This guide builds one from scratch in under 200 lines,…
To create a command palette in React, start by defining the component's behavior. It should be accessible with Cmd+K on macOS and Ctrl+K elsewhere, and it should render as a modal dialog with the search field focused. As the user types, the palette should filter the commands, and the active row should move with arrow keys while keeping focus in the input.
Pressing Enter should run the active command, and Escape or a backdrop click should close the palette, returning focus to its previous state. The palette must also provide screen reader feedback and animate smoothly or disable movement for users who prefer reduced motion. The provided code implements all these requirements in under 200 lines using React 19, Tailwind CSS 4, and Framer Motion.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to