@tanstack/react-hotkeys 0.6.0 → 0.8.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.
@@ -0,0 +1,206 @@
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 { UseHotkeySequenceOptions } from './useHotkeySequence'
6
+ import type {
7
+ HotkeyCallback,
8
+ HotkeySequence,
9
+ SequenceRegistrationHandle,
10
+ } from '@tanstack/hotkeys'
11
+
12
+ /**
13
+ * A single sequence definition for use with `useHotkeySequences`.
14
+ */
15
+ export interface UseHotkeySequenceDefinition {
16
+ /** Array of hotkey strings that form the sequence */
17
+ sequence: HotkeySequence
18
+ /** The function to call when the sequence is completed */
19
+ callback: HotkeyCallback
20
+ /** Per-sequence options (merged on top of commonOptions) */
21
+ options?: UseHotkeySequenceOptions
22
+ }
23
+
24
+ /**
25
+ * React hook for registering multiple keyboard shortcut sequences at once (Vim-style).
26
+ *
27
+ * Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
28
+ * register variable-length lists without violating the rules of hooks.
29
+ *
30
+ * Options are merged in this order:
31
+ * HotkeysProvider defaults < commonOptions < per-definition options
32
+ *
33
+ * Callbacks and options are synced on every render to avoid stale closures.
34
+ *
35
+ * Definitions with an empty `sequence` are skipped (no registration).
36
+ *
37
+ * @param definitions - Array of sequence definitions to register
38
+ * @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
39
+ * Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
40
+ * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
41
+ * via `setOptions` (no unregister/re-register churn).
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * function VimPalette() {
46
+ * useHotkeySequences([
47
+ * { sequence: ['G', 'G'], callback: () => scrollToTop() },
48
+ * { sequence: ['D', 'D'], callback: () => deleteLine() },
49
+ * { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
50
+ * ])
51
+ * }
52
+ * ```
53
+ *
54
+ * @example
55
+ * ```tsx
56
+ * function DynamicSequences({ items }) {
57
+ * useHotkeySequences(
58
+ * items.map((item) => ({
59
+ * sequence: item.chords,
60
+ * callback: item.action,
61
+ * options: { enabled: item.enabled },
62
+ * })),
63
+ * { preventDefault: true },
64
+ * )
65
+ * }
66
+ * ```
67
+ */
68
+ export function useHotkeySequences(
69
+ definitions: Array<UseHotkeySequenceDefinition>,
70
+ commonOptions: UseHotkeySequenceOptions = {},
71
+ ): void {
72
+ type RegistrationRecord = {
73
+ handle: SequenceRegistrationHandle
74
+ target: Document | HTMLElement | Window
75
+ }
76
+
77
+ const defaultOptions = useDefaultHotkeysOptions().hotkeySequence
78
+ const manager = getSequenceManager()
79
+
80
+ const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())
81
+ const definitionsRef = useRef(definitions)
82
+ const sequenceStringsRef = useRef<Array<string>>([])
83
+ const commonOptionsRef = useRef(commonOptions)
84
+ const defaultOptionsRef = useRef(defaultOptions)
85
+ const managerRef = useRef(manager)
86
+
87
+ const sequenceStrings = definitions.map((def) =>
88
+ formatHotkeySequence(def.sequence),
89
+ )
90
+
91
+ definitionsRef.current = definitions
92
+ sequenceStringsRef.current = sequenceStrings
93
+ commonOptionsRef.current = commonOptions
94
+ defaultOptionsRef.current = defaultOptions
95
+ managerRef.current = manager
96
+
97
+ useEffect(() => {
98
+ const prevRegistrations = registrationsRef.current
99
+ const nextRegistrations = new Map<string, RegistrationRecord>()
100
+
101
+ const rows: Array<{
102
+ registrationKey: string
103
+ def: (typeof definitionsRef.current)[number]
104
+ seq: HotkeySequence
105
+ seqStr: string
106
+ mergedOptions: UseHotkeySequenceOptions
107
+ resolvedTarget: Document | HTMLElement | Window
108
+ }> = []
109
+
110
+ for (let i = 0; i < definitionsRef.current.length; i++) {
111
+ const def = definitionsRef.current[i]!
112
+ const seqStr = sequenceStringsRef.current[i]!
113
+ const seq = def.sequence
114
+ if (seq.length === 0) {
115
+ continue
116
+ }
117
+
118
+ const mergedOptions = {
119
+ ...defaultOptionsRef.current,
120
+ ...commonOptionsRef.current,
121
+ ...def.options,
122
+ } as UseHotkeySequenceOptions
123
+
124
+ const resolvedTarget = isRef(mergedOptions.target)
125
+ ? mergedOptions.target.current
126
+ : (mergedOptions.target ??
127
+ (typeof document !== 'undefined' ? document : null))
128
+
129
+ if (!resolvedTarget) {
130
+ continue
131
+ }
132
+
133
+ const registrationKey = `${i}:${seqStr}`
134
+ rows.push({
135
+ registrationKey,
136
+ def,
137
+ seq,
138
+ seqStr,
139
+ mergedOptions,
140
+ resolvedTarget,
141
+ })
142
+ }
143
+
144
+ const nextKeys = new Set(rows.map((r) => r.registrationKey))
145
+
146
+ for (const [key, record] of prevRegistrations) {
147
+ if (!nextKeys.has(key) && record.handle.isActive) {
148
+ record.handle.unregister()
149
+ }
150
+ }
151
+
152
+ for (const row of rows) {
153
+ const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row
154
+
155
+ const existing = prevRegistrations.get(registrationKey)
156
+ if (existing?.handle.isActive && existing.target === resolvedTarget) {
157
+ nextRegistrations.set(registrationKey, existing)
158
+ continue
159
+ }
160
+
161
+ if (existing?.handle.isActive) {
162
+ existing.handle.unregister()
163
+ }
164
+
165
+ const handle = managerRef.current.register(seq, def.callback, {
166
+ ...mergedOptions,
167
+ target: resolvedTarget,
168
+ })
169
+ nextRegistrations.set(registrationKey, {
170
+ handle,
171
+ target: resolvedTarget,
172
+ })
173
+ }
174
+
175
+ registrationsRef.current = nextRegistrations
176
+ })
177
+
178
+ useEffect(() => {
179
+ return () => {
180
+ for (const { handle } of registrationsRef.current.values()) {
181
+ if (handle.isActive) {
182
+ handle.unregister()
183
+ }
184
+ }
185
+ registrationsRef.current = new Map()
186
+ }
187
+ }, [])
188
+
189
+ for (let i = 0; i < definitions.length; i++) {
190
+ const def = definitions[i]!
191
+ const seqStr = sequenceStrings[i]!
192
+ const registrationKey = `${i}:${seqStr}`
193
+ const handle = registrationsRef.current.get(registrationKey)?.handle
194
+
195
+ if (handle?.isActive && def.sequence.length > 0) {
196
+ handle.callback = def.callback
197
+ const mergedOptions = {
198
+ ...defaultOptions,
199
+ ...commonOptions,
200
+ ...def.options,
201
+ } as UseHotkeySequenceOptions
202
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions
203
+ handle.setOptions(optionsWithoutTarget)
204
+ }
205
+ }
206
+ }
package/src/useHotkeys.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { useEffect, useRef } from 'react'
2
2
  import {
3
3
  detectPlatform,
4
- formatHotkey,
5
4
  getHotkeyManager,
6
- rawHotkeyToParsedHotkey,
5
+ normalizeRegisterableHotkey,
7
6
  } from '@tanstack/hotkeys'
