Liking cljdoc? Tell your friends :D

x-command-palette

A searchable command list overlay (⌘K pattern). Opens as a modal with a text input that filters a list of commands supplied via the items JS property. Supports keyboard navigation, groups, disabled items, and a scrim backdrop.


Tag

<x-command-palette></x-command-palette>

Attributes

AttributeTypeDefaultDescription
openbooleanfalseWhether the palette is visible
modalboolean†trueAdds focus trap and blocks background interaction
dismissibleboolean†trueAllow closing via Escape or backdrop click
no-scrimbooleanfalseHides the backdrop scrim even in modal mode
close-on-scrimboolean(see below)Close when backdrop is clicked (default: same as scrim visibility)
close-on-escapeboolean†trueClose on Escape key
disabledbooleanfalseDisables the palette and all interactions
placeholderstring"Search…"Input placeholder text
empty-textstring"No results"Text shown when no items match the query
labelstringaria-label on the dialog element
portalstringCSS selector of element to portal the overlay into

†Default-true boolean: absent = true; attribute="false" = false.


Properties

PropertyTypeDescription
itemsarrayCommand items. Each item: { id, label, keywords?, group?, value?, icon?, disabled? }
openbooleanReflects open attribute
disabledbooleanReflects disabled attribute

Item shape

{
  id:        string           // required, unique identifier
  label:     string           // required, displayed text
  keywords?: string | string[] // extra search terms
  group?:    string           // group heading
  value?:    any              // arbitrary payload in select event detail
  icon?:     string           // icon glyph or URL
  disabled?: boolean          // greys out item, skips in keyboard nav
}

Events

EventCancelableDetailDescription
x-command-palette-open-requestyes{}Fired before opening
x-command-palette-openno{}Fired after opening
x-command-palette-close-requestyes{}Fired before closing
x-command-palette-closeno{}Fired after closing
x-command-palette-select-requestyes{ item }Fired before item selection
x-command-palette-selectno{ item }Fired after item selected
x-command-palette-query-changeno{ query: string }Fired as the user types

Keyboard

KeyAction
ArrowDownMove to next enabled item (wraps)
ArrowUpMove to previous enabled item (wraps)
EnterSelect highlighted item
EscapeClose palette (if close-on-escape is true)

Accessibility

  • Renders a <dialog> element.
  • Input has role="combobox" with aria-expanded, aria-controls, aria-activedescendant.
  • The list has role="listbox"; each item has role="option".
  • Disabled items have aria-disabled="true".

Examples

Basic usage

<x-command-palette id="palette"></x-command-palette>
<button onclick="document.getElementById('palette').setAttribute('open','')">
  Open (⌘K)
</button>
<script>
  const palette = document.getElementById('palette');
  palette.items = [
    { id: 'new',      label: 'New file',     group: 'File' },
    { id: 'open',     label: 'Open file…',   group: 'File' },
    { id: 'settings', label: 'Preferences',  group: 'App'  },
  ];
  palette.addEventListener('x-command-palette-select', e => {
    console.log('selected', e.detail.item);
    palette.removeAttribute('open');
  });
  palette.addEventListener('x-command-palette-close', () => {
    palette.removeAttribute('open');
  });
</script>

ClojureScript (hiccup renderer)

[:x-command-palette
 {:open (when (:palette-open state) "")
  :on-x-command-palette-close
  (fn [_] (swap! app dissoc :palette-open))
  :on-x-command-palette-select
  (fn [e]
    (handle-command (.. e -detail -item))
    (swap! app dissoc :palette-open))}]

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close