sdxc

Type to search, or start from one of these:

[ @sdxc/ui/mixins ]

keymap

Why JS.

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

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

Parameters

ParameterTypeDescription
bindingsKeymap.BindingsCombos mapped to what each runs.

Examples

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

isClaimedKeystroke(event: KeyboardEvent, scope?: Element | null): boolean
ParameterTypeDescription
eventKeyboardEventThe keystroke as it reached the document.
scope?Element | nullThe 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.

bindKeymap(document: Document, bindings: Keymap.Bindings, options: Keymap.Options): void
ParameterTypeDescription
documentDocumentThe document to listen on.
bindingsKeymap.BindingsCombos mapped to what each runs.
optionsKeymap.OptionsThe signal that removes the listener, and the bindings' own scope.
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.

MemberTypeDescription
signalAbortSignalTakes the bindings off the document.
scope?Element | nullThe 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.

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.