@tanstack/preact-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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Preact adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@tanstack/preact-store": "^0.11.2",
43
- "@tanstack/hotkeys": "0.5.0"
43
+ "@tanstack/hotkeys": "0.6.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@preact/preset-vite": "^2.10.5",
package/src/index.ts CHANGED
@@ -11,5 +11,6 @@ export * from './useHeldKeys'
11
11
  export * from './useHeldKeyCodes'
12
12
  export * from './useKeyHold'
13
13
  export * from './useHotkeySequence'
14
+ export * from './useHotkeySequences'
14
15
  export * from './useHotkeyRecorder'
15
16
  export * from './useHotkeySequenceRecorder'
package/src/useHotkey.ts CHANGED
@@ -1,15 +1,13 @@
1
1
  import { useEffect, useRef } from 'preact/hooks'
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'
10
9
  import type { RefObject } from 'preact'
11
10
  import type {
12
- Hotkey,
13
11
  HotkeyCallback,
14
12
  HotkeyOptions,
15
13
  HotkeyRegistrationHandle,
@@ -44,7 +42,8 @@ export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
44
42
  *
45
43
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
46
44
  * @param callback - The function to call when the hotkey is pressed
47
- * @param options - Options for the hotkey behavior
45
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
46
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
48
47
  *
49
48
  * @example
50
49
  * ```tsx
@@ -119,10 +118,7 @@ export function useHotkey(
119
118
 
120
119
  // Normalize to hotkey string
121
120
  const platform = mergedOptions.platform ?? detectPlatform()
122
- const hotkeyString: Hotkey =
123
- typeof hotkey === 'string'
124
- ? hotkey
125
- : (formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform)) as Hotkey)
121
+ const hotkeyString = normalizeRegisterableHotkey(hotkey, platform)
126
122
 
127
123
  // Extract options without target (target is handled separately)
128
124
  const { target: _target, ...optionsWithoutTarget } = mergedOptions
@@ -136,6 +132,12 @@ export function useHotkey(
136
132
 
137
133
  // Skip if no valid target (SSR or ref still null)
138
134
  if (!resolvedTarget) {
135
+ if (registrationRef.current?.isActive) {
136
+ registrationRef.current.unregister()
137
+ registrationRef.current = null
138
+ }
139
+ prevTargetRef.current = null
140
+ prevHotkeyRef.current = null
139
141
  return
140
142
  }
141
143
 
@@ -175,7 +177,7 @@ export function useHotkey(
175
177
  registrationRef.current = null
176
178
  }
177
179
  }
178
- }, [hotkeyString, options.enabled])
180
+ }, [hotkeyString])
179
181
 
180
182
  // Sync callback and options on EVERY render (outside useEffect)
181
183
  // This avoids stale closures - the callback always has access to latest state
@@ -41,7 +41,8 @@ export interface UseHotkeySequenceOptions extends Omit<
41
41
  *
42
42
  * @param sequence - Array of hotkey strings that form the sequence
43
43
  * @param callback - Function to call when the sequence is completed
44
- * @param options - Options for the sequence behavior
44
+ * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
45
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
45
46
  *
46
47
  * @example
47
48
  * ```tsx
@@ -89,11 +90,13 @@ export function useHotkeySequence(
89
90
  const callbackRef = useRef(callback)
90
91
  const optionsRef = useRef(mergedOptions)
91
92
  const managerRef = useRef(manager)
93
+ const sequenceRef = useRef(sequence)
92
94
 
93
95
  // Update refs on every render
94
96
  callbackRef.current = callback
95
97
  optionsRef.current = mergedOptions
96
98
  managerRef.current = manager
99
+ sequenceRef.current = sequence
97
100
 
98
101
  // Track previous target and sequence to detect changes requiring re-registration
99
102
  const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
@@ -106,7 +109,13 @@ export function useHotkeySequence(
106
109
  const { target: _target, ...optionsWithoutTarget } = mergedOptions
107
110
 
108
111
  useEffect(() => {
109
- if (sequence.length === 0) {
112
+ if (sequenceRef.current.length === 0) {
113
+ if (registrationRef.current?.isActive) {
114
+ registrationRef.current.unregister()
115
+ registrationRef.current = null
116
+ }
117
+ prevTargetRef.current = null
118
+ prevSequenceRef.current = null
110
119
  return
111
120
  }
112
121
 
@@ -118,6 +127,12 @@ export function useHotkeySequence(
118
127
 
119
128
  // Skip if no valid target (SSR or ref still null)
120
129
  if (!resolvedTarget) {
130
+ if (registrationRef.current?.isActive) {
131
+ registrationRef.current.unregister()
132
+ registrationRef.current = null
133
+ }
134
+ prevTargetRef.current = null
135
+ prevSequenceRef.current = null
121
136
  return
122
137
  }
123
138
 
@@ -140,7 +155,7 @@ export function useHotkeySequence(
140
155
  // Register if needed (no active registration)
141
156
  if (!registrationRef.current || !registrationRef.current.isActive) {
142
157
  registrationRef.current = managerRef.current.register(
143
- sequence,
158
+ sequenceRef.current,
144
159
  (event, context) => callbackRef.current(event, context),
145
160
  {
146
161
  ...optionsRef.current,
@@ -160,7 +175,7 @@ export function useHotkeySequence(
160
175
  registrationRef.current = null
161
176
  }
162
177
  }
163
- }, [hotkeySequenceString, mergedOptions.enabled, sequence])
178
+ }, [hotkeySequenceString])
164
179
 
165
180
  // Sync callback and options on EVERY render (outside useEffect)
166
181
  if (registrationRef.current?.isActive) {
@@ -0,0 +1,206 @@
1
+ import { useEffect, useRef } from 'preact/hooks'
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
+ * Preact 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 'preact/hooks'
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,18 +101,6 @@ export function useHotkeys(
101
101
  defaultOptionsRef.current = defaultOptions
102
102
  managerRef.current = manager
103
103
 
104
- const hotkeyKey = hotkeyStrings.join('\0')
105
- const enabledKey = hotkeys
106
- .map((def) => {
107
- const merged = {
108
- ...defaultOptions,
109
- ...commonOptions,
110
- ...def.options,
111
- }
112
- return merged.enabled ?? true
113
- })
114
- .join('\0')
115
-
116
104
  useEffect(() => {
117
105
  const prevRegistrations = registrationsRef.current
118
106
  const nextRegistrations = new Map<string, RegistrationRecord>()
@@ -186,7 +174,9 @@ export function useHotkeys(
186
174
  }
187
175
 
188
176
  registrationsRef.current = nextRegistrations
177
+ })
189
178
 
179
+ useEffect(() => {
190
180
  return () => {
191
181
  for (const { handle } of registrationsRef.current.values()) {
192
182
  if (handle.isActive) {
@@ -195,7 +185,7 @@ export function useHotkeys(
195
185
  }
196
186
  registrationsRef.current = new Map()
197
187
  }
198
- }, [hotkeyKey, enabledKey])
188
+ }, [])
199
189
 
200
190
  for (let i = 0; i < hotkeys.length; i++) {
201
191
  const def = hotkeys[i]!