skip to content
mpkcn

~/components/command-palette

Command Palette

A terminal-style Ctrl+K command palette with groups, fuzzy word filtering and full keyboard control, as a dialog or inline.

# navigate
cd ~/projectsdir
cd ~/docsdirG D
# links
open githublink
send emaillink
# actions
theme darkaction
theme lightaction
rm -rf /nope
pick a command

Installation

terminal
npx mpkcn add command-palette
deps:none beyond react· tested with react@19.3.0

Usage

Render it once, anywhere (a layout is a good place), and give it items. Ctrl+K (Cmd+K on macOS) opens it.

tsx
"use client";

import { useRouter } from "next/navigation";
import { useTheme } from "next-themes";
import { CommandPalette } from "@/components/mpkcn/command-palette";

export function Palette() {
  const router = useRouter();
  const { setTheme } = useTheme();
  return (
    <CommandPalette
      items={[
        { label: "cd ~/docs", group: "navigate", hint: "page", onSelect: () => router.push("/docs") },
        { label: "open github", group: "links", keywords: ["source"], onSelect: () => window.open("https://github.com/you") },
        { label: "theme dark", group: "actions", onSelect: () => setTheme("dark") },
        { label: "theme light", group: "actions", onSelect: () => setTheme("light") },
      ]}
    />
  );
}

Type to filter: every word of the query must appear in the label, group or keywords. ↑ ↓ move (and wrap), Home and End jump, Enter runs, Esc or a click outside closes. Items are listed under their group, in the order groups first appear.

Examples

Open it from a button

Control it with open and onOpenChange. The hotkey keeps working alongside.

tsx
const [open, setOpen] = useState(false);

<button onClick={() => setOpen(true)}>search</button>
<CommandPalette items={items} open={open} onOpenChange={setOpen} />

Inline

modal={false} renders the panel in place, always visible, with no backdrop. The hotkey then focuses its input.

tsx
<CommandPalette items={items} modal={false} hotkey={false} />

Another shortcut

hotkey is the key pressed with Ctrl or Cmd. Pass false if your site already uses Ctrl+K (this page does, so the previews above turn it off).

tsx
<CommandPalette items={items} hotkey="p" />

Props

PropTypeDefaultDescription
itemsCommandItem[]—{ label, onSelect, id?, group?, hint?, shortcut?, keywords?, disabled? }.
openboolean—Controlled open state.
defaultOpenbooleanfalseInitial open state when uncontrolled.
onOpenChange(open: boolean) => void—Called when it wants to open or close.
hotkeystring | false"k"Key pressed with Ctrl/Cmd to toggle it (focuses the input when inline).
modalbooleantrueCentered dialog over the page. false renders the panel inline.
placeholderstring"type a command…"Placeholder of the search input.
emptyTextReactNode"no match"Shown when nothing matches.
promptstring"$"Prompt in front of the input.
labelstring"Command palette"Accessible name of the dialog and list.
footerReactNode | false"↑↓ select · ⏎ run · esc close"Key-hint footer; false hides it.
closeOnSelectbooleantrueClose the dialog after an item runs.
filter(item, query) => booleanword matchCustom matcher.
classNamestring—Extra classes for the panel.

Accessibility

  • Built on the ARIA combobox pattern: the input is a combobox that controls a listbox of options, and aria-activedescendant follows the highlighted one, so focus never leaves the input.
  • Groups are role="group" with their heading as the label. Disabled items are aria-disabled and skipped by the arrow keys.
  • As a dialog it is role="dialog" with aria-modal, rendered in a portal. Focus moves to the input, Tab cannot leave, page scroll is locked, and focus goes back to where it was when it closes.
  • The picked command runs right after the dialog closes, so a command that moves focus or navigates is not undone by the focus return.