# Command (`<z-cmd>`)

> Lazy-mounted modal command palette with live multi-token search, grouped actions, descriptions, and keycap hints.

- **Kind:** hwc
- **Category:** overlays
- **Inherits:** `BaseSelectMixin`, `DialogMixin`, `Hwc`, `Base`

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `placeholder` | `string` | `''` | — | Overrides the search field placeholder from `i18n`. |
| `shortcut` | `string` | `''` | — | Global keyboard shortcut that toggles the palette, e.g. `mod+k` or `/`. |
| `prevent-close` | `boolean` | `false` | — | Keeps the palette open after an item is selected. |
| `no-footer` | `boolean` | `false` | — | Hides the keyboard-hint and close bar. |
| `icon` | `string` | `''` | — | Icon key that replaces the default `search` icon. |

**`shortcut` tokens:**

| Token | Aliases | Meaning |
| --- | --- | --- |
| `mod` | — | ⌘ on macOS/iOS, Ctrl on Windows/Linux. |
| `ctrl` | `control` | Ctrl modifier. |
| `meta` | `cmd`, `command` | ⌘ / Windows key modifier. |
| `alt` | `option` | Alt / Option modifier. |
| `shift` | — | Shift modifier. |

### Inherited from `DialogMixin`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean` | `false` | `DialogMixin` | Opens the dialog on first render and stays in sync with `show()` and `hide()`. |
| `center` | `boolean` | `false` | `DialogMixin` | Vertically centers the panel within the overlay. |
| `container` | `boolean` | `false` | `DialogMixin` | Adds `z-modal-container` to the overlay for a contained presentation. |
| `full` | `boolean` | `false` | `DialogMixin` | Adds `z-modal-full` to the overlay for a full-screen presentation. |
| `stack` | `boolean` | `false` | `DialogMixin` | Keeps other open dialogs open; without it, showing this dialog hides them. Stacked overlays receive increasing z-index. |
| `inline` | `boolean` | `false` | `DialogMixin` | Keeps the component in its original DOM position instead of teleporting it to `document.body`. |
| `no-bg-close` | `boolean` | `false` | `DialogMixin` | Disables closing when the overlay background is clicked. |
| `no-esc-close` | `boolean` | `false` | `DialogMixin` | Disables closing with the Esc key. |
| `unmount` | `boolean` | `false` | `DialogMixin` | Removes the rendered DOM after the close transition; the next show mounts it again. |
| `dialog-role` | `'dialog' \| 'alertdialog'` | `'dialog'` | `DialogMixin` | ARIA role of the panel: `dialog` or `alertdialog`. |
| `aria-label` | `string` | `''` | `DialogMixin` | Accessible name for the panel when no `aria-labelledby` is set. |
| `aria-labelledby` | `string` | `''` | `DialogMixin` | Id of the element that names the panel; takes precedence over `aria-label`. |

### Inherited from `Hwc`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `i18n` | `string` | `''` | `Hwc` | Per-instance text overrides as `key: text` pairs, merged over the global `z-i18n` script and the built-in defaults. |
| `floating` | `string` | `''` | `Hwc` | Floating UI placement options (for example `offset` and `shiftPadding`) parsed as `key: value` pairs. |
| `force-prevent-rerender` | `boolean` | `false` | `Hwc` | Skips re-rendering after the first render; an existing `data-host-inner` child is adopted as the rendered DOM. |
| `template` | `string` | `''` | `Hwc` | Selector or id of an external element or `<template>` whose inner content is cloned in place of an inline `template[data-fn="template"]` child. |

