Shortcut recorder
A field that records a key combination and refuses reserved or taken ones, saying why.
Demo
Usage
import { Field, ShortcutRecorder } from '@tuttn/slant'
// Uncontrolled, in a Field: the label names it, and a hidden input submits
// the shortcut's canonical text under `name`.
<Field>
<Field.Label>Open the command bar</Field.Label>
<ShortcutRecorder defaultValue="Mod+K" name="palette"
conflicts={[{ keys: 'Mod+S', label: 'Save' }, 'Mod+P']} />
</Field>
// Controlled: null means no shortcut, and the placeholder shows. One value
// serves every platform: "Mod+Shift+K" draws as ⌘ ⇧ K on a Mac, Ctrl Shift K elsewhere.
const [shortcut, setShortcut] = useState<string | null>('Mod+Shift+K')
<ShortcutRecorder label="Archive" value={shortcut} onValueChange={setShortcut} />
// Your own rule, after the built-in ones: return the reason to refuse.
<ShortcutRecorder label="Jump to a project"
validate={combo => combo.alt ? 'Alt is kept for the editor' : null} />
// Inside a Field: its description and error describe the recorder, and its
// invalid, disabled and readOnly reach it.
<Field invalid={!shortcut}>
<Field.Label>Archive</Field.Label>
<ShortcutRecorder value={shortcut} onValueChange={setShortcut} />
<Field.Error>Choose a shortcut, or turn archiving off.</Field.Error>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | null | — | The shortcut as canonical text ("Mod+Shift+K", Mod being ⌘ on Apple platforms and Ctrl elsewhere), or null for none. Pass it with onValueChange to control the recorder; leave it out to let it keep its own. |
defaultValue | string | null | null | The shortcut an uncontrolled recorder starts with. |
onValueChange | (value: string | null) => void | — | Called with the canonical text of a newly recorded shortcut, or null when it is cleared. |
recording | boolean | — | Whether it is listening for a shortcut. Pass it with onRecordingChange to control it; leave it out to let the recorder keep its own. |
defaultRecording | boolean | false | Whether an uncontrolled recorder starts listening (it takes focus to do so). |
onRecordingChange | (recording: boolean) => void | — | Called with the new state whenever recording starts or stops. |
conflicts | readonly ShortcutConflict[] | — | Shortcuts other actions already use, refused with the action's name when given (`{ keys: 'Mod+S', label: 'Save' }`). The recorder's own value is never a conflict, so the whole list of an app's shortcuts can be passed. |
allowReserved | boolean | false | Accepts the combinations the browser or the system keeps for itself (Mod+W, Mod+T, Mod+Q and the like), which a page can't reliably take. |
requireModifier | boolean | true | Refuses a printable key (a letter, digit, symbol or Space, with or without Shift) unless Mod, Ctrl, Alt or Meta is held, since it would fire while the reader types. Function keys and named keys such as F5 or ArrowUp may go alone. |
validate | (combo: KeyCombo, text: string) => string | null | undefined | — | Your own rule, run after the built-in ones: return a reason to refuse the combination, or nothing to accept it. `text` is its canonical text. |
clearable | boolean | true | Shows a × button that clears the shortcut while there is one. |
placeholder | ReactNode | 'None' | What the recorder shows while there is no shortcut. From the root's `messages.noShortcut`. |
label | string | — | The accessible name, unless a Field's label or `aria-labelledby` names the recorder. The shortcut is read after it. |
name | string | — | Submits the shortcut with a form: a hidden input with this name carries its canonical text (`''` while there is none). |
form | string | — | The id of the form the hidden input belongs to, when the recorder sits outside it. |
size | 'sm' | 'md' | 'lg' | 'md' | Height on the control scale: 28, 34 or 40px at regular density. |
invalid | boolean | — | Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this. |
readOnly | boolean | — | Focusable and showing its shortcut, but never records and has no clear button. Inside a Field, the field's `readOnly` does this. |
disabled | boolean | — | Dims the recorder and ignores input; a disabled recorder is not submitted. Inside a Field, the field's `disabled` does this. |
…rest | ButtonHTMLAttributes<HTMLButtonElement> | — | The ref and every other attribute (id, autoFocus, aria-*, data-*, handlers) reach the record button. Your onKeyDown and onClick run first and can call preventDefault to stop the recorder’s own handling. `className` and `style` go on the field drawn around it; `name` and `form` on the hidden input that carries the value. |
ShortcutConflict | string | { keys: string; label: string } | — | A shortcut another action has, in the text `value` takes (`"Mod+S"`), alone or with the action’s name: “⌘ S is already used by Save”. |
KeyCombo | { key: string; mod: boolean; ctrl: boolean; alt: boolean; shift: boolean; meta: boolean } | — | What `validate` is given: the key (an uppercase letter or digit, a symbol, or a name such as `ArrowUp`, `Space`, `F5`) and the modifiers held. `mod` is ⌘ on Apple platforms and Ctrl elsewhere; `ctrl` is Control on a Mac, and `meta` the Windows key elsewhere. |
Keyboard
| Enter / Space | start recording (a click does too, and a second click cancels) |
| Ctrl, Alt, Shift, ⌘ | while recording, held alone, show as key caps until the key that completes the shortcut |
| any other combination | while recording, is checked and kept, ending the recording, or refused with the reason under the field, recording on |
| Escape | while recording, cancels it and keeps the shortcut; nothing else hears it, so a Dialog around the field stays open |
| Backspace / Delete | while recording, clear the shortcut |
| Tab / Shift+Tab | end the recording without a change and move focus on, as ever |
Accessibility
- The record button is named by a Field’s label (or
label, oraria-labelledby) followed by the shortcut, througharia-labelledby: “Open the command bar, Control K”. The caps are hidden from screen readers and the shortcut is spoken in words, fromaria-keyshortcuts’ names. - Starting a recording is said politely, from a hint in the button’s description, and so is each outcome: the shortcut set, cleared, or the reason it was refused.
- A refusal shows under the field, in the button’s
aria-describedby, witharia-invalidon the button while it shows. The Field’s description and error describe it too, and itsinvalidsetsaria-invalid. - While recording, every key but Tab is the recorder’s, so nothing on the page or in the browser fires; Tab always leaves, and Escape always gets out. Moving focus away ends the recording.
- Read-only, the button stays focusable and reads its shortcut but never records. Disabled, it is a disabled button and submits nothing.
- The recorder doesn’t set
aria-keyshortcutson itself, which would claim the shortcut presses it. Put that on the control the shortcut triggers.
Recording
Pressing the field starts it listening: the border takes the accent, a slanted pixel pulses, and the field shows the modifiers held so far before the prompt. The first key that isn’t a modifier completes the combination, which is checked and either kept, ending the recording, or refused, with the reason under the field and the recording still on for another try. A key held down counts once. Escape cancels, Backspace or Delete clears, and Tab, a click elsewhere or a second click on the field ends it without a change. With recording and onRecordingChange you control it; setting it on moves focus to the field, which the keys need.
What it refuses
In this order, the first check that fails is the reason given:
- Combinations the browser or the system keeps (closing, opening and switching tabs and windows, quitting, the address bar, reloading), unless
allowReserved. - A printable key without Mod, Ctrl, Alt or Meta, which would fire while the reader types: K, Shift+K, Space. Named keys may go alone (F5, ArrowUp, Enter, Home).
requireModifier={false}lets single keys through, for a game or a tool with no text to type. - One of
conflicts, by the keys it presses on this platform, soMod+SandMeta+Sare the same on a Mac. The recorder’s own shortcut is never a conflict, so you can pass the app’s whole list. - Your
validate, given the combination and its text, which returns the reason as a string.
One value for every platform
The value is text: modifiers, then the key, joined with +, in a fixed order ("Mod+Alt+Shift+K"). Mod is ⌘ on Apple platforms and Ctrl elsewhere, so a shortcut recorded on a Mac works on Windows, and the field draws it in each platform’s own caps and order: ⌥ ⇧ ⌘ K, or Ctrl Alt Shift K. A shortcut with Ctrl on a Mac keeps Ctrl, and one with the Windows key keeps Meta. Letters are recorded as the letter on the key even where a modifier types something else, such as ⌥K on a Mac or a letter on a Hebrew layout. The platform is read once the field has mounted, so a server render and the browser agree.
Forms
Given a name, the recorder renders a hidden input carrying the shortcut’s text, or an empty string while there is none, so it submits with a plain form; form ties it to a form elsewhere on the page. The button itself is type="button", so it never submits.
Look
It is drawn like an Input at each size, with the shortcut as Kbd caps, kept in left-to-right order under right-to-left text, as shortcuts are written. With a shortcut and clearable, a small × at the inline end clears it and puts focus back on the field. A click on the Field’s label focuses the field without starting a recording.