@tanstack/react-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.
Files changed (94) hide show
  1. package/dist/HotkeysProvider.d.ts +6 -12
  2. package/dist/HotkeysProvider.js +1 -2
  3. package/dist/useHeldKeyCodes.d.ts +2 -4
  4. package/dist/useHeldKeyCodes.js +3 -3
  5. package/dist/useHeldKeys.d.ts +2 -4
  6. package/dist/useHeldKeys.js +3 -3
  7. package/dist/useHotkey.d.ts +3 -6
  8. package/dist/useHotkey.js +3 -3
  9. package/dist/useHotkeyHint.d.ts +2 -5
  10. package/dist/useHotkeyHint.js +1 -2
  11. package/dist/useHotkeyRecorder.d.ts +3 -6
  12. package/dist/useHotkeyRecorder.js +1 -2
  13. package/dist/useHotkeyRegistrations.d.ts +3 -6
  14. package/dist/useHotkeyRegistrations.js +1 -2
  15. package/dist/useHotkeySequence.d.ts +3 -6
  16. package/dist/useHotkeySequence.js +1 -2
  17. package/dist/useHotkeySequenceRecorder.d.ts +3 -6
  18. package/dist/useHotkeySequenceRecorder.js +1 -2
  19. package/dist/useHotkeySequences.d.ts +3 -6
  20. package/dist/useHotkeySequences.js +1 -2
  21. package/dist/useHotkeys.d.ts +3 -6
  22. package/dist/useHotkeys.js +1 -2
  23. package/dist/useKeyHold.d.ts +2 -5
  24. package/dist/useKeyHold.js +1 -2
  25. package/dist/utils.js +1 -2
  26. package/package.json +14 -19
  27. package/dist/HotkeysProvider.cjs +0 -23
  28. package/dist/HotkeysProvider.cjs.map +0 -1
  29. package/dist/HotkeysProvider.d.cts +0 -28
  30. package/dist/HotkeysProvider.js.map +0 -1
  31. package/dist/_virtual/_rolldown/runtime.cjs +0 -29
  32. package/dist/index.cjs +0 -35
  33. package/dist/index.d.cts +0 -14
  34. package/dist/useHeldKeyCodes.cjs +0 -38
  35. package/dist/useHeldKeyCodes.cjs.map +0 -1
  36. package/dist/useHeldKeyCodes.d.cts +0 -31
  37. package/dist/useHeldKeyCodes.js.map +0 -1
  38. package/dist/useHeldKeys.cjs +0 -34
  39. package/dist/useHeldKeys.cjs.map +0 -1
  40. package/dist/useHeldKeys.d.cts +0 -27
  41. package/dist/useHeldKeys.js.map +0 -1
  42. package/dist/useHotkey.cjs +0 -126
  43. package/dist/useHotkey.cjs.map +0 -1
  44. package/dist/useHotkey.d.cts +0 -74
  45. package/dist/useHotkey.js.map +0 -1
  46. package/dist/useHotkeyHint.cjs +0 -19
  47. package/dist/useHotkeyHint.cjs.map +0 -1
  48. package/dist/useHotkeyHint.d.cts +0 -14
  49. package/dist/useHotkeyHint.js.map +0 -1
  50. package/dist/useHotkeyRecorder.cjs +0 -72
  51. package/dist/useHotkeyRecorder.cjs.map +0 -1
  52. package/dist/useHotkeyRecorder.d.cts +0 -57
  53. package/dist/useHotkeyRecorder.js.map +0 -1
  54. package/dist/useHotkeyRegistrations.cjs +0 -49
  55. package/dist/useHotkeyRegistrations.cjs.map +0 -1
  56. package/dist/useHotkeyRegistrations.d.cts +0 -48
  57. package/dist/useHotkeyRegistrations.js.map +0 -1
  58. package/dist/useHotkeySequence.cjs +0 -121
  59. package/dist/useHotkeySequence.cjs.map +0 -1
  60. package/dist/useHotkeySequence.d.cts +0 -58
  61. package/dist/useHotkeySequence.js.map +0 -1
  62. package/dist/useHotkeySequenceRecorder.cjs +0 -42
  63. package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
  64. package/dist/useHotkeySequenceRecorder.d.cts +0 -25
  65. package/dist/useHotkeySequenceRecorder.js.map +0 -1
  66. package/dist/useHotkeySequences.cjs +0 -127
  67. package/dist/useHotkeySequences.cjs.map +0 -1
  68. package/dist/useHotkeySequences.d.cts +0 -63
  69. package/dist/useHotkeySequences.js.map +0 -1
  70. package/dist/useHotkeys.cjs +0 -125
  71. package/dist/useHotkeys.cjs.map +0 -1
  72. package/dist/useHotkeys.d.cts +0 -63
  73. package/dist/useHotkeys.js.map +0 -1
  74. package/dist/useKeyHold.cjs +0 -54
  75. package/dist/useKeyHold.cjs.map +0 -1
  76. package/dist/useKeyHold.d.cts +0 -47
  77. package/dist/useKeyHold.js.map +0 -1
  78. package/dist/utils.cjs +0 -12
  79. package/dist/utils.cjs.map +0 -1
  80. package/dist/utils.js.map +0 -1
  81. package/src/HotkeysProvider.tsx +0 -58
  82. package/src/index.ts +0 -18
  83. package/src/useHeldKeyCodes.ts +0 -33
  84. package/src/useHeldKeys.ts +0 -29
  85. package/src/useHotkey.ts +0 -190
  86. package/src/useHotkeyHint.ts +0 -19
  87. package/src/useHotkeyRecorder.ts +0 -101
  88. package/src/useHotkeyRegistrations.ts +0 -67
  89. package/src/useHotkeySequence.ts +0 -191
  90. package/src/useHotkeySequenceRecorder.ts +0 -70
  91. package/src/useHotkeySequences.ts +0 -192
  92. package/src/useHotkeys.ts +0 -193
  93. package/src/useKeyHold.ts +0 -52
  94. package/src/utils.ts +0 -10
