# Command

A command palette: search and quick actions behind a shortcut.

Source: https://www.zazz.sh/api/primitives/command/

## Installation

```bash
pnpm dlx zazz-ui add command
```

Also adds `fields`, `kbd`, `button`, `popover`, `dialog`, `menu`, `select`, which it depends on.

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<ui-command
  data-command-hotkey="mod+k"
  style="
    --w: 100%;
    --max-w: 96;
  "
>
  <button
    data-ui="input"
    type="button"
    popovertarget="command-example-1"
    data-command-slot="open"
    style="
      --display: inline-flex;
      --items: center;
      --justify: space-between;
      --pr: 1.5;
      --gap: 2;
      --w: 100%;
      --w--md: 64;
    "
  >
    <span style="--text: var(--color-muted-foreground)">Search...</span>
    <ui-kbd-group><kbd>⌘</kbd><kbd>K</kbd></ui-kbd-group>
  </button>
  <div
    id="command-example-1"
    data-command-slot="panel"
    data-popover-side="bottom"
    data-popover-align="start"
    popover="auto"
  >
    <header data-command-slot="header">
      <input
        data-command-slot="input"
        name="command"
        type="text"
        placeholder="Type a command or search..."
        role="combobox"
        aria-expanded="false"
        aria-autocomplete="list"
        autocomplete="off"
        aria-label="Search commands"
      />
    </header>
    <div role="listbox" data-command-slot="list" aria-label="Commands">
      <div role="group" data-command-slot="group" aria-labelledby="command-example-1-nav">
        <span
          data-command-slot="group-label"
          id="command-example-1-nav"
          style="
            --display: inline-flex;
            --font-size: var(--font-size-eyebrow);
            --font-weight: strong;
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
          >Navigation</span
        >
        <a
          role="option"
          data-command-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          href="#docs"
          tabindex="-1"
          data-command-keywords="documentation guides"
          data-command-hotkey="mod+shift+d"
          style="--justify: start"
        >
          Go to docs
          <ui-kbd-group><kbd>⇧</kbd><kbd>⌘</kbd><kbd>D</kbd></ui-kbd-group>
        </a>
        <a
          role="option"
          data-command-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          href="#settings"
          tabindex="-1"
          data-command-keywords="preferences options"
          data-command-hotkey="mod+shift+s"
          style="--justify: start"
        >
          Go to settings
          <ui-kbd-group><kbd>⇧</kbd><kbd>⌘</kbd><kbd>S</kbd></ui-kbd-group>
        </a>
      </div>
      <div role="group" data-command-slot="group" aria-labelledby="command-example-1-actions">
        <span
          data-command-slot="group-label"
          id="command-example-1-actions"
          style="
            --display: inline-flex;
            --font-size: var(--font-size-eyebrow);
            --font-weight: strong;
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
          >Actions</span
        >
        <button
          role="option"
          data-command-slot="item"
          data-ui="button"
          data-button-variant="ghost"
          type="button"
          command="show-modal"
          commandfor="command-example-1-dialog"
          tabindex="-1"
          data-command-keywords="modal open"
          style="--justify: start"
        >
          Open example dialog
        </button>
      </div>
    </div>
    <p
      data-command-slot="empty"
      style="
        --font-size: var(--font-size-sm);
        --leading: var(--leading-sm);
        --tracking: var(--tracking-sm);
        --text: var(--color-muted-foreground);
      "
    >
      No commands found.
    </p>
    <footer data-command-slot="footer">
      <span
        ><ui-kbd-group><kbd>↑</kbd><kbd>↓</kbd></ui-kbd-group> navigate</span
      >
      <span><kbd>↵</kbd> select</span>
      <span><kbd>esc</kbd> close</span>
    </footer>
  </div>
</ui-command>

<dialog id="command-example-1-dialog" data-ui="dialog" closedby="any">
  <div data-dialog-slot="content">
    <header data-dialog-slot="header">
      <h2
        style="
          --font-size: var(--font-size-lg);
          --font-weight: strong;
          --font-family: var(--font-family-heading);
          --leading: var(--leading-lg);
          --tracking: var(--tracking-lg);
        "
      >
        Opened by a command
      </h2>
    </header>
    <div data-dialog-slot="body">
      <p>The command item is a plain invoker button (no custom wiring).</p>
    </div>
  </div>
  <footer data-dialog-slot="footer">
    <button data-ui="button" type="button" commandfor="command-example-1-dialog" command="close">
      Close
    </button>
  </footer>
</dialog>
```

### Dialog

```html
<ui-command
  data-command-hotkey="mod+shift+k"
  style="
    --w: 100%;
    --max-w: 96;
  "
