AI Command Menu
Search and run anything from one field: tool actions with their logos and ⌘1–⌘5, everyday commands with icons, and a preview of what will run.
"use client"
import * as React from "react"
import {
CornerDownLeft,
FilePlus2,
FolderOpen,
Moon,
Search,
Settings2,
Share2,
type LucideIcon,
} from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Face,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
const ICONS = {
file: FilePlus2,
folder: FolderOpen,
share: Share2,
settings: Settings2,
moon: Moon,
} satisfies Record<string, LucideIcon>
export type CommandIcon = keyof typeof ICONS
export interface CommandItem {
id: string
label: string
/** Quiet text after the label: the app or the target. */
hint?: string
/** A brand mark in its own colour. */
brand?: string
/** An everyday icon, when there is no brand. */
icon?: CommandIcon
/** An agent face instead of a logo. */
face?: { tone: AiTone; shape: AiShape }
/** Keys shown on the right, e.g. ["⌘", "1"]. */
keys?: readonly string[]
/** What will happen, shown in the preview pane. */
preview?: string
}
export interface CommandGroup {
id: string
title: string
items: readonly CommandItem[]
}
export interface AiCommandMenuProps {
groups: readonly CommandGroup[]
placeholder?: string
/** Controlled query. */
query?: string
onQueryChange?: (query: string) => void
/** Controlled highlighted item. */
activeId?: string
onActiveChange?: (id: string) => void
/** Called on Enter, click or a ⌘ shortcut. */
onRun?: (item: CommandItem) => void
/** The item that just ran: it flashes and the footer confirms it. */
ranId?: string
className?: string
}
const CSS = `
.aik-cm{width:min(100%,820px);container-type:inline-size;--aik-r-shell:24px;--aik-rim:5px}
.aik-cm-card{display:grid;grid-template-columns:minmax(0,1fr);grid-template-rows:auto 1fr auto;overflow:hidden}
.aik-cm-field{display:flex;align-items:center;gap:12px;height:60px;padding:0 18px;border-bottom:1px solid var(--aik-line)}
.aik-cm-field svg{color:var(--aik-text-3)}
.aik-cm-field input{flex:1;min-width:0;height:100%;border:0;background:none;outline:none;font-size:16px;color:var(--aik-text)}
.aik-cm-field input::placeholder{color:var(--aik-text-3)}
.aik-cm-enter{display:grid;place-items:center;width:28px;height:28px;border-radius:8px;box-shadow:inset 0 0 0 1px var(--aik-line-2);color:var(--aik-text-3)}
.aik-cm-body{display:grid;grid-template-columns:minmax(0,1fr) 260px;grid-template-rows:minmax(0,1fr);min-height:0;height:440px;overflow:hidden}
.aik-cm-list{min-height:0;overflow-y:auto;padding:8px 8px 18px;scrollbar-width:thin;scroll-behavior:smooth;-webkit-mask:linear-gradient(#000 calc(100% - 26px),transparent);mask:linear-gradient(#000 calc(100% - 26px),transparent)}
.aik-cm-gt{display:block;padding:14px 12px 6px;font-size:11.5px;font-weight:600;letter-spacing:.08em;text-transform:uppercase;color:var(--aik-text-3)}
.aik-cm-gt:first-child{padding-top:6px}
.aik-cm-item{position:relative;display:flex;align-items:center;gap:12px;width:100%;height:50px;padding:0 10px;border-radius:12px;text-align:left;font-size:15px;transition:background var(--aik-d-state),transform var(--aik-d-press)}
.aik-cm-item[data-on]{background:var(--aik-raised)}
.aik-cm-item[data-on]::before{content:"";position:absolute;left:0;top:14px;bottom:14px;width:3px;border-radius:3px;background:var(--aik-blue);animation:aik-cm-bar var(--aik-d-enter) var(--aik-ease-out)}
.aik-cm-item:active{transform:scale(.99)}
.aik-cm-item[data-ran]{animation:aik-cm-ran 700ms var(--aik-ease-out)}
.aik-cm-ico{display:grid;place-items:center;width:32px;height:32px;flex:none;border-radius:9px;background:var(--aik-card);box-shadow:0 0 0 1px var(--aik-line-2);color:var(--aik-text-2)}
.aik-cm-label{min-width:0;display:flex;align-items:baseline;gap:9px;white-space:nowrap;overflow:hidden}
.aik-cm-label b{font-weight:500}
.aik-cm-label mark{background:color-mix(in oklab,var(--aik-blue) 22%,transparent);color:var(--aik-text);border-radius:4px;padding:0 1px}
.aik-cm-label span{color:var(--aik-text-3);overflow:hidden;text-overflow:ellipsis}
.aik-cm-keys{margin-left:auto;display:flex;gap:4px;flex:none}
.aik-cm-keys kbd,.aik-cm-foot kbd{display:grid;place-items:center;min-width:24px;height:24px;padding:0 6px;border-radius:7px;font:500 12px var(--aik-sans);color:var(--aik-text-2);background:var(--aik-card);box-shadow:inset 0 0 0 1px var(--aik-line-2),0 1px 0 var(--aik-line-2)}
.aik-cm-empty{display:grid;place-items:center;gap:10px;height:100%;color:var(--aik-text-3);font-size:14px;text-align:center}
.aik-cm-side{display:grid;align-content:start;padding:22px 20px;border-left:1px solid var(--aik-line);background:var(--aik-panel);min-width:0}
.aik-cm-sidein{display:grid;align-content:start;gap:14px}
.aik-cm-sidein > *{animation:aik-cm-in var(--aik-d-panel) var(--aik-ease-out) both}
.aik-cm-sidein > :nth-child(2){animation-delay:40ms}.aik-cm-sidein > :nth-child(3){animation-delay:80ms}
.aik-cm-big{display:grid;place-items:center;width:64px;height:64px;border-radius:18px;background:var(--aik-card);box-shadow:0 0 0 1px var(--aik-line-2),0 10px 30px -12px rgb(0 0 0 / 40%)}
.aik-cm-side h4{font-size:17px;font-weight:600;letter-spacing:-.01em}
.aik-cm-side p{font-size:14px;line-height:21px;color:var(--aik-text-2)}
.aik-cm-run{display:flex;align-items:center;gap:8px;margin-top:4px;font-size:13px;color:var(--aik-text-3)}
.aik-cm-foot{display:flex;align-items:center;gap:8px;height:48px;padding:0 16px;border-top:1px solid var(--aik-line);font-size:13px;color:var(--aik-text-3);white-space:nowrap;min-width:0;overflow:hidden}
.aik-cm-foot .aik-cm-done{color:var(--aik-green);display:flex;align-items:center;gap:6px;animation:aik-cm-in var(--aik-d-enter) var(--aik-ease-out)}
.aik-cm-foot .aik-cm-hint{display:flex;align-items:center;gap:8px}
.aik-cm-foot .aik-cm-push{margin-left:auto;display:flex;align-items:center;gap:8px}
@keyframes aik-cm-bar{from{transform:scaleY(0)}}
@keyframes aik-cm-in{from{opacity:0;transform:translateY(6px)}}
@keyframes aik-cm-ran{0%{background:color-mix(in oklab,var(--aik-green) 26%,var(--aik-raised))}100%{background:var(--aik-raised)}}
@container (max-width:640px){.aik-cm-body{grid-template-columns:minmax(0,1fr);height:420px}.aik-cm-side{display:none}.aik-cm-foot .aik-cm-hint{display:none}}
@container (max-width:420px){.aik-cm-label span{display:none}.aik-cm-keys{display:none}.aik-cm-foot .aik-cm-push{display:none}.aik-cm-field{padding:0 14px}}
@media (prefers-reduced-motion: reduce){.aik-cm-item,.aik-cm-sidein > *{animation:none}}
`
function useControlled<T>(
value: T | undefined,
initial: T,
onChange?: (next: T) => void
) {
const [inner, setInner] = React.useState(initial)
const current = value ?? inner
const set = React.useCallback(
(next: T) => {
if (value === undefined) setInner(next)
onChange?.(next)
},
[value, onChange]
)
return [current, set] as const
}
/** Wraps the first match of `query` in a <mark>. */
function highlight(text: string, query: string) {
const q = query.trim().toLowerCase()
if (!q) return text
const at = text.toLowerCase().indexOf(q)
if (at < 0) return text
return (
<>
{text.slice(0, at)}
<mark>{text.slice(at, at + q.length)}</mark>
{text.slice(at + q.length)}
</>
)
}
function Glyph({ item, size }: { item: CommandItem; size: number }) {
if (item.face)
return (
<Face
mood="idle"
shape={item.face.shape}
tone={item.face.tone}
size={size}
/>
)
if (item.brand)
return <BrandIcon name={item.brand} tint="brand" size={size * 0.85} />
const Icon = ICONS[item.icon ?? "file"]
return <Icon size={size * 0.8} aria-hidden="true" />
}
/**
* Search and run anything from one field. Typing filters every section and
* highlights the match, ↑↓ move, Tab jumps between sections, Enter runs, and
* ⌘1–⌘5 run the first five suggestions directly.
*/
export function AiCommandMenu({
groups,
placeholder = "Type a command or search…",
query: queryProp,
onQueryChange,
activeId: activeProp,
onActiveChange,
onRun,
ranId,
className,
}: AiCommandMenuProps) {
const [query, setQuery] = useControlled(queryProp, "", onQueryChange)
const listRef = React.useRef<HTMLDivElement>(null)
const visible = React.useMemo(() => {
const q = query.trim().toLowerCase()
return groups
.map((group) => ({
...group,
items: group.items.filter(
(item) =>
!q || `${item.label} ${item.hint ?? ""}`.toLowerCase().includes(q)
),
}))
.filter((group) => group.items.length > 0)
}, [groups, query])
const flat = visible.flatMap((group) => group.items)
const [active, setActive] = useControlled(
activeProp,
flat[0]?.id ?? "",
onActiveChange
)
const current = flat.find((item) => item.id === active) ?? flat[0]
React.useEffect(() => {
const node = listRef.current?.querySelector<HTMLElement>("[data-on]")
node?.scrollIntoView({ block: "nearest" })
}, [current?.id])
const move = (delta: number) => {
if (!flat.length) return
const at = Math.max(
0,
flat.findIndex((item) => item.id === current?.id)
)
setActive(flat[(at + delta + flat.length) % flat.length].id)
}
const jumpSection = (delta: number) => {
const at = visible.findIndex((g) =>
g.items.some((i) => i.id === current?.id)
)
const next = visible[(at + delta + visible.length) % visible.length]
if (next) setActive(next.items[0].id)
}
const onKey = (event: React.KeyboardEvent) => {
if (event.key === "ArrowDown") {
event.preventDefault()
move(1)
} else if (event.key === "ArrowUp") {
event.preventDefault()
move(-1)
} else if (event.key === "Tab") {
event.preventDefault()
jumpSection(event.shiftKey ? -1 : 1)
} else if (event.key === "Enter" && current) {
event.preventDefault()
onRun?.(current)
} else if ((event.metaKey || event.ctrlKey) && /^[1-9]$/.test(event.key)) {
const hit = flat.find((item) => item.keys?.at(-1) === event.key)
if (hit) {
event.preventDefault()
setActive(hit.id)
onRun?.(hit)
}
}
}
// The preview follows the highlight once it settles, so fast typing or
// arrowing doesn't restart its fade on every keystroke.
const [shown, setShown] = React.useState(current)
React.useEffect(() => {
if (current?.id === shown?.id) return
const id = window.setTimeout(() => setShown(current), shown ? 140 : 0)
return () => window.clearTimeout(id)
}, [current, shown])
const ran = flat.find((item) => item.id === ranId)
return (
<div className={cx("aik aik-cm", className)}>
<AiKitStyles />
<AiStyle id="ai-command-menu" css={CSS} />
<div className="aik-shell">
<div className="aik-surface aik-cm-card">
<label className="aik-cm-field">
<Search size={19} aria-hidden="true" />
<input
value={query}
placeholder={placeholder}
onChange={(event) => {
setQuery(event.target.value)
const q = event.target.value.trim().toLowerCase()
const first = groups
.flatMap((g) => g.items)
.find(
(i) =>
!q ||
`${i.label} ${i.hint ?? ""}`.toLowerCase().includes(q)
)
if (first) setActive(first.id)
}}
onKeyDown={onKey}
role="combobox"
aria-expanded="true"
aria-controls="aik-cm-list"
aria-activedescendant={
current ? `aik-cm-${current.id}` : undefined
}
aria-label="Command"
/>
<span className="aik-cm-enter" aria-hidden="true">
<CornerDownLeft size={14} />
</span>
</label>
<div className="aik-cm-body">
<div
ref={listRef}
className="aik-cm-list"
id="aik-cm-list"
role="listbox"
>
{visible.length === 0 ? (
<div className="aik-cm-empty">
<Face mood="oops" shape="orb" tone="ink" size={40} />
Nothing matches “{query}”
</div>
) : (
visible.map((group) => (
<div key={group.id} role="group" aria-label={group.title}>
<span className="aik-cm-gt">{group.title}</span>
{group.items.map((item) => (
<button
key={item.id}
id={`aik-cm-${item.id}`}
type="button"
role="option"
aria-selected={item.id === current?.id}
className="aik-cm-item"
data-on={item.id === current?.id ? "" : undefined}
data-ran={item.id === ranId ? "" : undefined}
onPointerMove={() =>
item.id !== current?.id && setActive(item.id)
}
onClick={() => onRun?.(item)}
>
<span className="aik-cm-ico">
<Glyph item={item} size={20} />
</span>
<span className="aik-cm-label">
<b>{highlight(item.label, query)}</b>
{item.hint ? (
<span>{highlight(item.hint, query)}</span>
) : null}
</span>
{item.keys ? (
<span className="aik-cm-keys" aria-hidden="true">
{item.keys.map((key) => (
<kbd key={key}>{key}</kbd>
))}
</span>
) : null}
</button>
))}
</div>
))
)}
</div>
<aside className="aik-cm-side" aria-live="polite">
{shown ? (
<div className="aik-cm-sidein" key={shown.id}>
<span className="aik-cm-big">
<Glyph item={shown} size={34} />
</span>
<h4>{shown.label}</h4>
<p>{shown.preview ?? shown.hint}</p>
<span className="aik-cm-run">
<kbd style={{ font: "500 12px var(--aik-sans)" }}>↵</kbd> to
run
</span>
</div>
) : null}
</aside>
</div>
<div className="aik-cm-foot">
{ran ? (
<span className="aik-cm-done" key={ran.id}>
✓ Ran “{ran.label}”
</span>
) : (
<span className="aik-cm-hint">
<kbd>Tab</kbd> to jump between sections
</span>
)}
<span className="aik-cm-push">
<kbd>↑</kbd>
<kbd>↓</kbd> to move
<kbd>⌘</kbd>
<kbd>K</kbd> to open
</span>
</div>
</div>
</div>
</div>
)
}
Installation
One command adds ai-command-menu and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/ai-command-menu.json