@tanstack/react-hotkeys 0.10.0 → 0.12.0

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 (92) hide show
  1. package/README.md +15 -1
  2. package/dist/HotkeysProvider.d.ts +6 -12
  3. package/dist/HotkeysProvider.js +1 -2
  4. package/dist/index.d.ts +2 -1
  5. package/dist/index.js +2 -1
  6. package/dist/useHeldKeyCodes.d.ts +2 -4
  7. package/dist/useHeldKeyCodes.js +3 -3
  8. package/dist/useHeldKeys.d.ts +2 -4
  9. package/dist/useHeldKeys.js +3 -3
  10. package/dist/useHotkey.d.ts +3 -6
  11. package/dist/useHotkey.js +12 -8
  12. package/dist/useHotkeyHint.d.ts +11 -0
  13. package/dist/useHotkeyHint.js +17 -0
  14. package/dist/useHotkeyRecorder.d.ts +3 -6
  15. package/dist/useHotkeyRecorder.js +1 -2
  16. package/dist/useHotkeyRegistrations.d.ts +3 -6
  17. package/dist/useHotkeyRegistrations.js +1 -2
  18. package/dist/useHotkeySequence.d.ts +3 -6
  19. package/dist/useHotkeySequence.js +10 -7
  20. package/dist/useHotkeySequenceRecorder.d.ts +3 -6
  21. package/dist/useHotkeySequenceRecorder.js +1 -2
  22. package/dist/useHotkeySequences.d.ts +3 -6
  23. package/dist/useHotkeySequences.js +4 -17
  24. package/dist/useHotkeys.d.ts +3 -6
  25. package/dist/useHotkeys.js +4 -17
  26. package/dist/useKeyHold.d.ts +2 -5
  27. package/dist/useKeyHold.js +1 -2
  28. package/dist/utils.js +1 -2
  29. package/package.json +14 -19
  30. package/dist/HotkeysProvider.cjs +0 -23
  31. package/dist/HotkeysProvider.cjs.map +0 -1
  32. package/dist/HotkeysProvider.d.cts +0 -28
  33. package/dist/HotkeysProvider.js.map +0 -1
  34. package/dist/_virtual/_rolldown/runtime.cjs +0 -29
  35. package/dist/index.cjs +0 -33
  36. package/dist/index.d.cts +0 -13
  37. package/dist/useHeldKeyCodes.cjs +0 -38
  38. package/dist/useHeldKeyCodes.cjs.map +0 -1
  39. package/dist/useHeldKeyCodes.d.cts +0 -31
  40. package/dist/useHeldKeyCodes.js.map +0 -1
  41. package/dist/useHeldKeys.cjs +0 -34
  42. package/dist/useHeldKeys.cjs.map +0 -1
  43. package/dist/useHeldKeys.d.cts +0 -27
  44. package/dist/useHeldKeys.js.map +0 -1
  45. package/dist/useHotkey.cjs +0 -122
  46. package/dist/useHotkey.cjs.map +0 -1
  47. package/dist/useHotkey.d.cts +0 -74
  48. package/dist/useHotkey.js.map +0 -1
  49. package/dist/useHotkeyRecorder.cjs +0 -72
  50. package/dist/useHotkeyRecorder.cjs.map +0 -1
  51. package/dist/useHotkeyRecorder.d.cts +0 -57
  52. package/dist/useHotkeyRecorder.js.map +0 -1
  53. package/dist/useHotkeyRegistrations.cjs +0 -49
  54. package/dist/useHotkeyRegistrations.cjs.map +0 -1
  55. package/dist/useHotkeyRegistrations.d.cts +0 -48
  56. package/dist/useHotkeyRegistrations.js.map +0 -1
  57. package/dist/useHotkeySequence.cjs +0 -117
  58. package/dist/useHotkeySequence.cjs.map +0 -1
  59. package/dist/useHotkeySequence.d.cts +0 -58
  60. package/dist/useHotkeySequence.js.map +0 -1
  61. package/dist/useHotkeySequenceRecorder.cjs +0 -42
  62. package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
  63. package/dist/useHotkeySequenceRecorder.d.cts +0 -25
  64. package/dist/useHotkeySequenceRecorder.js.map +0 -1
  65. package/dist/useHotkeySequences.cjs +0 -139
  66. package/dist/useHotkeySequences.cjs.map +0 -1
  67. package/dist/useHotkeySequences.d.cts +0 -63
  68. package/dist/useHotkeySequences.js.map +0 -1
  69. package/dist/useHotkeys.cjs +0 -137
  70. package/dist/useHotkeys.cjs.map +0 -1
  71. package/dist/useHotkeys.d.cts +0 -63
  72. package/dist/useHotkeys.js.map +0 -1
  73. package/dist/useKeyHold.cjs +0 -54
  74. package/dist/useKeyHold.cjs.map +0 -1
  75. package/dist/useKeyHold.d.cts +0 -47
  76. package/dist/useKeyHold.js.map +0 -1
  77. package/dist/utils.cjs +0 -12
  78. package/dist/utils.cjs.map +0 -1
  79. package/dist/utils.js.map +0 -1
  80. package/src/HotkeysProvider.tsx +0 -58
  81. package/src/index.ts +0 -17
  82. package/src/useHeldKeyCodes.ts +0 -33
  83. package/src/useHeldKeys.ts +0 -29
  84. package/src/useHotkey.ts +0 -187
  85. package/src/useHotkeyRecorder.ts +0 -101
  86. package/src/useHotkeyRegistrations.ts +0 -67
  87. package/src/useHotkeySequence.ts +0 -187
  88. package/src/useHotkeySequenceRecorder.ts +0 -70
  89. package/src/useHotkeySequences.ts +0 -206
  90. package/src/useHotkeys.ts +0 -208
  91. package/src/useKeyHold.ts +0 -52
  92. package/src/utils.ts +0 -10
