Navigation
CommandPalette
Search every action from one place (⌘K). A modal dialog with a combobox: type to filter, arrows to choose, Enter to run.
import { CommandPalette } from '@reactloom/ui';Examples
App commands
Groups, shortcuts hints and disabled commands.
Or press Shift + P with the shortcut prop.
import { Button, CommandPalette, Kbd, Text, type CommandItem } from '@reactloom/ui';
import { useState } from 'react';
const commands: CommandItem[] = [
{ id: 'invoice', label: 'New invoice', group: 'Create', shortcut: 'N' },
{ id: 'customer', label: 'New customer', group: 'Create' },
{ id: 'reports', label: 'Go to reports', group: 'Navigate', keywords: ['analytics', 'charts'] },
{ id: 'settings', label: 'Go to settings', group: 'Navigate' },
{ id: 'theme', label: 'Switch theme', group: 'Preferences', description: 'Light, dark or high contrast' },
{ id: 'export', label: 'Export all data', group: 'Preferences', disabled: true, description: 'Requires the admin role' },
];
export default function Example() {
const [open, setOpen] = useState(false);
const [last, setLast] = useState('');
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>
Search commands
</Button>
<Text size="sm" role="status">
{last && `Ran: ${last}`}
</Text>
<CommandPalette open={open} onOpenChange={setOpen} commands={commands} onSelect={(command) => setLast(command.label)} />
<Text size="sm" tone="muted">
Or press <Kbd>Shift</Kbd> + <Kbd>P</Kbd> with the shortcut prop.
</Text>
</>
);
}Server search
Control the query and supply results; filter returns 1 to keep them all.
import { Button, CommandPalette, type CommandItem } from '@reactloom/ui';
import { useEffect, useState } from 'react';
const people = ['Ada Lovelace', 'Alan Turing', 'Grace Hopper', 'Katherine Johnson', 'Margaret Hamilton', 'Tim Berners-Lee'];
// Server search: control the query, load matching commands, and keep every result (filter returns 1).
export default function Example() {
const [open, setOpen] = useState(false);
const [query, setQuery] = useState('');
const [results, setResults] = useState<CommandItem[]>([]);
useEffect(() => {
const timer = setTimeout(() => {
const q = query.toLowerCase();
setResults(people.filter((p) => p.toLowerCase().includes(q)).map((p) => ({ id: p, label: p, group: 'People' })));
}, 150);
return () => clearTimeout(timer);
}, [query]);
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>
Find a person
</Button>
<CommandPalette
aria-label="Find a person"
open={open}
onOpenChange={setOpen}
query={query}
onQueryChange={setQuery}
commands={results}
filter={() => 1}
onSelect={() => {}}
messages={{ placeholder: 'Search people…' }}
/>
</>
);
}API reference
| Prop | Type | Default |
|---|---|---|
commandsrequired{ id, label, group?, description?, icon?, shortcut?, keywords?, disabled? } | readonly CommandItem[] | — |
onSelectrequiredRuns the command; return false to keep the palette open. | (command) => unknown | — |
open / defaultOpen / onOpenChange | boolean | — |
shortcutOpt-in global shortcut, e.g. 'mod+k'. | string | — |
query / onQueryChangeControl the search text. | string | — |
filterScore, or 0 to hide. | (command, query) => number | — |
aria-label | string | 'Command palette' |
messagesPlaceholder, empty and result-count texts. | Partial<CommandPaletteMessages> | — |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-active] | present when on |
[data-disabled] | present when on |
Theme tokens it reads
--pl-color-accent-solid --pl-color-accent-subtle --pl-color-border-default --pl-color-fg-default --pl-color-fg-disabled --pl-color-fg-muted --pl-focus-ring-color --pl-font-size-lg --pl-font-size-sm --pl-font-size-xs --pl-radius-md
Accessibility
- The search field is a combobox with aria-activedescendant; the list is a listbox grouped by heading.
- The number of results is announced as you type.
- Focus returns to where it was when the palette closes.
| Key | Action |
|---|---|
| ↑ / ↓ | Move through results |
| Page Up / Page Down | Move by eight |
| Enter | Run the highlighted command |
| Escape | Close |
Usage guidelines
Do
- Add keywords (synonyms) so people find commands by their own words.
- Keep the same actions available in menus and buttons.
Don’t
- Don’t make the palette the only way to reach an action.