Shortcut sheet
Every keyboard shortcut in a dialog, grouped by where it works, with search.
Demo
Usage
import { Button, KeyHints, ShortcutSheet, type Shortcut } from '@tuttn/slant'
// The list a KeyHints bar takes. group is the section; description shows here only.
const shortcuts: Shortcut[] = [
{ id: 'new', keys: 'Mod+N', label: 'New task', group: 'General', onPress: create },
{ id: 'help', keys: '?', label: 'Shortcuts', group: 'General', onPress: () => setSheet(true) },
{ id: 'move', keys: ['ArrowUp', 'ArrowDown'], label: 'Move', group: 'List' },
{ id: 'rename', keys: 'F2', label: 'Rename', description: 'Rename the selected task', group: 'List', hint: false },
]
// Controlled, opened by "?" through the bar's bound keys.
const [sheet, setSheet] = useState(false)
<KeyHints shortcuts={shortcuts} bindKeys />
<ShortcutSheet shortcuts={shortcuts} open={sheet} onOpenChange={setSheet} groups={['General', 'List']} />
// Uncontrolled, from a trigger; no search for a short list.
<ShortcutSheet shortcuts={shortcuts} trigger={<Button>Keyboard shortcuts</Button>} searchable={false} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
shortcuts | readonly Shortcut[] | — | The shortcuts, as data: the same list a KeyHints bar takes. Every one shows here, whatever its `hint`. |
open | boolean | — | Whether it is open. Pass it with onOpenChange to control it; leave it out and it keeps its own state. |
defaultOpen | boolean | false | Whether it starts open, when uncontrolled. |
onOpenChange | (open: boolean) => void | — | Called with the new state whenever it opens or closes: the trigger, Escape, the backdrop, the close button. |
trigger | ReactElement | — | An element that opens it when clicked (a Button, say), rendered just before it with `aria-haspopup="dialog"`. |
title | ReactNode | 'Keyboard shortcuts' | The dialog's title, which names it. From the root's `messages.keyboardShortcuts`. |
groups | readonly string[] | [] | The sections' order, by group name. Groups it doesn't list follow, in the order they first appear in `shortcuts`. |
searchable | boolean | true | Shows a search field over the list, focused on opening, that filters by label, description, group and keys. |
className | string | — | A class for the dialog surface. |
style | CSSProperties | — | Styles for the dialog surface; `--sl-dialog-width` sets its width. |
ref | Ref<HTMLDialogElement> | — | Reaches the <dialog>. className and style go on it too, the dialog surface; there are no other attributes. |
Keyboard
| Typing | filters the list, from the search field, which has focus when the sheet opens |
| Escape | clears the search while it has text, then closes the sheet, with focus back where it was |
| Tab / Shift+Tab | move between the search, the close button and the list (when it scrolls), and stay inside the sheet |
Accessibility
- A modal Dialog named by its title ("Keyboard shortcuts" by default), with the rest of the page inert. The search field is named "Search shortcuts".
- Each group is a
<section>named by its<h3>, holding a<dl>: the label (and description) in the<dt>, the keys in the<dd>. - The keys read as their names ("Control+Shift+K", "ArrowUp / ArrowDown"), not as glyphs; the caps are hidden from screen readers.
- The number of matches is announced politely once the query settles, about 400 ms after the last keystroke; no match shows "No shortcuts for …".
Groups
Sections follow groups, then any group it doesn't list, in the order they first appear in shortcuts. Shortcuts without a group go under "General" (messages.generalShortcuts), together with any group of that name. Every shortcut shows, whatever its hint; disabled ones are muted.
Search
The search matches, ignoring case, the label, the description, the group and the keys, both as written and as drawn: "mod+k", "ctrl+k", "⌘k" and "control" all find Mod+K. It is cleared each time the sheet closes. searchable={false} leaves it out, and focus goes to the close button on opening.
Opening it
Pass a trigger, or hold open yourself and open it from a key: a ? shortcut with onPress in a KeyHints bar bound to the page does it. While the sheet is open, a bar bound to the document leaves the sheet's keys alone, so typing ? in its search only searches. It is a Dialog: Escape, the backdrop and the close button close it, and it is always rendered, so its rows are in the page's HTML.