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