Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-hotkey

Platform-aware keyboard shortcut display and declarative action binding. On <kbd> elements renders platform symbols; on other elements binds keyboard shortcuts to actions, honouring the user's single-key shortcut preference (WCAG 2.1.4).

Overview

data-hotkey has two jobs, depending on the element it is on:

  • Display — on a <kbd>, it renders the combo for the user's platform: ⌘K on Mac, Ctrl+K elsewhere, with a spelled-out name for screen readers.
  • Action — on any other element (a button, input, <details>, form), it binds a real keyboard shortcut that clicks, focuses, submits or toggles that element. It upgrades the native accesskey attribute.

Usage

Combo syntax

Keys are joined with +, modifiers first: meta+k, ctrl+shift+p, alt+f4, ?. Matching is case-insensitive.

  • Modifiers: meta (alias cmd) means Cmd on Mac and Ctrl elsewhere. The others are ctrl (control), alt (option) and shift.
  • Key aliases: esc, return, del, ins, up / down / left / right, space, pgup / pgdn, and plus (or a trailing +, as in ctrl++).
  • Shifted punctuation: for keys like ?, ! and +, Shift is not compared, because whether Shift is needed depends on the keyboard layout. ? and shift+? both fire on Shift+/ on a US keyboard, and ctrl++ fires whether or not Shift was needed.
  • Alt / Option combos: letters and digits also match by physical key (event.code). alt+k works on a Mac, where Option+K types "˚", and shift+1 works although the key types "!".

Display (<kbd>)

  1. Reads the combo and detects the platform.
  2. Renders an aria-hidden cap (<kbd>) for each key, plus a visually hidden spelled-out name ("Command Shift P").
  3. The original text ("Ctrl+K") is the no-JS fallback. Running it again is a no-op. A changed combo rebuilds the caps.

Action binding (other elements)

  • All shortcuts share one keydown listener on the document. Each binding runs the element's action (data-hotkey-action).
  • Bound elements get aria-keyshortcuts (for example Control+S, Meta+K on Mac, Shift+?, Space), unless you set one yourself.
  • A binding is dropped when its element leaves the document, and rebound when any data-hotkey* attribute changes. It is skipped while the element is hidden, inert, disabled, or inside a hidden or inert ancestor. The toggle action still works on a hidden element itself, so it can show it again.
  • Ignored keystrokes: auto-repeat (holding a key fires once), IME composition, keystrokes another handler already handled (defaultPrevented), and, unless the binding has data-hotkey-global, keys typed into form fields or contenteditable.
  • When a binding fires, the browser's default action for that key is prevented.
  • Every bound element is listed in the <short-cuts> dialog.

Options

Attribute Applies to Description
data-hotkey both The key combo (see the combo syntax above).
data-label-key display Overrides the label and spoken name of one key, e.g. data-label-ctrl="Strg". Read from the element or any ancestor. On Mac the modifier glyphs stay and only the spoken name changes.
data-hotkey-action action click (default) calls .click(). focus moves focus to the element. submit calls requestSubmit() on the closest form. toggle flips open on <details> / <dialog>, else the element's hidden.
data-hotkey-label action Description in the shortcuts dialog. Defaults to the element's accessible name: aria-label, aria-labelledby, its <label>, a <details>'s <summary>, or its text with <kbd> hints and hidden text left out. Then placeholder, title, or the combo.
data-hotkey-group action Group heading in the shortcuts dialog. Default "Page Actions".
data-hotkey-global action Fires even while the user is typing in a form field. This takes the key away from typing, so keep it to modifier combos such as Ctrl+S.
data-hotkey-scope action The shortcut fires only while focus is inside a region. The value is a CSS selector: the nearest matching ancestor, else the first match in the document. An empty value means the element itself. Scoped single-key shortcuts stay on when the user turns single-key shortcuts off (WCAG 2.1.4 "active only on focus").

Modifier Key Mapping

Modifier keys are automatically translated based on the user's platform:

Key name Mac Windows/Linux
meta, cmd ⌘ (spoken "Command") Ctrl (spoken "Control")
alt, option ⌥ (spoken "Option") Alt
shift ⇧ (spoken "Shift") Shift
ctrl, control ⌃ (spoken "Control") Ctrl (spoken "Control")

Named keys

Named keys get short labels on every platform:

