Command Palette
Every page and action a search away, opened with Ctrl K.
Installation
pnpm dlx @human-kit/design add command-palettenpx @human-kit/design add command-paletteyarn dlx @human-kit/design add command-palettebunx @human-kit/design add command-paletteThe command copies the files to src/lib/components/ui/command-palette. It also
installs the packages that the files import.
Usage
<script lang="ts">
import { goto } from '$app/navigation';
import * as CommandPalette from '$lib/components/ui/command-palette';
import { type CommandPaletteItem } from '$lib/components/ui/command-palette';
const items: CommandPaletteItem[] = [
{ id: 'calendar', label: 'Open the calendar', onSelect: () => goto('/calendar') },
{ id: 'patients', label: 'Find a patient', keywords: 'search record', onSelect: () => goto('/patients') }
];
</script>
<CommandPalette.Root {items} /> Examples
A long list
With virtualized, the palette draws only the rows near its viewport, for thousands of commands: one per patient record. The search, the arrows and Enter still reach every row.
Guidelines
- Mount one palette for the whole app, in the root layout. It listens for Ctrl K, or ⌘ K on a Mac, on the window, and two would both open.
- Put in it the pages and the actions people reach for often. It is a shortcut, not the only way: each command should also live somewhere on the screen.
- Start each
labelwith a verb, "Open the calendar", so a row says what it does. - Add
keywordswith the words people use for a command when they are not in its label: "appointment" for "New visit". shortcutonly shows the keys at the end of the row. Bind those keys yourself: the palette does not listen for them.- Set
hotkey={false}where Ctrl K already means something, such as inside a text editor.
Accessibility
- It opens as a modal dialog named by
label: the focus stays in it, and the page behind is hidden from a screen reader. - The search takes the focus when it opens, and the focus goes back to where it was when it closes, also when the palette was opened by its keys.
- Up and Down move through the rows that match, Enter runs one, and Escape closes the palette at once instead of first clearing the text.
- The search and the list are both named by
label. When nothing matches, the list saysemptyText. - Icons and shortcuts are hidden from assistive technology: the label of each row says it all.
API reference
CommandPalette.Root
| Prop | Type | Default |
|---|---|---|
items | CommandPaletteItem[] The commands, in the order they are listed. Required. | — |
open | boolean Whether the palette shows. Bindable, to open it from a button. | false |
hotkey | boolean Opens and closes it with Ctrl K, or ⌘ K on a Mac, from anywhere on the page. | true |
label | string The name of the dialog and of the search, read aloud. | 'Command palette' |
placeholder | string What the empty search shows. | 'Search for a page or an action…' |
emptyText | string What the list says when the search matches nothing. | 'Nothing matches' |
hints | boolean The keys at the foot, with what each does. | true |
hintLabels | { open: string; move: string; close: string } The words beside the keys, for a page in another language. | { open: 'Open', move: 'Move', close: 'Close' } |
It takes only these props: class styles the panel.
CommandPaletteItem
| Prop | Type | Default |
|---|---|---|
id | string A key unique among the items. Required. | — |
label | string What the row says and what the search matches. Required. | — |
onSelect | () => void What the command does. Required. | — |
icon | Component The icon at the start of the row. | — |
shortcut | string[] The keys shown at the end of the row, as | — |
keywords | string More words the search finds it by. | — |
The type of each entry of items. The palette closes and clears its search before it calls onSelect.