Skip to the content
human-kit design
30 — Overlays

Command Palette

Every page and action a search away, opened with Ctrl K.

or press CtrlK

Installation

pnpm dlx @human-kit/design add command-palette
npx @human-kit/design add command-palette
yarn dlx @human-kit/design add command-palette
bunx @human-kit/design add command-palette

The 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 label with a verb, "Open the calendar", so a row says what it does.
  • Add keywords with the words people use for a command when they are not in its label: "appointment" for "New visit".
  • shortcut only 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 says emptyText.
  • Icons and shortcuts are hidden from assistive technology: the label of each row says it all.

API reference

CommandPalette.Root

PropTypeDefault
itemsCommandPaletteItem[]

The commands, in the order they are listed. Required.

—
openboolean

Whether the palette shows. Bindable, to open it from a button.

false
hotkeyboolean

Opens and closes it with Ctrl K, or ⌘ K on a Mac, from anywhere on the page.

true
labelstring

The name of the dialog and of the search, read aloud.

'Command palette'
placeholderstring

What the empty search shows.

'Search for a page or an action…'
emptyTextstring

What the list says when the search matches nothing.

'Nothing matches'
hintsboolean

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

PropTypeDefault
idstring

A key unique among the items. Required.

—
labelstring

What the row says and what the search matches. Required.

—
onSelect() => void

What the command does. Required.

—
iconComponent

The icon at the start of the row.

—
shortcutstring[]

The keys shown at the end of the row, as ['Ctrl', 'N'].

—
keywordsstring

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.