### Inherited from `Base`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `dialog`. Default `stl` target: `dialog`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `dialog` | `z-modal-dialog z-cmd` | ✓ | ✓ | Dialog container; the default `cls` and `stl` target. |
| `divider` | `z-hr` | ✓ | ✓ | Horizontal rule between the search field and the results. |
| `empty` | `z-cmd-empty` | ✓ | ✓ | Empty-state message shown when no command matches. |
| `footer` | `z-cmd-footer` | ✓ | ✓ | Bottom hint bar. |
| `footer-close` | `z-cmd-footer-item z-cmd-footer-close` | ✓ | ✓ | Close button with the Esc hint. |
| `footer-group` | `z-cmd-footer-group` | ✓ | ✓ | Group of navigation hints. |
| `footer-item` | `z-cmd-footer-item` | ✓ | ✓ | Single hint: icon keycaps plus a label. |
| `footer-kbd` | `z-kbd` | ✓ | ✓ | Keycap inside a footer hint. |
| `item` | — | ✓ | ✓ | One command row (`role="option"`). |
| `item-active` | `z-active` | ✓ | — | Class on the focused command. |
| `item-content` | `z-cmd-item-content` | ✓ | ✓ | Text column: label and optional description. |
| `item-disabled` | `z-disabled` | ✓ | — | Class on disabled commands. |
| `item-header` | `z-nav-header` | ✓ | ✓ | Group header row. |
| `item-icon` | `z-custom-select-item-icon` | ✓ | ✓ | Wrapper for the command's icon. |
| `item-link` | — | ✓ | ✓ | Clickable link covering the command row. |
| `item-shortcut` | `z-cmd-item-shortcut` | ✓ | ✓ | Keycap hint container on the right. |
| `item-subtitle` | `z-cmd-item-subtitle` | ✓ | ✓ | Secondary description line. |
| `item-text` | — | ✓ | ✓ | Command label. |
| `item-wrapper` | `z-custom-select-item-wrapper` | ✓ | ✓ | Layout wrapper for the icon and text column. |
| `kbd` | — | ✓ | ✓ | Individual keycap. |
| `list` | `z-nav z-dropdown-nav z-overflow-auto z-cmd-list` | ✓ | ✓ | Scrollable results listbox. |
| `overlay` | `z-modal` | ✓ | ✓ | Modal overlay behind the palette. |
| `panel` | `z-cmd-panel` | ✓ | ✓ | Panel holding the search field and results list. |
| `search` | `z-custom-select-search` | ✓ | ✓ | Search field wrapper (`role="search"`). |
| `search-icon` | — | ✓ | ✓ | Wrapper for the search icon. |
| `search-input` | — | ✓ | ✓ | Combobox input that drives filtering. |
| `separator` | `z-nav-divider` | ✓ | ✓ | Divider inserted between groups. |

## CSS Variables

| Variable | Default | Scope | Affects | Description |
| --- | --- | --- | --- | --- |
| `--z-cmd-bg` | `color-mix(in srgb, var(--z-muted) calc(var(--z-muted-o) * 0.75), var(--z-bg))` | `.z-cmd` | `background-color` | Background of the palette surface. |
| `--z-cmd-border-color` | `var(--z-border)` | `.z-cmd` | `border-block-end`, `box-shadow` | Border and shadow color of the search divider. |
| `--z-cmd-border-color-o` | `var(--z-border-o)` | `.z-cmd` | `border-block-end`, `box-shadow` | Opacity applied to the divider border color. |
| `--z-cmd-empty-padding` | `2.5rem 1rem` | `.z-cmd` | `padding` | Padding of the empty-state message. |
| `--z-cmd-list-max-height` | `18rem` | `.z-cmd` | `max-height` | Maximum height of the results list before it scrolls. |
| `--z-cmd-offset-top` | `10vh` | `.z-cmd` | `margin-block-start` | Top offset of the palette from the viewport edge. |
| `--z-cmd-panel-bg` | `var(--z-drop)` | `.z-cmd` | `background-color` | Background of the panel. |
| `--z-cmd-panel-color` | `var(--z-drop-f)` | `.z-cmd` | `color` | Text color of the panel. |
| `--z-cmd-panel-radius` | `0.75rem` | `.z-cmd` | `border-end-end-radius`, `border-end-start-radius` | Bottom corner radius of the panel. |
| `--z-cmd-radius` | `0.875rem` | `.z-cmd` | `border-radius` | Corner radius of the dialog. |
| `--z-cmd-shadow` | `0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1)` | `.z-cmd` | `box-shadow` | Drop shadow of the dialog. |
| `--z-cmd-width` | `34rem` | `.z-cmd` | — | Width of the palette dialog; feeds `--z-modal-dialog-width`. |