package/src/useHotkey.ts DELETED
@@ -1,187 +0,0 @@
1
- import { useEffect, useRef } from 'react'
2
- import {
3
- detectPlatform,
4
- getHotkeyManager,
5
- normalizeRegisterableHotkey,
6
- } from '@tanstack/hotkeys'
7
- import { useDefaultHotkeysOptions } from './HotkeysProvider'
8
- import { isRef } from './utils'
9
- import type {
10
- HotkeyCallback,
11
- HotkeyOptions,
12
- HotkeyRegistrationHandle,
13
- RegisterableHotkey,
14
- } from '@tanstack/hotkeys'
15
-
16
- export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
17
- /**
18
- * The DOM element to attach the event listener to.
19
- * Can be a React ref, direct DOM element, or null.
20
- * Defaults to document.
21
- */
22
- target?:
23
- | React.RefObject<HTMLElement | null>
24
- | HTMLElement
25
- | Document
26
- | Window
27
- | null
28
- }
29
-
30
- /**
31
- * React hook for registering a keyboard hotkey.
32
- *
33
- * Uses the singleton HotkeyManager for efficient event handling.
34
- * The callback receives both the keyboard event and a context object
35
- * containing the hotkey string and parsed hotkey.
36
- *
37
- * This hook syncs the callback and options on every render to avoid
38
- * stale closures. This means
39
- * callbacks that reference React state will always have access to
40
- * the latest values.
41
- *
42
- * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
43
- * @param callback - The function to call when the hotkey is pressed
44
- * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
45
- * and only suppresses firing; the hook updates the existing handle instead of unregistering.
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 = normalizeRegisterableHotkey(hotkey, platform)
121
-
122
- // Extract options without target (target is handled separately)
123
- const { target: _target, ...optionsWithoutTarget } = mergedOptions
124
-
125
- useEffect(() => {
126
- // Resolve target inside the effect so refs are already attached after mount
127
- const resolvedTarget = isRef(optionsRef.current.target)
128
- ? optionsRef.current.target.current
129
- : (optionsRef.current.target ??
130
- (typeof document !== 'undefined' ? document : null))
131
-
132
- // Skip if no valid target (SSR or ref still null)
133
- if (!resolvedTarget) {
134
- if (registrationRef.current?.isActive) {
135
- registrationRef.current.unregister()
136
- registrationRef.current = null
137
- }
138
- prevTargetRef.current = null
139
- prevHotkeyRef.current = null
140
- return
141
- }
142
-
143
- // Check if we need to re-register (target or hotkey changed)
144
- const targetChanged =
145
- prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget
146
- const hotkeyChanged =
147
- prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString
148
-
149
- // If we have an active registration and target/hotkey changed, unregister first
150
- if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
151
- registrationRef.current.unregister()
152
- registrationRef.current = null
153
- }
154
-
155
- // Register if needed (no active registration)
156
- // Use refs to access current values without adding them to dependencies
157
- if (!registrationRef.current || !registrationRef.current.isActive) {
158
- registrationRef.current = managerRef.current.register(
159
- hotkeyString,
160
- callbackRef.current,
161
- {
162
- ...optionsRef.current,
163
- target: resolvedTarget,
164
- },
165
- )
166
- }
167
-
168
- // Update tracking refs
169
- prevTargetRef.current = resolvedTarget
170
- prevHotkeyRef.current = hotkeyString
171
-
172
- // Cleanup on unmount
173
- return () => {
174
- if (registrationRef.current?.isActive) {
175
- registrationRef.current.unregister()
176
- registrationRef.current = null
177
- }
178
- }
179
- }, [hotkeyString])
180
-
181
- // Sync callback and options on EVERY render (outside useEffect)
182
- // This avoids stale closures - the callback always has access to latest state
183
- if (registrationRef.current?.isActive) {
184
- registrationRef.current.callback = callback
185
- registrationRef.current.setOptions(optionsWithoutTarget)
186
- }
187
- }
@@ -1,101 +0,0 @@
1
- import { useEffect, useRef } from 'react'
2
- import { useSelector } 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 useSelector (same pattern as useHeldKeys)
78
- const isRecording = useSelector(
79
- recorderRef.current.store,
80
- (state) => state.isRecording,
81
- )
82
- const recordedHotkey = useSelector(
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
- }
@@ -1,67 +0,0 @@
1
- import { useSelector } from '@tanstack/react-store'
2
- import {
3
- getHotkeyManager,
4
- getSequenceManager,
5
- toHotkeyRegistrationView,
6
- } from '@tanstack/hotkeys'
7
- import type {
8
- HotkeyRegistrationView,
9
- SequenceRegistrationView,
10
- } from '@tanstack/hotkeys'
11
-
12
- /**
13
- * Return type for useHotkeyRegistrations.
14
- */
15
- export interface HotkeyRegistrationsResult {
16
- /** All registered hotkeys (public view, no callbacks) */
17
- hotkeys: Array<HotkeyRegistrationView>
18
- /** All registered sequences */
19
- sequences: Array<SequenceRegistrationView>
20
- }
21
-
22
- /**
23
- * React hook that reactively reads all hotkey and sequence registrations
24
- * from the singleton managers.
25
- *
26
- * This is a standalone hook that does NOT require the HotkeysProvider.
27
- * It subscribes to both HotkeyManager and SequenceManager stores and
28
- * re-renders when registrations change.
29
- *
30
- * @returns Object with `hotkeys` and `sequences` arrays
31
- *
32
- * @example
33
- * ```tsx
34
- * function ShortcutPalette() {
35
- * const { hotkeys, sequences } = useHotkeyRegistrations()
36
- *
37
- * return (
38
- * <ul>
39
- * {hotkeys.map((reg) => (
40
- * <li key={reg.id}>
41
- * {reg.options.meta?.name ?? reg.hotkey}
42
- * </li>
43
- * ))}
44
- * {sequences.map((reg) => (
45
- * <li key={reg.id}>
46
- * {reg.options.meta?.name ?? reg.sequence.join(' ')}
47
- * </li>
48
- * ))}
49
- * </ul>
50
- * )
51
- * }
52
- * ```
53
- */
54
- export function useHotkeyRegistrations(): HotkeyRegistrationsResult {
55
- const hotkeyManager = getHotkeyManager()
56
- const sequenceManager = getSequenceManager()
57
-
58
- const hotkeys = useSelector(hotkeyManager.registrations, (state) =>
59
- Array.from(state.values()).map(toHotkeyRegistrationView),
60
- )
61
-
62
- const sequences = useSelector(sequenceManager.registrations, (state) =>
63
- Array.from(state.values()),
64
- )
65
-
66
- return { hotkeys, sequences }
67
- }
@@ -1,187 +0,0 @@
1
- import { useEffect, useRef } from 'react'
2
- import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
3
- import { useDefaultHotkeysOptions } from './HotkeysProvider'
4
- import { isRef } from './utils'
5
- import type {
6
- HotkeyCallback,
7
- HotkeyCallbackContext,
8
- HotkeySequence,
9
- SequenceOptions,
10
- SequenceRegistrationHandle,
11
- } from '@tanstack/hotkeys'
12
-
13
- export interface UseHotkeySequenceOptions extends Omit<
14
- SequenceOptions,
15
- 'target'
16
- > {
17
- /**
18
- * The DOM element to attach the event listener to.
19
- * Can be a React ref, direct DOM element, or null.
20
- * Defaults to document.
21
- */
22
- target?:
23
- | React.RefObject<HTMLElement | null>
24
- | HTMLElement
25
- | Document
26
- | Window
27
- | null
28
- }
29
-
30
- /**
31
- * React hook for registering a keyboard shortcut sequence (Vim-style).
32
- *
33
- * This hook allows you to register multi-key sequences like 'g g' or 'd d'
34
- * that trigger when the full sequence is pressed within a timeout.
35
- *
36
- * Each step may include modifiers. You can chain the same modifier across
37
- * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,
38
- * Control, Alt, or Meta pressed alone) are ignored while matching—they do not
39
- * advance the sequence or reset progress.
40
- *
41
- * @param sequence - Array of hotkey strings that form the sequence
42
- * @param callback - Function to call when the sequence is completed
43
- * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
44
- * and only suppresses firing; the hook updates the existing handle instead of unregistering.
45
- *
46
- * @example
47
- * ```tsx
48
- * function VimEditor() {
49
- * // 'g g' to go to top
50
- * useHotkeySequence(['G', 'G'], () => {
51
- * scrollToTop()
52
- * })
53
- *
54
- * // 'd d' to delete line
55
- * useHotkeySequence(['D', 'D'], () => {
56
- * deleteLine()
57
- * })
58
- *
59
- * // 'd i w' to delete inner word
60
- * useHotkeySequence(['D', 'I', 'W'], () => {
61
- * deleteInnerWord()
62
- * }, { timeout: 500 })
63
- *
64
- * // Same modifier on consecutive steps (bare Shift between chords is ignored)
65
- * useHotkeySequence(['Shift+R', 'Shift+T'], () => {
66
- * nextAction()
67
- * })
68
- *
69
- * return <div>...</div>
70
- * }
71
- * ```
72
- */
73
- export function useHotkeySequence(
74
- sequence: HotkeySequence,
75
- callback: HotkeyCallback,
76
- options: UseHotkeySequenceOptions = {},
77
- ): void {
78
- const mergedOptions = {
79
- ...useDefaultHotkeysOptions().hotkeySequence,
80
- ...options,
81
- } as UseHotkeySequenceOptions
82
-
83
- const manager = getSequenceManager()
84
-
85
- // Stable ref for registration handle
86
- const registrationRef = useRef<SequenceRegistrationHandle | null>(null)
87
-
88
- // Refs to capture current values for use in effect without adding dependencies
89
- const callbackRef = useRef(callback)
90
- const optionsRef = useRef(mergedOptions)
91
- const managerRef = useRef(manager)
92
- const sequenceRef = useRef(sequence)
93
-
94
- // Update refs on every render
95
- callbackRef.current = callback
96
- optionsRef.current = mergedOptions
97
- managerRef.current = manager
98
- sequenceRef.current = sequence
99
-
100
- // Track previous target and sequence to detect changes requiring re-registration
101
- const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
102
- const prevSequenceRef = useRef<string | null>(null)
103
-
104
- // Normalize to hotkey sequence string (join with spaces)
105
- const hotkeySequenceString = formatHotkeySequence(sequence)
106
-
107
- // Extract options without target (target is handled separately)
108
- const { target: _target, ...optionsWithoutTarget } = mergedOptions
109
-
110
- useEffect(() => {
111
- if (sequenceRef.current.length === 0) {
112
- if (registrationRef.current?.isActive) {
113
- registrationRef.current.unregister()
114
- registrationRef.current = null
115
- }
116
- prevTargetRef.current = null
117
- prevSequenceRef.current = null
118
- return
119
- }
120
-
121
- // Resolve target inside the effect so refs are already attached after mount
122
- const resolvedTarget = isRef(optionsRef.current.target)
123
- ? optionsRef.current.target.current
124
- : (optionsRef.current.target ??
125
- (typeof document !== 'undefined' ? document : null))
126
-
127
- // Skip if no valid target (SSR or ref still null)
128
- if (!resolvedTarget) {
129
- if (registrationRef.current?.isActive) {
130
- registrationRef.current.unregister()
131
- registrationRef.current = null
132
- }
133
- prevTargetRef.current = null
134
- prevSequenceRef.current = null
135
- return
136
- }
137
-
138
- // Check if we need to re-register (target or sequence changed)
139
- const targetChanged =
140
- prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget
141
- const sequenceChanged =
142
- prevSequenceRef.current !== null &&
143
- prevSequenceRef.current !== hotkeySequenceString
144
-
145
- // If we have an active registration and target/sequence changed, unregister first
146
- if (
147
- registrationRef.current?.isActive &&
148
- (targetChanged || sequenceChanged)
149
- ) {
150
- registrationRef.current.unregister()
151
- registrationRef.current = null
152
- }
153
-
154
- // Register if needed (no active registration)
155
- if (!registrationRef.current || !registrationRef.current.isActive) {
156
- registrationRef.current = managerRef.current.register(
157
- sequenceRef.current,
158
- (event, context) => callbackRef.current(event, context),
159
- {
160
- ...optionsRef.current,
161
- target: resolvedTarget,
162
- },
163
- )
164
- }
165
-
166
- // Update tracking refs
167
- prevTargetRef.current = resolvedTarget
168
- prevSequenceRef.current = hotkeySequenceString
169
-
170
- // Cleanup on unmount
171
- return () => {
172
- if (registrationRef.current?.isActive) {
173
- registrationRef.current.unregister()
174
- registrationRef.current = null
175
- }
176
- }
177
- }, [hotkeySequenceString])
178
-
179
- // Sync callback and options on EVERY render (outside useEffect)
180
- if (registrationRef.current?.isActive) {
181
- registrationRef.current.callback = (
182
- event: KeyboardEvent,
183
- context: HotkeyCallbackContext,
184
- ) => callbackRef.current(event, context)
185
- registrationRef.current.setOptions(optionsWithoutTarget)
186
- }
187
- }
@@ -1,70 +0,0 @@
1
- import { useEffect, useRef } from 'react'
2
- import { useSelector } from '@tanstack/react-store'
3
- import { HotkeySequenceRecorder } from '@tanstack/hotkeys'
4
- import { useDefaultHotkeysOptions } from './HotkeysProvider'
5
- import type {
6
- HotkeySequence,
7
- HotkeySequenceRecorderOptions,
8
- } from '@tanstack/hotkeys'
9
-
10
- export interface ReactHotkeySequenceRecorder {
11
- /** Whether recording is currently active */
12
- isRecording: boolean
13
- /** Chords captured in the current session */
14
- steps: HotkeySequence
15
- /** Last committed sequence */
16
- recordedSequence: HotkeySequence | null
17
- startRecording: () => void
18
- stopRecording: () => void
19
- cancelRecording: () => void
20
- /** Commit current steps (no-op if empty) */
21
- commitRecording: () => void
22
- }
23
-
24
- /**
25
- * React hook for recording multi-chord sequences (Vim-style shortcuts).
26
- *
27
- * @param options - Configuration options for the hotkey sequence recorder
28
- */
29
- export function useHotkeySequenceRecorder(
30
- options: HotkeySequenceRecorderOptions,
31
- ): ReactHotkeySequenceRecorder {
32
- const mergedOptions = {
33
- ...useDefaultHotkeysOptions().hotkeySequenceRecorder,
34
- ...options,
35
- } as HotkeySequenceRecorderOptions
36
-
37
- const recorderRef = useRef<HotkeySequenceRecorder | null>(null)
38
-
39
- if (!recorderRef.current) {
40
- recorderRef.current = new HotkeySequenceRecorder(mergedOptions)
41
- }
42
-
43
- recorderRef.current.setOptions(mergedOptions)
44
-
45
- const isRecording = useSelector(
46
- recorderRef.current.store,
47
- (state) => state.isRecording,
48
- )
49
- const steps = useSelector(recorderRef.current.store, (state) => state.steps)
50
- const recordedSequence = useSelector(
51
- recorderRef.current.store,
52
- (state) => state.recordedSequence,
53
- )
54
-
55
- useEffect(() => {
56
- return () => {
57
- recorderRef.current?.destroy()
58
- }
59
- }, [])
60
-
61
- return {
62
- isRecording,
63
- steps,
64
- recordedSequence,
65
- startRecording: () => recorderRef.current?.start(),
66
- stopRecording: () => recorderRef.current?.stop(),
67
- cancelRecording: () => recorderRef.current?.cancel(),
68
- commitRecording: () => recorderRef.current?.commit(),
69
- }
70
- }