---
title: "Command"
description: "Command component - searchable command palette with keyboard navigation."
slug: "command"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeCard, TableCard, ApiSection } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="command" title="Command" component="command">
  A searchable command palette for quick actions. Built on native <code>&lt;dialog&gt;</code> with search filtering. Open with <kbd>Cmd+K</kbd>.
</PageHeader>

<SkillPanel component="command">
  <SkillField label="Native basis">
<SkillText><code>&lt;dialog&gt;</code> + search input for a command palette.</SkillText>
  </SkillField>

  <SkillField label="Web Platform APIs">
<SkillRow>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog" title="modal with focus trap"><code>&lt;dialog&gt;</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style" title="entry animation"><code>@starting-style</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/::backdrop" title="scrim overlay"><code>::backdrop</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible" title="keyboard-only focus ring"><code>:focus-visible</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion" title="suppresses animations"><code>prefers-reduced-motion</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors" title="maps to system colors"><code>forced-colors</code></ApiPill>
  </SkillRow>
  </SkillField>

  <SkillField label="Classes">
<SkillRow>
    <code>.command</code>
    <code>.command-input-wrapper</code>
    <code>.command-input</code>
    <code>.command-list</code>
    <code>.command-group</code>
    <code>.command-group-heading</code>
    <code>.command-item</code>
    <code>.command-separator</code>
    <code>.command-shortcut</code>
    <code>.command-empty</code>
  </SkillRow>
  </SkillField>

  <SkillField label="Data attributes">
<SkillText>• <code>data-command-trigger</code></SkillText>
<SkillText>• <code>data-filter="none"</code> on the dialog - the page writes the items (a search of its own)</SkillText>
  </SkillField>

  <SkillField label="State attributes (managed by JS)">
<SkillText>• <code>data-highlighted</code></SkillText>
  </SkillField>

  <SkillField label="Wiring conventions">
<SkillText dense>• <code>data-command-trigger</code> on any element - opens the component</SkillText>
  </SkillField>

  <SkillField label="Notes">
<p class="text-muted-foreground m-0">• With <code>data-filter="none"</code> the component filters nothing: the page writes the items it ranked (the docs search does), and the component keeps groups, the empty state, the highlight and the keys following them.</p>
<p class="text-muted-foreground m-0">• While the palette is modal, <code>html:has(dialog.command:modal)</code> sets <code>overflow: hidden</code> + <code>scrollbar-gutter: stable</code> - the page behind cannot scroll and its position is preserved for when the palette closes (no JS scroll-lock).</p>
  </SkillField>
</SkillPanel>

```html example label="Default" hint="Click the button or press ⌘K to open. Use Arrow keys to navigate, Enter to select, Escape to close." height="28"
<button class="btn" data-variant="outline" data-command-trigger="demo-cmd">Open Command Menu</button>
<dialog id="demo-cmd" class="command" role="dialog" aria-modal="true" aria-label="Command menu">
  <div class="command-content">
    <div class="command-input-wrapper">
      <svg
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
      >
        <circle cx="11" cy="11" r="8"></circle>
        <path d="m21 21-4.3-4.3"></path>
      </svg>
      <input
        class="command-input"
        type="text"
        placeholder="Type a command or search..."
        autocomplete="off"
        autocorrect="off"
        spellcheck="false"
      />
    </div>
    <div class="command-list">
      <div class="command-group">
        <p class="command-group-heading">Suggestions</p>
        <button class="command-item">Calendar</button>
        <button class="command-item">Search Emoji</button>
        <button class="command-item">Calculator</button>
      </div>
      <div class="command-separator"></div>
      <div class="command-group">
        <p class="command-group-heading">Settings</p>
        <button class="command-item">Profile <span class="command-shortcut">⌘P</span></button>
        <button class="command-item">Billing <span class="command-shortcut">⌘B</span></button>
        <button class="command-item">Settings <span class="command-shortcut">⌘S</span></button>
      </div>
    </div>
    <div class="command-empty" hidden>No results found.</div>
  </div>
</dialog>
```