## i18n Keys

| Key | Default | Description |
| --- | --- | --- |
| `close-hint` | `Esc` | Keycap text on the close button. |
| `close-label` | `Close` | Label and aria-label of the close button. |
| `dialog-label` | `Command menu` | Accessible name of the dialog. |
| `empty-message` | `No results found.` | Text shown when no command matches the search. |
| `list-label` | `Commands` | Accessible name of the results listbox. |
| `navigate-label` | `Navigate` | Footer label for the arrow-key hint. |
| `search-placeholder` | `Search for apps and commands...` | Placeholder and aria-label of the search input. |
| `select-label` | `Open` | Footer label for the Enter hint. |

## Icon Keys

| Key | Used in | Description |
| --- | --- | --- |
| `arrow-down` | `footer-item` | Down-arrow keycap in the navigate hint. |
| `arrow-up` | `footer-item` | Up-arrow keycap in the navigate hint. |
| `corner-down-left` | `footer-item` | Enter keycap in the select hint. |
| `search` | `search-icon` | Icon in the search field; replace it via the `icon` attribute. |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-cmd:before-hide` | ✓ | — | Fires before closing; call `preventDefault()` to abort. |
| `z-cmd:before-show` | ✓ | — | Fires before opening; call `preventDefault()` to abort. |
| `z-cmd:destroy` | — | — | Fires when the mounted DOM is removed, via `unmount` or `destroy()`. |
| `z-cmd:hidden` | — | — | Fires after the close transition completes and focus is restored to the opener. |
| `z-cmd:hide` | — | — | Fires when the dialog starts closing. |
| `z-cmd:mount` | — | — | Fires once when the dialog DOM is first mounted. |
| `z-cmd:search` | — | `{ value: string }` | Fires when the search term changes; `detail.value` carries the current term. |
| `z-cmd:select` | ✓ | `{ value: string; text: string; group: string; item: OptionItem; originalEvent: MouseEvent \| KeyboardEvent \| undefined }` | Fires when a command is chosen; detail carries value, text, group, the option item, and the originating event. `preventDefault()` keeps the palette open. |
| `z-cmd:show` | — | — | Fires when the dialog starts opening, before the transition completes. |
| `z-cmd:shown` | — | — | Fires after the open transition completes. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `destroy()` | `removeHost?: boolean = false` | `Promise<void>` | Closes and unmounts the dialog DOM; optionally removes the host element as well. |
| `hide()` | — | `Promise<void>` | Closes the dialog and restores focus to the opener. |
| `show()` | `trigger?: HTMLElement \| null` | `Promise<void>` | Opens the dialog; the trigger element receives focus back on close. |
| `toggle()` | `trigger?: HTMLElement \| null` | `Promise<void>` | Opens the dialog if closed, otherwise closes it. |

## Data Hooks

| Attribute | Values | Applies to | Role | Description |
| --- | --- | --- | --- | --- |
| `data-close` | — | `external-trigger` | `trigger` | Closes the dialog matched by the selector, or the nearest enclosing dialog when no value is given. |
| `data-host-inner` | — | `descendant` | `config` | Marks the rendered inner wrapper so `force-prevent-rerender` can adopt server-rendered DOM without re-rendering. |
| `data-index` | — | `descendant` | `marker` | Index of the command within its group; used to resolve Enter selection. |
| `data-key` | — | `descendant` | `marker` | Group key of the command; used with `data-index` to resolve selection. |
| `data-open` | — | `external-trigger` | `trigger` | Clicking the element opens the dialog matched by the selector without toggling. |
| `data-part` | `dialog`, `list`, `overlay`, `search-input` | `descendant` | `marker` | Marks structural parts (overlay, dialog, list, search-input) for the component's DOM references. |
| `data-reactive` | — | `descendant` | `config` | Opts the marked child (config script, data source, icons, or template) into live re-parsing when its DOM mutates. |
| `data-toggle` | — | `external-trigger` | `trigger` | Clicking the element toggles the dialog matched by the selector (usually `#id`). |

## Child Content

