UNPKG

@tanstack/hotkeys

Version:

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

224 lines • 9.62 kB
import { Store } from "./_store.js"; import { HotkeyConflict, HotkeyConflictOptions } from "./conflicts.js"; import { Hotkey, HotkeyChord, HotkeySequence, ParsedHotkeyChord } from "./hotkey.types.js"; /** * How the recorder writes the pressed key: * * - `'auto'` (default): by name when the press has one, such as `Mod+S` or * `Shift+Enter`, and by physical code otherwise, such as `Alt+[Digit1]` for * macOS Option+1. The result reads like a hotkey written by hand, so it * compares equal to one, and its label follows the user's keyboard layout. * Recorded by code: Shift or macOS Option with a digit or a symbol, dead * keys and the numpad. * - `'code'`: always by physical code, such as `Mod+[KeyS]`. The hotkey names * a position on the keyboard, whatever that key types. * - `'key'`: always by the character or key name the press produced, such as * `Alt+ß` for macOS Option+S. */ export type RecorderKeyMode = 'auto' | 'code' | 'key'; /** * Why the recorder rejected a key press or a recording: * * - `'alt-graph'`: AltGr was held. Only `recordBy: 'key'` records AltGr * presses, as the characters AltGr types. It still rejects AltGr that typed * nothing new, such as AltGr+Home on Linux, because no hotkey matches that * press. * - `'missing-code'`: the event has no usable physical key code. Recording by * code always needs one. `recordBy: 'auto'` records what the press typed * instead, and rejects only a press that typed nothing it can record. * - `'whitespace'`: the key is a whitespace character, which a hotkey can't * hold. * - `'validation'`: your `validate` callback rejected the recording. * - `'conflict'`: the recording conflicts with registered hotkeys. */ export type RecorderRejectionReason = 'alt-graph' | 'missing-code' | 'whitespace' | 'validation' | 'conflict'; export interface RecorderRejection { reason: RecorderRejectionReason; /** The message your `validate` callback returned, when it returned one. */ message?: string; /** The rejected chord or sequence, when one was recorded. */ hotkey?: Hotkey; conflicts?: Array<HotkeyConflict>; } export interface RecorderOptions { /** How the pressed key is written. Default: `'auto'`. */ recordBy?: RecorderKeyMode; /** Platform used for Mod and AltGraph handling. Defaults to platform detection. */ platform?: 'mac' | 'windows' | 'linux'; /** Opt in to live-registry conflicts, including sequence prefixes. Default: false. */ detectConflicts?: boolean | HotkeyConflictOptions; /** Rejected candidates leave recording active. */ onReject?: (rejection: RecorderRejection) => void; } /** Validation context for a recorded chord. */ export interface HotkeyChordRecorderValidationContext { kind: 'chord'; /** The event that recorded the chord. */ event: KeyboardEvent; /** The chord with `Mod` resolved for the recorder's platform. */ parsedChord: ParsedHotkeyChord; } /** Validation context for a recorded sequence. */ export interface HotkeySequenceRecorderValidationContext { kind: 'sequence'; /** The event that recorded each step. */ events: ReadonlyArray<KeyboardEvent>; /** Each step with `Mod` resolved for the recorder's platform. */ parsedSequence: Array<ParsedHotkeyChord>; } /** * Validation context for a recorded chord or sequence. Narrow with `kind` to * read `parsedChord` or `parsedSequence`. */ export type HotkeyRecorderValidationContext = HotkeyChordRecorderValidationContext | HotkeySequenceRecorderValidationContext; /** * State interface for the HotkeyRecorder. */ export interface HotkeyRecorderState { /** Whether recording is currently active */ isRecording: boolean; /** Chords recorded so far in a multi-step recording (empty for `maxSteps: 1`) */ steps: HotkeySequence; /** The last committed hotkey: a chord, or a sequence of two or more steps */ recordedHotkey: Hotkey | null; } /** Which keys commit a multi-step recording before it reaches `maxSteps`. */ export type HotkeyRecorderCommitKeys = 'enter' | 'none'; interface HotkeyRecorderBaseOptions extends RecorderOptions { /** Optional callback when recording is cancelled: Escape, or Tab without `recordTab` */ onCancel?: () => void; /** * Optional callback when the recording is cleared: Backspace/Delete with no * modifiers and no recorded steps */ onClear?: () => void; /** * Whether to ignore keyboard events from input-like elements (text inputs, * textarea, select, contenteditable) while recording. When true, typing in * inputs passes through normally (Escape still cancels). Defaults to true. */ ignoreInputs?: boolean; /** * Whether to record Tab and Shift+Tab. By default they cancel recording and * move focus, so keyboard users can leave the recorder. Tab with Control, * Alt, or Meta isn't affected. When true, tell users that Escape cancels. * Default: false */ recordTab?: boolean; } /** Options for recording a single chord. This is the default (`maxSteps: 1`). */ export interface HotkeyChordRecorderOptions extends HotkeyRecorderBaseOptions { /** Maximum number of chords to record. Default: 1 */ maxSteps?: 1; /** Return true to accept, or false or a message to reject while staying in recording mode. The message is passed to `onReject`. */ validate?: (hotkey: HotkeyChord, context: HotkeyChordRecorderValidationContext) => boolean | string; /** * Callback when a chord is successfully recorded. `context` is the one * `validate` received, with the key press that recorded the chord. */ onRecord: (hotkey: HotkeyChord, context: HotkeyChordRecorderValidationContext) => void; } /** * Options for recording up to `maxSteps` chords. A one-step recording commits * as a chord string; two or more steps commit as a sequence. */ export interface HotkeySequenceRecorderOptions extends HotkeyRecorderBaseOptions { /** Maximum number of chords to record. Reaching it commits automatically. Use `Infinity` for no limit. */ maxSteps: number; /** Return true to accept, or false or a message to reject while staying in recording mode. The message is passed to `onReject`. */ validate?: (hotkey: Hotkey, context: HotkeyRecorderValidationContext) => boolean | string; /** * Callback when a hotkey is successfully recorded. `context` is the one * `validate` received, with the key press that recorded each step. */ onRecord: (hotkey: Hotkey, context: HotkeyRecorderValidationContext) => void; /** Whether plain Enter commits the recorded steps. Default: 'enter' */ commitKeys?: HotkeyRecorderCommitKeys; /** Commit automatically after this many milliseconds without a new step. Disabled when unset or 0. */ idleTimeoutMs?: number; } /** * Options for HotkeyRecorder. `maxSteps` (default 1) selects between recording * a single chord and recording a sequence. */ export type HotkeyRecorderOptions = HotkeyChordRecorderOptions | HotkeySequenceRecorderOptions; /** * Framework-agnostic class for recording keyboard shortcuts. * * With the default `maxSteps: 1`, the first valid chord commits. With a larger * `maxSteps`, each chord adds a step; the recording commits on Enter (unless * `commitKeys: 'none'`), after `idleTimeoutMs`, or when it reaches `maxSteps`. * Backspace/Delete removes the last step, and on an empty recording calls * `onClear`. Escape cancels. Tab and Shift+Tab cancel and move focus unless * `recordTab` is true. A one-step result is a chord string. * * State Management: * - Uses TanStack Store for reactive state management * - State can be accessed via `recorder.store.state` when using the class directly * - When using framework adapters, use the `useHotkeyRecorder` hook for reactive state * * @example * ```ts * const recorder = new HotkeyRecorder({ * onRecord: (hotkey) => { * console.log('Recorded:', hotkey) // 'Mod+S' * }, * }) * * const sequenceRecorder = new HotkeyRecorder({ * maxSteps: Infinity, * onRecord: (hotkey) => { * console.log('Recorded:', hotkey) // 'Mod+S' or ['G', 'H'] * }, * }) * * recorder.start() * // ... later * recorder.destroy() * ``` */ export declare class HotkeyRecorder { #private; readonly store: Store<HotkeyRecorderState>; constructor(options: HotkeyChordRecorderOptions | (() => HotkeyChordRecorderOptions)); constructor(options: HotkeySequenceRecorderOptions | (() => HotkeySequenceRecorderOptions)); /** * Updates the recorder options, including callbacks. * This allows framework adapters to sync callback changes without recreating the recorder. */ setOptions(options: Partial<HotkeyRecorderOptions>): void; /** * Start recording a new hotkey. * * Sets up a keydown event listener that captures keyboard events * and records chords according to `maxSteps`. */ start(): void; /** * Commits the recorded steps after validation and conflict checks. * Called automatically at `maxSteps`, on Enter, or after `idleTimeoutMs`. * A rejected commit leaves recording active with its steps intact. */ commit(): void; /** * Stop recording (same as cancel, but doesn't call onCancel). * * Removes the event listener and resets the recording state. */ stop(): void; /** * Cancel recording without saving. * * Removes the event listener, resets the recording state, and calls * the onCancel callback if provided. */ cancel(): void; /** * Clean up event listeners and reset state. * * Call this when you're done with the recorder to ensure * all event listeners are properly removed. */ destroy(): void; } export {};