UNPKG

@tanstack/hotkeys

Version:

Type-safe, framework-agnostic keyboard hotkey management for the browser

130 lines (129 loc) • 3.84 kB
// src/constants.ts /** * Canonical order for modifiers in normalized hotkey strings. * * Defines the standard order in which modifiers should appear when formatting * hotkeys. This ensures consistent, predictable output across the library. * * Order: Control → Alt → Shift → Meta * * @example * ```ts * // Input: 'Shift+Control+Meta+S' * // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER) * ``` */ const MODIFIER_ORDER = [ "Control", "Alt", "Shift", "Meta" ]; /** * Set of canonical modifier key names for fast lookup. * * Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when * a modifier is released so we can clear non-modifier keys whose keyup events * may have been swallowed by the OS (e.g. macOS Cmd+key combos). */ const MODIFIER_KEYS = /* @__PURE__ */ new Set(MODIFIER_ORDER); /** * Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'. * * This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option') * which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the * platform-adaptive 'Mod' token. * * The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()` * based on the detected platform (Command on Mac, Control elsewhere). * * @remarks Case-insensitive lookups are supported via lowercase variants * * @example * ```ts * MODIFIER_ALIASES['Ctrl'] // 'Control' * MODIFIER_ALIASES['Cmd'] // 'Meta' * MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime) * ``` */ const MODIFIER_ALIASES = { Control: "Control", Ctrl: "Control", control: "Control", ctrl: "Control", Shift: "Shift", shift: "Shift", Alt: "Alt", Option: "Alt", alt: "Alt", option: "Alt", Command: "Meta", Cmd: "Meta", Meta: "Meta", command: "Meta", cmd: "Meta", meta: "Meta", OS: "Meta", os: "Meta", Win: "Meta", win: "Meta", CommandOrControl: "Mod", Mod: "Mod", commandorcontrol: "Mod", mod: "Mod" }; /** * Maps `KeyboardEvent.code` values for punctuation keys to their canonical characters. * * On macOS, holding the Option (Alt) key transforms punctuation keys into special characters * (e.g., Option+Minus → en-dash '–'), causing `event.key` to differ from the expected character. * However, `event.code` still reports the physical key (e.g., 'Minus'). This map enables * falling back to `event.code` for punctuation keys, similar to the existing `Key*`/`Digit*` * fallbacks for letters and digits. */ const PUNCTUATION_CODE_MAP = { Backquote: "`", Backslash: "\\", BracketLeft: "[", BracketRight: "]", Comma: ",", Equal: "=", Minus: "-", Period: ".", Quote: "'", Semicolon: ";", Slash: "/" }; /** Whether a key is a single Unicode letter, such as `'a'` or `'é'`. */ function isSingleLetterKey(key) { return /^\p{Letter}$/u.test(key); } /** * Normalizes a key name from a `KeyboardEvent` or a hotkey: single letters * become uppercase, and the space character becomes `Space`. Other names are * returned as written, because key names are case-sensitive. * * Turkish dotless `ı` stays `ı`. Its uppercase is ASCII `I`, the uppercase of * `i`, so uppercasing it would merge two different letters. * * @param key - The key name to normalize * @returns The canonical key name * * @example * ```ts * normalizeKeyName('a') // 'A' * normalizeKeyName('ı') // 'ı' (Turkish dotless i) * normalizeKeyName(' ') // 'Space' * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical) * ``` */ function normalizeKeyName(key) { if (!key) return ""; if (isSingleLetterKey(key)) { if (key === "ı" || key === "ſ") return key; const upper = key.toUpperCase(); return [...upper].length === 1 ? upper : key; } return key === " " ? "Space" : key; } export { MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, PUNCTUATION_CODE_MAP, isSingleLetterKey, normalizeKeyName };