Command bar

Slant / Controls

A searchable command palette with keyboard navigation.

Demo

Command bar

Usage

@tuttn/slant
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

PropTypeDefaultDescription
commandsCommand[]—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.
valuestring—The query in the search box. Pass it to control the query; leave it out and the bar keeps its own.
defaultValuestring''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.
labelstring'Search commands'The search box's accessible name. From the root's `messages.searchCommands`.
placeholderstring'Search commands…'Shown in the empty search box. From the root's `messages.searchCommandsPlaceholder`.
shortcutReactNode'⌘K'The key hint after the search box; pass null to hide it.
statusReactNode'↑ ↓ to move · Enter to run'The live status line under the list. From the root's `messages.commandHint`.
emptyText(query: string) => ReactNodequery => `No commands match “${query}”`What the list shows when nothing matches. From the root's `messages.noCommands`.
autoFocusbooleanfalseFocuses the search box on mount.
…restHTMLAttributes<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.

Full source