Command bar
Slant / Controls
A searchable command palette with keyboard navigation.
Demo
Usage
import { Card, CommandBar, Slant, type Command } from '@tuttn/slant'
const commands: Command[] = [
{ id: 'new', name: 'New task', hint: 'T', keywords: ['create'] },
{ id: 'save', name: 'Save checkpoint', hint: 'S' },
// Listed but can't run: the arrows reach it and its reason shows under it.
{ id: 'deploy', name: 'Deploy preview', disabled: true, reason: 'Commit or stash your changes first' },
]
// Uncontrolled: it keeps its own query, which clears after a command runs.
// It fills its container edge to edge: put it in a Card with padding={false}.
<Card padding={false}>
<CommandBar commands={commands} onRun={command => run(command.id)} />
</Card>
// Controlled: own the query to prefill it, keep it, or clear it from outside.
const [query, setQuery] = useState('')
<CommandBar commands={commands} value={query} onValueChange={setQuery} onRun={command => run(command.id)} />
// The built-in strings come from the root's messages; a prop still wins.
<Slant messages={{ searchCommands: 'Rechercher', noCommands: q => `Aucune commande « ${q} »` }}>
<CommandBar commands={commands} onRun={command => run(command.id)} />
</Slant>Props
| Prop | Type | Default | Description |
|---|---|---|---|
commands | Command[] | — | The commands to filter. The query matches each one's name and keywords, ignoring case. |
onRun | (command: Command) => void | — | Called with the command run by Enter or a click; the query then clears. Never called for a disabled command. |
value | string | — | The query in the search box. Pass it to control the query; leave it out and the bar keeps its own. |
defaultValue | string | '' | The query the search box starts with when uncontrolled. |
onValueChange | (value: string) => void | — | Called with the new query as the user types, and with `''` after a command runs. |
label | string | 'Search commands' | The search box's accessible name. From the root's `messages.searchCommands`. |
placeholder | string | 'Search commands…' | Shown in the empty search box. From the root's `messages.searchCommandsPlaceholder`. |
shortcut | ReactNode | '⌘K' | The key hint after the search box; pass null to hide it. |
status | ReactNode | '↑ ↓ to move · Enter to run' | The live status line under the list. From the root's `messages.commandHint`. |
emptyText | (query: string) => ReactNode | query => `No commands match “${query}”` | What the list shows when nothing matches. From the root's `messages.noCommands`. |
autoFocus | boolean | false | Focuses the search box on mount. |
…rest | HTMLAttributes<HTMLDivElement> | — | The ref, className, style and other attributes go on the root div. |
A Command is { id, name, hint?, keywords?, disabled?, reason? }. The query matches the name and keywords, ignoring case. The id may hold any characters: the option elements get ids made safe from it, so aria-activedescendant always resolves.
Disabled commands
A disabled command stays in the list, muted and marked aria-disabled. Unlike a disabled option in a Select, the arrows and the pointer still move onto it, so a reader can find out why it can’t run: its reason shows as a line under its name and is the option’s accessible description, read once (it is kept out of the name). Enter and a click on it run nothing and keep the query, and Enter still doesn’t reach a form around the bar.