@@ -1,192 +0,0 @@
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
- // Publish callback/options only after commit, never during render.
158
- existing.handle.callback = def.callback
159
- const { target: _target, ...optionsWithoutTarget } = mergedOptions
160
- existing.handle.setOptions(optionsWithoutTarget)
161
- nextRegistrations.set(registrationKey, existing)
162
- continue
163
- }
164
-
165
- if (existing?.handle.isActive) {
166
- existing.handle.unregister()
167
- }
168
-
169
- const handle = managerRef.current.register(seq, def.callback, {
170
- ...mergedOptions,
171
- target: resolvedTarget,
172
- })
173
- nextRegistrations.set(registrationKey, {
174
- handle,
175
- target: resolvedTarget,
176
- })
177
- }
178
-
179
- registrationsRef.current = nextRegistrations
180
- })
181
-
182
- useEffect(() => {
183
- return () => {
184
- for (const { handle } of registrationsRef.current.values()) {
185
- if (handle.isActive) {
186
- handle.unregister()
187
- }
188
- }
189
- registrationsRef.current = new Map()
190
- }
191
- }, [])
192
- }
package/src/useHotkeys.ts DELETED
@@ -1,193 +0,0 @@
1
- import { useEffect, useRef } from 'react'
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
- * React 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/react-store'
2
- import { getKeyStateTracker } from '@tanstack/hotkeys'
3
- import type { IndividualKey } from '@tanstack/hotkeys'
4
-
5
- /**
6
- * React hook that returns whether a specific key is currently being held.
7
- *
8
- * This hook uses `useSelector` from `@tanstack/react-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,10 +0,0 @@
1
- import type React from 'react'
2
-
3
- /**
4
- * Type guard to check if a value is a React ref-like object.
5
- */
6
- export function isRef(
7
- value: unknown,
8
- ): value is React.RefObject<HTMLElement | null> {
9
- return value !== null && typeof value === 'object' && 'current' in value
10
- }