SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

keymap

Why JS.

tsx
import { keymap } from "@sdxc/ui/mixins";

Usage

Binds a map of page-wide shortcuts for as long as the host stays mounted, with the host as the bindings' scope: a dialog inside it answers them, any other dialog and any field being typed in keeps its keys. The latest map passed is the one a keystroke runs.

Signature

typescript
keymap(bindings: Keymap.Bindings): MixinDescriptor<HTMLElement>

Parameters

ParameterTypeDefaultRequiredDescription
bindingsKeymap.Bindings—YesCombos mapped to what each runs.

Examples

tsx
<div mix={[keymap({ j: next, k: previous, "?": openHelp })]}>…</div>

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

Function

isClaimedKeystroke

Whether a keystroke belongs to someone else: handled nearer its target already, part of an IME composition, typed into a field, or pressed inside a dialog outside scope.

typescript
isClaimedKeystroke(event: KeyboardEvent, scope?: Element | null): boolean
ParameterTypeDefaultRequiredDescription
eventKeyboardEvent—YesThe keystroke as it reached the document.
scopeElement | null—NoThe element whose own dialogs still answer the bindings.
Function

bindKeymap

Listens for bindings on document in the bubble phase, so a handler nearer the target that claims the keystroke first keeps it.

typescript
bindKeymap(document: Document, bindings: Keymap.Bindings, options: Keymap.Options): void
ParameterTypeDefaultRequiredDescription
documentDocument—YesThe document to listen on.
bindingsKeymap.Bindings—YesCombos mapped to what each runs.
optionsKeymap.Options—YesThe signal that removes the listener, and the bindings' own scope.
tsx
bindKeymap(document, { j: next, k: previous, "?": openHelp }, { signal });
Interface

Keymap.Bindings

Each combo, as parseKeyCombo reads it, mapped to what pressing it runs. The first entry a keystroke matches runs, and the keystroke is then default-prevented.

Interface

Keymap.Options

Options for bindKeymap.

MemberTypeDefaultRequiredDescription
signalAbortSignal—YesTakes the bindings off the document.
scopeElement | null—NoThe element the bindings belong to. A keystroke inside a <dialog> stands the bindings down unless that dialog sits inside this element, so a modal layered over the page keeps its keys while the keymap's own help panel still answers them.
@sdxc/ui

Can’t find it? Contact us