@tanstack/react-hotkeys
Version:
React adapter for TanStack Hotkeys
44 lines • 1.87 kB
TypeScript
import { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderActions, HotkeyRecorderState, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
/** The recorder state and controls returned by `useHotkeyRecorder`. */
export interface ReactHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends HotkeyRecorderState<THotkey>, HotkeyRecorderActions {}
/**
* React hook for recording keyboard shortcuts.
*
* With the default `maxSteps: 1`, the first valid chord is recorded and
* `onRecord` receives a chord such as `'Mod+S'`. With a larger `maxSteps`, each
* chord adds a step; the recording commits on Enter (unless
* `commitKeys: 'none'`), after `idleTimeoutMs`, or when it reaches `maxSteps`,
* and `onRecord` receives a chord (one step) or a sequence.
*
* Escape cancels, and so do Tab and Shift+Tab unless `recordTab` is set.
* Backspace/Delete removes the last step, or calls `onClear` when nothing is
* recorded.
*
* @param options - Configuration options for the recorder
* @returns The recorder state and controls
*
* @example
* ```tsx
* function ShortcutInput() {
* const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
* const recorder = useHotkeyRecorder({ onRecord: setShortcut })
* const formatForDisplay = useFormatForDisplay()
*
* return (
* <button onClick={recorder.startRecording}>
* {recorder.isRecording ? 'Press keys…' : formatForDisplay(shortcut)}
* </button>
* )
* }
* ```
*
* @example
* ```tsx
* const recorder = useHotkeyRecorder({
* maxSteps: Infinity,
* onRecord: (hotkey) => setShortcut(hotkey), // 'Mod+S' or ['G', 'H']
* })
* ```
*/
export declare function useHotkeyRecorder(options: HotkeyChordRecorderOptions): ReactHotkeyRecorder<HotkeyChord>;
export declare function useHotkeyRecorder(options: HotkeySequenceRecorderOptions): ReactHotkeyRecorder;