CommandPalette
CommandPalette is a command menu opened with Ctrl+K or ⌘K: a search input above a list of commands filtered with fuzzy matching, with groups, nested pages, recent commands, async search and full keyboard support, in a modal dialog or in place.
Import #
import { CommandPalette } from '@exhaustive-ui/core/commandpalette';Basic #
CommandPalette opens a search input above a list of commands in a modal dialog. It listens to Ctrl+K, or ⌘K on a Mac, anywhere on the page: press it now, or use the button. Typing filters the commands with fuzzy matching, the arrow keys move the active command and enter runs its command callback. visible opens and closes it, and the focus goes back to where it was when it closes.
Last command: none
Groups #
An item with items is a group: its label is a heading above its commands. A command can have an icon, a description, keywords that find it without being shown, a shortcut hint rendered as keys (mod reads ⌘ on Apple devices and Ctrl elsewhere) and a disabled state. Besides command, a routerLink or a url navigates. While searching, the best matches come first, inside and across groups. A shortcut of false leaves the keyboard shortcut to the first palette of this page.
Nested Pages #
An item with children opens a page of other commands instead of running. The search starts again on the new page, with the placeholder of the item, and a breadcrumb leads back to the previous pages. backspace in the empty input goes back one page, and onPageChange follows the navigation. Pages can be nested at any depth.
Theme: System, language: English
Custom Filter #
The built-in filter matches the characters of the query in order, ignoring case and accents, in the label and the keywords; consecutive characters, word starts and exact matches rank higher, and the matched characters of the label are highlighted. A filter function replaces it: it receives an item and the query and returns false to hide the item, or a score, higher first. Here only the words that start with the query match, and a match on the label outranks one on a keyword. commandPaletteFuzzyMatch is exported to build on the built-in matching.
Async Search #
For a server-side search, set shouldFilter to false and answer onSearch by setting items: the palette shows them as given. delay waits for a pause in typing before the event, and loading shows a spinner in the search row, and a loading message while there is no result yet. Here a fake server answers after 600 milliseconds, and the handler keeps only the latest answer.
Inline and Recent #
With inline, the palette is rendered in place, without the dialog, in a sidebar or a launcher page for instance. Its shortcut gives the focus to its search input, and escape clears the query, then leaves the page. recent adds a group of the last commands run at the top of the root page, up to recentLimit; with recentStorageKey they are kept in the local storage between visits. Run a command to see it.
Last command: none
Template #
The item template renders an option, with the item, whether it is active, the query and the segments of the label to highlight; group renders a heading with its label and its count. The header template goes above the search input, empty replaces the empty message and footer replaces the key hints, all with the query, the page and the number of results.
Accessibility #
Screen Reader
The palette opens in a modal dialog labelled by the title label, with the focus trapped inside and given back to the element that had it when the palette closes. The search input follows the combobox pattern with a listbox popup, while the focus stays in the input: it has the combobox role, aria-autocomplete="list", aria-expanded while there are results, aria-controls pointing to the listbox and aria-activedescendant to the active option, which has aria-selected. Disabled commands have aria-disabled, and a command that opens a page has aria-haspopup.
Each group is a group element labelled by its heading. A polite live region announces the number of results as the query changes, the empty and loading messages, and the name of a page when it opens. The breadcrumb of the pages is a navigation landmark whose current page has aria-current="page"; its buttons are reached with the mouse, while the keyboard goes back with backspace. The key hints of the footer are hidden from screen readers, which announce the keys of their own.
Every text comes from the labels input: the title of the dialog, the inputLabel of the search input (or ariaLabel and ariaLabelledBy), the listLabel of the listbox, the resultsMessage, emptyMessage, loadingMessage and pageMessage of the live region, and the hints. The empty message defaults to the emptySearchMessage of the translation configuration.
<p-command-palette [items]="items" [labels]="{ title: 'Commandes', inputLabel: 'Rechercher une commande', resultsMessage: '{0} résultats' }" />
<span id="launcher-label">Launcher</span>
<p-command-palette inline [items]="items" ariaLabelledBy="launcher-label" />Keyboard Support
| Key | Function |
|---|---|
| ctrl + k / ⌘ + k | Opens the palette from anywhere on the page, or closes it. Another key is set with shortcut; in the inline mode it gives the focus to the search input. |
| down arrow | Activates the next command, from the last one to the first unless loop is false. Disabled commands are skipped. |
| up arrow | Activates the previous command, from the first one to the last unless loop is false. |
| home / end | Activates the first or the last command. |
| page up / page down | Moves the active command by the number of commands visible in the list. |
| enter | Runs the active command, or opens its page. |
| backspace | In the empty search input, goes back to the previous page. |
| escape | Closes the palette and gives the focus back. In the inline mode, clears the query, then goes back to the previous page. |
| tab | Stays in the dialog, whose only field is the search input. |