>
  <button
    data-ui="button"
    type="button"
    data-button-variant="outline"
    command="show-modal"
    commandfor="command-example-dialog-1"
    data-command-slot="open"
    style="
      --justify: space-between;
      --w: 100%;
    "
  >
    <span style="--text: var(--color-muted-foreground)">Command palette...</span>
    <ui-kbd-group><kbd>⌘</kbd><kbd>⇧</kbd><kbd>K</kbd></ui-kbd-group>
  </button>
  <dialog id="command-example-dialog-1" data-ui="dialog" data-command-slot="panel" closedby="any">
    <div data-dialog-slot="content">
      <header data-command-slot="header" data-dialog-slot="header">
        <input
          data-command-slot="input"
          name="search"
          type="text"
          placeholder="Type a command or search..."
          role="combobox"
          aria-expanded="false"
          aria-autocomplete="list"
          autocomplete="off"
          aria-label="Search commands"
        />
        <button
          data-ui="button"
          data-button-variant="ghost"
          data-button-size="icon"
          type="button"
          command="close"
          commandfor="command-example-dialog-1"
          aria-label="Close"
        >
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
            <rect width="256" height="256" fill="none" />
            <line
              x1="200"
              y1="56"
              x2="56"
              y2="200"
              fill="none"
              stroke="currentColor"
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="16"
            />
            <line
              x1="200"
              y1="200"
              x2="56"
              y2="56"
              fill="none"
              stroke="currentColor"
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="16"
            />
          </svg>
        </button>
      </header>
      <div role="listbox" data-command-slot="list" data-dialog-slot="body" aria-label="Commands">
        <div role="group" data-command-slot="group" aria-labelledby="command-example-dialog-1-nav">
          <span
            data-command-slot="group-label"
            id="command-example-dialog-1-nav"
            style="
              --display: inline-flex;
              --font-size: var(--font-size-eyebrow);
              --font-weight: strong;
              --font-weight: var(--font-weight-eyebrow);
              --leading: var(--leading-eyebrow);
              --tracking: var(--tracking-eyebrow);
              --text-transform: uppercase;
              --text: var(--color-muted-foreground);
            "
            >Navigation</span
          >
          <a
            role="option"
            data-command-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            href="#home"
            tabindex="-1"
            style="--justify: start"
          >
            Go home
          </a>
          <a
            role="option"
            data-command-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            href="#projects"
            tabindex="-1"
            data-command-keywords="work portfolio"
            style="--justify: start"
          >
            Go to projects
          </a>
          <a
            role="option"
            data-command-slot="item"
            data-ui="button"
            data-button-variant="ghost"
            href="#team"
            tabindex="-1"
            data-command-keywords="people members"
            style="--justify: start"
          >
            Go to team
          </a>
        </div>
      </div>
      <p data-command-slot="empty" style="--text: var(--color-muted-foreground)">
        No commands found.
      </p>
      <footer data-command-slot="footer" data-dialog-slot="footer">
        <span
          ><ui-kbd-group><kbd>↑</kbd><kbd>↓</kbd></ui-kbd-group> navigate</span
        >
        <span><kbd>↵</kbd> select</span>
        <span><kbd>esc</kbd> close</span>
      </footer>
    </div>
  </dialog>
</ui-command>
```

### Actions

```html
<!-- command-actions.js shows how to register custom actions: listen for your
     `--command` on its `commandfor` target, or for `zazz:command-select` on
     the root. -->
<ui-command
  data-command-hotkey="mod+j"
  id="command-actions-target"
  style="
    --w: 100%;
    --max-w: 96;
  "
>
  <button
    data-ui="button"
    type="button"
    data-button-variant="outline"
    popovertarget="command-example-actions-1"
    data-command-slot="open"
    style="
      --justify: space-between;
      --w: 100%;
    "
  >
    <span style="--text: var(--color-muted-foreground)">Quick actions...</span>
    <ui-kbd-group><kbd>⌘</kbd><kbd>J</kbd></ui-kbd-group>
  </button>
  <div
    id="command-example-actions-1"
    data-command-slot="panel"
    data-popover-side="bottom"
    data-popover-align="start"
    popover="auto"
  >
    <header data-command-slot="header">
      <input
        data-command-slot="input"
        name="action"
        type="text"
        placeholder="Type a command..."
        role="combobox"
        aria-expanded="false"
        aria-autocomplete="list"
        autocomplete="off"
        aria-label="Search commands"
      />
    </header>
    <div role="listbox" data-command-slot="list" aria-label="Commands">
      <button
        role="option"
        data-command-slot="item"
        data-ui="button"
        data-button-variant="ghost"
        type="button"
        command="--theme-toggle"
        commandfor="command-actions-target"
        tabindex="-1"
        data-command-keywords="dark light mode appearance"
        data-command-stay-open
        style="--justify: start"
      >
        Toggle theme
      </button>
      <button
        role="option"
        data-command-slot="item"
        data-ui="button"
        data-button-variant="ghost"
        type="button"
        command="--copy-link"
        commandfor="command-actions-target"
        tabindex="-1"
        data-command-keywords="share url clipboard"
        style="--justify: start"
      >
        Copy link
      </button>
      <button
        role="option"
        data-command-slot="item"
        data-ui="button"
        data-button-variant="ghost"
        type="button"
        command="--toast-success"
        commandfor="command-actions-toaster"
        data-toaster-title="Saved"
        data-toaster-description="Your changes are safe."
        tabindex="-1"
        data-command-keywords="notify notification"
        style="--justify: start"
      >
        Show a toast
      </button>
    </div>
    <p data-command-slot="empty" style="--text: var(--color-muted-foreground)">
      No commands found.
    </p>
    <footer data-command-slot="footer">
      <span
        ><ui-kbd-group><kbd>↑</kbd><kbd>↓</kbd></ui-kbd-group> navigate</span
      >
      <span><kbd>↵</kbd> select</span>
    </footer>
  </div>
