Overlays

CommandPalette

Modal search overlay for keyboard-first command launching and navigation. Filtering is consumer-owned: the `items` snippet receives `{ query }` and renders only the matching rows.

import { CommandPalette } from '@lostgradient/cinder';
overlaycommand
01

Overview

Reference usage

Modal search overlay for keyboard-first commands, destinations, and record jumpers.

CommandPalette intentionally uses a native modal <dialog> for top-layer, backdrop, focus-trap, and aria-modal semantics. See the native dialog contract. For a non-modal single-value picker, use Combobox.

The headings follow Cinder’s group label pattern.

Bottom sheet, key hints and prefixes

  • Sheet: below sheetBelow (default 640px; false keeps the centred panel) the palette opens as a bottom sheet: docked to the bottom edge with a grabber, a bordered search field with a Close button (closeLabel), and 44px rows. It rides above the on-screen keyboard, clears the bottom safe area, and its list scrolls inside the sheet. Under reduced motion it cross-fades instead of sliding. At wider widths it is the centred panel.
  • Key hints: give a CommandItem keys={['G', 'H']} and the row ends with "G then H", each key a Kbd; keys={['C']} shows the single key. The hint is text in the row, so it is part of the option's accessible name.
  • Prefixes: filtering stays with the page, which receives the query in items. parseCommandQuery(query, [{ prefix: '>', scope: 'actions' }, { prefix: '@', scope: 'agents' }]) reads a leading prefix off the query and returns { scope, term }, so typing > narrows to actions and @ to agents. Say so in the footer snippet: "> actions", "@ agents", "Enter to open".

Usage

svelte
<script lang="ts">
  import { Button } from '@lostgradient/cinder';
  import { CommandItem } from '@lostgradient/cinder';
  import { CommandPalette } from '@lostgradient/cinder';

  let open = $state(false);
  let triggerRef: HTMLElement | null = $state(null);
  let lastSelected = $state('');

  const commands = [
    { value: 'new-file', label: 'New file' },
    { value: 'open-settings', label: 'Open settings' },
    { value: 'sign-out', label: 'Sign out' },
  ];

  function select(label: string) {
    lastSelected = label;
    open = false;
  }
</script>

