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
| Class | Description |
|---|---|
.cb-command | Backdrop. .is-open shows it. |
.cb-command__panel | Palette box. |
.cb-command__head, .cb-command__input | Header with the search field. |
.cb-command__results, .cb-command__group, .cb-command__item | List, group titles and results (.is-active is the selected one). |
.cb-command__hint, .cb-command__empty, .cb-command__foot | Hint, empty state and footer with shortcuts. |
data-cb-* attributes
| Attribute | Description |
|---|---|
data-cb-command | Enables 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-items | JSON with label, href, group, keywords and hint. |
data-cb-command-empty | Text shown before the query when there are no results. Default: "No results for". |
JavaScript
| Event | When it fires |
|---|---|
cb:open / cb:close | On open or close. |
cb:select | When 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
comboboxwitharia-activedescendantpointing to the active result. - Results form a
listboxwitharia-selectedoptions. - Focus is trapped inside the palette; Esc closes it and returns focus.