@tanstack/preact-hotkeys 0.6.0 → 0.7.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/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/useHotkey.cjs +12 -3
- package/dist/useHotkey.cjs.map +1 -1
- package/dist/useHotkey.d.cts +2 -1
- package/dist/useHotkey.d.ts +2 -1
- package/dist/useHotkey.js +12 -3
- package/dist/useHotkey.js.map +1 -1
- package/dist/useHotkeySequence.cjs +24 -9
- package/dist/useHotkeySequence.cjs.map +1 -1
- package/dist/useHotkeySequence.d.cts +2 -1
- package/dist/useHotkeySequence.d.ts +2 -1
- package/dist/useHotkeySequence.js +24 -9
- package/dist/useHotkeySequence.js.map +1 -1
- package/dist/useHotkeySequences.cjs +138 -0
- package/dist/useHotkeySequences.cjs.map +1 -0
- package/dist/useHotkeySequences.d.cts +63 -0
- package/dist/useHotkeySequences.d.ts +63 -0
- package/dist/useHotkeySequences.js +138 -0
- package/dist/useHotkeySequences.js.map +1 -0
- package/dist/useHotkeys.cjs +7 -8
- package/dist/useHotkeys.cjs.map +1 -1
- package/dist/useHotkeys.d.cts +4 -1
- package/dist/useHotkeys.d.ts +4 -1
- package/dist/useHotkeys.js +7 -8
- package/dist/useHotkeys.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/useHotkey.ts +9 -2
- package/src/useHotkeySequence.ts +19 -4
- package/src/useHotkeySequences.ts +206 -0
- package/src/useHotkeys.ts +7 -14
|
@@ -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
|
@@ -40,7 +40,10 @@ export interface UseHotkeyDefinition {
|
|
|
40
40
|
* Callbacks and options are synced on every render to avoid stale closures.
|
|
41
41
|
*
|
|
42
42
|
* @param hotkeys - Array of hotkey definitions to register
|
|
43
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
|
|
43
|
+
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).
|
|
44
|
+
* Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row
|
|
45
|
+
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
46
|
+
* via `setOptions` (no unregister/re-register churn).
|
|
44
47
|
*
|
|
45
48
|
* @example
|
|
46
49
|
* ```tsx
|
|
@@ -101,18 +104,6 @@ export function useHotkeys(
|
|
|
101
104
|
defaultOptionsRef.current = defaultOptions
|
|
102
105
|
managerRef.current = manager
|
|
103
106
|
|
|
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
107
|
useEffect(() => {
|
|
117
108
|
const prevRegistrations = registrationsRef.current
|
|
118
109
|
const nextRegistrations = new Map<string, RegistrationRecord>()
|
|
@@ -186,7 +177,9 @@ export function useHotkeys(
|
|
|
186
177
|
}
|
|
187
178
|
|
|
188
179
|
registrationsRef.current = nextRegistrations
|
|
180
|
+
})
|
|
189
181
|
|
|
182
|
+
useEffect(() => {
|
|
190
183
|
return () => {
|
|
191
184
|
for (const { handle } of registrationsRef.current.values()) {
|
|
192
185
|
if (handle.isActive) {
|
|
@@ -195,7 +188,7 @@ export function useHotkeys(
|
|
|
195
188
|
}
|
|
196
189
|
registrationsRef.current = new Map()
|
|
197
190
|
}
|
|
198
|
-
}, [
|
|
191
|
+
}, [])
|
|
199
192
|
|
|
200
193
|
for (let i = 0; i < hotkeys.length; i++) {
|
|
201
194
|
const def = hotkeys[i]!
|