Grouped Search Results
Results grouped by section with visible breadcrumb paths and <mark>-highlighted matches — users recognise where a result lives faster than they read its snippet.
The ⌘K switcher done properly: focus stays in the input while aria-activedescendant moves, arrows wrap, and the highlight resets on every query change.
components/command-palette.tsxnpx hoverlab add command-palette
Or over MCP, from your editor's agent — no account needed.
Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.
Start a page with this section — add more, order them, and leave with the page source.
Actions
Navigation
Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.
'use client'
/**
* <CommandPalette> — the ⌘K quick-switcher.
*
* Deceptively hard, which is why it is worth lifting whole. What is handled
* here and usually is not:
*
* - Roving focus stays in the input while the *highlight* moves through the
* list. Moving real DOM focus to each item breaks typing, so the input
* keeps focus and `aria-activedescendant` points at the highlighted row —
* the combobox pattern.
* - Arrow keys wrap at both ends, and the highlight resets to the first
* result whenever the query changes. Leaving it at index 7 after the list
* shrinks to two results is how palettes select the wrong thing.
* - The highlighted item is scrolled into view with `block: 'nearest'`, so
* keyboard travel does not jump the list around.
* - ⌘K / Ctrl-K toggles, Escape closes, and the shortcut calls
* `preventDefault` — otherwise Firefox steals it for its search bar.
*
* Rendered inline rather than in a portal so it previews in place; move it
* into a portal with a focus trap when you drop it into a real app.
*/
import * as React from 'react'
import {
Search,
FileText,
Settings,
Users,
CreditCard,
Plus,
CornerDownLeft,
ArrowUp,
ArrowDown,
} from 'lucide-react'
export interface CommandItem {
id: string
label: string
group: string
icon?: React.ReactNode
shortcut?: string
onSelect?: () => void
}
export interface CommandPaletteProps {
items?: CommandItem[]
placeholder?: string
/** Start open — the default, so the block previews as something visible. */
defaultOpen?: boolean
/**
* Render as a demo inside a larger page. Drops the document-level
* Ctrl/Cmd-K and Escape binding, which a preview has no business owning —
* on a page of previews it would fight the host app for the same keys —
* and the search field's `autoFocus`, which would scroll the browser to
* this card on load.
*/
embedded?: boolean
className?: string
}
const DEFAULT_ITEMS: CommandItem[] = [
{ id: 'new-project', label: 'Create new project', group: 'Actions', icon: <Plus className="h-4 w-4" />, shortcut: 'N' },
{ id: 'invite', label: 'Invite a teammate', group: 'Actions', icon: <Users className="h-4 w-4" /> },
{ id: 'docs', label: 'Search documentation', group: 'Navigation', icon: <FileText className="h-4 w-4" /> },
{ id: 'billing', label: 'Go to billing', group: 'Navigation', icon: <CreditCard className="h-4 w-4" /> },
{ id: 'settings', label: 'Open settings', group: 'Navigation', icon: <Settings className="h-4 w-4" />, shortcut: ',' },
{ id: 'members', label: 'Manage members', group: 'Navigation', icon: <Users className="h-4 w-4" /> },
]
export function CommandPalette({
items = DEFAULT_ITEMS,
placeholder = 'Type a command or search',
defaultOpen = true,
embedded = false,
className = '',
}: CommandPaletteProps) {
// Per-instance ids. A literal id in a reusable component is a
// collision waiting for the second copy on the page — and a <label>
// then resolves to whichever input rendered first.
const uid = React.useId()
const [open, setOpen] = React.useState(defaultOpen)
const [query, setQuery] = React.useState('')
const [highlight, setHighlight] = React.useState(0)
const listRef = React.useRef<HTMLDivElement>(null)
const results = React.useMemo(() => {
const term = query.trim().toLowerCase()
if (!term) return items
return items.filter((item) => item.label.toLowerCase().includes(term))
}, [items, query])
// A stale highlight after the list shrinks selects the wrong row.
React.useEffect(() => {
setHighlight(0)
}, [query])
// Keep the highlighted row visible without yanking the list around.
//
// Skipped on the first commit. `scrollIntoView` walks up *every* scrollable
// ancestor, the document included, so running it on mount scrolls the whole
// page to wherever this palette happens to sit. On mount the highlight is
// row 0, which is already in view, so there was never anything to correct.
const settled = React.useRef(false)
React.useEffect(() => {
if (!settled.current) {
settled.current = true
return
}
const node = listRef.current?.querySelector<HTMLElement>('[data-highlighted="true"]')
node?.scrollIntoView({ block: 'nearest' })
}, [highlight])
React.useEffect(() => {
if (embedded) return
function onKey(event: KeyboardEvent) {
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'k') {
// Firefox binds Ctrl-K to its search bar unless we take it.
event.preventDefault()
setOpen((v) => !v)
} else if (event.key === 'Escape') {
setOpen(false)
}
}
document.addEventListener('keydown', onKey)
return () => document.removeEventListener('keydown', onKey)
}, [embedded])
function handleKeyDown(event: React.KeyboardEvent) {
if (results.length === 0) return
if (event.key === 'ArrowDown') {
event.preventDefault()
setHighlight((i) => (i + 1) % results.length)
} else if (event.key === 'ArrowUp') {
event.preventDefault()
setHighlight((i) => (i - 1 + results.length) % results.length)
} else if (event.key === 'Enter') {
event.preventDefault()
const item = results[highlight]
item?.onSelect?.()
setOpen(false)
}
}
// Preserve authored group order.
const grouped = React.useMemo(() => {
const map = new Map<string, CommandItem[]>()
for (const item of results) {
const list = map.get(item.group)
if (list) list.push(item)
else map.set(item.group, [item])
}
return [...map.entries()]
}, [results])
if (!open) {
return (
<div className={`flex items-center justify-center p-10 ${className}`}>
<button
type="button"
onClick={() => setOpen(true)}
className="inline-flex items-center gap-2 rounded-xl border border-border/60 bg-background px-4 py-2 text-sm text-muted-foreground transition-colors hover:bg-muted"
>
<Search aria-hidden className="h-4 w-4" />
Search
<kbd className="ms-2 rounded border border-border/60 bg-muted px-1.5 py-0.5 font-mono text-xs">
⌘K
</kbd>
</button>
</div>
)
}
let flatIndex = -1
return (
<div className={`flex justify-center p-6 ${className}`}>
<div className="w-full max-w-lg overflow-y-hidden rounded-2xl border border-border/60 bg-card shadow-2xl">
<div className="flex items-center gap-3 border-b border-border/60 px-4">
<Search aria-hidden className="h-4 w-4 shrink-0 text-muted-foreground" />
<input
autoFocus={!embedded}
type="text"
role="combobox"
aria-expanded="true"
aria-controls={`${uid}-command-list`}
aria-activedescendant={
results[highlight] ? `command-item-${results[highlight].id}` : undefined
}
aria-label={placeholder}
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={placeholder}
className="min-w-0 flex-1 bg-transparent py-3.5 text-sm outline-none placeholder:text-muted-foreground border border-transparent"
/>
<kbd className="shrink-0 rounded border border-border/60 bg-muted px-1.5 py-0.5 font-mono text-xs text-muted-foreground">
esc
</kbd>
</div>
<div
ref={listRef}
id={`${uid}-command-list`}
role="listbox"
aria-label="Commands"
className="max-h-72 overflow-y-auto p-2"
>
{results.length === 0 ? (
<p className="px-3 py-8 text-center text-sm text-muted-foreground">
No commands match “{query}”
</p>
) : (
grouped.map(([group, groupItems]) => (
<div key={group} className="mb-1 last:mb-0">
<p className="px-2 py-1.5 text-xs font-bold uppercase tracking-wider text-muted-foreground">
{group}
</p>
{groupItems.map((item) => {
flatIndex += 1
const active = flatIndex === highlight
return (
<button
key={item.id}
id={`${uid}-command-item-${item.id}`}
type="button"
role="option"
aria-selected={active}
data-highlighted={active}
onMouseEnter={() => setHighlight(results.indexOf(item))}
onClick={() => {
item.onSelect?.()
setOpen(false)
}}
className={`flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors ${
active ? 'bg-primary/10 text-foreground' : 'text-muted-foreground'
}`}
>
<span className={active ? 'text-primary' : ''}>{item.icon}</span>
<span className="flex-1 truncate">{item.label}</span>
{item.shortcut ? (
<kbd className="rounded border border-border/60 bg-muted px-1.5 py-0.5 font-mono text-xs">
{item.shortcut}
</kbd>
) : null}
</button>
)
})}
</div>
))
)}
</div>
<div className="flex items-center gap-4 border-t border-border/60 px-4 py-2 text-xs text-muted-foreground">
<span className="inline-flex items-center gap-1">
<ArrowUp aria-hidden className="h-3 w-3" />
<ArrowDown aria-hidden className="h-3 w-3" />
navigate
</span>
<span className="inline-flex items-center gap-1">
<CornerDownLeft aria-hidden className="h-3 w-3" />
select
</span>
<span className="ms-auto">
{results.length} {results.length === 1 ? 'result' : 'results'}
</span>
</div>
</div>
</div>
)
}
bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.Drop it at components/command-palette.tsx and import it where you need the section:
import { CommandPalette } from '@/components/command-palette'3 of this block’s props are simple enough to drive from here. Change them and the block below re-renders — it is the same component whose source is above, not a mock of it. Everything else it accepts is in the table underneath.
Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.
| Prop | Type | Default |
|---|---|---|
items | CommandItem[] | DEFAULT_ITEMS |
placeholder | string | 'Type a command or search' |
defaultOpenStart open — the default, so the block previews as something visible. | boolean | true |
embeddedRender as a demo inside a larger page. Drops the document-level Ctrl/Cmd-K and Escape binding, which a preview has no business owning — on a page of previews it would fight the host app for the same keys — and the search field's `autoFocus`, which would scroll the browser to this card on load. | boolean | false |
className | string | '' |
The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.
This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.
<!--
Command Palette — markup from the Hoverlab catalog.
This is the block rendered once to HTML and wrapped as a component
file. It is not a port of the React source: the Tailwind classes carry
the design, which is the part that took the work, and they are the same
in every framework.
This block is interactive in React and the handlers are NOT here.
Buttons, toggles and menus render in their initial state and do
nothing until you wire them up.
-->
<div class="flex justify-center p-6 ">
<div class="w-full max-w-lg overflow-y-hidden rounded-2xl border border-border/60 bg-card shadow-2xl">
<div class="flex items-center gap-3 border-b border-border/60 px-4">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-search h-4 w-4 shrink-0 text-muted-foreground" aria-hidden="true">
<path d="m21 21-4.34-4.34"></path>
<circle cx="11" cy="11" r="8"></circle>
</svg>
<input autofocus="" type="text" role="combobox" aria-expanded="true" aria-controls="_R_0_-command-list" aria-activedescendant="command-item-new-project" aria-label="Type a command or search" placeholder="Type a command or search" class="min-w-0 flex-1 bg-transparent py-3.5 text-sm outline-none placeholder:text-muted-foreground border border-transparent" value="" />
<kbd class="shrink-0 rounded border border-border/60 bg-muted px-1.5 py-0.5 font-mono text-xs text-muted-foreground">esc</kbd>
</div>
<div id="_R_0_-command-list" role="listbox" aria-label="Commands" class="max-h-72 overflow-y-auto p-2">
<div class="mb-1 last:mb-0">
<p class="px-2 py-1.5 text-xs font-bold uppercase tracking-wider text-muted-foreground">Actions</p>
<button id="_R_0_-command-item-new-project" type="button" role="option" aria-selected="true" data-highlighted="true" class="flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors bg-primary/10 text-foreground">
<span class="text-primary">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-plus h-4 w-4" aria-hidden="true">
<path d="M5 12h14"></path>
<path d="M12 5v14"></path>
</svg>
</span>
<span class="flex-1 truncate">Create new project</span>
<kbd class="rounded border border-border/60 bg-muted px-1.5 py-0.5 font-mono text-xs">N</kbd>
</button>
<button id="_R_0_-command-item-invite" type="button" role="option" aria-selected="false" data-highlighted="false" class="flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors text-muted-foreground">
<span class="">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-users h-4 w-4" aria-hidden="true">
<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"></path>
<path d="M16 3.128a4 4 0 0 1 0 7.744"></path>
<path d="M22 21v-2a4 4 0 0 0-3-3.87"></path>
<circle cx="9" cy="7" r="4"></circle>
</svg>
</span>
<span class="flex-1 truncate">Invite a teammate</span>
</button>
</div>
<div class="mb-1 last:mb-0">
<p class="px-2 py-1.5 text-xs font-bold uppercase tracking-wider text-muted-foreground">Navigation</p>
<button id="_R_0_-command-item-docs" type="button" role="option" aria-selected="false" data-highlighted="false" class="flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors text-muted-foreground">
<span class="">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-file-text h-4 w-4" aria-hidden="true">
<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"></path>
<path d="M14 2v4a2 2 0 0 0 2 2h4"></path>
<path d="M10 9H8"></path>
<path d="M16 13H8"></path>
<path d="M16 17H8"></path>
</svg>
</span>
<span class="flex-1 truncate">Search documentation</span>
</button>
<button id="_R_0_-command-item-billing" type="button" role="option" aria-selected="false" data-highlighted="false" class="flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors text-muted-foreground">
<span class="">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-credit-card h-4 w-4" aria-hidden="true">
<rect width="20" height="14" x="2" y="5" rx="2"></rect>
<line x1="2" x2="22" y1="10" y2="10"></line>
</svg>
</span>
<span class="flex-1 truncate">Go to billing</span>
</button>
<button id="_R_0_-command-item-settings" type="button" role="option" aria-selected="false" data-highlighted="false" class="flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors text-muted-foreground">
<span class="">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-settings h-4 w-4" aria-hidden="true">
<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"></path>
<circle cx="12" cy="12" r="3"></circle>
</svg>
</span>
<span class="flex-1 truncate">Open settings</span>
<kbd class="rounded border border-border/60 bg-muted px-1.5 py-0.5 font-mono text-xs">,</kbd>
</button>
<button id="_R_0_-command-item-members" type="button" role="option" aria-selected="false" data-highlighted="false" class="flex w-full items-center gap-3 rounded-xl px-3 py-2 text-start text-sm transition-colors text-muted-foreground">
<span class="">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-users h-4 w-4" aria-hidden="true">
<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"></path>
<path d="M16 3.128a4 4 0 0 1 0 7.744"></path>
<path d="M22 21v-2a4 4 0 0 0-3-3.87"></path>
<circle cx="9" cy="7" r="4"></circle>
</svg>
</span>
<span class="flex-1 truncate">Manage members</span>
</button>
</div>
</div>
<div class="flex items-center gap-4 border-t border-border/60 px-4 py-2 text-xs text-muted-foreground">
<span class="inline-flex items-center gap-1"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-up h-3 w-3" aria-hidden="true"><path d="m5 12 7-7 7 7"></path><path d="M12 19V5"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-down h-3 w-3" aria-hidden="true"><path d="M12 5v14"></path><path d="m19 12-7 7-7-7"></path></svg>navigate</span>
<span class="inline-flex items-center gap-1"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-corner-down-left h-3 w-3" aria-hidden="true"><path d="M20 4v7a4 4 0 0 1-4 4H4"></path><path d="m9 10-5 5 5 5"></path></svg>select</span>
<span class="ms-auto">6 results</span>
</div>
</div>
</div>
What each framework gets across the whole catalog — effects convert properly; this rung is markup.
The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.
Want the whole screen instead of this one section? Open a page and copy it entire.
All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.
Browse Command & SearchCopying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.
Results grouped by section with visible breadcrumb paths and <mark>-highlighted matches — users recognise where a result lives faster than they read its snippet.
The "?" overlay, with chords written once in one notation and rendered per platform — mod becomes Command on Apple and Ctrl everywhere else, decided after mount so the markup never mismatches.
The strip above a result set that answers "why am I only seeing four things" — every active constraint as a removable chip, with the excluded count beside it.
The field in the header, not the palette over it: Enter searches for what you typed unless you arrowed to a suggestion, late responses are dropped by sequence number, and recent searches are removable one at a time.
Faceted search with result counts on every option, zero-count options kept visible and disabled so the list stays stable, and a note explaining why the counts do not sum.
One query counted across every scope it could have matched, as a real tablist with arrow-key movement — so an answer that is nowhere is visible without opening four tabs.
The empty state of a search box, filled with the searches that are worth repeating and the ones worth saving.
The whole faceted search assembled - rail, chips, live count and sort - with the intersection actually applied and relevance offered only when there is a query.
The facet that is a number: a distribution over a dual-thumb slider built from two real range inputs, with typed bounds clamped on blur rather than per keystroke.