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.

Overview

The data-hotkey attribute enhances native <kbd> elements with platform-aware key rendering. On Mac, modifier keys display as symbols; on other platforms, as text labels with + separators.

How It Works

  1. Reads the data-hotkey attribute value (e.g., "meta+k")
  2. Detects the user's platform (Mac vs. other)
  3. Maps modifier names to platform-appropriate symbols or labels
  4. Generates a <kbd> element for each key in the combo
  5. Replaces the element's content with the styled key caps
  6. Sets aria-label with the readable text

The original text content (e.g., "Ctrl+K") serves as a no-JS fallback. Nested <kbd> inside <kbd> is valid HTML for key combinations per the spec.

Attributes

Attribute Type Description
data-hotkey string Key combination string. Keys separated by +. Supported modifiers: meta, alt, shift, ctrl.
data-hotkey-init boolean Set automatically to prevent double-enhancement. Do not set manually.

Modifier Key Mapping

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

Key name Mac Windows/Linux
meta Ctrl
alt Alt
shift Shift
ctrl Ctrl

Non-modifier keys are displayed in uppercase (e.g., k becomes K).

Examples

Common keyboard shortcuts rendered with platform-aware symbols:

Search: Ctrl+K Save: Ctrl+S Undo: Ctrl+Z Command palette: Ctrl+Shift+P

Inline Usage

Works naturally inline with paragraph text:

Press Ctrl+K to open the search palette.

Use Ctrl+Shift+P for the command palette.

Common Use Cases

Keyboard Shortcuts Table

Display a reference table of available shortcuts:

Action Shortcut
Search Ctrl+K
Save Ctrl+S
Undo Ctrl+Z
Redo Ctrl+Shift+Z
Select all Ctrl+A

With Tooltips

Combine with <tool-tip> to show shortcuts on hover:

Dynamic Elements

Elements added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.

Action binding attributes

Attribute Value Description
data-hotkey-action string Action type: "click" (default), "focus", "submit", "toggle".
data-hotkey-label string Description for the shortcuts dialog. Defaults to element text content.
data-hotkey-group string Group name in shortcuts dialog. Default: "Page Actions".
data-hotkey-global boolean If present, fires even when typing in an input field.

Action types

Action Behavior Use Case
click Triggers .click() Buttons, links
focus Moves focus to element Search inputs
submit Submits closest form Save shortcuts
toggle Toggles open or hidden Panels, drawers

Focus search on /

Toggle a details panel

Shortcuts dialog integration

All hotkey-bound elements automatically appear in the <short-cuts> overlay (press ? to open). Add <short-cuts></short-cuts> once in your page to enable it.

Accessibility

  • Sets aria-label on the element with the full text content for screen readers
  • Uses semantic <kbd> elements which are recognized by assistive technology
  • Nested <kbd> inside <kbd> is valid HTML for key combinations per the spec
  • The original text content serves as a no-JS fallback
  • On Mac, separators between keys are omitted since the symbols are self-explanatory; on other platforms, + separators are included for clarity