<div style="display: flex; flex-direction: column; gap: 1rem; align-items: flex-start;">
  <Button
    label="Open palette"
    onclick={(event: MouseEvent) => {
      triggerRef = event.currentTarget as HTMLElement;
      open = true;
    }}
  />

  {#if lastSelected}
    <p style="font-size: 0.875rem; color: var(--cinder-text-muted);">
      Selected: <strong>{lastSelected}</strong>
    </p>
  {/if}
</div>

<CommandPalette bind:open label="Basic palette" {triggerRef}>
  {#snippet items({ query })}
    {@const filteredCommands = commands.filter((command) =>
      command.label.toLowerCase().includes(query.toLowerCase()),
    )}
    {#each filteredCommands as command (command.value)}
      <CommandItem
        value={command.value}
        accessibleLabel={command.label}
        onSelect={() => select(command.label)}
      >
        {command.label}
      </CommandItem>
    {/each}
  {/snippet}

  {#snippet empty()}
    No commands found.
  {/snippet}
</CommandPalette>

Grouped Results

Command items register with the palette by DOM node, so grouped results can use valid nested list markup without changing keyboard navigation. Use a presentational outer list item, an inner ul[role="group"] with an accessible name, and CommandItem children inside that inner list.

svelte
<CommandPalette bind:open label="Command palette">
  {#snippet items()}
    <li role="presentation" class="cinder-command-group">
      <span class="cinder-command-group__label cinder-group-label">Recent files</span>
      <ul role="group" aria-label="Recent files">
        <CommandItem value="roadmap" onSelect={() => openRoadmap()}>Roadmap</CommandItem>
        <CommandItem value="settings" onSelect={() => openSettings()}>Settings</CommandItem>
      </ul>
    </li>
  {/snippet}
</CommandPalette>

Props

A backdrop click closes the palette only when the press starts and the click ends on the backdrop.

Prop Type Required Default Description
class string no — Class merged onto the palette panel.
closeLabel string no "Close" Accessible name and visible text of the Close button the sheet form shows beside the search field.
escapeHint string | false no "esc" The key hint at the end of the search field, drawn as a Kbd ("esc"), which says how to close the palette. false leaves it out. The bottom sheet shows a Close button instead and never the hint.
label string no — Accessible name for the dialog, wired via aria-label.
open boolean yes — Bindable open state. The component mutates open = false on Escape, backdrop click, or any explicit close path, then fires onClose.
placeholder string no — Placeholder rendered inside the search input.
query string no — Bindable search query. Mutated by the input's oninput handler. Exposed to the items snippet so consumers can filter. Reset to an empty string each time the palette opens.
sheetBelow string | false no "640px" Viewport width below which the palette opens as a bottom sheet instead of a centred panel: it docks to the bottom edge with a grabber, a search field with a Close button, 44px rows, and a height capped so the list scrolls inside it. It rides above the on-screen keyboard and respects the bottom safe area. A CSS length such as "640px", or false to keep the centred panel at every width.
empty (opaque) no — Rendered when zero items are registered after filtering. Not expressible in JSON Schema; see the component types for the signature.
footer (opaque) no — Optional footer, e.g. for keybinding hints. Not part of the listbox. Not expressible in JSON Schema; see the component types for the signature.
items (opaque) yes — Receives the current query so consumers can filter. Not expressible in JSON Schema; see the component types for the signature.
onClose (opaque) no — Fired after any close path routed through the palette close lifecycle. Not expressible in JSON Schema; see the component types for the signature.
triggerRef (opaque) no — Element to restore focus to on close. Falls back to captureFocus(). Not expressible in JSON Schema; see the component types for the signature.
Live preview

Basic command palette

02

When to use

Use when
  • Exposing a global keyboard-first launcher for power-user actions and navigation.
  • Letting users search across heterogeneous results such as commands, pages, and records in one input.
Avoid when
  • Picking a single value bound to a form field — use combobox instead.
  • Showing a contextual action menu attached to a trigger — use dropdown instead.
03

Examples

Basic command palette

A minimal palette with three items opened by a button.

Grouped command palette

Command items grouped with named listbox sections.

Key hints and prefixes

Navigation rows end with a key sequence read as "G then H"; a single key shows alone. Typing > narrows the list to actions and @ to agents. Below 640px the palette opens as a bottom sheet.

Search, recent, and keyed actions

Cmd+K opens the palette. Three sections: filtered search results, recent items, and keyed actions separated by visual group headers.

04

Props

Props for command-palette
Name
Type
Default
Description
open bindable boolean false Bindable open state. The component mutates open = false on Escape, backdrop click, or any explicit close path, then fires onClose.
placeholder text 'Search…' Placeholder rendered inside the search input.
label text 'Command palette' Accessible name for the dialog, wired via aria-label.
query bindable text '' Bindable search query. Mutated by the input's oninput handler. Exposed to the items snippet so consumers can filter. Reset to an empty string each time the palette opens.
onClose () => void Fired after any close path routed through the palette close lifecycle.
triggerRef HTMLElement | null null Element to restore focus to on close. Falls back to captureFocus().
items required snippet Receives the current query so consumers can filter.
empty snippet Rendered when zero items are registered after filtering.
footer snippet Optional footer, e.g. for keybinding hints. Not part of the listbox.
sheetBelow text '640px' Viewport width below which the palette opens as a bottom sheet instead of a centred panel: it docks to the bottom edge with a grabber, a search field with a Close button, 44px rows, and a height capped so the list scrolls inside it. It rides above the on-screen keyboard and respects the bottom safe area. A CSS length such as "640px", or false to keep the centred panel at every width.
closeLabel text 'Close' Accessible name and visible text of the Close button the sheet form shows beside the search field.
escapeHint text 'esc' The key hint at the end of the search field, drawn as a Kbd ("esc"), which says how to close the palette. false leaves it out. The bottom sheet shows a Close button instead and never the hint.