```html example label="Density" hint="Set data-density on the component root to scale its internal whitespace. A whitespace policy, not a zoom: only gaps and padding scale (ratio 0.75 / 1 / 1.25), typography and fixed dimensions stay identical. comfortable matches the unsized default." height="28"
<div class="flex items-center gap-4 mb-3">
  <span class="demo-label demo-label-w75">Compact</span>
  <button class="btn" data-variant="outline" data-command-trigger="density-cmd-compact">Open Compact palette</button
  ><dialog
    id="density-cmd-compact"
    class="command"
    data-density="compact"
    role="dialog"
    aria-modal="true"
    aria-label="Compact command palette"
    ><div class="command-content"
      ><div class="command-input-wrapper"
        ><input class="command-input" type="text" placeholder="Type a command or search..." autocomplete="off" /></div
      ><div class="command-list"
        ><div class="command-group"
          ><p class="command-group-heading">Suggestions</p><button class="command-item">Calendar</button
          ><button class="command-item">Search Emoji</button><button class="command-item">Calculator</button></div
        ></div
      ></div
    ></dialog
  >
</div>
<div class="flex items-center gap-4 mb-3">
  <span class="demo-label demo-label-w75">Comfortable</span>
  <button class="btn" data-variant="outline" data-command-trigger="density-cmd-comfortable"
    >Open Comfortable palette</button
  ><dialog
    id="density-cmd-comfortable"
    class="command"
    data-density="comfortable"
    role="dialog"
    aria-modal="true"
    aria-label="Comfortable command palette"
    ><div class="command-content"
      ><div class="command-input-wrapper"
        ><input class="command-input" type="text" placeholder="Type a command or search..." autocomplete="off" /></div
      ><div class="command-list"
        ><div class="command-group"
          ><p class="command-group-heading">Suggestions</p><button class="command-item">Calendar</button
          ><button class="command-item">Search Emoji</button><button class="command-item">Calculator</button></div
        ></div
      ></div
    ></dialog
  >
</div>
<div class="flex items-center gap-4 mb-3">
  <span class="demo-label demo-label-w75">Spacious</span>
  <button class="btn" data-variant="outline" data-command-trigger="density-cmd-spacious">Open Spacious palette</button
  ><dialog
    id="density-cmd-spacious"
    class="command"
    data-density="spacious"
    role="dialog"
    aria-modal="true"
    aria-label="Spacious command palette"
    ><div class="command-content"
      ><div class="command-input-wrapper"
        ><input class="command-input" type="text" placeholder="Type a command or search..." autocomplete="off" /></div
      ><div class="command-list"
        ><div class="command-group"
          ><p class="command-group-heading">Suggestions</p><button class="command-item">Calendar</button
          ><button class="command-item">Search Emoji</button><button class="command-item">Calculator</button></div
        ></div
      ></div
    ></dialog
  >
</div>
```

```html example label="Results from your own search" hint="data-filter=&quot;none&quot; leaves the items to the page: a search of its own writes its ranked results into the list, and the palette keeps the groups, the empty state, the highlight and the keys. Type a fruit, a colour or a season." height="28"
<button class="btn" data-variant="outline" data-command-trigger="search-cmd">Search the market</button>
<dialog
  id="search-cmd"
  class="command"
  data-filter="none"
  role="dialog"
  aria-modal="true"
  aria-label="Search the market"
>
  <div class="command-content">
    <div class="command-input-wrapper">
      <input class="command-input" type="text" placeholder="A fruit, a colour or a season..." autocomplete="off" />
    </div>
    <div class="command-list"></div>
    <div class="command-empty" hidden>No results found.</div>
  </div>
</dialog>
<script>
  const produce = [
    { name: 'Apple', words: 'red green autumn' },
    { name: 'Banana', words: 'yellow tropical' },
    { name: 'Cherry', words: 'red summer' },
    { name: 'Lemon', words: 'yellow winter citrus' },
    { name: 'Plum', words: 'purple summer' },
    { name: 'Pumpkin', words: 'orange autumn' },
  ];
  addEventListener('DOMContentLoaded', () => {
    const list = df$('#search-cmd .command-list');
    df$('#search-cmd .command-input').on('input', (event) => {
      const terms = event.target.value.toLowerCase().split(/\s+/).filter(Boolean);
      // the more words an item matches, the higher it ranks
      const hits = produce
        .map((item) => ({
          item,
          score: terms.filter((t) => `${item.name} ${item.words}`.toLowerCase().includes(t)).length,
        }))
        .filter((hit) => terms.length && hit.score > 0)
        .sort((a, b) => b.score - a.score);
      const items = hits.map(
        ({ item }) =>
          `<button class="command-item">${item.name} <span class="command-shortcut">${item.words}</span></button>`,
      );
      list.html(
        items.length
          ? `<div class="command-group"><p class="command-group-heading">Best matches</p>${items.join('')}</div>`
          : '',
      );
    });
  });
</script>
```

<StatesSection>
  <p class="text-sm text-muted-foreground m-0 mb-3">Named states via the shared State API, driven per instance through the bound <code>api</code>:</p>
  <ul class="text-sm text-muted-foreground m-0 mb-3 ps-5">
    <li><code>default</code> - closed (the authored state; trigger or <code>Cmd/Ctrl+K</code> opens it)</li>
    <li><code>open</code> - shown modally with the search input focused</li>
  </ul>
  <p class="text-sm text-muted-foreground">The demo element carries <code>data-state-demo</code>; <code>bun run screenshots</code> drives it via <code>el.api.setState(name)</code> and captures <code>screenshots/\{mode\}/command-\{state\}.png</code>.</p>

Machine contract - verified against <a class="link" href="../schemas/command.schema.json"><code>command.schema.json</code></a> by <code>bun run verify</code>:

```states
| State | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean` | `true`, `false` | `false` | Shown via `showModal()`; hidden via `close()` (native `open` property). |
```

</StatesSection>

<ApiSection component="command" />

<SourceFiles component="command" section="css">
  <SourceNote for="css">Styles for the command component. Uses design tokens for colors, spacing, and radius.</SourceNote>
</SourceFiles>

<SourceFiles component="command" section="js">
  <SourceNote for="js">Interaction logic for the command component. Uses data attributes for wiring.</SourceNote>
</SourceFiles>

</DocPage>
