@tanstack/preact-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 +24 -9
  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 +24 -9
  20. package/dist/useHotkeySequence.js.map +1 -1
  21. package/dist/useHotkeySequenceRecorder.cjs +39 -0
  22. package/dist/useHotkeySequenceRecorder.cjs.map +1 -0
  23. package/dist/useHotkeySequenceRecorder.d.cts +19 -0
  24. package/dist/useHotkeySequenceRecorder.d.ts +19 -0
  25. package/dist/useHotkeySequenceRecorder.js +39 -0
  26. package/dist/useHotkeySequenceRecorder.js.map +1 -0
  27. package/dist/useHotkeySequences.cjs +138 -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 +19 -4
  44. package/src/useHotkeySequenceRecorder.ts +64 -0
  45. package/src/useHotkeySequences.ts +206 -0
  46. package/src/useHotkeys.ts +7 -14
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\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 * Preact 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 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 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(options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AAI;IACR;AAED,UAAO,WAAW,qBAAqB"}
1
+ {"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\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 * Preact 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 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,MAAM,WACJ,cAAc,YAAY,gBAAgB,YAAY,gBAAgB;CAExE,MAAM,mBAAm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AAW,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;AAEN,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/preact-hotkeys",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "description": "Preact 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/preact-store": "^0.11.2",
43
- "@tanstack/hotkeys": "0.4.3"
43
+ "@tanstack/hotkeys": "0.5.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@preact/preset-vite": "^2.10.5",
@@ -1,13 +1,17 @@
1
1
  import { createContext } from 'preact'
2
2
  import { useContext, useMemo } from 'preact/hooks'
3
3
  import type { ComponentChildren } from 'preact'
4
- import type { HotkeyRecorderOptions } from '@tanstack/hotkeys'
4
+ import type {
5
+ HotkeyRecorderOptions,
6
+ HotkeySequenceRecorderOptions,
7
+ } from '@tanstack/hotkeys'
5
8
  import type { UseHotkeyOptions } from './useHotkey'
6
9
  import type { UseHotkeySequenceOptions } from './useHotkeySequence'
7
10
 
8
11
  export interface HotkeysProviderOptions {
9
12
  hotkey?: Partial<UseHotkeyOptions>
10
13
  hotkeyRecorder?: Partial<HotkeyRecorderOptions>
14
+ hotkeySequenceRecorder?: Partial<HotkeySequenceRecorderOptions>
11
15
  hotkeySequence?: Partial<UseHotkeySequenceOptions>
12
16
  }
13
17
 
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
@@ -44,7 +44,8 @@ export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
44
44
  *
45
45
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
46
46
  * @param callback - The function to call when the hotkey is pressed
47
- * @param options - Options for the hotkey behavior
47
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
48
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
48
49
  *
49
50
  * @example
50
51
  * ```tsx
@@ -136,6 +137,12 @@ export function useHotkey(
136
137
 
137
138
  // Skip if no valid target (SSR or ref still null)
138
139
  if (!resolvedTarget) {
140
+ if (registrationRef.current?.isActive) {
141
+ registrationRef.current.unregister()
142
+ registrationRef.current = null
143
+ }
144
+ prevTargetRef.current = null
145
+ prevHotkeyRef.current = null
139
146
  return
140
147
  }
141
148
 
@@ -175,7 +182,7 @@ export function useHotkey(
175
182
  registrationRef.current = null
176
183
  }
177
184
  }
178
- }, [hotkeyString, options.enabled])
185
+ }, [hotkeyString])
179
186
 
180
187
  // Sync callback and options on EVERY render (outside useEffect)
181
188
  // This avoids stale closures - the callback always has access to latest state
@@ -41,7 +41,8 @@ export interface UseHotkeySequenceOptions extends Omit<
41
41
  *
42
42
  * @param sequence - Array of hotkey strings that form the sequence
43
43
  * @param callback - Function to call when the sequence is completed
44
- * @param options - Options for the sequence behavior
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.
45
46
  *
46
47
  * @example
47
48
  * ```tsx
@@ -89,11 +90,13 @@ export function useHotkeySequence(
89
90
  const callbackRef = useRef(callback)
90
91
  const optionsRef = useRef(mergedOptions)
91
92
  const managerRef = useRef(manager)
93
+ const sequenceRef = useRef(sequence)
92
94
 
93
95
  // Update refs on every render
94
96
  callbackRef.current = callback
95
97
  optionsRef.current = mergedOptions
96
98
  managerRef.current = manager
99
+ sequenceRef.current = sequence
97
100
 
98
101
  // Track previous target and sequence to detect changes requiring re-registration
99
102
  const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
@@ -106,7 +109,13 @@ export function useHotkeySequence(
106
109
  const { target: _target, ...optionsWithoutTarget } = mergedOptions
107
110
 
108
111
  useEffect(() => {
109
- if (sequence.length === 0) {
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
110
119
  return
111
120
  }
112
121
 
@@ -118,6 +127,12 @@ export function useHotkeySequence(
118
127
 
119
128
  // Skip if no valid target (SSR or ref still null)
120
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
121
136
  return
122
137
  }
123
138
 
@@ -140,7 +155,7 @@ export function useHotkeySequence(
140
155
  // Register if needed (no active registration)
141
156
  if (!registrationRef.current || !registrationRef.current.isActive) {
142
157
  registrationRef.current = managerRef.current.register(
143
- sequence,
158
+ sequenceRef.current,
144
159
  (event, context) => callbackRef.current(event, context),
145
160
  {
146
161
  ...optionsRef.current,
@@ -160,7 +175,7 @@ export function useHotkeySequence(
160
175
  registrationRef.current = null
161
176
  }
162
177
  }
163
- }, [hotkeySequenceString, mergedOptions.enabled, sequence])
178
+ }, [hotkeySequenceString])
164
179
 
165
180
  // Sync callback and options on EVERY render (outside useEffect)
166
181
  if (registrationRef.current?.isActive) {
@@ -0,0 +1,64 @@
1
+ import { useEffect, useRef } from 'preact/hooks'
2
+ import { useStore } 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 = useStore(
40
+ recorderRef.current.store,
41
+ (state) => state.isRecording,
42
+ )
43
+ const steps = useStore(recorderRef.current.store, (state) => state.steps)
44
+ const recordedSequence = useStore(
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
+ }
@@ -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
- }, [hotkeyKey, enabledKey])
191
+ }, [])
199
192
 
200
193
  for (let i = 0; i < hotkeys.length; i++) {
201
194
  const def = hotkeys[i]!