8
7
  import { useDefaultHotkeysOptions } from './HotkeysProvider'
9
8
  import { isRef } from './utils'
@@ -40,7 +39,10 @@ export interface UseHotkeyDefinition {
40
39
  * Callbacks and options are synced on every render to avoid stale closures.
41
40
  *
42
41
  * @param hotkeys - Array of hotkey definitions to register
43
- * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
42
+ * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).
43
+ * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row
44
+ * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
45
+ * via `setOptions` (no unregister/re-register churn).
44
46
  *
45
47
  * @example
46
48
  * ```tsx
@@ -90,9 +92,7 @@ export function useHotkeys(
90
92
  const managerRef = useRef(manager)
91
93
 
92
94
  const hotkeyStrings = hotkeys.map((def) =>
93
- typeof def.hotkey === 'string'
94
- ? def.hotkey
95
- : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),
95
+ normalizeRegisterableHotkey(def.hotkey, platform),
96
96
  )
97
97
 
98
98
  hotkeysRef.current = hotkeys
@@ -101,19 +101,6 @@ export function useHotkeys(
101
101
  defaultOptionsRef.current = defaultOptions
102
102
  managerRef.current = manager
103
103
 
104
- // Stable serialized keys for effect dependencies
105
- const hotkeyKey = hotkeyStrings.join('\0')
106
- const enabledKey = hotkeys
107
- .map((def) => {
108
- const merged = {
109
- ...defaultOptions,
110
- ...commonOptions,
111
- ...def.options,
112
- }
113
- return merged.enabled ?? true
114
- })
115
- .join('\0')
116
-
117
104
  useEffect(() => {
118
105
  const prevRegistrations = registrationsRef.current
119
106
  const nextRegistrations = new Map<string, RegistrationRecord>()
@@ -187,7 +174,9 @@ export function useHotkeys(
187
174
  }
188
175
 
189
176
  registrationsRef.current = nextRegistrations
177
+ })
190
178
 
179
+ useEffect(() => {
191
180
  return () => {
192
181
  for (const { handle } of registrationsRef.current.values()) {
193
182
  if (handle.isActive) {
@@ -196,7 +185,7 @@ export function useHotkeys(
196
185
  }
197
186
  registrationsRef.current = new Map()
198
187
  }
199
- }, [hotkeyKey, enabledKey])
188
+ }, [])
200
189
 
201
190
  // Sync callbacks and options on EVERY render (outside useEffect)
202
191
  for (let i = 0; i < hotkeys.length; i++) {