Overlays
Command palette
Dialog opened with Ctrl+K to find a command by typing.
Example
cr-command-palette is a modal dialog with a field that filters a list of commands. Commands are child option elements (value and label); data-group puts them under a group heading, in the order the groups appear. A disabled option is shown but cannot be chosen.
Ctrl+K (⌘K on Mac) opens it from anywhere in the page; shortcut={false} turns this shortcut off, if your page already uses it or has several palettes. You can also drive it with open. onCrSelect gives the chosen value, then the palette closes; set your state back to false in onCrClose.
Search ignores accents and case ("parametres" finds "Paramètres"), and the matching letters turn bold and coloured (::part(match) to restyle them). With no match, the palette shows the "No results" empty state.
The dialog drops into place with a slight bounce, and the highlight of the active command slides from row to row.
'use client';
import { useState } from 'react';
import { CrBox, CrButton, CrCommandPalette } from '@cristal-ui/react';
export default function CommandPaletteBasic() {
const [open, setOpen] = useState(false);
const [command, setCommand] = useState('');
return (
<CrBox display="flex" gap="sm" align="center">
<CrButton variant="outline" onClick={() => setOpen(true)}>
Commands
</CrButton>
<span>{command && `Chosen command: ${command}`}</span>
<CrCommandPalette label="Command palette" shortcut={false} open={open} onCrSelect={(event) => setCommand(event.detail)} onCrClose={() => setOpen(false)}>
<option value="home" data-group="Go to">
Home
</option>
<option value="orders" data-group="Go to">
My orders
</option>
<option value="settings" data-group="Go to">
Settings
</option>
<option value="invoice" data-group="Actions">
Create an invoice
</option>
<option value="export" data-group="Actions">
Export as CSV
</option>
<option value="logout" data-group="Actions">
Sign out
</option>
</CrCommandPalette>
</CrBox>
);
}On this site, Ctrl+K already opens the documentation search, so the demo turns its shortcut off with shortcut={false}.
If the user asked their system to reduce motion, the dialog appears without dropping and the highlight jumps straight from row to row.
Accessibility
- The palette is a modal dialog (native
dialog) named bylabel: the rest of the page is inert while it is open. - The field follows the combobox pattern:
role="combobox", arole="listbox"list linked witharia-controls, the active command given byaria-activedescendant; focus stays in the field while moving through the list. - Up and Down move through the commands, skipping disabled ones, Enter picks, Escape closes. Clicking outside the dialog closes it too.
- Groups are
role="group"elements named by their heading. - "No results" is announced by a status region; the field then has
aria-expanded="false". - On close, focus goes back to the element that had it before opening.
- The active command also stands out without colour: a bar on the left and bolder text.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
label (obligatoire) | string | — | Nom de la fenêtre et du champ pour les lecteurs d'écran (« Palette de commandes »). |
open | boolean | false | Ouvre ou ferme la palette. |
placeholder | string | — | Texte indicatif du champ (« Rechercher… » par défaut). |
shortcut | boolean | true | Ouvre la palette avec Ctrl+K (⌘K sur Mac), où que soit le focus dans la page. |
Events
| React | event.detail | Description |
|---|---|---|
onCrClose | void | Émis à chaque fermeture : Échap, choix d'une commande, clic hors de la fenêtre ou open à false. |
onCrSelect | string | Émis au choix d'une commande, juste avant la fermeture ; detail : sa valeur. |
CSS parts
To style it from outside: cr-command-palette::part(name) { … }
| Part | Description |
|---|---|
dialog | L'élément dialog. |
group | Un titre de groupe. |
input | Le champ de recherche. |
listbox | La liste des commandes. |
match | Les lettres qui correspondent à la recherche. |
option | Une commande. |