UNPKG

@tanstack/hotkeys

Version:

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

96 lines • 3.61 kB
import { Hotkey } from "./hotkey.types.js"; /** A hotkey to validate: a chord string, a sequence, or untyped input such as saved data. */ export type ValidatableHotkey = Hotkey | (string & {}) | ReadonlyArray<string>; /** * Validates a hotkey and returns any warnings or errors. * Each issue has a `reason` code, so apps can write their own messages. * Issues in a sequence carry the index of their `step`. * * Checks for: * - Valid syntax (modifier+...+key format) * - Known modifiers * - A key that isn't a modifier * - Known keys * * @param hotkey - The hotkey string to validate * @returns A ValidationResult with validity status, warnings, and errors * * @example * ```ts * validateHotkey('Mod+S') * // { valid: true, warnings: [], errors: [] } * * validateHotkey('') * // { valid: false, warnings: [], errors: [{ reason: 'empty' }] } * * validateHotkey(['G', 'Foo+H']) * // { valid: false, warnings: [], errors: [{ reason: 'unknown-modifier', part: 'Foo', step: 1 }] } * ``` */ export declare function validateHotkey(hotkey: ValidatableHotkey): ValidationResult; /** * Validates a hotkey and throws an error if invalid. * Useful for development-time validation. * * @param hotkey - The hotkey string to validate * @throws Error if the hotkey is invalid * * @example * ```ts * assertValidHotkey('Mod+S') // OK * assertValidHotkey('') // Throws Error: [hotkeys] Invalid hotkey '': empty * ``` */ export declare function assertValidHotkey(hotkey: ValidatableHotkey): void; /** * Validates a hotkey and logs errors and warnings to the console. * Useful for development-time feedback. Logs nothing in production builds, or * where `process.env.NODE_ENV` can't be read, such as a page with no bundler. * Use `validateHotkey` there. * * @param hotkey - The hotkey string to validate * @returns True if the hotkey is valid (may still have warnings) * * @example * ```ts * checkHotkey('Control+escape') * // Console: [hotkeys] Hotkey 'Control+escape' may never fire: unknown-key 'escape' * // Returns: true * * checkHotkey('Ctl+K') * // Console: [hotkeys] Invalid hotkey 'Ctl+K': unknown-modifier 'Ctl' * // Returns: false * ``` */ export declare function checkHotkey(hotkey: ValidatableHotkey): boolean; /** * Why a hotkey is invalid or may never fire: * * - `'empty'`: the hotkey or sequence is empty. * - `'empty-part'`: a part between `+` signs is empty or whitespace. * - `'unknown-modifier'`: a modifier isn't one the library knows, such as `Ctl`. * - `'invalid-physical-key'`: a bracketed key isn't a `KeyboardEvent.code`. * - `'modifier-only'`: the key is a modifier, such as `Shift` or `[ShiftLeft]`. * - `'unknown-key'` (warning): the key isn't written like a known key name. * Key names are case-sensitive, so `escape` never matches `Escape`. */ export type HotkeyIssueReason = 'empty' | 'empty-part' | 'unknown-modifier' | 'invalid-physical-key' | 'modifier-only' | 'unknown-key'; /** One problem found by `validateHotkey`. */ export interface HotkeyIssue { reason: HotkeyIssueReason; /** The part of the chord with the problem, such as `'Ctl'` in `'Ctl+K'`. */ part?: string; /** For a sequence, the index of the step with the problem. */ step?: number; } /** * Result of validating a hotkey string. */ export interface ValidationResult { /** Whether the hotkey is valid (can still have warnings) */ valid: boolean; /** Problems that may stop the hotkey from firing, such as an unknown key */ warnings: Array<HotkeyIssue>; /** Problems that stop the hotkey from registering */ errors: Array<HotkeyIssue>; }