</ui-command>

<ui-toaster id="command-actions-toaster" data-toaster-position="bottom-end"></ui-toaster>
```

## Behavior

`primitives/command/command.js`

`<ui-command>`: a command menu for search and quick actions.
Light-DOM custom element on the shared typeahead engine
(`base/typeahead.ts`). The panel (a `popover="auto"`
dropdown or a `<dialog data-ui="dialog">`) contains the search input;
items rank by match score and the best match auto-highlights.

Actions are the platform's own vocabulary: navigation items are real
`<a href>` links, and action items are `<button command commandfor>` invoker
buttons (including custom `--commands`). Enter activates the highlighted
item exactly as a click would; every activation also dispatches a bubbling
`zazz:command-select` CustomEvent (`detail: { item, value }`) from the root.
Without JavaScript the trigger still opens the panel natively and every
item still works: only filtering, highlight, and hotkeys are lost.

Attributes:
- `data-command-hotkey` (root): global toggle shortcut, e.g. `"mod+k"`.
- `data-command-hotkey` (item): global accelerator that activates the item, active
  while the element is connected (even with the panel closed).
- `data-command-stay-open` (item): keep the panel open after activation.
- `data-command-sort="document"` (root): opt out of score ranking (default: score).
- `data-command-filter="none"` (root): opt out of filtering for a list another
  source already filtered (a search index); items added later are highlighted
  and navigable like typed matches.

Parts: `command-open` (trigger), `command-panel`, `command-header`,
`command-input`, `command-list`, `command-group` / `command-group-label`,
`command-item` (`data-command-value`, `data-command-keywords`), `command-kbd`,
`command-footer`, `command-empty`.

For complex custom actions, see the example `command-actions.ts`: listen
for your `--command` on its target, or for `zazz:command-select` on the root.

`primitives/command/command-actions.js`

Example: composing custom command actions for `<ui-command>`.
This file is a template, not part of the kit runtime: copy it
into your project and register your own actions. It is loaded only by the
`command-actions` docs example.

Simple actions need no script at all: a navigation item is an `<a href>`,
and opening a dialog/popover is a `<button command commandfor>` invoker.
Reach for a script only when an action runs real logic. Two hooks exist:

1. **Custom invoker commands**: give the item `command="--your-action"`
   and `commandfor="<target id>"`, then listen for the `command` event on
   the target. The invokers polyfill re-dispatches commands in browsers
   with native support, so de-dupe per task (see below).
2. **`zazz:command-select`**: every activation bubbles this CustomEvent
   from the `<ui-command>` root (`detail: { item, value }`); use it for
   palette-wide concerns like analytics.

Events: `zazz:command-select`, `zazz:dialog-close`

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="command"` | identity | free value |
| `<ui-command>` | tag | free value |
| `data-command-slot` | slot | `empty` `footer` `group` `group-label` `header` `input` `item` `list` `open` `panel` |
| `data-command-state` | state | `highlighted` |
| `data-command-filter` | config | free value |
| `data-command-hotkey` | config | free value |
| `data-command-keywords` | config | free value |
| `data-command-min-length` | config | free value |
| `data-command-sort` | config | free value |
| `data-command-stay-open` | config | free value |
| `data-command-value` | config | free value |

Requires: popover, button, fields, dialog, menu, select, kbd.

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-command-footer-border` | `1px solid var(--color-border)` | — |
| `--ui-command-footer-p` | `calc(var(--spacing) * 2)` | — |
| `--ui-command-group-label-p` | `calc(var(--spacing) * 2) calc(var(--spacing) * 2) var(--spacing)` | — |
| `--ui-command-input-border` | `1px solid var(--color-border)` | — |
| `--ui-command-input-p` | `calc(var(--spacing) * 3)` | — |
| `--ui-command-max-h` | `calc(var(--spacing) * 96)` | — |
| `--ui-command-min-w` | `calc(var(--spacing) * 96)` | — |
| `--ui-command-option-gap` | `var(--ui-field-option-gap)` | — |
| `--ui-command-shadow` | `var(--ui-menu-shadow)` | — |
