@tanstack/react-hotkeys 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +121 -45
  3. package/dist/HotkeysProvider.cjs +23 -0
  4. package/dist/HotkeysProvider.cjs.map +1 -0
  5. package/dist/HotkeysProvider.d.cts +27 -0
  6. package/dist/HotkeysProvider.d.ts +27 -0
  7. package/dist/HotkeysProvider.js +19 -0
  8. package/dist/HotkeysProvider.js.map +1 -0
  9. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  10. package/dist/index.cjs +25 -0
  11. package/dist/index.d.cts +9 -0
  12. package/dist/index.d.ts +9 -0
  13. package/dist/index.js +11 -0
  14. package/dist/useHeldKeyCodes.cjs +38 -0
  15. package/dist/useHeldKeyCodes.cjs.map +1 -0
  16. package/dist/useHeldKeyCodes.d.cts +31 -0
  17. package/dist/useHeldKeyCodes.d.ts +31 -0
  18. package/dist/useHeldKeyCodes.js +37 -0
  19. package/dist/useHeldKeyCodes.js.map +1 -0
  20. package/dist/useHeldKeys.cjs +34 -0
  21. package/dist/useHeldKeys.cjs.map +1 -0
  22. package/dist/useHeldKeys.d.cts +27 -0
  23. package/dist/useHeldKeys.d.ts +27 -0
  24. package/dist/useHeldKeys.js +33 -0
  25. package/dist/useHeldKeys.js.map +1 -0
  26. package/dist/useHotkey.cjs +119 -0
  27. package/dist/useHotkey.cjs.map +1 -0
  28. package/dist/useHotkey.d.cts +73 -0
  29. package/dist/useHotkey.d.ts +73 -0
  30. package/dist/useHotkey.js +118 -0
  31. package/dist/useHotkey.js.map +1 -0
  32. package/dist/useHotkeyRecorder.cjs +72 -0
  33. package/dist/useHotkeyRecorder.cjs.map +1 -0
  34. package/dist/useHotkeyRecorder.d.cts +57 -0
  35. package/dist/useHotkeyRecorder.d.ts +57 -0
  36. package/dist/useHotkeyRecorder.js +71 -0
  37. package/dist/useHotkeyRecorder.js.map +1 -0
  38. package/dist/useHotkeySequence.cjs +65 -0
  39. package/dist/useHotkeySequence.cjs.map +1 -0
  40. package/dist/useHotkeySequence.d.cts +43 -0
  41. package/dist/useHotkeySequence.d.ts +43 -0
  42. package/dist/useHotkeySequence.js +64 -0
  43. package/dist/useHotkeySequence.js.map +1 -0
  44. package/dist/useKeyHold.cjs +54 -0
  45. package/dist/useKeyHold.cjs.map +1 -0
  46. package/dist/useKeyHold.d.cts +47 -0
  47. package/dist/useKeyHold.d.ts +47 -0
  48. package/dist/useKeyHold.js +53 -0
  49. package/dist/useKeyHold.js.map +1 -0
  50. package/package.json +66 -7
  51. package/src/HotkeysProvider.tsx +51 -0
  52. package/src/index.ts +13 -0
  53. package/src/useHeldKeyCodes.ts +33 -0
  54. package/src/useHeldKeys.ts +29 -0
  55. package/src/useHotkey.ts +191 -0
  56. package/src/useHotkeyRecorder.ts +101 -0
  57. package/src/useHotkeySequence.ts +92 -0
  58. package/src/useKeyHold.ts +52 -0
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ // Re-export everything from the core package
2
+ export * from '@tanstack/hotkeys'
3
+
4
+ // provider
5
+ export * from './HotkeysProvider'
6
+
7
+ // React-specific exports
8
+ export * from './useHotkey'
9
+ export * from './useHeldKeys'
10
+ export * from './useHeldKeyCodes'
11
+ export * from './useKeyHold'
12
+ export * from './useHotkeySequence'
13
+ export * from './useHotkeyRecorder'
@@ -0,0 +1,33 @@
1
+ import { useStore } from '@tanstack/react-store'
2
+ import { getKeyStateTracker } from '@tanstack/hotkeys'
3
+
4
+ /**
5
+ * React hook that returns a map of currently held key names to their physical `event.code` values.
6
+ *
7
+ * This is useful for debugging which physical key was pressed (e.g. distinguishing
8
+ * left vs right Shift via "ShiftLeft" / "ShiftRight").
9
+ *
10
+ * @returns Record mapping normalized key names to their `event.code` values
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * function KeyDebugDisplay() {
15
+ * const heldKeys = useHeldKeys()
16
+ * const heldCodes = useHeldKeyCodes()
17
+ *
18
+ * return (
19
+ * <div>
20
+ * {heldKeys.map((key) => (
21
+ * <kbd key={key}>
22
+ * {key} <small>{heldCodes[key]}</small>
23
+ * </kbd>
24
+ * ))}
25
+ * </div>
26
+ * )
27
+ * }
28
+ * ```
29
+ */
30
+ export function useHeldKeyCodes(): Record<string, string> {
31
+ const tracker = getKeyStateTracker()
32
+ return useStore(tracker.store, (state) => state.heldCodes)
33
+ }
@@ -0,0 +1,29 @@
1
+ import { useStore } from '@tanstack/react-store'
2
+ import { getKeyStateTracker } from '@tanstack/hotkeys'
3
+
4
+ /**
5
+ * React hook that returns an array of currently held keyboard keys.
6
+ *
7
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
8
+ * to the global KeyStateTracker and updates whenever keys are pressed
9
+ * or released.
10
+ *
11
+ * @returns Array of currently held key names
12
+ *
13
+ * @example
14
+ * ```tsx
15
+ * function KeyDisplay() {
16
+ * const heldKeys = useHeldKeys()
17
+ *
18
+ * return (
19
+ * <div>
20
+ * Currently pressed: {heldKeys.join(' + ') || 'None'}
21
+ * </div>
22
+ * )
23
+ * }
24
+ * ```
25
+ */
26
+ export function useHeldKeys(): Array<string> {
27
+ const tracker = getKeyStateTracker()
28
+ return useStore(tracker.store, (state) => state.heldKeys)
29
+ }
@@ -0,0 +1,191 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import {
3
+ detectPlatform,
4
+ formatHotkey,
5
+ getHotkeyManager,
6
+ rawHotkeyToParsedHotkey,
7
+ } from '@tanstack/hotkeys'
8
+ import { useDefaultHotkeysOptions } from './HotkeysProvider'
9
+ import type {
10
+ Hotkey,
11
+ HotkeyCallback,
12
+ HotkeyOptions,
13
+ HotkeyRegistrationHandle,
14
+ RegisterableHotkey,
15
+ } from '@tanstack/hotkeys'
16
+
17
+ export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
18
+ /**
19
+ * The DOM element to attach the event listener to.
20
+ * Can be a React ref, direct DOM element, or null.
21
+ * Defaults to document.
22
+ */
23
+ target?:
24
+ | React.RefObject<HTMLElement | null>
25
+ | HTMLElement
26
+ | Document
27
+ | Window
28
+ | null
29
+ }
30
+
31
+ /**
32
+ * React hook for registering a keyboard hotkey.
33
+ *
34
+ * Uses the singleton HotkeyManager for efficient event handling.
35
+ * The callback receives both the keyboard event and a context object
36
+ * containing the hotkey string and parsed hotkey.
37
+ *
38
+ * This hook syncs the callback and options on every render to avoid
39
+ * stale closures. This means
40
+ * callbacks that reference React state will always have access to
41
+ * the latest values.
42
+ *
43
+ * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
44
+ * @param callback - The function to call when the hotkey is pressed
45
+ * @param options - Options for the hotkey behavior
46
+ *
47
+ * @example
48
+ * ```tsx
49
+ * function SaveButton() {
50
+ * const [count, setCount] = useState(0)
51
+ *
52
+ * // Callback always has access to latest count value
53
+ * useHotkey('Mod+S', (event, { hotkey }) => {
54
+ * console.log(`Save triggered, count is ${count}`)
55
+ * handleSave()
56
+ * })
57
+ *
58
+ * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
59
+ * }
60
+ * ```
61
+ *
62
+ * @example
63
+ * ```tsx
64
+ * function Modal({ isOpen, onClose }) {
65
+ * // enabled option is synced on every render
66
+ * useHotkey('Escape', () => {
67
+ * onClose()
68
+ * }, { enabled: isOpen })
69
+ *
70
+ * if (!isOpen) return null
71
+ * return <div className="modal">...</div>
72
+ * }
73
+ * ```
74
+ *
75
+ * @example
76
+ * ```tsx
77
+ * function Editor() {
78
+ * const editorRef = useRef<HTMLDivElement>(null)
79
+ *
80
+ * // Scoped to a specific element
81
+ * useHotkey('Mod+S', () => {
82
+ * save()
83
+ * }, { target: editorRef })
84
+ *
85
+ * return <div ref={editorRef}>...</div>
86
+ * }
87
+ * ```
88
+ */
89
+ export function useHotkey(
90
+ hotkey: RegisterableHotkey,
91
+ callback: HotkeyCallback,
92
+ options: UseHotkeyOptions = {},
93
+ ): void {
94
+ const mergedOptions = {
95
+ ...useDefaultHotkeysOptions().hotkey,
96
+ ...options,
97
+ } as UseHotkeyOptions
98
+
99
+ const manager = getHotkeyManager()
100
+
101
+ // Stable ref for registration handle
102
+ const registrationRef = useRef<HotkeyRegistrationHandle | null>(null)
103
+
104
+ // Refs to capture current values for use in effect without adding dependencies
105
+ const callbackRef = useRef(callback)
106
+ const optionsRef = useRef(mergedOptions)
107
+ const managerRef = useRef(manager)
108
+
109
+ // Update refs on every render
110
+ callbackRef.current = callback
111
+ optionsRef.current = mergedOptions
112
+ managerRef.current = manager
113
+
114
+ // Track previous target and hotkey to detect changes requiring re-registration
115
+ const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
116
+ const prevHotkeyRef = useRef<string | null>(null)
117
+
118
+ // Normalize to hotkey string
119
+ const platform = mergedOptions.platform ?? detectPlatform()
120
+ const hotkeyString: Hotkey =
121
+ typeof hotkey === 'string'
122
+ ? hotkey
123
+ : (formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform)) as Hotkey)
124
+
125
+ // Extract options without target (target is handled separately)
126
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions
127
+
128
+ useEffect(() => {
129
+ // Resolve target inside the effect so refs are already attached after mount
130
+ const resolvedTarget = isRef(optionsRef.current.target)
131
+ ? optionsRef.current.target.current
132
+ : (optionsRef.current.target ??
133
+ (typeof document !== 'undefined' ? document : null))
134
+
135
+ // Skip if no valid target (SSR or ref still null)
136
+ if (!resolvedTarget) {
137
+ return
138
+ }
139
+
140
+ // Check if we need to re-register (target or hotkey changed)
141
+ const targetChanged =
142
+ prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget
143
+ const hotkeyChanged =
144
+ prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString
145
+
146
+ // If we have an active registration and target/hotkey changed, unregister first
147
+ if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
148
+ registrationRef.current.unregister()
149
+ registrationRef.current = null
150
+ }
151
+
152
+ // Register if needed (no active registration)
153
+ // Use refs to access current values without adding them to dependencies
154
+ if (!registrationRef.current || !registrationRef.current.isActive) {
155
+ registrationRef.current = managerRef.current.register(
156
+ hotkeyString,
157
+ callbackRef.current,
158
+ {
159
+ ...optionsRef.current,
160
+ target: resolvedTarget,
161
+ },
162
+ )
163
+ }
164
+
165
+ // Update tracking refs
166
+ prevTargetRef.current = resolvedTarget
167
+ prevHotkeyRef.current = hotkeyString
168
+
169
+ // Cleanup on unmount
170
+ return () => {
171
+ if (registrationRef.current?.isActive) {
172
+ registrationRef.current.unregister()
173
+ registrationRef.current = null
174
+ }
175
+ }
176
+ }, [hotkeyString, options.enabled])
177
+
178
+ // Sync callback and options on EVERY render (outside useEffect)
179
+ // This avoids stale closures - the callback always has access to latest state
180
+ if (registrationRef.current?.isActive) {
181
+ registrationRef.current.callback = callback
182
+ registrationRef.current.setOptions(optionsWithoutTarget)
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Type guard to check if a value is a React ref-like object.
188
+ */
189
+ function isRef(value: unknown): value is React.RefObject<HTMLElement | null> {
190
+ return value !== null && typeof value === 'object' && 'current' in value
191
+ }
@@ -0,0 +1,101 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import { useStore } from '@tanstack/react-store'
3
+ import { HotkeyRecorder } from '@tanstack/hotkeys'
4
+ import { useDefaultHotkeysOptions } from './HotkeysProvider'
5
+ import type { Hotkey, HotkeyRecorderOptions } from '@tanstack/hotkeys'
6
+
7
+ export interface ReactHotkeyRecorder {
8
+ /** Whether recording is currently active */
9
+ isRecording: boolean
10
+ /** The currently recorded hotkey (for live preview) */
11
+ recordedHotkey: Hotkey | null
12
+ /** Start recording a new hotkey */
13
+ startRecording: () => void
14
+ /** Stop recording (same as cancel) */
15
+ stopRecording: () => void
16
+ /** Cancel recording without saving */
17
+ cancelRecording: () => void
18
+ }
19
+
20
+ /**
21
+ * React hook for recording keyboard shortcuts.
22
+ *
23
+ * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`
24
+ * class, managing all the complexity of capturing keyboard events, converting them
25
+ * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete
26
+ * to clear.
27
+ *
28
+ * @param options - Configuration options for the recorder
29
+ * @returns An object with recording state and control functions
30
+ *
31
+ * @example
32
+ * ```tsx
33
+ * function ShortcutSettings() {
34
+ * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
35
+ *
36
+ * const recorder = useHotkeyRecorder({
37
+ * onRecord: (hotkey) => {
38
+ * setShortcut(hotkey)
39
+ * },
40
+ * onCancel: () => {
41
+ * console.log('Recording cancelled')
42
+ * },
43
+ * })
44
+ *
45
+ * return (
46
+ * <div>
47
+ * <button onClick={recorder.startRecording}>
48
+ * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}
49
+ * </button>
50
+ * {recorder.recordedHotkey && (
51
+ * <div>Recording: {recorder.recordedHotkey}</div>
52
+ * )}
53
+ * </div>
54
+ * )
55
+ * }
56
+ * ```
57
+ */
58
+ export function useHotkeyRecorder(
59
+ options: HotkeyRecorderOptions,
60
+ ): ReactHotkeyRecorder {
61
+ const mergedOptions = {
62
+ ...useDefaultHotkeysOptions().hotkeyRecorder,
63
+ ...options,
64
+ } as HotkeyRecorderOptions
65
+
66
+ const recorderRef = useRef<HotkeyRecorder | null>(null)
67
+
68
+ // Create recorder instance once
69
+ if (!recorderRef.current) {
70
+ recorderRef.current = new HotkeyRecorder(mergedOptions)
71
+ }
72
+
73
+ // Sync options on every render (same pattern as useHotkey)
74
+ // This ensures callbacks always have access to latest values
75
+ recorderRef.current.setOptions(mergedOptions)
76
+
77
+ // Subscribe to recorder state using useStore (same pattern as useHeldKeys)
78
+ const isRecording = useStore(
79
+ recorderRef.current.store,
80
+ (state) => state.isRecording,
81
+ )
82
+ const recordedHotkey = useStore(
83
+ recorderRef.current.store,
84
+ (state) => state.recordedHotkey,
85
+ )
86
+
87
+ // Cleanup on unmount
88
+ useEffect(() => {
89
+ return () => {
90
+ recorderRef.current?.destroy()
91
+ }
92
+ }, [])
93
+
94
+ return {
95
+ isRecording,
96
+ recordedHotkey,
97
+ startRecording: () => recorderRef.current?.start(),
98
+ stopRecording: () => recorderRef.current?.stop(),
99
+ cancelRecording: () => recorderRef.current?.cancel(),
100
+ }
101
+ }
@@ -0,0 +1,92 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import { getSequenceManager } from '@tanstack/hotkeys'
3
+ import { useDefaultHotkeysOptions } from './HotkeysProvider'
4
+ import type {
5
+ HotkeyCallback,
6
+ HotkeySequence,
7
+ SequenceOptions,
8
+ } from '@tanstack/hotkeys'
9
+
10
+ export interface UseHotkeySequenceOptions extends Omit<
11
+ SequenceOptions,
12
+ 'enabled'
13
+ > {
14
+ /** Whether the sequence is enabled. Defaults to true. */
15
+ enabled?: boolean
16
+ }
17
+
18
+ /**
19
+ * React hook for registering a keyboard shortcut sequence (Vim-style).
20
+ *
21
+ * This hook allows you to register multi-key sequences like 'g g' or 'd d'
22
+ * that trigger when the full sequence is pressed within a timeout.
23
+ *
24
+ * @param sequence - Array of hotkey strings that form the sequence
25
+ * @param callback - Function to call when the sequence is completed
26
+ * @param options - Options for the sequence behavior
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * function VimEditor() {
31
+ * // 'g g' to go to top
32
+ * useHotkeySequence(['G', 'G'], () => {
33
+ * scrollToTop()
34
+ * })
35
+ *
36
+ * // 'd d' to delete line
37
+ * useHotkeySequence(['D', 'D'], () => {
38
+ * deleteLine()
39
+ * })
40
+ *
41
+ * // 'd i w' to delete inner word
42
+ * useHotkeySequence(['D', 'I', 'W'], () => {
43
+ * deleteInnerWord()
44
+ * }, { timeout: 500 })
45
+ *
46
+ * return <div>...</div>
47
+ * }
48
+ * ```
49
+ */
50
+ export function useHotkeySequence(
51
+ sequence: HotkeySequence,
52
+ callback: HotkeyCallback,
53
+ options: UseHotkeySequenceOptions = {},
54
+ ): void {
55
+ const mergedOptions = {
56
+ ...useDefaultHotkeysOptions().hotkeySequence,
57
+ ...options,
58
+ } as UseHotkeySequenceOptions
59
+
60
+ const { enabled = true, ...sequenceOptions } = mergedOptions
61
+
62
+ // Extract options for stable dependencies
63
+ const { timeout, platform } = sequenceOptions
64
+
65
+ // Use refs to keep callback stable
66
+ const callbackRef = useRef(callback)
67
+ callbackRef.current = callback
68
+
69
+ // Serialize sequence for dependency comparison
70
+ const sequenceKey = sequence.join('|')
71
+
72
+ useEffect(() => {
73
+ if (!enabled || sequence.length === 0) {
74
+ return
75
+ }
76
+
77
+ const manager = getSequenceManager()
78
+
79
+ // Build options object conditionally to avoid overwriting manager defaults with undefined
80
+ const registerOptions: SequenceOptions = { enabled: true }
81
+ if (timeout !== undefined) registerOptions.timeout = timeout
82
+ if (platform !== undefined) registerOptions.platform = platform
83
+
84
+ const unregister = manager.register(
85
+ sequence,
86
+ (event, context) => callbackRef.current(event, context),
87
+ registerOptions,
88
+ )
89
+
90
+ return unregister
91
+ }, [enabled, sequence, sequenceKey, timeout, platform])
92
+ }
@@ -0,0 +1,52 @@
1
+ import { useStore } from '@tanstack/react-store'
2
+ import { getKeyStateTracker } from '@tanstack/hotkeys'
3
+ import type { HeldKey } from '@tanstack/hotkeys'
4
+
5
+ /**
6
+ * React hook that returns whether a specific key is currently being held.
7
+ *
8
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
9
+ * to the global KeyStateTracker and uses a selector to determine if
10
+ * the specified key is held.
11
+ *
12
+ * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
13
+ * @returns True if the key is currently held down
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * function ShiftIndicator() {
18
+ * const isShiftHeld = useKeyHold('Shift')
19
+ *
20
+ * return (
21
+ * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
22
+ * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
23
+ * </div>
24
+ * )
25
+ * }
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * function ModifierIndicators() {
31
+ * const ctrl = useKeyHold('Control')
32
+ * const shift = useKeyHold('Shift')
33
+ * const alt = useKeyHold('Alt')
34
+ *
35
+ * return (
36
+ * <div>
37
+ * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
38
+ * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
39
+ * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
40
+ * </div>
41
+ * )
42
+ * }
43
+ * ```
44
+ */
45
+ export function useKeyHold(key: HeldKey): boolean {
46
+ const tracker = getKeyStateTracker()
47
+ const normalizedKey = key.toLowerCase()
48
+
49
+ return useStore(tracker.store, (state) =>
50
+ state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),
51
+ )
52
+ }