- `<script data-fn="config">` — JSON configuration script parsed by the base class (i18n, cls, stl, floating).

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `config` | Marks the script as configuration (value: `config`). |
  | `data-reactive` | — | `boolean` | Re-parses the configuration when the script mutates. |

- `<select data-fn="data-source">` — Hidden select holding the commands; nothing renders until the palette opens.

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `data-source` | Marks the select as the data source (value: `data-source`). |
  | `data-reactive` | — | `boolean` | Re-parses the commands when the select mutates. |

  - `<optgroup>` — Groups commands under a header and inserts a separator before later groups.

    | Attribute | Required | Type | Description |
    | --- | --- | --- | --- |
    | `label` | ✓ | `string` | Group header text; also the default group key. |
    | `disabled` | — | `boolean` | Makes the group non-selectable and skipped by keyboard navigation. |
    | `data-key` | — | `string` | Overrides the group key used in the parsed data. |
    | `data-keywords` | — | `comma-separated string` | Comma-separated keywords stored with the group's data. |

    - `<option>` — One command within the group.

      | Attribute | Required | Type | Description |
      | --- | --- | --- | --- |
      | `value` | — | `string` | Command identifier emitted in `event.detail.value`. |
      | `disabled` | — | `boolean` | Makes the command non-selectable and skipped by keyboard navigation. |
      | `selected` | — | `boolean` | Marks the command as initially selected. |
      | `data-keywords` | — | `comma-separated string` | Extra search terms, comma-separated; searched alongside the label and value. |
      | `data-description` | — | `string` | Secondary line shown under the label; also searched. |
      | `data-icon` | — | `string` | Icon key resolved through `$icons` for this command. |
      | `data-kbd` | — | `string` | Keycap hint: `+` joins keys (⌘+K), spaces separate them (G S). |
      | `data-shortcut` | — | `string` | Alternative keycap hint, used when `data-kbd` is absent. |

  - `<option>` — One ungrouped command.

    | Attribute | Required | Type | Description |
    | --- | --- | --- | --- |
    | `value` | — | `string` | Command identifier emitted in `event.detail.value`. |
    | `disabled` | — | `boolean` | Makes the command non-selectable and skipped by keyboard navigation. |
    | `selected` | — | `boolean` | Marks the command as initially selected. |
    | `data-keywords` | — | `comma-separated string` | Extra search terms, comma-separated. |
    | `data-description` | — | `string` | Secondary line shown under the label; also searched. |
    | `data-icon` | — | `string` | Icon key resolved through `$icons` for this command. |
    | `data-kbd` | — | `string` | Keycap hint: `+` joins keys (⌘+K), spaces separate them (G S). |
    | `data-shortcut` | — | `string` | Alternative keycap hint, used when `data-kbd` is absent. |

- `<template data-fn="icons">` — Instance-level icon repository.

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `icons` | Marks the template as an icon source (value: `icons`). |
  | `data-reactive` | — | `boolean` | Re-parses the icons when the template mutates. |

  - `<*>` — One icon element.

    | Attribute | Required | Type | Description |
    | --- | --- | --- | --- |
    | `data-key` | ✓ | `string` | Icon key referenced by `data-icon` or the `icon` attribute. |

- `<template data-fn="template">` — Deferred content template mounted by the base class.

  | Attribute | Required | Type | Description |
  | --- | --- | --- | --- |
  | `data-fn` | ✓ | `template` | Marks the template for deferred mounting (value: `template`). |
  | `data-reactive` | — | `boolean` | Re-parses the template when it mutates. |

## CSS Classes

