@tanstack/react-hotkeys 0.5.1 → 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.
Files changed (46) hide show
  1. package/dist/HotkeysProvider.cjs.map +1 -1
  2. package/dist/HotkeysProvider.d.cts +2 -1
  3. package/dist/HotkeysProvider.d.ts +2 -1
  4. package/dist/HotkeysProvider.js.map +1 -1
  5. package/dist/index.cjs +4 -0
  6. package/dist/index.d.cts +3 -1
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.js +3 -1
  9. package/dist/useHotkey.cjs +12 -3
  10. package/dist/useHotkey.cjs.map +1 -1
  11. package/dist/useHotkey.d.cts +2 -1
  12. package/dist/useHotkey.d.ts +2 -1
  13. package/dist/useHotkey.js +12 -3
  14. package/dist/useHotkey.js.map +1 -1
  15. package/dist/useHotkeySequence.cjs +21 -4
  16. package/dist/useHotkeySequence.cjs.map +1 -1
  17. package/dist/useHotkeySequence.d.cts +2 -1
  18. package/dist/useHotkeySequence.d.ts +2 -1
  19. package/dist/useHotkeySequence.js +21 -4
  20. package/dist/useHotkeySequence.js.map +1 -1
  21. package/dist/useHotkeySequenceRecorder.cjs +42 -0
  22. package/dist/useHotkeySequenceRecorder.cjs.map +1 -0
  23. package/dist/useHotkeySequenceRecorder.d.cts +25 -0
  24. package/dist/useHotkeySequenceRecorder.d.ts +25 -0
  25. package/dist/useHotkeySequenceRecorder.js +41 -0
  26. package/dist/useHotkeySequenceRecorder.js.map +1 -0
  27. package/dist/useHotkeySequences.cjs +139 -0
  28. package/dist/useHotkeySequences.cjs.map +1 -0
  29. package/dist/useHotkeySequences.d.cts +63 -0
  30. package/dist/useHotkeySequences.d.ts +63 -0
  31. package/dist/useHotkeySequences.js +138 -0
  32. package/dist/useHotkeySequences.js.map +1 -0
  33. package/dist/useHotkeys.cjs +7 -8
  34. package/dist/useHotkeys.cjs.map +1 -1
  35. package/dist/useHotkeys.d.cts +4 -1
  36. package/dist/useHotkeys.d.ts +4 -1
  37. package/dist/useHotkeys.js +7 -8
  38. package/dist/useHotkeys.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/HotkeysProvider.tsx +5 -1
  41. package/src/index.ts +2 -0
  42. package/src/useHotkey.ts +9 -2
  43. package/src/useHotkeySequence.ts +15 -2
  44. package/src/useHotkeySequenceRecorder.ts +70 -0
  45. package/src/useHotkeySequences.ts +206 -0
  46. package/src/useHotkeys.ts +7 -15
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * React hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n // Stable serialized keys for effect dependencies\n const hotkeyKey = hotkeyStrings.join('\\0')\n const enabledKey = hotkeys\n .map((def) => {\n const merged = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n }\n return merged.enabled ?? true\n })\n .join('\\0')\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [hotkeyKey, enabledKey])\n\n // Sync callbacks and options on EVERY render (outside useEffect)\n for (let i = 0; i < hotkeys.length; i++) {\n const def = hotkeys[i]!\n const hotkeyStr = hotkeyStrings[i]!\n const registrationKey = `${i}:${hotkeyStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeyOptions\n const { target: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,yBALN;IACpB,GAAG;IACH,GAAG;IACH,GAAG,IAAI;IACR;AAED,UAAO,WAAW,qBAAqB"}
1
+ {"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * React hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).\n * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n\n // Sync callbacks and options on EVERY render (outside useEffect)\n for (let i = 0; i < hotkeys.length; i++) {\n const def = hotkeys[i]!\n const hotkeyStr = hotkeyStrings[i]!\n const registrationKey = `${i}:${hotkeyStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeyOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,SAAgB,WACd,SACA,gBAAkC,EAAE,EAC9B;CAMN,MAAM,iBAAiB,0BAA0B,CAAC;CAClD,MAAM,UAAU,kBAAkB;CAClC,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GACtB,MAAM,EAAE,iBAAiB,KAAK,WAAW,eAAe,mBACtD;GAEF,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AACnE,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,WAAW,IAAI,UAAU;IAClE,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;GAC3B;AAEF,iBAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC;AAGN,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,MAAM,QAAQ;EACpB,MAAM,YAAY,cAAc;EAChC,MAAM,kBAAkB,GAAG,EAAE,GAAG;EAChC,MAAM,SAAS,iBAAiB,QAAQ,IAAI,gBAAgB,EAAE;AAE9D,MAAI,QAAQ,UAAU;AACpB,UAAO,WAAW,IAAI;GAMtB,MAAM,EAAE,QAAQ,SAAS,GAAG,yBALN;IACpB,GAAG;IACH,GAAG;IACH,GAAG,IAAI;IACR;AAED,UAAO,WAAW,qBAAqB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-hotkeys",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "description": "React 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/react-store": "^0.9.2",
43
- "@tanstack/hotkeys": "0.4.3"
43
+ "@tanstack/hotkeys": "0.5.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@eslint-react/eslint-plugin": "^3.0.0",
@@ -1,12 +1,16 @@
1
1
  import React, { createContext, useContext, useMemo } from 'react'
2
2
  import type { ReactNode } from 'react'
3
- import type { HotkeyRecorderOptions } from '@tanstack/hotkeys'
3
+ import type {
4
+ HotkeyRecorderOptions,
5
+ HotkeySequenceRecorderOptions,
6
+ } from '@tanstack/hotkeys'
4
7
  import type { UseHotkeyOptions } from './useHotkey'
5
8
  import type { UseHotkeySequenceOptions } from './useHotkeySequence'
6
9
 
7
10
  export interface HotkeysProviderOptions {
8
11
  hotkey?: Partial<UseHotkeyOptions>
9
12
  hotkeyRecorder?: Partial<HotkeyRecorderOptions>
13
+ hotkeySequenceRecorder?: Partial<HotkeySequenceRecorderOptions>
10
14
  hotkeySequence?: Partial<UseHotkeySequenceOptions>
11
15
  }
12
16
 
package/src/index.ts CHANGED
@@ -11,4 +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'
16
+ export * from './useHotkeySequenceRecorder'
package/src/useHotkey.ts CHANGED
@@ -43,7 +43,8 @@ export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
43
43
  *
44
44
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
45
45
  * @param callback - The function to call when the hotkey is pressed
46
- * @param options - Options for the hotkey behavior
46
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
47
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
47
48
  *
48
49
  * @example
49
50
  * ```tsx
@@ -135,6 +136,12 @@ export function useHotkey(
135
136
 
136
137
  // Skip if no valid target (SSR or ref still null)
137
138
  if (!resolvedTarget) {
139
+ if (registrationRef.current?.isActive) {
140
+ registrationRef.current.unregister()
141
+ registrationRef.current = null
142
+ }
143
+ prevTargetRef.current = null
144
+ prevHotkeyRef.current = null
138
145
  return
139
146
  }
140
147
 
@@ -174,7 +181,7 @@ export function useHotkey(
174
181
  registrationRef.current = null
175
182
  }
176
183
  }
177
- }, [hotkeyString, options.enabled])
184
+ }, [hotkeyString])
178
185
 
179
186
  // Sync callback and options on EVERY render (outside useEffect)
180
187
  // This avoids stale closures - the callback always has access to latest state
@@ -40,7 +40,8 @@ export interface UseHotkeySequenceOptions extends Omit<
40
40
  *
41
41
  * @param sequence - Array of hotkey strings that form the sequence
42
42
  * @param callback - Function to call when the sequence is completed
43
- * @param options - Options for the sequence behavior
43
+ * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
44
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
44
45
  *
45
46
  * @example
46
47
  * ```tsx
@@ -108,6 +109,12 @@ export function useHotkeySequence(
108
109
 
109
110
  useEffect(() => {
110
111
  if (sequenceRef.current.length === 0) {
112
+ if (registrationRef.current?.isActive) {
113
+ registrationRef.current.unregister()
114
+ registrationRef.current = null
115
+ }
116
+ prevTargetRef.current = null
117
+ prevSequenceRef.current = null
111
118
  return
112
119
  }
113
120
 
@@ -119,6 +126,12 @@ export function useHotkeySequence(
119
126
 
120
127
  // Skip if no valid target (SSR or ref still null)
121
128
  if (!resolvedTarget) {
129
+ if (registrationRef.current?.isActive) {
130
+ registrationRef.current.unregister()
131
+ registrationRef.current = null
132
+ }
133
+ prevTargetRef.current = null
134
+ prevSequenceRef.current = null
122
135
  return
123
136
  }
124
137
 
@@ -161,7 +174,7 @@ export function useHotkeySequence(
161
174
  registrationRef.current = null
162
175
  }
163
176
  }
164
- }, [hotkeySequenceString, mergedOptions.enabled])
177
+ }, [hotkeySequenceString])
165
178
 
