Empty state command palettes
Five command palettes built around the states that are usually an afterthought: no results, loading, error, recent searches and suggestions. Every state is reachable by typing in the real input. Each is self-contained (no cmdk), sized, token-driven, filters as you type and supports arrow keys plus Enter.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/command-emptyDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/command-empty.tsx"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import {
Calculator,
Calendar,
Clock,
CreditCard,
FileText,
Lightbulb,
RotateCcw,
Search,
SearchX,
Settings,
Smile,
TriangleAlert,
User,
} from "lucide-react"
import { cn } from "@/lib/utils"
/* Empty command family: 5 SELF-CONTAINED command palettes, all focused on the
states OFF the happy path. NO cmdk, NO radix, NO portal. Every state is
reachable by typing into the real input: a query with no match opens the
empty/error/suggest state, every keystroke starts the loading phase, and an
empty query shows the recent searches. The loading duration is a FIXED number
(no Date.now/Math.random) and is cancelled in the effect cleanup. Color comes
ONLY from tokens, via alpha color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const paletteWidth: Record<StyledSize, string> = {
sm: "w-64",
md: "w-80",
lg: "w-96",
xl: "w-[28rem]",
}
export interface CommandItem {
label: React.ReactNode
group?: string
icon?: React.ReactNode
onSelect?: () => void
}
interface CommandProps {
className?: string
size?: StyledSize
placeholder?: string
items?: CommandItem[]
}
/* The label can be a ReactNode; flatten it to plain text for filtering. */
function nodeText(node: React.ReactNode): string {
if (node === null || node === undefined || typeof node === "boolean") return ""
if (typeof node === "string" || typeof node === "number") return String(node)
if (Array.isArray(node)) return node.map(nodeText).join("")
if (React.isValidElement(node)) {
return nodeText((node.props as { children?: React.ReactNode }).children)
}
return ""
}
/* Varsayilan liste: prop'suz render eder. */
const defaultItems: CommandItem[] = [
{ label: "Calendar", group: "Suggestions", icon: <Calendar /> },
{ label: "Search Emoji", group: "Suggestions", icon: <Smile /> },
{ label: "Calculator", group: "Suggestions", icon: <Calculator /> },
{ label: "Profile", group: "Settings", icon: <User /> },
{ label: "Billing", group: "Settings", icon: <CreditCard /> },
{ label: "Settings", group: "Settings", icon: <Settings /> },
{ label: "New Document", group: "Actions", icon: <FileText /> },
]
/* Sabitler: hepsi deterministik. */
const LOADING_MS = 420
const SKELETON_ROWS = 4
const recentQueries = ["calendar", "billing", "settings"]
/* Shared state plus combobox/listbox ids. useId keeps several instances on
the same page from colliding. */
function usePalette(source: CommandItem[]) {
const uid = React.useId()
const [query, setQuery] = React.useState("")
const [active, setActive] = React.useState(0)
const filtered = React.useMemo(() => {
const q = query.trim().toLowerCase()
if (q.length === 0) return source
return source.filter((item) => nodeText(item.label).toLowerCase().includes(q))
}, [query, source])
React.useEffect(() => {
setActive((prev) => (prev >= filtered.length ? 0 : prev))
}, [filtered.length])
const select = React.useCallback(
(index: number) => {
const item = filtered[index]
if (item) item.onSelect?.()
},
[filtered]
)
const onKeyDown = React.useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "ArrowDown") {
e.preventDefault()
setActive((prev) => (filtered.length === 0 ? 0 : (prev + 1) % filtered.length))
} else if (e.key === "ArrowUp") {
e.preventDefault()
setActive((prev) =>
filtered.length === 0 ? 0 : (prev - 1 + filtered.length) % filtered.length
)
} else if (e.key === "Enter") {
e.preventDefault()
select(active)
} else if (e.key === "Escape") {
e.preventDefault()
setQuery("")
setActive(0)
}
},
[filtered.length, active, select]
)
const listId = `${uid}-list`
const optionId = React.useCallback((i: number) => `${uid}-option-${i}`, [uid])
return { query, setQuery, active, setActive, filtered, select, onKeyDown, listId, optionId }
}
const rootBase =
"flex flex-col overflow-hidden rounded-xl border border-border bg-popover text-popover-foreground shadow-lg outline-none"
const inputRowBase = "flex items-center gap-2 border-b border-border px-3"
const inputBase =
"h-11 w-full rounded-md bg-transparent py-3 text-sm text-popover-foreground outline-none placeholder:text-muted-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50"
const listBase = "max-h-72 overflow-y-auto p-1.5"
const optionBase =
"flex cursor-pointer select-none items-center gap-2 rounded-lg px-2.5 py-2 text-sm outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:text-muted-foreground [&_i]:text-base [&_i]:leading-none [&_i]:text-muted-foreground"
const stateWrap = "flex flex-col items-center gap-2 px-6 py-8 text-center"
const stateTitle = "text-sm font-medium text-foreground"
const stateBody = "text-xs text-muted-foreground"
const chipBtn =
"inline-flex h-7 select-none items-center gap-1.5 rounded-full border border-border bg-[color-mix(in_oklab,var(--color-foreground)_5%,transparent)] px-2.5 text-xs text-muted-foreground outline-none transition-colors hover:bg-[color-mix(in_oklab,var(--color-foreground)_10%,transparent)] hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-3 [&_svg]:shrink-0 [&_i]:text-xs [&_i]:leading-none"
/* Search row: every palette uses the same header. */
function SearchRow({
p,
placeholder,
}: {
p: ReturnType<typeof usePalette>
placeholder: string
}) {
return (
<div className={inputRowBase}>
<Search className="size-4 shrink-0 text-muted-foreground" />
<input
role="combobox"
aria-expanded={true}
aria-controls={p.listId}
aria-autocomplete="list"
aria-activedescendant={p.filtered.length > 0 ? p.optionId(p.active) : undefined}
value={p.query}
placeholder={placeholder}
onChange={(e) => p.setQuery(e.target.value)}
onKeyDown={p.onKeyDown}
className={inputBase}
/>
</div>
)
}
/* Filtrelenmis satirlar: tum paletlerde ortak. */
function Rows({ p, reduce }: { p: ReturnType<typeof usePalette>; reduce: boolean }) {
return (
<>
{p.filtered.map((item, i) => (
<motion.div
key={nodeText(item.label) || String(i)}
id={p.optionId(i)}
role="option"
aria-selected={i === p.active}
tabIndex={-1}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={reduce ? { duration: 0 } : { duration: 0.15, ease: "easeOut" }}
onMouseEnter={() => p.setActive(i)}
onClick={() => p.select(i)}
className={cn(optionBase, i === p.active && "bg-accent text-accent-foreground")}
>
{item.icon}
<span className="flex-1 truncate">{item.label}</span>
</motion.div>
))}
</>
)
}
/* EmptyCommand: eslesme yoksa acikli bir bos durum. Yazarak deneyin. */
export function EmptyCommand({
className,
size = "md",
placeholder = "Type a command...",
items = defaultItems,
}: CommandProps) {
const p = usePalette(items)
const reduce = useReducedMotion() ?? false
return (
<div data-slot="styled-command" className={cn(rootBase, paletteWidth[size], className)}>
<SearchRow p={p} placeholder={placeholder} />
<div id={p.listId} role="listbox" aria-label="Command palette" className={listBase}>
{p.filtered.length === 0 ? (
<div className={stateWrap}>
<SearchX className="size-6 text-muted-foreground" />
<p className={stateTitle}>No results found</p>
<p className={stateBody}>
Nothing matches that query. Try a shorter word or clear the search.
</p>
<button type="button" className={chipBtn} onClick={() => p.setQuery("")}>
<RotateCcw />
Clear search
</button>
</div>
) : (
<Rows p={p} reduce={reduce} />
)}
</div>
</div>
)
}
/* LoadingCommand: every keystroke opens a loading phase of FIXED duration; skeleton
rows are shown. The timeout is cancelled in the effect cleanup. */
export function LoadingCommand({
className,
size = "md",
placeholder = "Type a command...",
items = defaultItems,
}: CommandProps) {
const p = usePalette(items)
const reduce = useReducedMotion() ?? false
const [loading, setLoading] = React.useState(false)
React.useEffect(() => {
setLoading(true)
const id = window.setTimeout(() => setLoading(false), LOADING_MS)
return () => window.clearTimeout(id)
}, [p.query])
return (
<div data-slot="styled-command" className={cn(rootBase, paletteWidth[size], className)}>
<SearchRow p={p} placeholder={placeholder} />
<div
id={p.listId}
role="listbox"
aria-label="Command palette"
aria-busy={loading}
className={listBase}
>
{loading ? (
<div className="flex flex-col gap-1 p-1">
{Array.from({ length: SKELETON_ROWS }).map((_, i) => (
<div key={i} className="flex items-center gap-2 rounded-lg px-1.5 py-2">
<span className="size-4 shrink-0 animate-pulse rounded bg-[color-mix(in_oklab,var(--color-foreground)_12%,transparent)] motion-reduce:animate-none" />
<span
className={cn(
"h-3 animate-pulse rounded bg-[color-mix(in_oklab,var(--color-foreground)_12%,transparent)] motion-reduce:animate-none",
i % 2 === 0 ? "w-2/3" : "w-1/2"
)}
/>
</div>
))}
</div>
) : p.filtered.length === 0 ? (
<p className="py-6 text-center text-sm text-muted-foreground">No results found.</p>
) : (
<Rows p={p} reduce={reduce} />
)}
</div>
</div>
)
}
/* ErrorCommand: a non-matching query represents a failed search; it shows a
danger-toned error card and a retry action. */
export function ErrorCommand({
className,
size = "md",
placeholder = "Type a command...",
items = defaultItems,
}: CommandProps) {
const p = usePalette(items)
const reduce = useReducedMotion() ?? false
const failed = p.query.trim().length > 0 && p.filtered.length === 0
return (
<div data-slot="styled-command" className={cn(rootBase, paletteWidth[size], className)}>
<SearchRow p={p} placeholder={placeholder} />
<div id={p.listId} role="listbox" aria-label="Command palette" className={listBase}>
{failed ? (
<motion.div
key="error"
role="alert"
initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}
animate={reduce ? { opacity: 1 } : { opacity: 1, y: 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.18, ease: "easeOut" }}
className={cn(
stateWrap,
"m-1 rounded-lg border border-[color-mix(in_oklab,var(--color-danger)_45%,transparent)] bg-[color-mix(in_oklab,var(--color-danger)_10%,transparent)]"
)}
>
<TriangleAlert className="size-6 text-danger" />
<p className={stateTitle}>Search failed</p>
<p className={stateBody}>
The command index could not answer that query. Retry or search for something else.
</p>
<button type="button" className={chipBtn} onClick={() => p.setQuery("")}>
<RotateCcw />
Retry
</button>
</motion.div>
) : (
<Rows p={p} reduce={reduce} />
)}
</div>
</div>
)
}
/* RecentCommand: sorgu bosken son aramalar; yazinca normal sonuclar. */
export function RecentCommand({
className,
size = "md",
placeholder = "Type a command...",
items = defaultItems,
}: CommandProps) {
const p = usePalette(items)
const reduce = useReducedMotion() ?? false
const blank = p.query.trim().length === 0
return (
<div data-slot="styled-command" className={cn(rootBase, paletteWidth[size], className)}>
<SearchRow p={p} placeholder={placeholder} />
<div id={p.listId} role="listbox" aria-label="Command palette" className={listBase}>
{blank ? (
<div className="p-1">
<p className="px-1.5 py-1.5 text-xs font-medium text-muted-foreground">
Recent searches
</p>
<div className="flex flex-col gap-1">
{recentQueries.map((q) => (
<button
key={q}
type="button"
className={cn(optionBase, "w-full text-left hover:bg-accent")}
onClick={() => p.setQuery(q)}
>
<Clock />
<span className="flex-1 truncate">{q}</span>
</button>
))}
</div>
</div>
) : p.filtered.length === 0 ? (
<p className="py-6 text-center text-sm text-muted-foreground">No results found.</p>
) : (
<Rows p={p} reduce={reduce} />
)}
</div>
</div>
)
}
/* SuggestCommand: hand-picked suggestions when nothing matches. */
export function SuggestCommand({
className,
size = "md",
placeholder = "Type a command...",
items = defaultItems,
}: CommandProps) {
const p = usePalette(items)
const reduce = useReducedMotion() ?? false
const suggestions = React.useMemo(() => items.slice(0, 3), [items])
return (
<div data-slot="styled-command" className={cn(rootBase, paletteWidth[size], className)}>
<SearchRow p={p} placeholder={placeholder} />
<div id={p.listId} role="listbox" aria-label="Command palette" className={listBase}>
{p.filtered.length === 0 ? (
<div className={stateWrap}>
<Lightbulb className="size-6 text-muted-foreground" />
<p className={stateTitle}>No direct match</p>
<p className={stateBody}>Here is what people usually look for instead.</p>
<div className="flex flex-wrap justify-center gap-1.5 pt-1">
{suggestions.map((item, i) => (
<button
key={nodeText(item.label) || String(i)}
type="button"
className={chipBtn}
onClick={() => p.setQuery(nodeText(item.label))}
>
{item.label}
</button>
))}
</div>
</div>
) : (
<Rows p={p} reduce={reduce} />
)}
</div>
</div>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Empty
A written out no-results state with a clear action.
import { EmptyCommand } from "@/components/ui/command-empty"
<EmptyCommand />Loading
Skeleton rows for a fixed beat after every keystroke.
import { LoadingCommand } from "@/components/ui/command-empty"
<LoadingCommand />Error
A danger toned failure card with a retry.
import { ErrorCommand } from "@/components/ui/command-empty"
<ErrorCommand />Recent
Recent searches while the query is blank.
Recent searches
import { RecentCommand } from "@/components/ui/command-empty"
<RecentCommand />Recent searches
Recent searches
Recent searches
Recent searches
Suggest
Suggestion chips when nothing matches.
import { SuggestCommand } from "@/components/ui/command-empty"
<SuggestCommand />ai2 Empty state command palettes: 5 styled variations on the token system
The ai2 Empty state command palettes are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around the non-happy states of a command palette. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion fades the state panels in and out as the query changes. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the transforms are skipped and the panels fade or swap instantly.
What is in the ai2 Empty state command palettes?
5 exports in one file: Empty, Loading, Error, Recent and Suggest. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion fades the state panels in and out as the query changes.
- Reduced-motion aware: Under prefers-reduced-motion, the transforms are skipped and the panels fade or swap instantly.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Empty state command palettes sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.