Skip to content

Overlay

CommandPalette

Searchable command palette for quick actions and navigation.

import { CommandPalette } from '@var-ui/react';

CommandPalette

⌘K-style searchable command overlay. Give each item an onSelect, or handle everything in a single onAction(id); the palette closes itself afterwards unless closeOnAction={false}. You own open / onOpenChange, or hand the palette a controller from createCommandPalette() / useCommandPalette() to open it from outside React. Items match on title, meta, and optional keywords. The palette is a native modal <dialog> (showModal()): it renders in place, sits in the top layer, and enter/exit is pure CSS on the <dialog> itself ([open], @starting-style), so style it with .var-ui-command-palette[open]. Its contents stay mounted until the exit animation ends. Escape and a backdrop click are handled by the platform and reported through onOpenChange. onOpenChange(open, details) receives details.reason (escape-key, outside-press for a backdrop click, trigger-press for choosing an item, imperative for the hotkey) and details.cancel() to veto the change; defaultOpen starts it open when uncontrolled.

Examples

Default

Open from a button (demos disable the global hotkey so pages stay calm).

Demo.tsxTSX
import { CommandPalette } from '@var-ui/react';

<CommandPalette
  open={open}
  onOpenChange={setOpen}
  items={[{ id: 'docs', title: 'Docs', meta: 'Guides', onSelect: () => navigate('/docs') }]}
/>

Grouped results via meta

Use meta (and keywords) so related commands are scannable and searchable even when titles are short. Set group on items to render section headings. Only consecutive items sharing a group are merged, so items for one group scattered through items produce repeated headings; sort or group items yourself first. Arrow-key navigation continues through all items in order.

Demo.tsxTSX
import { Button, CommandPalette } from '@var-ui/react';
import { useState } from 'react';

const [open, setOpen] = useState(false);

<Button onClick={() => setOpen(true)}>
  Open palette
</Button>
<CommandPalette
  open={open}
  onOpenChange={setOpen}
  hotkey={false}
  items={[
    { id: 'new-file', title: 'New file', meta: 'File', keywords: ['create'] },
    { id: 'theme', title: 'Toggle theme', meta: 'Appearance', keywords: ['dark'] },
    { id: 'settings', title: 'Open settings', meta: 'Navigation' },
  ]}
/>

Icons, shortcut hints and highlighting

icon (an icon name such as page or component, shown in a small type tile) and shortcut decorate each row (the shortcut is display-only), and meta is a muted one-line subtitle. Matching text is highlighted by default (highlightMatches={false} turns it off). Set recentKey to remember chosen items in localStorage and list them under “Recent” while the query is empty. A controller lets any code open the palette. hotkey accepts a combo ('mod+k', 'ctrl+shift+p') or an array of combos.

Last command: Nothing yet

Demo.tsxTSX
import { CommandPalette, createCommandPalette } from '@var-ui/react';

// Drive the palette from anywhere (event handlers, route loaders, other modules).
export const palette = createCommandPalette();
// palette.open(); palette.toggle();

<CommandPalette
  controller={palette}
  hotkey={['mod+k', 'mod+shift+p']}
  highlightMatches
  items={[
    {
      id: 'save',
      title: 'Save',
      group: 'File',
      icon: <SaveIcon />,
      shortcut: ['⌘', 'S'],
      onSelect: save,
    },
  ]}
/>

Async results

Pass filter={false} when items are already the results for the query, listen with onQueryChange, and show loading while fetching.

Demo.tsxTSX
<CommandPalette
  open={open}
  onOpenChange={setOpen}
  filter={false}            // items already are the server's results
  highlightMatches
  loading={loading}
  onQueryChange={setQuery}  // debounce + fetch, then setItems
  items={items}
/>

Props

Accessibility

When open, focus moves to the search field and returns to the previously focused element on close (native <dialog> behavior); arrow keys move through results, Enter activates, Escape closes. Provide clear title text — meta is secondary. The default hotkey is ⌘K / Ctrl+K (hotkey); disable it in embedded demos if it would fight the host page.