166
179
  // Sync callback and options on EVERY render (outside useEffect)
167
180
  if (registrationRef.current?.isActive) {
@@ -0,0 +1,70 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import { useStore } from '@tanstack/react-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 ReactHotkeySequenceRecorder {
11
+ /** Whether recording is currently active */
12
+ isRecording: boolean
13
+ /** Chords captured in the current session */
14
+ steps: HotkeySequence
15
+ /** Last committed sequence */
16
+ recordedSequence: HotkeySequence | null
17
+ startRecording: () => void
18
+ stopRecording: () => void
19
+ cancelRecording: () => void
20
+ /** Commit current steps (no-op if empty) */
21
+ commitRecording: () => void
22
+ }
23
+
24
+ /**
25
+ * React hook for recording multi-chord sequences (Vim-style shortcuts).
26
+ *
27
+ * @param options - Configuration options for the hotkey sequence recorder
28
+ */
29
+ export function useHotkeySequenceRecorder(
30
+ options: HotkeySequenceRecorderOptions,
31
+ ): ReactHotkeySequenceRecorder {
32
+ const mergedOptions = {
33
+ ...useDefaultHotkeysOptions().hotkeySequenceRecorder,
34
+ ...options,
35
+ } as HotkeySequenceRecorderOptions
36
+
37
+ const recorderRef = useRef<HotkeySequenceRecorder | null>(null)
38
+
39
+ if (!recorderRef.current) {
40
+ recorderRef.current = new HotkeySequenceRecorder(mergedOptions)
41
+ }
42
+
43
+ recorderRef.current.setOptions(mergedOptions)
44
+
45
+ const isRecording = useStore(
46
+ recorderRef.current.store,
47
+ (state) => state.isRecording,
48
+ )
49
+ const steps = useStore(recorderRef.current.store, (state) => state.steps)
50
+ const recordedSequence = useStore(
51
+ recorderRef.current.store,
52
+ (state) => state.recordedSequence,
53
+ )
54
+
55
+ useEffect(() => {
56
+ return () => {
57
+ recorderRef.current?.destroy()
58
+ }
59
+ }, [])
60
+
61
+ return {
62
+ isRecording,
63
+ steps,
64
+ recordedSequence,
65
+ startRecording: () => recorderRef.current?.start(),
66
+ stopRecording: () => recorderRef.current?.stop(),
67
+ cancelRecording: () => recorderRef.current?.cancel(),
68
+ commitRecording: () => recorderRef.current?.commit(),
69
+ }
70
+ }
@@ -0,0 +1,206 @@
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
+ 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,19 +104,6 @@ export function useHotkeys(
101
104
  defaultOptionsRef.current = defaultOptions
102
105
  managerRef.current = manager
103
106
 
104
- // Stable serialized keys for effect dependencies
105
- const hotkeyKey = hotkeyStrings.join('\0')
106
- const enabledKey = hotkeys
107
- .map((def) => {
108
- const merged = {
109
- ...defaultOptions,
110
- ...commonOptions,
111
- ...def.options,
112
- }
113
- return merged.enabled ?? true
114
- })
115
- .join('\0')
116
-
117
107
  useEffect(() => {
118
108
  const prevRegistrations = registrationsRef.current
119
109
  const nextRegistrations = new Map<string, RegistrationRecord>()
@@ -187,7 +177,9 @@ export function useHotkeys(
187
177
  }
188
178
 
189
179
  registrationsRef.current = nextRegistrations
180
+ })
190
181
 
182
+ useEffect(() => {
191
183
  return () => {
192
184
  for (const { handle } of registrationsRef.current.values()) {
193
185
  if (handle.isActive) {
@@ -196,7 +188,7 @@ export function useHotkeys(
196
188
  }
197
189
  registrationsRef.current = new Map()
198
190
  }
199
- }, [hotkeyKey, enabledKey])
191
+ }, [])
200
192
 
201
193
  // Sync callbacks and options on EVERY render (outside useEffect)
202
194
  for (let i = 0; i < hotkeys.length; i++) {