ShortcutRegistry
One keyboard-shortcut system for an app page.
import { ShortcutRegistry } from "@sdxc/ui/app";Usage
Holds every shortcut on a page and answers keystrokes: the candidates a keystroke completes are ranked by layer (topmost open modal, then open sheets, then the page), then priority, then the latest registered, and the first that runs without declining claims the key.
Signature
new ShortcutRegistry(options?: Shortcuts.Options)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
options | Shortcuts.Options | No | How long a sequence waits for its next key. |
Methods
| Method | Description |
|---|---|
register(registration: Shortcuts.Registration): () => void | Adds a shortcut until signal aborts or the returned function runs. |
list(document?: Document): Shortcuts.Entry[] | Every labelled shortcut, registered or declared in document, in registration then document order, one row per distinct keys and label. |
listen(document: Document, signal?: AbortSignal): void | Answers keystrokes on document until signal aborts, in the bubble phase so a handler nearer the target that claims a key keeps it. |
handle(event: KeyboardEvent): boolean | Runs the shortcut a keystroke completes, default-preventing the keystroke when one ran or a sequence is waiting for its next key. |
Examples
let registry = shortcutRegistry(document);
registry.register({ keys: "g m", label: "Go to Monitors", group: "Navigation", run: () => location.assign("/monitors"), signal });Used with it
The events, constants and types its module publishes, each imported from @sdxc/ui/app too.
SHORTCUT_ATTRIBUTE
Attribute a link, button or field declares its shortcut on, as "g m", "c" or
"s, /" (comma-separated alternatives). Pressing it clicks the element, or focuses it
when it is a field.
const SHORTCUT_ATTRIBUTE = "data-shortcut"SHORTCUT_LABEL_ATTRIBUTE
Attribute naming a declared shortcut in the help sheet; an empty value keeps it unlisted.
const SHORTCUT_LABEL_ATTRIBUTE = "data-shortcut-label"SHORTCUT_GROUP_ATTRIBUTE
Attribute naming the help-sheet group of every shortcut declared on or inside its element.
const SHORTCUT_GROUP_ATTRIBUTE = "data-shortcut-group"SHORTCUT_SCOPE_ATTRIBUTE
Attribute marking an element as a shortcut layer, "sheet" or "modal", for a panel built
from something other than <dialog> or [popover]. The layer counts as open while it and
its ancestors are free of hidden and inert.
const SHORTCUT_SCOPE_ATTRIBUTE = "data-shortcut-scope"SHORTCUT_SEQUENCE_TIMEOUT_MS
Milliseconds a sequence waits for its next key before the keys typed so far are dropped.
const SHORTCUT_SEQUENCE_TIMEOUT_MS = 1200ESCAPE_PRIORITY
Priorities for the Escape ladder: with every handler declining when it has nothing to undo, one press clears the selection, the next clears the filters, and only then does a panel close.
const ESCAPE_PRIORITY = { selection: 30, filters: 20, close: 10 } as constshortcutAlternatives
Splits keys into its alternatives: an array as given, a string at its commas. A lone
"," stays the comma key.
shortcutAlternatives(keys: string | readonly string[]): string[]| Parameter | Type | Required | Description |
|---|---|---|---|
keys | string | readonly string[] | Yes | The keys as registered. |
shortcutGlyphs
The key caps a hint prints for one alternative, sequence steps in press order, with arrows and the space bar as their symbols. A letter prints as its cap, upper-cased, and a capital letter, which only Shift types, prints with the Shift cap before it.
shortcutGlyphs(alternative: string, apple: boolean): string[]| Parameter | Type | Required | Description |
|---|---|---|---|
alternative | string | Yes | A combo or sequence, e.g. "g m", "mod+k", "arrowdown". |
apple | boolean | Yes | Whether the reader's keyboard carries the Apple modifier glyphs. |
shortcutGlyphs("g m", true) // ["G", "M"]isApplePlatform
Whether the reader's keyboard prints the Apple modifier glyphs, read from the platform the
browser reports. On a server it answers true, the glyphs most readers see.
isApplePlatform(): booleanshortcutRegistry
The registry every part on a page shares, created and bound to document on first use, so
a palette, a help sheet and a list's row keys resolve against one another.
shortcutRegistry(document: Document): ShortcutRegistry| Parameter | Type | Required | Description |
|---|---|---|---|
document | Document | Yes | The page's document. |
shortcutRegistry(document).register({ keys: "?", label: "Show shortcuts", run: open, signal });Shortcuts.Registration
A shortcut registered from script.
| Member | Type | Required | Description |
|---|---|---|---|
keys | string | readonly string[] | Yes | The keys that run it: a combo ("mod+k", "?", "escape"), a space-separated sequence ("g m"), or several of either as alternatives. |
run | boolean | void | Yes | Runs the shortcut. Returning false declines the keystroke, which then reaches the next candidate, so an Escape handler with nothing to clear lets the next one act. |
label | string | No | What it does, as the help sheet lists it; unlabelled shortcuts stay unlisted. |
group | string | No | The help-sheet heading it lists under. |
scope | Element | null | No | An element inside the layer it belongs to: its nearest open <dialog>, [popover] or [data-shortcut-scope]. Omitted, it belongs to the page. |
priority | number | No | Breaks a tie within one layer; the higher number answers first. @default 0 |
whileTyping | boolean | No | Answers while focus is in a text field, as Escape and mod+k do. @default false |
global | boolean | No | Answers while a modal above its layer is open, below that modal's own shortcuts: the key that toggles a palette closes it again. @default false |
signal | AbortSignal | No | Takes the shortcut out of the registry. |
Shortcuts.Entry
One row of the help sheet.
| Member | Type | Required | Description |
|---|---|---|---|
keys | string[] | Yes | The alternatives that run it, each a combo or sequence as registered. |
label | string | Yes | |
group | string | No |
Shortcuts.Options
Options for ShortcutRegistry.
| Member | Type | Required | Description |
|---|---|---|---|
sequenceTimeout | number | No | Milliseconds a sequence waits for its next key. @default SHORTCUT_SEQUENCE_TIMEOUT_MS |
Shortcuts.Events
Events a ShortcutRegistry dispatches.
| Member | Type | Required | Description |
|---|---|---|---|
change | Event | Yes | A shortcut was registered or removed, so a help sheet lists it anew. |
Shortcuts.Layer
The layers a shortcut can belong to, from lowest to highest.
type Shortcuts.Layer = "page" | "sheet" | "modal"