What we're building
The Command Palette example shows a ⌘K-style command palette - the kind you'd find in VS Code or Linear. Clicking the search trigger (or imagining a keyboard shortcut) opens a modal dialog where you can filter commands, navigate with the arrow keys, and select an item to close the palette.
The animations lean on several Motion APIs:
AnimatePresenceto animate the backdrop and dialog in and out- The
animate,initial, andexitprops onmotioncomponents for enter/exit transitions - The
layoutprop to smoothly reflow groups as the filter changes - The
layoutIdprop for the selection highlight, which animates between items as you navigate
Get started
Here's the basic shell of the component - the dialog structure with no animations yet.
"use client"
import { useState, useRef, useCallback, useEffect, useMemo } from "react"
const COMMANDS = [
{
label: "General",
items: [
{ label: "New File", icon: FileIcon, shortcut: ["⌘", "N"] },
{ label: "Open File", icon: FolderIcon, shortcut: ["⌘", "O"] },
{ label: "Save", icon: SaveIcon, shortcut: ["⌘", "S"] },
],
},
{
label: "Editor",
items: [
{ label: "Format Document", icon: SparkleIcon },
{ label: "Go to Line", icon: HashIcon },
],
},
]
export default function CommandPalette() {
const [open, setOpen] = useState(false)
const [query, setQuery] = useState("")
const [selectedIndex, setSelectedIndex] = useState(0)
const inputRef = useRef(null)
const listRef = useRef(null)
const filtered = useMemo(() => {
if (!query.trim()) return COMMANDS
const q = query.toLowerCase()
return COMMANDS.map((group) => ({
...group,
items: group.items.filter((item) =>
item.label.toLowerCase().includes(q)
),
})).filter((group) => group.items.length > 0)
}, [query])
const flatItems = useMemo(
() => filtered.flatMap((g) => g.items),
[filtered]
)
const handleOpen = useCallback(() => {
setOpen(true)
setQuery("")
setSelectedIndex(0)
}, [])
const handleClose = useCallback(() => setOpen(false), [])
const handleSelect = useCallback(() => {
handleClose()
}, [handleClose])
const handleKeyDown = useCallback(
(e) => {
if (e.key === "ArrowDown") {
e.preventDefault()
setSelectedIndex((i) => (i < flatItems.length - 1 ? i + 1 : 0))
} else if (e.key === "ArrowUp") {
e.preventDefault()
setSelectedIndex((i) => (i > 0 ? i - 1 : flatItems.length - 1))
} else if (e.key === "Enter") {
e.preventDefault()
handleSelect()
} else if (e.key === "Escape") {
e.preventDefault()
handleClose()
}
},
[flatItems.length, handleSelect, handleClose]
)
useEffect(() => {
setSelectedIndex(0)
}, [query])
useEffect(() => {
if (!open) return
const raf = requestAnimationFrame(() => inputRef.current?.focus())
return () => cancelAnimationFrame(raf)
}, [open])
useEffect(() => {
if (!listRef.current) return
const selected = listRef.current.querySelector("[data-selected=true]")
selected?.scrollIntoView({ block: "nearest" })
}, [selectedIndex])
let runningIndex = 0
return (
<div style={wrapperStyle}>
<button style={triggerStyle} onClick={handleOpen}>
<SearchIcon />
<span style={triggerLabelStyle}>