# `LedgerUI.Components.CommandPalette`
[🔗](https://github.com/devaction-labs/ledger_ui/blob/v0.2.0/lib/ledger_ui/components/command_palette.ex#L1)

A dialog opened with Ctrl K (or `LedgerUI.open/1`) where the person types, narrows the results
by scope and jumps to a page or runs an action from the keyboard (`<.command_palette>`).

    <.command_palette
      id="palette"
      scopes={[{"All", "all"}, {"Pages", "pages"}, {"Actions", "actions"}]}
      items={[
        %{group: "Go to", scope: "pages", label: "Orders", icon: "cart", navigate: "/orders", keys: ["G", "O"]},
        %{group: "Actions", scope: "actions", label: "New order", icon: "plus", value: "new-order"}
      ]}
      assistant={%{label: "Ask the assistant: “%{query}”", sub: "Answers with your data"}}
      on_select="palette-select"
    />

    <.button phx-click={LedgerUI.open("palette")}>Search</.button>

The palette is a modal: `LedgerUI.open/close/toggle`, `open`, `on_open` and `on_close` work as for
`LedgerUI.Components.Modal`, and `shortcut` toggles it from anywhere. The items are filtered in the
browser by label, `sub` and `keywords` (ignoring case and accents) and by scope; with `on_search`
the server filters instead. Arrows move the active row, Enter presses it, Ctrl Enter opens a link
in a new tab and Esc closes. The behaviour comes from the `LedgerUI.Overlay` hook
(`priv/static/ledger_ui/hooks/overlay.js`); the ARIA is rendered by the server: the card is a
`role="dialog"` (`aria-modal`, `aria-label`), the input a `role="combobox"` with
`aria-controls`/`aria-expanded`/`aria-autocomplete`, the list a `role="listbox"` and the rows
`role="option"` with stable ids (`"#{id}-opt-N"`); the hook only moves `aria-selected` and
`aria-activedescendant`. The static previews (`inline`/`bare`) keep the design's `menuitem` rows.

Each item is a map with `label` (required) and optionally `value` (what `on_select` receives),
`sub`, `icon`, `avatar` (`%{initials: "CD", tint: "#d97a1e"}`), `keys` (key names on the right),
`count` (a badge), `group`, `scope`, `keywords`, `style` and one of `navigate`, `patch` or `href`.

Events (`on_select`: `%{"id", "value", "new_tab"}`, `on_assistant`: `%{"id", "query"}`,
`on_search`: `%{"id", "query", "scope"}`, `on_open`/`on_close`: `%{"id"}`) take an event name or a
`%JS{}` and go to `target` when given.

`inline` renders the dialog over its own in-page scrim (a static preview, always open, no hook);
`bare` renders only the `lu-cmd` card.

# `command_palette`

## Attributes

* `id` (`:string`) (required)
* `open` (`:boolean`) - Defaults to `false`.
* `label` (`:string`) - Defaults to `"Command palette"`.
* `placeholder` (`:string`) - Defaults to `"Search or type a command"`.
* `scopes` (`:list`) - Defaults to `[]`.
* `scopes_label` (`:string`) - Defaults to `"Where to search"`.
* `items` (`:list`) - Defaults to `[]`.
* `empty` (`:string`) - Defaults to `"Nothing found"`.
* `assistant` (`:map`) - Defaults to `nil`.
* `assistant_mode` (`:string`) - Defaults to `"no_match"`. Must be one of `"no_match"`, or `"always"`.
* `on_select` (`:any`) - Defaults to `nil`.
* `on_assistant` (`:any`) - Defaults to `nil`.
* `on_search` (`:string`) - Defaults to `nil`.
* `debounce` (`:integer`) - Defaults to `200`.
* `query` (`:string`) - Defaults to `""`.
* `on_open` (`:any`) - Defaults to `nil`.
* `on_close` (`:any`) - Defaults to `nil`.
* `target` (`:any`) - Defaults to `nil`.
* `shortcut` (`:any`) - Defaults to `"Mod+k"`.
* `variant` (`:string`) - Defaults to `"canvas"`. Must be one of `"canvas"`, or `"classic"`.
* `hints` (`:boolean`) - Defaults to `true`.
* `highlight` (`:string`) - Defaults to `"always"`. Must be one of `"always"`, or `"typed"`.
* `hint_navigate` (`:string`) - Defaults to `"Navigate"`.
* `hint_open` (`:string`) - Defaults to `"Open"`.
* `hint_new_tab` (`:any`) - Defaults to `"Open in new tab"`.
* `hint_close` (`:string`) - Defaults to `"Close"`.
* `class` (`:any`) - Defaults to `nil`.
* `list_style` (`:string`) - Inline style of the list (replaces the variant padding). Defaults to `nil`.
* `inline` (`:boolean`) - Static preview over an in-page scrim: always open, no hook. Defaults to `false`.
* `bare` (`:boolean`) - Only the `lu-cmd` card, always open, no hook. Defaults to `false`.
* Global attributes are accepted.
## Slots

* `footer`
* `item_icon`

# `command_palette_dialog`

Same as `command_palette/1` (same attributes and slots), under the name the design vitrine uses.
## Attributes

* `id` (`:string`) (required)
* `open` (`:boolean`) - Defaults to `false`.
* `label` (`:string`) - Defaults to `"Command palette"`.
* `placeholder` (`:string`) - Defaults to `"Search or type a command"`.
* `scopes` (`:list`) - Defaults to `[]`.
* `scopes_label` (`:string`) - Defaults to `"Where to search"`.
* `items` (`:list`) - Defaults to `[]`.
* `empty` (`:string`) - Defaults to `"Nothing found"`.
* `assistant` (`:map`) - Defaults to `nil`.
* `assistant_mode` (`:string`) - Defaults to `"no_match"`. Must be one of `"no_match"`, or `"always"`.
* `on_select` (`:any`) - Defaults to `nil`.
* `on_assistant` (`:any`) - Defaults to `nil`.
* `on_search` (`:string`) - Defaults to `nil`.
* `debounce` (`:integer`) - Defaults to `200`.
* `query` (`:string`) - Defaults to `""`.
* `on_open` (`:any`) - Defaults to `nil`.
* `on_close` (`:any`) - Defaults to `nil`.
* `target` (`:any`) - Defaults to `nil`.
* `shortcut` (`:any`) - Defaults to `"Mod+k"`.
* `variant` (`:string`) - Defaults to `"canvas"`. Must be one of `"canvas"`, or `"classic"`.
* `hints` (`:boolean`) - Defaults to `true`.
* `highlight` (`:string`) - Defaults to `"always"`. Must be one of `"always"`, or `"typed"`.
* `hint_navigate` (`:string`) - Defaults to `"Navigate"`.
* `hint_open` (`:string`) - Defaults to `"Open"`.
* `hint_new_tab` (`:any`) - Defaults to `"Open in new tab"`.
* `hint_close` (`:string`) - Defaults to `"Close"`.
* `class` (`:any`) - Defaults to `nil`.
* `list_style` (`:string`) - Inline style of the list (replaces the variant padding). Defaults to `nil`.
* `inline` (`:boolean`) - Static preview over an in-page scrim: always open, no hook. Defaults to `false`.
* `bare` (`:boolean`) - Only the `lu-cmd` card, always open, no hook. Defaults to `false`.
* Global attributes are accepted.
## Slots

* `footer`
* `item_icon`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
