Key hints
A bar of the keys that work right now, each one pressable, in a primary and a secondary tier.
Demo
Usage
import { KeyHints, ShortcutSheet, type Shortcut } from '@tuttn/slant'
// One list of shortcuts as data, for the bar and the sheet.
const shortcuts: Shortcut[] = [
{ id: 'open', keys: 'Enter', label: 'Open', group: 'List', onPress: open },
{ id: 'move', keys: ['ArrowUp', 'ArrowDown'], label: 'Move', group: 'List' }, // no onPress: a hint, not a button
{ id: 'new', keys: 'Mod+N', label: 'New task', group: 'General', onPress: create },
{ id: 'search', keys: 'Mod+K', label: 'Search', group: 'General', hint: 'secondary', onPress: search },
{ id: 'help', keys: '?', label: 'Shortcuts', group: 'General', hint: 'secondary', onPress: () => setSheet(true) },
{ id: 'rename', keys: 'F2', label: 'Rename', description: 'Rename the selected task', group: 'List', hint: false },
]
// The bar, with the keys bound on the whole page.
<KeyHints shortcuts={shortcuts} bindKeys />
<ShortcutSheet shortcuts={shortcuts} open={sheet} onOpenChange={setSheet} groups={['General', 'List']} />
// Bound to one element: the keys work only while focus is inside it.
const panel = useRef<HTMLDivElement>(null)
<div ref={panel} tabIndex={-1}>
<TaskList />
<KeyHints shortcuts={shortcuts} bindKeys={panel} label="Task list keys" size="md" />
</div>
// No bindKeys: the bar only shows the keys and presses them; your app handles the keys.
<KeyHints shortcuts={shortcuts} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
shortcuts | readonly Shortcut[] | — | The shortcuts, as data: the same list a ShortcutSheet takes. Hints show in the list's order, the primary tier first. |
label | string | 'Keyboard shortcuts' | The bar's accessible name. From the root's `messages.keyboardShortcuts`. |
bindKeys | boolean | RefObject<HTMLElement | null> | — | Runs a shortcut's `onPress` when its keys are pressed: `true` listens on the whole document, a ref on that element only. Keys typed into a field run only shortcuts with Mod, Ctrl, Alt or Meta, and keys from inside an open modal dialog that doesn't hold the bar are left to it. |
size | 'sm' | 'md' | 'sm' | Height of each hint on the control scale: 28 or 34px at regular density. |
Shortcut.id | string | — | A stable id, unique in the list. |
Shortcut.keys | string | readonly string[] | — | The keys as text: modifiers, then the key, joined with "+" ("Mod+Shift+K", "Alt+ArrowUp", "?", "F2"). Mod is ⌘ on Apple platforms and Ctrl elsewhere. An array lists alternatives, written "↑ / ↓". |
Shortcut.label | string | — | What it does, in a few words: the hint's text and the sheet's row. |
Shortcut.description | string | — | A longer explanation, shown in the ShortcutSheet only. |
Shortcut.group | string | — | Where it works: its ShortcutSheet section. Without one it goes under "General" (messages.generalShortcuts). |
Shortcut.hint | 'primary' | 'secondary' | false | 'primary' | Where the bar shows it: the primary tier, the muted secondary tier that hides when the bar is narrow, or not at all (the sheet only). |
Shortcut.onPress | () => void | — | Runs it. The hint becomes a button, and with bindKeys the keys run it too. |
Shortcut.disabled | boolean | — | Shown muted; the button is disabled and the keys don't run it. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root, the role="toolbar" (or "group") div. |
Keyboard
| Tab | moves into the bar, to the last hint that had focus (else the first enabled one), and on out of it: the bar is one tab stop |
| ← / → | move focus to the previous or next pressable hint; they wrap at the ends, skip disabled and hidden hints, and swap under right-to-left text |
| Home / End | jump to the first or last pressable hint |
| Enter / Space | press the focused hint: its onPress runs |
| A shortcut's keys | with bindKeys, run its onPress, on the whole page or inside the bound element |
Accessibility
- A
role="toolbar"namedlabel("Keyboard shortcuts" by default) when any hint is pressable, else arole="group". Pressable hints are native buttons witharia-keyshortcuts; the others are plain text. - The caps are drawn with Kbd inside one
<kbd>per combo and hidden from screen readers, which read the keys' names instead ("Control+Shift+K", "Meta+K"), not glyphs such as ⌘ and ⇧. - The secondary tier is hidden, not just squeezed, below about 32rem of the bar's width: out of the tab order and the arrows, and still listed in the ShortcutSheet.
- Keys typed into a field (input, textarea, select, contenteditable) only run shortcuts with Mod, Ctrl, Alt or Meta, so typing never fires a single-key shortcut.
Shortcuts as data
A shortcut is { id, keys, label, description?, group?, hint?, onPress?, disabled? }. The same list drives the bar and the ShortcutSheet: the bar shows the keys that work right now, the sheet every one of them, grouped. Swap the list as the context changes (a list focused, a dialog open) and the bar follows. hint: false keeps a shortcut to the sheet.
Tiers
Primary hints come first. Secondary ones follow at the inline end, muted, and step aside when the bar is narrower than about 32rem: a container query on the bar itself, so it holds before hydration too. The bar is its own container and takes its width from where it sits, so in a flex row give it room (flex: 1).
Binding keys
bindKeys runs the first enabled shortcut with onPress whose keys (any alternative) match the keydown, and prevents its default. true listens on the document; a ref takes only keys pressed inside that element (the listener stays on the document, so a key a component inside already handled counts as taken). It skips keys another handler already took (defaultPrevented), held-down repeats, Enter and Space on a focused button or link (they press it), keys typed into a field unless the combo has Mod, Ctrl, Alt or Meta, and, on the document, keys from inside an open modal dialog that doesn't hold the bar. Shortcuts the browser keeps for itself, such as ⌘W, ⌘T and ⌘N, still bind, but the browser may take them first; prefer combos it leaves alone.
Platforms
The keys are written once, with Mod for the primary modifier, and drawn for the reader's platform: ⌘ ⇧ K on a Mac, Ctrl Shift K elsewhere. A server render and the first client render draw the non-Apple caps, so the markup matches, then the browser's own. A combo that doesn't parse shows as written, with one console warning in development.