Command menu
A keyboard-first quick-actions menu with instant filtering, grouped results and shortcut hints.
"use client";
import { useId, useMemo, useState, type KeyboardEvent } from "react";
import "./command-menu.css";
export type Command = {
id: string;
label: string;
group: string;
shortcut?: string;
run?: () => void;
};
type CommandMenuProps = {
commands: Command[];
placeholder?: string;
theme?: "light" | "dark";
};
/**
* CommandMenu
* - Type to filter (every typed word must match).
* - Arrow Up/Down to move, Enter to run, Escape to clear.
* - Uses the ARIA combobox + listbox pattern for screen readers.
*/
export function CommandMenu({
commands,
placeholder = "Type a command…",
theme = "light",
}: CommandMenuProps) {
const [query, setQuery] = useState("");
const [active, setActive] = useState(0);
const [last, setLast] = useState<string | null>(null);
const id = useId();
const results = useMemo(() => {
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
return commands.filter((c) =>
words.every((w) => `${c.label} ${c.group}`.toLowerCase().includes(w)),
);
}, [commands, query]);
const run = (c?: Command) => {
if (!c) return;
c.run?.();
setLast(c.label);
};
const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === "ArrowDown") {
e.preventDefault();
setActive((a) => (a + 1) % Math.max(results.length, 1));
} else if (e.key === "ArrowUp") {
e.preventDefault();
setActive((a) => (a - 1 + results.length) % Math.max(results.length, 1));
} else if (e.key === "Enter") {
e.preventDefault();
run(results[active]);
} else if (e.key === "Escape") {
setQuery("");
setActive(0);
}
};
return (
<div className="gu-cmd" data-theme={theme}>
<div className="gu-cmd__field">
<svg viewBox="0 0 20 20" aria-hidden="true">
<circle cx="8.5" cy="8.5" r="6" />
<path d="m13 13 5 5" />
</svg>
<input
value={query}
onChange={(e) => {
setQuery(e.target.value);
setActive(0);
}}
onKeyDown={onKeyDown}
placeholder={placeholder}
role="combobox"
aria-expanded="true"
aria-controls={`${id}-list`}
aria-activedescendant={
results[active] ? `${id}-${results[active].id}` : undefined
}
aria-label="Search commands"
/>
</div>
<ul
className="gu-cmd__list"
id={`${id}-list`}
role="listbox"
aria-label="Commands"
>
{results.length === 0 && (
<li className="gu-cmd__empty">No command matches “{query}”.</li>
)}
{results.map((c, i) => {
// Show the group title above the first command of each group.
const heading =
i === 0 || results[i - 1].group !== c.group ? c.group : null;
return (
<li key={c.id} role="presentation">
{heading && <p className="gu-cmd__group">{heading}</p>}
<div
id={`${id}-${c.id}`}
role="option"
aria-selected={i === active}
className="gu-cmd__item"
style={{ animationDelay: `${i * 25}ms` }}
onMouseMove={() => setActive(i)}
onClick={() => run(c)}
>
<span>{c.label}</span>
{c.shortcut && <kbd>{c.shortcut}</kbd>}
</div>
</li>
);
})}
</ul>
<p className="gu-cmd__status" role="status">
{last ? `Ran “${last}”` : "↑ ↓ to move · Enter to run"}
</p>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Command menu
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
An inline command menu (the panel you see behind Ctrl/Cmd+K in modern apps): a search field on top, grouped commands below with keyboard shortcut hints, and a status line at the bottom.
## Layout and look
- Panel: max width 440px, 20px radius, 1px border rgba(14,14,16,0.12), background #fbf8f2 (dark: #1a1a1e with border rgba(243,238,227,0.1)), soft long shadow.
- Field: 56px tall, magnifier icon (18px, muted stroke), input with no border, 16px medium text. Focused: 2px #ff5a1f line under the field.
- List: max height 280px, scrolls, 6px padding. Group titles 12px bold muted. Rows 44px tall, 12px radius, label left, shortcut <kbd> right (small outlined pill, 70% opacity).
- Highlighted row: background #ff5a1f, text #0e0e10.
- Status line: 12.5px muted text, top border.
## Behavior and motion
- Filtering: split the query into words; a command matches when every word appears in its label or group.
- Arrow Down/Up move the highlight (wrapping around), Enter runs the highlighted command, Escape clears the query. Mouse hover moves the highlight; click runs.
- Each row slides in 6px with a fade over 350ms, staggered 25ms per row, when the list re-renders.
- Status line announces the last command that ran ("Ran “New project”").
## Accessibility
- ARIA combobox pattern: input has role="combobox", aria-controls the listbox, aria-activedescendant points to the highlighted option. Rows have role="option" and aria-selected. Status line has role="status".
- Empty state: "No command matches “…”".
- prefers-reduced-motion: no row animation, no color transition.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### CommandMenu.tsx
```tsx
"use client";
import { useId, useMemo, useState, type KeyboardEvent } from "react";
import "./command-menu.css";
export type Command = {
id: string;
label: string;
group: string;
shortcut?: string;
run?: () => void;
};
type CommandMenuProps = {
commands: Command[];
placeholder?: string;
theme?: "light" | "dark";
};
/**
* CommandMenu
* - Type to filter (every typed word must match).
* - Arrow Up/Down to move, Enter to run, Escape to clear.
* - Uses the ARIA combobox + listbox pattern for screen readers.
*/
export function CommandMenu({
commands,
placeholder = "Type a command…",
theme = "light",
}: CommandMenuProps) {
const [query, setQuery] = useState("");
const [active, setActive] = useState(0);
const [last, setLast] = useState<string | null>(null);
const id = useId();
const results = useMemo(() => {
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
return commands.filter((c) =>
words.every((w) => `${c.label} ${c.group}`.toLowerCase().includes(w)),
);
}, [commands, query]);
const run = (c?: Command) => {
if (!c) return;
c.run?.();
setLast(c.label);
};
const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === "ArrowDown") {
e.preventDefault();
setActive((a) => (a + 1) % Math.max(results.length, 1));
} else if (e.key === "ArrowUp") {
e.preventDefault();
setActive((a) => (a - 1 + results.length) % Math.max(results.length, 1));
} else if (e.key === "Enter") {
e.preventDefault();
run(results[active]);
} else if (e.key === "Escape") {
setQuery("");
setActive(0);
}
};
return (
<div className="gu-cmd" data-theme={theme}>
<div className="gu-cmd__field">
<svg viewBox="0 0 20 20" aria-hidden="true">
<circle cx="8.5" cy="8.5" r="6" />
<path d="m13 13 5 5" />
</svg>
<input
value={query}
onChange={(e) => {
setQuery(e.target.value);
setActive(0);
}}
onKeyDown={onKeyDown}
placeholder={placeholder}
role="combobox"
aria-expanded="true"
aria-controls={`${id}-list`}
aria-activedescendant={
results[active] ? `${id}-${results[active].id}` : undefined
}
aria-label="Search commands"
/>
</div>
<ul
className="gu-cmd__list"
id={`${id}-list`}
role="listbox"
aria-label="Commands"
>
{results.length === 0 && (
<li className="gu-cmd__empty">No command matches “{query}”.</li>
)}
{results.map((c, i) => {
// Show the group title above the first command of each group.
const heading =
i === 0 || results[i - 1].group !== c.group ? c.group : null;
return (
<li key={c.id} role="presentation">
{heading && <p className="gu-cmd__group">{heading}</p>}
<div
id={`${id}-${c.id}`}
role="option"
aria-selected={i === active}
className="gu-cmd__item"
style={{ animationDelay: `${i * 25}ms` }}
onMouseMove={() => setActive(i)}
onClick={() => run(c)}
>
<span>{c.label}</span>
{c.shortcut && <kbd>{c.shortcut}</kbd>}
</div>
</li>
);
})}
</ul>
<p className="gu-cmd__status" role="status">
{last ? `Ran “${last}”` : "↑ ↓ to move · Enter to run"}
</p>
</div>
);
}
```
### command-menu.css
```css
.gu-cmd {
--bg: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.12);
--hover: #ff5a1f;
--hover-text: #0e0e10;
width: min(100%, 440px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--bg);
color: var(--text);
font-family: system-ui, sans-serif;
box-shadow: 0 30px 60px -30px rgb(14 14 16 / 0.35);
}
.gu-cmd[data-theme="dark"] {
--bg: #1a1a1e;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.1);
box-shadow: 0 30px 60px -30px rgb(0 0 0 / 0.8);
}
.gu-cmd__field {
display: flex;
align-items: center;
gap: 10px;
padding: 0 18px;
border-bottom: 1px solid var(--line);
}
.gu-cmd__field svg {
width: 18px;
height: 18px;
flex: none;
fill: none;
stroke: var(--muted);
stroke-width: 2;
stroke-linecap: round;
}
.gu-cmd__field input {
width: 100%;
height: 56px;
border: 0;
background: transparent;
color: inherit;
font:
500 1rem system-ui,
sans-serif;
outline: none;
}
.gu-cmd__field input::placeholder {
color: var(--muted);
}
.gu-cmd__field:has(input:focus-visible) {
box-shadow: inset 0 -2px 0 var(--hover);
}
.gu-cmd__list {
max-height: 280px;
margin: 0;
padding: 6px;
overflow-y: auto;
list-style: none;
}
.gu-cmd__group {
margin: 10px 12px 4px;
color: var(--muted);
font-size: 0.75rem;
font-weight: 700;
}
.gu-cmd__item {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 44px;
padding: 0 12px;
border-radius: 12px;
font-size: 0.95rem;
font-weight: 500;
cursor: pointer;
animation: gu-cmd-in 0.35s cubic-bezier(0.16, 1, 0.3, 1) both;
transition:
background-color 0.15s,
color 0.15s;
}
.gu-cmd__item[aria-selected="true"] {
background: var(--hover);
color: var(--hover-text);
}
.gu-cmd__item kbd {
padding: 3px 7px;
border: 1px solid currentColor;
border-radius: 6px;
font:
600 0.72rem system-ui,
sans-serif;
opacity: 0.7;
}
.gu-cmd__empty {
padding: 28px 12px;
color: var(--muted);
text-align: center;
}
.gu-cmd__status {
margin: 0;
padding: 10px 18px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 0.78rem;
}
@keyframes gu-cmd-in {
from {
opacity: 0;
transform: translateY(6px);
}
}
@media (prefers-reduced-motion: reduce) {
.gu-cmd__item {
animation: none;
transition: none;
}
}
```
You might also want
Next piece: Segmented controlDate range picker
Components, advanced
One-time code input
Components, intermediate
Search with suggestions
Components, intermediate