Key name Shown Spoken
escape, escEscEscape
enter, returnEnterEnter
tab, space, backspace, home, endTab, Space, Backspace, Home, Endas shown
delete, del / insertDel / InsDelete / Insert
pageup / pagedownPgUp / PgDnPage up / Page down
arrowup (up), arrowdown, arrowleft, arrowright↑ ↓ ← →Up arrow, Down arrow, …
plus (or trailing +)+Plus

Any other key is displayed in uppercase (e.g., k becomes K).

Localised key names

Key names default to English. Override any of them with data-label-key on the element or an ancestor:

Events

data-hotkey fires no events of its own. The action fires the element's own events (click, focus, submit, toggle). Adding or removing a binding dispatches vb:command-registry-change (detail: { action: 'add' | 'remove', element }) on the document, so <short-cuts> and <command-palette> can refresh.

Styling

The display hooks are kbd[data-hotkey] (an inline-flex row of caps that stays left-to-right in RTL pages) and its child kbd caps. Action-bound elements keep their own styles. Select them with [data-hotkey]:not(kbd) or [aria-keyshortcuts] if you want a visual hint.

Accessibility

Single-key shortcuts (WCAG 2.1.4)

A shortcut that is a single letter, digit, punctuation or symbol key (/, ?, j, with or without Shift) fires by accident for speech-input users and some keyboard users. WCAG 2.1.4 requires that users can turn such shortcuts off or remap them, or that they work only while the relevant region has focus. VB covers this site-wide:

  • The Single-Key Shortcuts switch in <settings-panel> (Accessibility) turns them all off. The choice is stored as charShortcuts in vb:settings:extensions and restored as soon as the hotkey code loads.
  • While it is off, <html data-char-shortcuts="off"> is set. You can also author that attribute to opt a page out.
  • While it is off, unscoped single-key shortcuts don't fire: data-hotkey actions, <short-cuts>'s ?, and any component built on VB's hotkey binder. They are also left out of the shortcuts dialog. Shortcuts with Ctrl, Cmd or Alt keep working, and so do named keys such as Escape.
  • Shortcuts with data-hotkey-scope stay active, because they only fire while focus is inside their region.

Prefer modifier combos for page-wide actions. If you use a single key, give the action a visible control as well.

Other notes

  • Action-bound elements expose their shortcut through aria-keyshortcuts, which screen readers can announce.
  • Screen readers in browse mode use letter keys to navigate (H for headings, B for buttons), so the page never receives those keystrokes. Single-key shortcuts mostly work in focus mode, which is one more reason to prefer modifier combos.
  • data-hotkey-global overrides typing in form fields. Use it only for modifier combos users expect to work everywhere, such as Ctrl+S.
  • Don't rebind keys that browsers or assistive technology rely on (Ctrl+L, Ctrl+T, Ctrl+F, the arrow keys, Tab). VB does not detect clashes with browser shortcuts. In development it only logs a console warning when the same normalised combo is bound twice (meta+k and cmd+k, or ? and shift+?), and the last binding wins.
  • In the display form, the visible caps are aria-hidden and a visually hidden span carries the spelled-out combo ("Command Shift P"). Screen readers read the Mac glyphs inconsistently, and aria-label is not allowed on <kbd>.
  • Nested <kbd> inside <kbd> is valid HTML for key combinations. The caps keep left-to-right order in right-to-left pages.

Examples

Inline text, reference tables and tooltips

These are display-only. A reference table that lists browser or editor shortcuts such as Ctrl+Z documents them. Never put those combos on action elements, because binding them would replace the browser's own Undo or Select All.

Dynamic elements

Elements added after page load are enhanced automatically, both <kbd> displays and action bindings. Removing an element removes its binding.

Related

  • <short-cuts> — the ? dialog that lists every bound shortcut.
  • <command-palette> — a searchable list of commands, opened with a shortcut.
  • <context-menu> — menu items with shortcut hints and aria-keyshortcuts.
  • accesskey — the native attribute that data-hotkey actions upgrade.
  • <kbd> — the element the display form enhances.
  • <settings-panel> — holds the Single-Key Shortcuts preference.
  • ARIA states — aria-keyshortcuts, which announces a bound shortcut.

Which to choose

  • data-hotkey on an element — one shortcut for one control already on the page.
  • <command-palette> — many commands that should be searchable by name, not only reachable by keys.
  • <short-cuts> — not a binder: it lists what data-hotkey and the command registry have bound. Add it once per page.
  • accesskey — native and requires no script, but the modifier differs by browser and platform, and it cannot be listed or turned off.
↑