Skip to content
npm ↗ GitHub ↗

Documentation

29/Overlays

Command palette

⌘K search with groups, arrow key navigation and accent-insensitive matching. It is the same one this documentation uses.

Palette

Results are read from a script type="application/json" inside the palette.

Esc
↑ ↓ navegar↵ abrirEsc cerrar
HTML
<button class="cb-search-btn" type="button" data-cb-command-open="demo-command"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg><span>Abrir paleta</span></button>

<div class="cb-command" id="demo-command" data-cb-command data-cb-command-empty="Nada para">
  <div class="cb-command__panel" aria-label="Buscar">
    <div class="cb-command__head">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>
      <input class="cb-command__input" data-cb-command-input placeholder="Buscar..." aria-label="Buscar">
      <kbd>Esc</kbd>
    </div>
    <div class="cb-command__results" data-cb-command-results></div>
    <div class="cb-command__foot"><span><kbd>↑</kbd> <kbd>↓</kbd> navegar</span><span><kbd>↵</kbd> abrir</span><span><kbd>Esc</kbd> cerrar</span></div>
  </div>
  <script type="application/json" data-cb-command-items>
    [
      { "group": "Páginas", "label": "Inicio", "href": "#paleta" },
      { "group": "Páginas", "label": "Facturación", "href": "#paleta", "keywords": "nit factura" },
      { "group": "Acciones", "label": "Crear cliente", "hint": "C" }
    ]
  </script>
</div>

Classes and modifiers

ClassDescription
.cb-commandBackdrop. .is-open shows it.
.cb-command__panelPalette box.
.cb-command__head, .cb-command__inputHeader with the search field.
.cb-command__results, .cb-command__group, .cb-command__itemList, group titles and results (.is-active is the selected one).
.cb-command__hint, .cb-command__empty, .cb-command__footHint, empty state and footer with shortcuts.

data-cb-* attributes

AttributeDescription
data-cb-commandEnables the palette.
data-cb-command-hotkey="k"Shortcut with ⌘ or Ctrl. Without a value it uses K.
data-cb-command-open="id"Button that opens it.
data-cb-command-itemsJSON with label, href, group, keywords and hint.
data-cb-command-emptyText shown before the query when there are no results. Default: "No results for".

JavaScript

EventWhen it fires
cb:open / cb:closeOn open or close.
cb:selectWhen a result is chosen (detail.item). Cancelable so you can handle navigation yourself.

Load results from JS (for example, from an API):

JS
cobalto.components.command.setItems('demo-command', [
  { group: 'Clientes', label: 'María Ramírez', href: '/clientes/12' },
]);

Accessibility

  • The input is a combobox with aria-activedescendant pointing to the active result.
  • Results form a listbox with aria-selected options.
  • Focus is trapped inside the palette; Esc closes it and returns focus.