Documentation / Navigation & actions
CommandPalette
A dialog to reach any action of the application from the keyboard: a search field over a list of entries, opened from anywhere by ⌘K (Ctrl K outside Apple systems).
Three pieces: CommandPalette (the state, controlled or not), CommandGroup (a heading over its entries) and CommandItem (an action, its icon, a detail, its keys). The content stays declarative: the application writes its entries in the order they are shown, the palette hides those that do not match what is typed — accents and case aside, every word typed having to be found in the text or the keywords — and writes in bold what matches. A group left without entries goes with its heading.
The focus stays in the field: the arrows move over the entries (passing over the disabled ones), Enter chooses, Escape closes and gives the focus back to where it was. An entry chosen calls its onSelect and closes the palette. The shortcuts of an entry are shown only: the application binds them.
To search a server, filter={false} leaves the sorting to the application, query and onQueryChange hand it what is typed and loading shows the wait. A "use client" component, rendered under the <body> of the window it is shown in, with the accessibility contract of Modal.
Examples
Basic usage
Groups, icons, shortcuts shown. ⌘K (Ctrl K) opens and closes the palette from anywhere in the page; the button does the same for whoever has no keyboard.
import { useState } from "react";
import { Button, CommandGroup, CommandItem, CommandPalette, toast, Toaster } from "@synertech/ui";
function Icon({ d }: { d: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8}>
<path strokeLinecap="round" strokeLinejoin="round" d={d} />
</svg>
);
}
const INVOICE = "M7 3h7l4 4v14H7zM14 3v4h4M10 12h5M10 16h5";
const CLIENT = "M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM5 21a7 7 0 0 1 14 0";
const CHART = "M4 20V10M10 20V4M16 20v-7M22 20H2";
const GEAR =
"M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-2.9 1.2V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-2.9-1.2l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1A1.7 1.7 0 0 0 3 15H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.2-2.9l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1A1.7 1.7 0 0 0 9 3.1V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 2.9 1.2l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1A1.7 1.7 0 0 0 20.9 9H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 2z";
export function Basic() {
const [open, setOpen] = useState(false);
const run = (what: string) => toast(what);
return (
<>
<Button onClick={() => setOpen(true)}>Rechercher une action</Button>
<CommandPalette open={open} onOpenChange={setOpen}>
<CommandGroup label="Actions">
<CommandItem
icon={<Icon d={INVOICE} />}
shortcut="N"
onSelect={() => run("Nouvelle facture")}
>
Créer une facture
</CommandItem>
<CommandItem icon={<Icon d={CLIENT} />} onSelect={() => run("Nouveau client")}>
Créer un client
</CommandItem>
<CommandItem icon={<Icon d={INVOICE} />} disabled>
Créer un avoir
</CommandItem>
</CommandGroup>
<CommandGroup label="Navigation">
<CommandItem
icon={<Icon d={CHART} />}
shortcut={["G", "R"]}
onSelect={() => run("Rapports")}
>
Aller aux rapports
</CommandItem>
<CommandItem
icon={<Icon d={GEAR} />}
shortcut={["G", "P"]}
onSelect={() => run("Paramètres")}
>
Aller aux paramètres
</CommandItem>
</CommandGroup>
</CommandPalette>
<Toaster />
</>
);
}Keywords and details
keywords adds words that find the entry without being shown: type "bill" or "avoir". description writes a second line, which is not searched. An entry whose text is not a plain string is searched by its textValue.
import { useState } from "react";
import { Button, CommandGroup, CommandItem, CommandPalette } from "@synertech/ui";
export function KeywordsAndDetails() {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>Ouvrir la palette</Button>
<CommandPalette open={open} onOpenChange={setOpen}>
<CommandGroup label="Factures">
<CommandItem keywords={["bill", "invoice"]} description="Brouillon, à envoyer">
Créer une facture
</CommandItem>
<CommandItem keywords={["credit note", "remboursement"]}>Créer un avoir</CommandItem>
</CommandGroup>
<CommandGroup label="Clients">
<CommandItem description="contact@atelier-boreal.example">Atelier Boréal</CommandItem>
<CommandItem description="compta@eclat.example">Éclat Studio</CommandItem>
<CommandItem textValue="Coopérative du Vieux-Port" description="3 factures en retard">
<span className="font-medium">Coopérative du Vieux-Port</span>{" "}
<span className="text-xs text-red-700 dark:text-red-400">· en retard</span>
</CommandItem>
</CommandGroup>
</CommandPalette>
</>
);
}Searching a server
filter={false}: the palette sets nothing aside, the application hands it the results of its search. query and onQueryChange hand it what is typed, loading puts a spinner in the field and aria-busy on the list. The actions of the first group are still filtered by the palette.
import { useEffect, useState } from "react";
import { Button, CommandGroup, CommandItem, CommandPalette, toast, Toaster } from "@synertech/ui";
interface Client {
id: string;
name: string;
city: string;
}
const CLIENTS: Client[] = [
{ id: "1", name: "Atelier Boréal", city: "Montréal" },
{ id: "2", name: "Éclat Studio", city: "Québec" },
{ id: "3", name: "Coopérative du Vieux-Port", city: "Marseille" },
{ id: "4", name: "Brasserie des Îles", city: "Gaspé" },
{ id: "5", name: "Menuiserie Ouellet", city: "Rimouski" },
];
function searchClients(query: string): Promise<Client[]> {
const folded = (text: string) => text.normalize("NFD").replace(/\p{M}/gu, "").toLowerCase();
return new Promise((resolve) =>
setTimeout(
() => resolve(CLIENTS.filter((client) => folded(client.name).includes(folded(query)))),
400
)
);
}
function Icon({ d }: { d: string }) {
return (
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8}>
<path strokeLinecap="round" strokeLinejoin="round" d={d} />
</svg>
);
}
const CLIENT = "M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM5 21a7 7 0 0 1 14 0";
export function ServerSearch() {
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
const [clients, setClients] = useState<Client[]>([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
if (!open || query.trim() === "") {
setClients([]);
setLoading(false);
return;
}
let current = true;
setLoading(true);
searchClients(query.trim()).then((found) => {
if (!current) return;
setClients(found);
setLoading(false);
});
return () => {
current = false;
};
}, [open, query]);
return (
<>
<Button onClick={() => setOpen(true)}>Chercher un client</Button>
<CommandPalette
open={open}
onOpenChange={(next) => {
setOpen(next);
if (!next) setQuery("");
}}
query={query}
onQueryChange={setQuery}
loading={loading}
filter={false}
placeholder="Nom d’un client, ou une action..."
>
<CommandGroup label="Actions" filter>
<CommandItem icon={<Icon d={CLIENT} />}>Créer un client</CommandItem>
</CommandGroup>
<CommandGroup label="Clients">
{clients.map((client) => (
<CommandItem
key={client.id}
icon={<Icon d={CLIENT} />}
description={client.city}
onSelect={() => toast(client.name)}
>
{client.name}
</CommandItem>
))}
</CommandGroup>
</CommandPalette>
<Toaster />
</>
);
}Empty state
emptyState takes the place of "No results" when no entry is left: an EmptyState of size sm, here with an action that takes up what was typed.
import { useState } from "react";
import { Button, CommandGroup, CommandItem, CommandPalette, EmptyState, toast, Toaster } from "@synertech/ui";
interface Client {
id: string;
name: string;
city: string;
}
export function CustomEmptyState() {
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
return (
<>
<Button onClick={() => setOpen(true)}>Ouvrir la palette</Button>
<CommandPalette
open={open}
onOpenChange={(next) => {
setOpen(next);
if (!next) setQuery("");
}}
query={query}
onQueryChange={setQuery}
emptyState={
<EmptyState
size="sm"
title={`Aucun client « ${query.trim()} »`}
description="Créez-le maintenant, vous compléterez sa fiche plus tard."
action={
<Button
size="sm"
onClick={() => {
toast.success(`Client « ${query.trim()} » créé`);
setOpen(false);
setQuery("");
}}
>
Créer le client
</Button>
}
className="py-6"
/>
}
>
<CommandGroup label="Clients">
<CommandItem>Atelier Boréal</CommandItem>
<CommandItem>Éclat Studio</CommandItem>
</CommandGroup>
</CommandPalette>
<Toaster />
</>
);
}Staying open
closeOnSelect={false} keeps the palette open after a choice: to toggle several settings in a row. An entry can also say it for itself alone.
import { useState } from "react";
import { Button, CommandGroup, CommandItem, CommandPalette } from "@synertech/ui";
export function StayOpen() {
const [open, setOpen] = useState(false);
const [settings, setSettings] = useState({ compact: false, weekends: true, totals: true });
const toggle = (key: keyof typeof settings) =>
setSettings((current) => ({ ...current, [key]: !current[key] }));
const mark = (on: boolean) => (on ? "Activé" : "Désactivé");
return (
<>
<Button onClick={() => setOpen(true)}>Réglages d’affichage</Button>
<CommandPalette open={open} onOpenChange={setOpen} closeOnSelect={false}>
<CommandGroup label="Affichage">
<CommandItem description={mark(settings.compact)} onSelect={() => toggle("compact")}>
Mode compact
</CommandItem>
<CommandItem description={mark(settings.weekends)} onSelect={() => toggle("weekends")}>
Afficher les week-ends
</CommandItem>
<CommandItem description={mark(settings.totals)} onSelect={() => toggle("totals")}>
Afficher les totaux
</CommandItem>
</CommandGroup>
</CommandPalette>
</>
);
}API reference
Components
A dialog to reach any action of the application from the keyboard: a search field over a list of entries, opened from anywhere by ⌘K (Ctrl K elsewhere than on Apple systems).
The entries are declared by the application, CommandItems in CommandGroups, and keep their order: the palette hides those that do not match what is typed (accents and case aside) and writes in bold where they do. The focus stays in the field; the arrows move over the entries and Enter chooses one, which calls its onSelect and closes the palette.
A modal dialog under the <body> of the window it is shown in, with the contract of Modal: the focus kept inside, Escape, the focus given back, the page behind it inert.
import { CommandPalette } from "@synertech/ui";Props — CommandPaletteProps
| Prop | Type | Default | Description |
|---|---|---|---|
children |
ReactNode |
— | The |
className |
string |
— | Classes of the panel. |
closeOnSelect |
boolean |
true |
Close the palette once an entry is chosen. A |
defaultOpen |
boolean |
false |
Whether the palette starts open, when it holds the state itself. |
emptyState |
ReactNode |
— | Shown when no entry is offered, in place of "No results". An |
filter |
boolean |
true |
Hide the entries that do not match the search: every word typed must be found in an entry's text or keywords, case and accents aside. |
footer |
ReactNode |
— | The foot of the palette. Left out, the keys it answers to; |
hotkey |
string | false |
"mod+k" |
The key combination that opens and closes the palette from anywhere in the document: |
label |
string |
— | Accessible name of the dialog. Defaults to the |
labels |
UILabelsOf<"commandPalette"> |
— | The texts of this palette, over those of the |
loading |
boolean |
false |
The entries are being loaded: a spinner in the field, "Loading..." instead of "No results", and the list |
onOpenChange |
(open: boolean) => void |
— | Called with the state the palette asks for: the hotkey, Escape, a press outside, an entry chosen. |
onQueryChange |
(query: string) => void |
— | Called with the text of the search field at every keystroke. |
open |
boolean |
— | Whether the palette is open, when the application holds the state. |
overlayClassName |
string |
— | Classes of the overlay behind the panel. |
placeholder |
string |
— | Placeholder of the search field. Defaults to the |
query |
string |
— | The text of the search field, when the application holds it: to search a server with it, or to keep it from one opening to the next. Left out, the palette holds it and empties it when it closes. |
A group of entries of a CommandPalette, under its heading. A group none of whose entries matches the search is hidden with its heading.
import { CommandGroup } from "@synertech/ui";Props — CommandGroupProps
| Prop | Type | Default | Description |
|---|---|---|---|
children |
ReactNode |
— | The |
filter |
boolean |
— | Whether the palette hides the entries of this group that do not match the search. Left out, the palette's |
label |
ReactNode |
— | The heading of the group, its accessible name too: "Actions", "Go to". |
+ 278 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).
An entry of a CommandPalette: what it does, an icon, a detail and its keys. Hidden while it does not match the search, unless the palette or its group does not filter.
import { CommandItem } from "@synertech/ui";Props — CommandItemProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | What the entry does, in a few words: "Create an invoice". A string is written with what matches the search in bold. |
closeOnSelect |
boolean |
— | Close the palette once chosen. Left out, the palette's |
description |
ReactNode |
— | A second line under the text: an address, a detail. Not searched. |
disabled |
boolean |
false |
The entry is shown but cannot be chosen; the arrows pass over it. |
icon |
ReactNode |
— | A picture before the text, decorative: hidden from assistive technologies. |
keywords |
string[] |
— | More words the entry is found by, shown nowhere: "bill" and "facture" for "Create an invoice". |
onSelect |
() => void |
— | Called when the entry is chosen, by a click or by Enter. |
shortcut |
string | string[] |
— | The keys of the action, shown at the end of the entry: |
textValue |
string |
— | The text the entry is searched by. Left out, the text of |
+ 277 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).