@tanstack/preact-hotkeys 0.11.0 → 0.12.1
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/HotkeysProvider.d.ts +6 -13
- package/dist/HotkeysProvider.js +1 -2
- package/dist/useHeldKeyCodes.d.ts +2 -4
- package/dist/useHeldKeyCodes.js +3 -3
- package/dist/useHeldKeys.d.ts +2 -4
- package/dist/useHeldKeys.js +3 -3
- package/dist/useHotkey.d.ts +3 -6
- package/dist/useHotkey.js +3 -3
- package/dist/useHotkeyHint.d.ts +2 -5
- package/dist/useHotkeyHint.js +1 -2
- package/dist/useHotkeyRecorder.d.ts +3 -6
- package/dist/useHotkeyRecorder.js +1 -2
- package/dist/useHotkeyRegistrations.d.ts +3 -6
- package/dist/useHotkeyRegistrations.js +1 -2
- package/dist/useHotkeySequence.d.ts +3 -6
- package/dist/useHotkeySequence.js +1 -2
- package/dist/useHotkeySequenceRecorder.d.ts +3 -6
- package/dist/useHotkeySequenceRecorder.js +1 -2
- package/dist/useHotkeySequences.d.ts +3 -6
- package/dist/useHotkeySequences.js +1 -2
- package/dist/useHotkeys.d.ts +3 -6
- package/dist/useHotkeys.js +1 -2
- package/dist/useKeyHold.d.ts +2 -5
- package/dist/useKeyHold.js +1 -2
- package/dist/utils.js +1 -2
- package/package.json +11 -16
- package/dist/HotkeysProvider.cjs +0 -26
- package/dist/HotkeysProvider.cjs.map +0 -1
- package/dist/HotkeysProvider.d.cts +0 -29
- package/dist/HotkeysProvider.js.map +0 -1
- package/dist/index.cjs +0 -35
- package/dist/index.d.cts +0 -14
- package/dist/useHeldKeyCodes.cjs +0 -37
- package/dist/useHeldKeyCodes.cjs.map +0 -1
- package/dist/useHeldKeyCodes.d.cts +0 -31
- package/dist/useHeldKeyCodes.js.map +0 -1
- package/dist/useHeldKeys.cjs +0 -33
- package/dist/useHeldKeys.cjs.map +0 -1
- package/dist/useHeldKeys.d.cts +0 -27
- package/dist/useHeldKeys.js.map +0 -1
- package/dist/useHotkey.cjs +0 -125
- package/dist/useHotkey.cjs.map +0 -1
- package/dist/useHotkey.d.cts +0 -75
- package/dist/useHotkey.js.map +0 -1
- package/dist/useHotkeyHint.cjs +0 -18
- package/dist/useHotkeyHint.cjs.map +0 -1
- package/dist/useHotkeyHint.d.cts +0 -14
- package/dist/useHotkeyHint.js.map +0 -1
- package/dist/useHotkeyRecorder.cjs +0 -71
- package/dist/useHotkeyRecorder.cjs.map +0 -1
- package/dist/useHotkeyRecorder.d.cts +0 -57
- package/dist/useHotkeyRecorder.js.map +0 -1
- package/dist/useHotkeyRegistrations.cjs +0 -29
- package/dist/useHotkeyRegistrations.cjs.map +0 -1
- package/dist/useHotkeyRegistrations.d.cts +0 -26
- package/dist/useHotkeyRegistrations.js.map +0 -1
- package/dist/useHotkeySequence.cjs +0 -120
- package/dist/useHotkeySequence.cjs.map +0 -1
- package/dist/useHotkeySequence.d.cts +0 -59
- package/dist/useHotkeySequence.js.map +0 -1
- package/dist/useHotkeySequenceRecorder.cjs +0 -39
- package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
- package/dist/useHotkeySequenceRecorder.d.cts +0 -19
- package/dist/useHotkeySequenceRecorder.js.map +0 -1
- package/dist/useHotkeySequences.cjs +0 -126
- package/dist/useHotkeySequences.cjs.map +0 -1
- package/dist/useHotkeySequences.d.cts +0 -63
- package/dist/useHotkeySequences.js.map +0 -1
- package/dist/useHotkeys.cjs +0 -124
- package/dist/useHotkeys.cjs.map +0 -1
- package/dist/useHotkeys.d.cts +0 -63
- package/dist/useHotkeys.js.map +0 -1
- package/dist/useKeyHold.cjs +0 -53
- package/dist/useKeyHold.cjs.map +0 -1
- package/dist/useKeyHold.d.cts +0 -47
- package/dist/useKeyHold.js.map +0 -1
- package/dist/utils.cjs +0 -12
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/HotkeysProvider.tsx +0 -56
- package/src/index.ts +0 -18
- package/src/useHeldKeyCodes.ts +0 -33
- package/src/useHeldKeys.ts +0 -29
- package/src/useHotkey.ts +0 -191
- package/src/useHotkeyHint.ts +0 -19
- package/src/useHotkeyRecorder.ts +0 -101
- package/src/useHotkeyRegistrations.ts +0 -51
- package/src/useHotkeySequence.ts +0 -192
- package/src/useHotkeySequenceRecorder.ts +0 -64
- package/src/useHotkeySequences.ts +0 -192
- package/src/useHotkeys.ts +0 -193
- package/src/useKeyHold.ts +0 -52
- package/src/utils.ts +0 -8
package/src/useHotkeys.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'preact/hooks'
|
|
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 { UseHotkeyOptions } from './useHotkey'
|
|
10
|
-
import type {
|
|
11
|
-
Hotkey,
|
|
12
|
-
HotkeyCallback,
|
|
13
|
-
HotkeyRegistrationHandle,
|
|
14
|
-
RegisterableHotkey,
|
|
15
|
-
} from '@tanstack/hotkeys'
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A single hotkey definition for use with `useHotkeys`.
|
|
19
|
-
*/
|
|
20
|
-
export interface UseHotkeyDefinition {
|
|
21
|
-
/** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
|
|
22
|
-
hotkey: RegisterableHotkey
|
|
23
|
-
/** The function to call when the hotkey is pressed */
|
|
24
|
-
callback: HotkeyCallback
|
|
25
|
-
/** Per-hotkey options (merged on top of commonOptions) */
|
|
26
|
-
options?: UseHotkeyOptions
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Preact hook for registering multiple keyboard hotkeys at once.
|
|
31
|
-
*
|
|
32
|
-
* Uses the singleton HotkeyManager for efficient event handling.
|
|
33
|
-
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
34
|
-
* with variable-length lists without violating the rules of hooks.
|
|
35
|
-
*
|
|
36
|
-
* Options are merged in this order:
|
|
37
|
-
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
38
|
-
*
|
|
39
|
-
* Callbacks and options are synced on every render to avoid stale closures.
|
|
40
|
-
*
|
|
41
|
-
* @param hotkeys - Array of hotkey definitions to register
|
|
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).
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```tsx
|
|
49
|
-
* function Editor() {
|
|
50
|
-
* useHotkeys([
|
|
51
|
-
* { hotkey: 'Mod+S', callback: () => save() },
|
|
52
|
-
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
53
|
-
* { hotkey: 'Escape', callback: () => close() },
|
|
54
|
-
* ])
|
|
55
|
-
* }
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```tsx
|
|
60
|
-
* function MenuShortcuts({ items }) {
|
|
61
|
-
* // Dynamic hotkeys from data -- safe because it's a single hook call
|
|
62
|
-
* useHotkeys(
|
|
63
|
-
* items.map((item) => ({
|
|
64
|
-
* hotkey: item.shortcut,
|
|
65
|
-
* callback: item.action,
|
|
66
|
-
* options: { enabled: item.enabled },
|
|
67
|
-
* })),
|
|
68
|
-
* { preventDefault: true },
|
|
69
|
-
* )
|
|
70
|
-
* }
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
export function useHotkeys(
|
|
74
|
-
hotkeys: Array<UseHotkeyDefinition>,
|
|
75
|
-
commonOptions: UseHotkeyOptions = {},
|
|
76
|
-
): void {
|
|
77
|
-
type RegistrationRecord = {
|
|
78
|
-
handle: HotkeyRegistrationHandle
|
|
79
|
-
target: Document | HTMLElement | Window
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const defaultOptions = useDefaultHotkeysOptions().hotkey
|
|
83
|
-
const manager = getHotkeyManager()
|
|
84
|
-
const platform =
|
|
85
|
-
commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()
|
|
86
|
-
|
|
87
|
-
const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())
|
|
88
|
-
const hotkeysRef = useRef(hotkeys)
|
|
89
|
-
const hotkeyStringsRef = useRef<Array<Hotkey>>([])
|
|
90
|
-
const commonOptionsRef = useRef(commonOptions)
|
|
91
|
-
const defaultOptionsRef = useRef(defaultOptions)
|
|
92
|
-
const managerRef = useRef(manager)
|
|
93
|
-
|
|
94
|
-
const hotkeyStrings = hotkeys.map((def) =>
|
|
95
|
-
normalizeRegisterableHotkey(def.hotkey, platform),
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
hotkeysRef.current = hotkeys
|
|
99
|
-
hotkeyStringsRef.current = hotkeyStrings
|
|
100
|
-
commonOptionsRef.current = commonOptions
|
|
101
|
-
defaultOptionsRef.current = defaultOptions
|
|
102
|
-
managerRef.current = manager
|
|
103
|
-
|
|
104
|
-
useEffect(() => {
|
|
105
|
-
const prevRegistrations = registrationsRef.current
|
|
106
|
-
const nextRegistrations = new Map<string, RegistrationRecord>()
|
|
107
|
-
|
|
108
|
-
const rows: Array<{
|
|
109
|
-
registrationKey: string
|
|
110
|
-
def: (typeof hotkeysRef.current)[number]
|
|
111
|
-
hotkeyStr: Hotkey
|
|
112
|
-
mergedOptions: UseHotkeyOptions
|
|
113
|
-
resolvedTarget: Document | HTMLElement | Window
|
|
114
|
-
}> = []
|
|
115
|
-
|
|
116
|
-
for (let i = 0; i < hotkeysRef.current.length; i++) {
|
|
117
|
-
const def = hotkeysRef.current[i]!
|
|
118
|
-
const hotkeyStr = hotkeyStringsRef.current[i]!
|
|
119
|
-
const mergedOptions = {
|
|
120
|
-
...defaultOptionsRef.current,
|
|
121
|
-
...commonOptionsRef.current,
|
|
122
|
-
...def.options,
|
|
123
|
-
} as UseHotkeyOptions
|
|
124
|
-
|
|
125
|
-
const resolvedTarget = isRef(mergedOptions.target)
|
|
126
|
-
? mergedOptions.target.current
|
|
127
|
-
: (mergedOptions.target ??
|
|
128
|
-
(typeof document !== 'undefined' ? document : null))
|
|
129
|
-
|
|
130
|
-
if (!resolvedTarget) {
|
|
131
|
-
continue
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const registrationKey = `${i}:${hotkeyStr}`
|
|
135
|
-
rows.push({
|
|
136
|
-
registrationKey,
|
|
137
|
-
def,
|
|
138
|
-
hotkeyStr,
|
|
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, hotkeyStr, mergedOptions, resolvedTarget } =
|
|
154
|
-
row
|
|
155
|
-
|
|
156
|
-
const existing = prevRegistrations.get(registrationKey)
|
|
157
|
-
if (existing?.handle.isActive && existing.target === resolvedTarget) {
|
|
158
|
-
// Publish callback/options only after commit, never during render.
|
|
159
|
-
existing.handle.callback = def.callback
|
|
160
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
161
|
-
existing.handle.setOptions(optionsWithoutTarget)
|
|
162
|
-
nextRegistrations.set(registrationKey, existing)
|
|
163
|
-
continue
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (existing?.handle.isActive) {
|
|
167
|
-
existing.handle.unregister()
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const handle = managerRef.current.register(hotkeyStr, def.callback, {
|
|
171
|
-
...mergedOptions,
|
|
172
|
-
target: resolvedTarget,
|
|
173
|
-
})
|
|
174
|
-
nextRegistrations.set(registrationKey, {
|
|
175
|
-
handle,
|
|
176
|
-
target: resolvedTarget,
|
|
177
|
-
})
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
registrationsRef.current = nextRegistrations
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
useEffect(() => {
|
|
184
|
-
return () => {
|
|
185
|
-
for (const { handle } of registrationsRef.current.values()) {
|
|
186
|
-
if (handle.isActive) {
|
|
187
|
-
handle.unregister()
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
registrationsRef.current = new Map()
|
|
191
|
-
}
|
|
192
|
-
}, [])
|
|
193
|
-
}
|
package/src/useKeyHold.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
2
|
-
import { getKeyStateTracker } from '@tanstack/hotkeys'
|
|
3
|
-
import type { IndividualKey } from '@tanstack/hotkeys'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Preact hook that returns whether a specific key is currently being held.
|
|
7
|
-
*
|
|
8
|
-
* This hook uses `useSelector` from `@tanstack/preact-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: IndividualKey): boolean {
|
|
46
|
-
const tracker = getKeyStateTracker()
|
|
47
|
-
const normalizedKey = key.toLowerCase()
|
|
48
|
-
|
|
49
|
-
return useSelector(tracker.store, (state) =>
|
|
50
|
-
state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),
|
|
51
|
-
)
|
|
52
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { RefObject } from 'preact'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Type guard to check if a value is a Preact ref-like object.
|
|
5
|
-
*/
|
|
6
|
-
export function isRef(value: unknown): value is RefObject<HTMLElement | null> {
|
|
7
|
-
return value !== null && typeof value === 'object' && 'current' in value
|
|
8
|
-
}
|