| Class | Role | Applies to | Description |
| --- | --- | --- | --- |
| `.z-active` | `state` | `.z-cmd` | State class on the focused command. |
| `.z-cmd` | `root` | `z-cmd` | Root palette class on the dialog. |
| `.z-cmd-empty` | `element` | `.z-cmd` | Empty-state message styling. |
| `.z-cmd-footer` | `element` | `.z-cmd` | Footer bar layout. |
| `.z-cmd-footer-close` | `element` | `.z-cmd` | Close button styling. |
| `.z-cmd-footer-group` | `element` | `.z-cmd` | Hint group layout. |
| `.z-cmd-footer-item` | `element` | `.z-cmd` | Single hint layout. |
| `.z-cmd-item-content` | `element` | `.z-cmd` | Text column layout. |
| `.z-cmd-item-shortcut` | `element` | `.z-cmd` | Keycap hint layout. |
| `.z-cmd-item-subtitle` | `element` | `.z-cmd` | Description line styling. |
| `.z-cmd-list` | `element` | `.z-cmd` | Results list sizing and scrolling. |
| `.z-cmd-panel` | `element` | `.z-cmd` | Panel surface styling. |
| `.z-custom-select-item-icon` | `element` | `.z-cmd` | Command icon sizing. |
| `.z-hr` | `element` | `.z-cmd` | Divider rule styling. |
| `.z-kbd` | `element` | `.z-cmd` | Keycap styling. |
| `.z-nav-divider` | `element` | `.z-cmd` | Group separator styling. |
| `.z-nav-header` | `element` | `.z-cmd` | Group header styling. |

## Keyboard

| Keys | Action | When |
| --- | --- | --- |
| `ArrowDown` / `ArrowUp` | Move to the next / previous enabled command, wrapping at the ends. | The palette is open |
| `Home` / `End` | Jump to the first / last enabled command. | The palette is open |
| `Enter` | Select the focused command (`z-cmd:select`). | The palette is open |
| `Escape` | Close the palette and restore focus to the opener. | The palette is open |

## Accessibility

| Part | Attributes | Description |
| --- | --- | --- |
| `dialog` | `aria-label="${this.getI18nText('dialog-label', this.defaultI18n)}"`, `aria-modal="true"`, `role="dialog"` | Modal dialog container (`aria-modal`), labelled by `dialog-label`. |
| `divider` | — | Separator between the search field and the results. |
| `empty` | `aria-live="polite"`, `role="status"` | Status region announcing the empty message (`aria-live="polite"`). |
| `footer` | — | Hint bar container. |
| `footer-close` | `aria-label="${closeLabel}"` | Close button labelled by `close-label`. |
| `footer-group` | — | Navigation hint group. |
| `footer-item` | — | Single navigation hint. |
| `item` | `aria-disabled="${item.disabled ? 'true' : 'false'}"`, `aria-selected="${isFocused ? 'true' : 'false'}"`, `role="option"` | Command option; `aria-selected` tracks focus and `aria-disabled` marks unavailable commands. |
| `item-content` | — | Text column wrapper. |
| `item-header` | `role="presentation"` | Group header (`role="presentation"`). |
| `item-icon` | — | Command icon wrapper. |
| `item-link` | `aria-label="${item.text}"` | Link labelled by the command text. |
| `item-shortcut` | — | Keycap hint wrapper. |
| `item-subtitle` | — | Description text. |
| `item-text` | — | Command label text. |
| `item-wrapper` | — | Layout wrapper for icon and text. |
| `list` | `aria-activedescendant="${activeDescendant ?? ''}"`, `aria-label="${this.getI18nText('list-label', this.defaultI18n)}"`, `role="listbox"` | Listbox of commands; the active option is announced via `aria-activedescendant`. |
| `overlay` | — | Overlay wrapper. |
| `panel` | — | Panel wrapper. |
| `search` | `role="search"` | Search landmark (`role="search"`). |
| `search-icon` | — | Search icon wrapper. |
| `search-input` | `aria-autocomplete="list"`, `aria-controls="${this.listboxId}"`, `aria-expanded="${this.$open ? 'true' : 'false'}"`, `aria-label="${searchPlaceholder}"`, `role="combobox"` | Combobox controlling the listbox; `aria-expanded` tracks the open state. |
| `separator` | `role="separator"` | Group separator (`role="separator"`). |

> Disabled commands receive `aria-disabled="true"` and are skipped by arrow-key navigation.

> Focus moves to the search input on open; the listbox tracks the active option with `aria-activedescendant`.

## Notes

- The palette renders nothing until first opened (lazy mount); `unmount` removes the DOM after close.
- Unmodified shortcuts are ignored while focus is in an input, textarea, select, or contenteditable element.
- Search matches every whitespace-separated token against the label, value, group title, description, and keywords.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)
