SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

ShortcutRegistry

One keyboard-shortcut system for an app page.

tsx
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

typescript
new ShortcutRegistry(options?: Shortcuts.Options)

Parameters

ParameterTypeRequiredDescription
optionsShortcuts.OptionsNoHow long a sequence waits for its next key.

Methods

MethodDescription
register(registration: Shortcuts.Registration): () => voidAdds 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): voidAnswers 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): booleanRuns the shortcut a keystroke completes, default-preventing the keystroke when one ran or a sequence is waiting for its next key.

Examples

tsx
let registry = shortcutRegistry(document);
registry.register({ keys: "g m", label: "Go to Monitors", group: "Navigation", run: () => location.assign("/monitors"), signal });

The events, constants and types its module publishes, each imported from @sdxc/ui/app too.

Constant

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.

typescript
const SHORTCUT_ATTRIBUTE = "data-shortcut"
Constant

SHORTCUT_LABEL_ATTRIBUTE

Attribute naming a declared shortcut in the help sheet; an empty value keeps it unlisted.

typescript
const SHORTCUT_LABEL_ATTRIBUTE = "data-shortcut-label"
Constant

SHORTCUT_GROUP_ATTRIBUTE

Attribute naming the help-sheet group of every shortcut declared on or inside its element.

typescript
const SHORTCUT_GROUP_ATTRIBUTE = "data-shortcut-group"
Constant

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.

typescript
const SHORTCUT_SCOPE_ATTRIBUTE = "data-shortcut-scope"
Constant

SHORTCUT_SEQUENCE_TIMEOUT_MS

Milliseconds a sequence waits for its next key before the keys typed so far are dropped.

typescript
const SHORTCUT_SEQUENCE_TIMEOUT_MS = 1200
Constant

ESCAPE_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.

typescript
const ESCAPE_PRIORITY = { selection: 30, filters: 20, close: 10 } as const
Function

shortcutAlternatives

Splits keys into its alternatives: an array as given, a string at its commas. A lone "," stays the comma key.

typescript
shortcutAlternatives(keys: string | readonly string[]): string[]
ParameterTypeRequiredDescription
keysstring | readonly string[]YesThe keys as registered.
Function

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.

typescript
shortcutGlyphs(alternative: string, apple: boolean): string[]
ParameterTypeRequiredDescription
alternativestringYesA combo or sequence, e.g. "g m", "mod+k", "arrowdown".
applebooleanYesWhether the reader's keyboard carries the Apple modifier glyphs.
tsx
shortcutGlyphs("g m", true) // ["G", "M"]
Function

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.

typescript
isApplePlatform(): boolean
Function

shortcutRegistry

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.

typescript
shortcutRegistry(document: Document): ShortcutRegistry
ParameterTypeRequiredDescription
documentDocumentYesThe page's document.
tsx
shortcutRegistry(document).register({ keys: "?", label: "Show shortcuts", run: open, signal });
Interface

Shortcuts.Registration

A shortcut registered from script.

MemberTypeRequiredDescription
keysstring | readonly string[]YesThe keys that run it: a combo ("mod+k", "?", "escape"), a space-separated sequence ("g m"), or several of either as alternatives.
runboolean | voidYesRuns 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.
labelstringNoWhat it does, as the help sheet lists it; unlabelled shortcuts stay unlisted.
groupstringNoThe help-sheet heading it lists under.
scopeElement | nullNoAn element inside the layer it belongs to: its nearest open <dialog>, [popover] or [data-shortcut-scope]. Omitted, it belongs to the page.
prioritynumberNoBreaks a tie within one layer; the higher number answers first. @default 0
whileTypingbooleanNoAnswers while focus is in a text field, as Escape and mod+k do. @default false
globalbooleanNoAnswers 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
signalAbortSignalNoTakes the shortcut out of the registry.
Interface

Shortcuts.Entry

One row of the help sheet.

MemberTypeRequiredDescription
keysstring[]YesThe alternatives that run it, each a combo or sequence as registered.
labelstringYes
groupstringNo
Interface

Shortcuts.Options

Options for ShortcutRegistry.

MemberTypeRequiredDescription
sequenceTimeoutnumberNoMilliseconds a sequence waits for its next key. @default SHORTCUT_SEQUENCE_TIMEOUT_MS
Interface

Shortcuts.Events

Events a ShortcutRegistry dispatches.

MemberTypeRequiredDescription
changeEventYesA shortcut was registered or removed, so a help sheet lists it anew.
Type

Shortcuts.Layer

The layers a shortcut can belong to, from lowest to highest.

typescript
type Shortcuts.Layer = "page" | "sheet" | "modal"
@sdxc/ui

Can’t find it? Contact us