@tanstack/preact-hotkeys 0.6.0 → 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.
package/dist/index.cjs CHANGED
@@ -6,6 +6,7 @@ const require_useHeldKeys = require('./useHeldKeys.cjs');
6
6
  const require_useHeldKeyCodes = require('./useHeldKeyCodes.cjs');
7
7
  const require_useKeyHold = require('./useKeyHold.cjs');
8
8
  const require_useHotkeySequence = require('./useHotkeySequence.cjs');
9
+ const require_useHotkeySequences = require('./useHotkeySequences.cjs');
9
10
  const require_useHotkeyRecorder = require('./useHotkeyRecorder.cjs');
10
11
  const require_useHotkeySequenceRecorder = require('./useHotkeySequenceRecorder.cjs');
11
12
 
@@ -17,6 +18,7 @@ exports.useHotkey = require_useHotkey.useHotkey;
17
18
  exports.useHotkeyRecorder = require_useHotkeyRecorder.useHotkeyRecorder;
18
19
  exports.useHotkeySequence = require_useHotkeySequence.useHotkeySequence;
19
20
  exports.useHotkeySequenceRecorder = require_useHotkeySequenceRecorder.useHotkeySequenceRecorder;
21
+ exports.useHotkeySequences = require_useHotkeySequences.useHotkeySequences;
20
22
  exports.useHotkeys = require_useHotkeys.useHotkeys;
21
23
  exports.useHotkeysContext = require_HotkeysProvider.useHotkeysContext;
22
24
  exports.useKeyHold = require_useKeyHold.useKeyHold;
package/dist/index.d.cts CHANGED
@@ -5,7 +5,8 @@ import { UseHotkeyDefinition, useHotkeys } from "./useHotkeys.cjs";
5
5
  import { useHeldKeys } from "./useHeldKeys.cjs";
6
6
  import { useHeldKeyCodes } from "./useHeldKeyCodes.cjs";
7
7
  import { useKeyHold } from "./useKeyHold.cjs";
8
+ import { UseHotkeySequenceDefinition, useHotkeySequences } from "./useHotkeySequences.cjs";
8
9
  import { PreactHotkeyRecorder, useHotkeyRecorder } from "./useHotkeyRecorder.cjs";
9
10
  import { PreactHotkeySequenceRecorder, useHotkeySequenceRecorder } from "./useHotkeySequenceRecorder.cjs";
10
11
  export * from "@tanstack/hotkeys";
11
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, PreactHotkeySequenceRecorder, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeySequenceRecorder, useHotkeys, useHotkeysContext, useKeyHold };
12
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, PreactHotkeySequenceRecorder, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeySequenceDefinition, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeySequenceRecorder, useHotkeySequences, useHotkeys, useHotkeysContext, useKeyHold };
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ import { UseHotkeyDefinition, useHotkeys } from "./useHotkeys.js";
5
5
  import { useHeldKeys } from "./useHeldKeys.js";
6
6
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
7
7
  import { useKeyHold } from "./useKeyHold.js";
8
+ import { UseHotkeySequenceDefinition, useHotkeySequences } from "./useHotkeySequences.js";
8
9
  import { PreactHotkeyRecorder, useHotkeyRecorder } from "./useHotkeyRecorder.js";
9
10
  import { PreactHotkeySequenceRecorder, useHotkeySequenceRecorder } from "./useHotkeySequenceRecorder.js";
10
11
  export * from "@tanstack/hotkeys";
11
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, PreactHotkeySequenceRecorder, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeySequenceRecorder, useHotkeys, useHotkeysContext, useKeyHold };
12
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, PreactHotkeySequenceRecorder, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeySequenceDefinition, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeySequenceRecorder, useHotkeySequences, useHotkeys, useHotkeysContext, useKeyHold };
package/dist/index.js CHANGED
@@ -5,9 +5,10 @@ import { useHeldKeys } from "./useHeldKeys.js";
5
5
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
6
6
  import { useKeyHold } from "./useKeyHold.js";
7
7
  import { useHotkeySequence } from "./useHotkeySequence.js";
8
+ import { useHotkeySequences } from "./useHotkeySequences.js";
8
9
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
9
10
  import { useHotkeySequenceRecorder } from "./useHotkeySequenceRecorder.js";
10
11
 
11
12
  export * from "@tanstack/hotkeys"
12
13
 
13
- export { HotkeysProvider, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeySequenceRecorder, useHotkeys, useHotkeysContext, useKeyHold };
14
+ export { HotkeysProvider, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeySequenceRecorder, useHotkeySequences, useHotkeys, useHotkeysContext, useKeyHold };
@@ -18,7 +18,8 @@ let preact_hooks = require("preact/hooks");
18
18
  *
19
19
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
20
20
  * @param callback - The function to call when the hotkey is pressed
21
- * @param options - Options for the hotkey behavior
21
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
22
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
22
23
  *
23
24
  * @example
24
25
  * ```tsx
@@ -82,7 +83,15 @@ function useHotkey(hotkey, callback, options = {}) {
82
83
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
83
84
  (0, preact_hooks.useEffect)(() => {
84
85
  const resolvedTarget = require_utils.isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
85
- if (!resolvedTarget) return;
86
+ if (!resolvedTarget) {
87
+ if (registrationRef.current?.isActive) {
88
+ registrationRef.current.unregister();
89
+ registrationRef.current = null;
90
+ }
91
+ prevTargetRef.current = null;
92
+ prevHotkeyRef.current = null;
93
+ return;
94
+ }
86
95
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
87
96
  const hotkeyChanged = prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString;
88
97
  if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
@@ -101,7 +110,7 @@ function useHotkey(hotkey, callback, options = {}) {
101
110
  registrationRef.current = null;
102
111
  }
103
112
  };
104
- }, [hotkeyString, options.enabled]);
113
+ }, [hotkeyString]);
105
114
  if (registrationRef.current?.isActive) {
106
115
  registrationRef.current.callback = callback;
107
116
  registrationRef.current.setOptions(optionsWithoutTarget);
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkey.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkey.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 { RefObject } from 'preact'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyOptions,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard hotkey.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * The callback receives both the keyboard event and a context object\n * containing the hotkey string and parsed hotkey.\n *\n * This hook syncs the callback and options on every render to avoid\n * stale closures. This means\n * callbacks that reference Preact state will always have access to\n * the latest values.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior\n *\n * @example\n * ```tsx\n * function SaveButton() {\n * const [count, setCount] = useState(0)\n *\n * // Callback always has access to latest count value\n * useHotkey('Mod+S', (event, { hotkey }) => {\n * console.log(`Save triggered, count is ${count}`)\n * handleSave()\n * })\n *\n * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Modal({ isOpen, onClose }) {\n * // enabled option is synced on every render\n * useHotkey('Escape', () => {\n * onClose()\n * }, { enabled: isOpen })\n *\n * if (!isOpen) return null\n * return <div className=\"modal\">...</div>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Editor() {\n * const editorRef = useRef<HTMLDivElement>(null)\n *\n * // Scoped to a specific element\n * useHotkey('Mod+S', () => {\n * save()\n * }, { target: editorRef })\n *\n * return <div ref={editorRef}>...</div>\n * }\n * ```\n */\nexport function useHotkey(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: UseHotkeyOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkey,\n ...options,\n } as UseHotkeyOptions\n\n const manager = getHotkeyManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<HotkeyRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and hotkey to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevHotkeyRef = useRef<string | null>(null)\n\n // Normalize to hotkey string\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof hotkey === 'string'\n ? hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform)) as Hotkey)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n return\n }\n\n // Check if we need to re-register (target or hotkey changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const hotkeyChanged =\n prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString\n\n // If we have an active registration and target/hotkey changed, unregister first\n if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n // Use refs to access current values without adding them to dependencies\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n hotkeyString,\n callbackRef.current,\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevHotkeyRef.current = hotkeyString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeyString, options.enabled])\n\n // Sync callback and options on EVERY render (outside useEffect)\n // This avoids stale closures - the callback always has access to latest state\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = callback\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AAgB,QAAQ,WAAW;AACnC,kBAAgB,QAAQ,WAAW,qBAAqB"}
1
+ {"version":3,"file":"useHotkey.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkey.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 { RefObject } from 'preact'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyOptions,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard hotkey.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * The callback receives both the keyboard event and a context object\n * containing the hotkey string and parsed hotkey.\n *\n * This hook syncs the callback and options on every render to avoid\n * stale closures. This means\n * callbacks that reference Preact state will always have access to\n * the latest values.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)\n * and only suppresses firing; the hook updates the existing handle instead of unregistering.\n *\n * @example\n * ```tsx\n * function SaveButton() {\n * const [count, setCount] = useState(0)\n *\n * // Callback always has access to latest count value\n * useHotkey('Mod+S', (event, { hotkey }) => {\n * console.log(`Save triggered, count is ${count}`)\n * handleSave()\n * })\n *\n * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Modal({ isOpen, onClose }) {\n * // enabled option is synced on every render\n * useHotkey('Escape', () => {\n * onClose()\n * }, { enabled: isOpen })\n *\n * if (!isOpen) return null\n * return <div className=\"modal\">...</div>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Editor() {\n * const editorRef = useRef<HTMLDivElement>(null)\n *\n * // Scoped to a specific element\n * useHotkey('Mod+S', () => {\n * save()\n * }, { target: editorRef })\n *\n * return <div ref={editorRef}>...</div>\n * }\n * ```\n */\nexport function useHotkey(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: UseHotkeyOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkey,\n ...options,\n } as UseHotkeyOptions\n\n const manager = getHotkeyManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<HotkeyRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and hotkey to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevHotkeyRef = useRef<string | null>(null)\n\n // Normalize to hotkey string\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof hotkey === 'string'\n ? hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform)) as Hotkey)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevHotkeyRef.current = null\n return\n }\n\n // Check if we need to re-register (target or hotkey changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const hotkeyChanged =\n prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString\n\n // If we have an active registration and target/hotkey changed, unregister first\n if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n // Use refs to access current values without adding them to dependencies\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n hotkeyString,\n callbackRef.current,\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevHotkeyRef.current = hotkeyString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeyString])\n\n // Sync callback and options on EVERY render (outside useEffect)\n // This avoids stale closures - the callback always has access to latest state\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = callback\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,SAAgB,UACd,QACA,UACA,UAA4B,EAAE,EACxB;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,mDAA4B;CAGlC,MAAM,2CAA0D,KAAK;CAGrE,MAAM,uCAAqB,SAAS;CACpC,MAAM,sCAAoB,cAAc;CACxC,MAAM,sCAAoB,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,yCAA+D,KAAK;CAC1E,MAAM,yCAAsC,KAAK;CAGjD,MAAM,WAAW,cAAc,mDAA4B;CAC3D,MAAM,eACJ,OAAO,WAAW,WACd,4FACsC,QAAQ,SAAS,CAAC;CAG9D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,mCAAgB;EAEd,MAAM,iBAAiBC,oBAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,gBAAgB;AACnB,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;AAE5B,iBAAc,UAAU;AACxB,iBAAc,UAAU;AACxB;;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;AAG9D,MAAI,gBAAgB,SAAS,aAAa,iBAAiB,gBAAgB;AACzE,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAK5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,cACA,YAAY,SACZ;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,gBAAc,UAAU;AAGxB,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,CAAC,aAAa,CAAC;AAIlB,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,WAAW;AACnC,kBAAgB,QAAQ,WAAW,qBAAqB"}
@@ -24,7 +24,8 @@ interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
24
24
  *
25
25
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
26
26
  * @param callback - The function to call when the hotkey is pressed
27
- * @param options - Options for the hotkey behavior
27
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
28
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
28
29
  *
29
30
  * @example
30
31
  * ```tsx
@@ -24,7 +24,8 @@ interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
24
24
  *
25
25
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
26
26
  * @param callback - The function to call when the hotkey is pressed
27
- * @param options - Options for the hotkey behavior
27
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
28
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
28
29
  *
29
30
  * @example
30
31
  * ```tsx
package/dist/useHotkey.js CHANGED
@@ -18,7 +18,8 @@ import { useEffect, useRef } from "preact/hooks";
18
18
  *
19
19
  * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
20
20
  * @param callback - The function to call when the hotkey is pressed
21
- * @param options - Options for the hotkey behavior
21
+ * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
22
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
22
23
  *
23
24
  * @example
24
25
  * ```tsx
@@ -82,7 +83,15 @@ function useHotkey(hotkey, callback, options = {}) {
82
83
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
83
84
  useEffect(() => {
84
85
  const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
85
- if (!resolvedTarget) return;
86
+ if (!resolvedTarget) {
87
+ if (registrationRef.current?.isActive) {
88
+ registrationRef.current.unregister();
89
+ registrationRef.current = null;
90
+ }
91
+ prevTargetRef.current = null;
92
+ prevHotkeyRef.current = null;
93
+ return;
94
+ }
86
95
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
87
96
  const hotkeyChanged = prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString;
88
97
  if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
@@ -101,7 +110,7 @@ function useHotkey(hotkey, callback, options = {}) {
101
110
  registrationRef.current = null;
102
111
  }
103
112
  };
104
- }, [hotkeyString, options.enabled]);
113
+ }, [hotkeyString]);
105
114
  if (registrationRef.current?.isActive) {
106
115
  registrationRef.current.callback = callback;
107
116
  registrationRef.current.setOptions(optionsWithoutTarget);
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkey.js","names":[],"sources":["../src/useHotkey.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 { RefObject } from 'preact'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyOptions,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard hotkey.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * The callback receives both the keyboard event and a context object\n * containing the hotkey string and parsed hotkey.\n *\n * This hook syncs the callback and options on every render to avoid\n * stale closures. This means\n * callbacks that reference Preact state will always have access to\n * the latest values.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior\n *\n * @example\n * ```tsx\n * function SaveButton() {\n * const [count, setCount] = useState(0)\n *\n * // Callback always has access to latest count value\n * useHotkey('Mod+S', (event, { hotkey }) => {\n * console.log(`Save triggered, count is ${count}`)\n * handleSave()\n * })\n *\n * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Modal({ isOpen, onClose }) {\n * // enabled option is synced on every render\n * useHotkey('Escape', () => {\n * onClose()\n * }, { enabled: isOpen })\n *\n * if (!isOpen) return null\n * return <div className=\"modal\">...</div>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Editor() {\n * const editorRef = useRef<HTMLDivElement>(null)\n *\n * // Scoped to a specific element\n * useHotkey('Mod+S', () => {\n * save()\n * }, { target: editorRef })\n *\n * return <div ref={editorRef}>...</div>\n * }\n * ```\n */\nexport function useHotkey(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: UseHotkeyOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkey,\n ...options,\n } as UseHotkeyOptions\n\n const manager = getHotkeyManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<HotkeyRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and hotkey to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevHotkeyRef = useRef<string | null>(null)\n\n // Normalize to hotkey string\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof hotkey === 'string'\n ? hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform)) as Hotkey)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n return\n }\n\n // Check if we need to re-register (target or hotkey changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const hotkeyChanged =\n prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString\n\n // If we have an active registration and target/hotkey changed, unregister first\n if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n // Use refs to access current values without adding them to dependencies\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n hotkeyString,\n callbackRef.current,\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevHotkeyRef.current = hotkeyString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeyString, options.enabled])\n\n // Sync callback and options on EVERY render (outside useEffect)\n // This avoids stale closures - the callback always has access to latest state\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = callback\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0FA,SAAgB,UACd,QACA,UACA,UAA4B,EAAE,EACxB;CACN,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,UAAU,kBAAkB;CAGlC,MAAM,kBAAkB,OAAwC,KAAK;CAGrE,MAAM,cAAc,OAAO,SAAS;CACpC,MAAM,aAAa,OAAO,cAAc;CACxC,MAAM,aAAa,OAAO,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,gBAAgB,OAA+C,KAAK;CAC1E,MAAM,gBAAgB,OAAsB,KAAK;CAGjD,MAAM,WAAW,cAAc,YAAY,gBAAgB;CAC3D,MAAM,eACJ,OAAO,WAAW,WACd,SACC,aAAa,wBAAwB,QAAQ,SAAS,CAAC;CAG9D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,iBAAgB;EAEd,MAAM,iBAAiB,MAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,eACH;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;AAG9D,MAAI,gBAAgB,SAAS,aAAa,iBAAiB,gBAAgB;AACzE,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAK5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,cACA,YAAY,SACZ;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,gBAAc,UAAU;AAGxB,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,CAAC,cAAc,QAAQ,QAAQ,CAAC;AAInC,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,WAAW;AACnC,kBAAgB,QAAQ,WAAW,qBAAqB"}
1
+ {"version":3,"file":"useHotkey.js","names":[],"sources":["../src/useHotkey.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 { RefObject } from 'preact'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyOptions,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard hotkey.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * The callback receives both the keyboard event and a context object\n * containing the hotkey string and parsed hotkey.\n *\n * This hook syncs the callback and options on every render to avoid\n * stale closures. This means\n * callbacks that reference Preact state will always have access to\n * the latest values.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)\n * and only suppresses firing; the hook updates the existing handle instead of unregistering.\n *\n * @example\n * ```tsx\n * function SaveButton() {\n * const [count, setCount] = useState(0)\n *\n * // Callback always has access to latest count value\n * useHotkey('Mod+S', (event, { hotkey }) => {\n * console.log(`Save triggered, count is ${count}`)\n * handleSave()\n * })\n *\n * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Modal({ isOpen, onClose }) {\n * // enabled option is synced on every render\n * useHotkey('Escape', () => {\n * onClose()\n * }, { enabled: isOpen })\n *\n * if (!isOpen) return null\n * return <div className=\"modal\">...</div>\n * }\n * ```\n *\n * @example\n * ```tsx\n * function Editor() {\n * const editorRef = useRef<HTMLDivElement>(null)\n *\n * // Scoped to a specific element\n * useHotkey('Mod+S', () => {\n * save()\n * }, { target: editorRef })\n *\n * return <div ref={editorRef}>...</div>\n * }\n * ```\n */\nexport function useHotkey(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: UseHotkeyOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkey,\n ...options,\n } as UseHotkeyOptions\n\n const manager = getHotkeyManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<HotkeyRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and hotkey to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevHotkeyRef = useRef<string | null>(null)\n\n // Normalize to hotkey string\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof hotkey === 'string'\n ? hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform)) as Hotkey)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevHotkeyRef.current = null\n return\n }\n\n // Check if we need to re-register (target or hotkey changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const hotkeyChanged =\n prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString\n\n // If we have an active registration and target/hotkey changed, unregister first\n if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n // Use refs to access current values without adding them to dependencies\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n hotkeyString,\n callbackRef.current,\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevHotkeyRef.current = hotkeyString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeyString])\n\n // Sync callback and options on EVERY render (outside useEffect)\n // This avoids stale closures - the callback always has access to latest state\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = callback\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,SAAgB,UACd,QACA,UACA,UAA4B,EAAE,EACxB;CACN,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,UAAU,kBAAkB;CAGlC,MAAM,kBAAkB,OAAwC,KAAK;CAGrE,MAAM,cAAc,OAAO,SAAS;CACpC,MAAM,aAAa,OAAO,cAAc;CACxC,MAAM,aAAa,OAAO,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,gBAAgB,OAA+C,KAAK;CAC1E,MAAM,gBAAgB,OAAsB,KAAK;CAGjD,MAAM,WAAW,cAAc,YAAY,gBAAgB;CAC3D,MAAM,eACJ,OAAO,WAAW,WACd,SACC,aAAa,wBAAwB,QAAQ,SAAS,CAAC;CAG9D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,iBAAgB;EAEd,MAAM,iBAAiB,MAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,gBAAgB;AACnB,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;AAE5B,iBAAc,UAAU;AACxB,iBAAc,UAAU;AACxB;;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;AAG9D,MAAI,gBAAgB,SAAS,aAAa,iBAAiB,gBAAgB;AACzE,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAK5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,cACA,YAAY,SACZ;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,gBAAc,UAAU;AAGxB,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,CAAC,aAAa,CAAC;AAIlB,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,WAAW;AACnC,kBAAgB,QAAQ,WAAW,qBAAqB"}
@@ -17,7 +17,8 @@ let preact_hooks = require("preact/hooks");
17
17
  *
18
18
  * @param sequence - Array of hotkey strings that form the sequence
19
19
  * @param callback - Function to call when the sequence is completed
20
- * @param options - Options for the sequence behavior
20
+ * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
21
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
21
22
  *
22
23
  * @example
23
24
  * ```tsx
@@ -56,24 +57,42 @@ function useHotkeySequence(sequence, callback, options = {}) {
56
57
  const callbackRef = (0, preact_hooks.useRef)(callback);
57
58
  const optionsRef = (0, preact_hooks.useRef)(mergedOptions);
58
59
  const managerRef = (0, preact_hooks.useRef)(manager);
60
+ const sequenceRef = (0, preact_hooks.useRef)(sequence);
59
61
  callbackRef.current = callback;
60
62
  optionsRef.current = mergedOptions;
61
63
  managerRef.current = manager;
64
+ sequenceRef.current = sequence;
62
65
  const prevTargetRef = (0, preact_hooks.useRef)(null);
63
66
  const prevSequenceRef = (0, preact_hooks.useRef)(null);
64
67
  const hotkeySequenceString = (0, _tanstack_hotkeys.formatHotkeySequence)(sequence);
65
68
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
66
69
  (0, preact_hooks.useEffect)(() => {
67
- if (sequence.length === 0) return;
70
+ if (sequenceRef.current.length === 0) {
71
+ if (registrationRef.current?.isActive) {
72
+ registrationRef.current.unregister();
73
+ registrationRef.current = null;
74
+ }
75
+ prevTargetRef.current = null;
76
+ prevSequenceRef.current = null;
77
+ return;
78
+ }
68
79
  const resolvedTarget = require_utils.isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
69
- if (!resolvedTarget) return;
80
+ if (!resolvedTarget) {
81
+ if (registrationRef.current?.isActive) {
82
+ registrationRef.current.unregister();
83
+ registrationRef.current = null;
84
+ }
85
+ prevTargetRef.current = null;
86
+ prevSequenceRef.current = null;
87
+ return;
88
+ }
70
89
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
71
90
  const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
72
91
  if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
73
92
  registrationRef.current.unregister();
74
93
  registrationRef.current = null;
75
94
  }
76
- if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
95
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequenceRef.current, (event, context) => callbackRef.current(event, context), {
77
96
  ...optionsRef.current,
78
97
  target: resolvedTarget
79
98
  });
@@ -85,11 +104,7 @@ function useHotkeySequence(sequence, callback, options = {}) {
85
104
  registrationRef.current = null;
86
105
  }
87
106
  };
88
- }, [
89
- hotkeySequenceString,
90
- mergedOptions.enabled,
91
- sequence
92
- ]);
107
+ }, [hotkeySequenceString]);
93
108
  if (registrationRef.current?.isActive) {
94
109
  registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
95
110
  registrationRef.current.setOptions(optionsWithoutTarget);
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * useHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequence.length === 0) {\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeySequenceString, mergedOptions.enabled, sequence])\n\n // Sync callback and options on EVERY render (outside useEffect)\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,qDAA8B;CAGpC,MAAM,2CAA4D,KAAK;CAGvE,MAAM,uCAAqB,SAAS;CACpC,MAAM,sCAAoB,cAAc;CACxC,MAAM,sCAAoB,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,yCAA+D,KAAK;CAC1E,MAAM,2CAAwC,KAAK;CAGnD,MAAM,mEAA4C,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,mCAAgB;AACd,MAAI,SAAS,WAAW,EACtB;EAIF,MAAM,iBAAiBC,oBAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,eACH;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,kBACJ,gBAAgB,YAAY,QAC5B,gBAAgB,YAAY;AAG9B,MACE,gBAAgB,SAAS,aACxB,iBAAiB,kBAClB;AACA,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAI5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;AAG1B,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B;EAAC;EAAsB,cAAc;EAAS;EAAS,CAAC;AAG3D,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB"}
1
+ {"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)\n * and only suppresses firing; the hook updates the existing handle instead of unregistering.\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * useHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n const sequenceRef = useRef(sequence)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n sequenceRef.current = sequence\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequenceRef.current.length === 0) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequenceRef.current,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeySequenceString])\n\n // Sync callback and options on EVERY render (outside useEffect)\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,qDAA8B;CAGpC,MAAM,2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a;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,CAAC,qBAAqB,CAAC;AAG1B,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB"}
@@ -23,7 +23,8 @@ interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
23
23
  *
24
24
  * @param sequence - Array of hotkey strings that form the sequence
25
25
  * @param callback - Function to call when the sequence is completed
26
- * @param options - Options for the sequence behavior
26
+ * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
27
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
27
28
  *
28
29
  * @example
29
30
  * ```tsx
@@ -23,7 +23,8 @@ interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
23
23
  *
24
24
  * @param sequence - Array of hotkey strings that form the sequence
25
25
  * @param callback - Function to call when the sequence is completed
26
- * @param options - Options for the sequence behavior
26
+ * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
27
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
27
28
  *
28
29
  * @example
29
30
  * ```tsx
@@ -17,7 +17,8 @@ import { useEffect, useRef } from "preact/hooks";
17
17
  *
18
18
  * @param sequence - Array of hotkey strings that form the sequence
19
19
  * @param callback - Function to call when the sequence is completed
20
- * @param options - Options for the sequence behavior
20
+ * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
21
+ * and only suppresses firing; the hook updates the existing handle instead of unregistering.
21
22
  *
22
23
  * @example
23
24
  * ```tsx
@@ -56,24 +57,42 @@ function useHotkeySequence(sequence, callback, options = {}) {
56
57
  const callbackRef = useRef(callback);
57
58
  const optionsRef = useRef(mergedOptions);
58
59
  const managerRef = useRef(manager);
60
+ const sequenceRef = useRef(sequence);
59
61
  callbackRef.current = callback;
60
62
  optionsRef.current = mergedOptions;
61
63
  managerRef.current = manager;
64
+ sequenceRef.current = sequence;
62
65
  const prevTargetRef = useRef(null);
63
66
  const prevSequenceRef = useRef(null);
64
67
  const hotkeySequenceString = formatHotkeySequence(sequence);
65
68
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
66
69
  useEffect(() => {
67
- if (sequence.length === 0) return;
70
+ if (sequenceRef.current.length === 0) {
71
+ if (registrationRef.current?.isActive) {
72
+ registrationRef.current.unregister();
73
+ registrationRef.current = null;
74
+ }
75
+ prevTargetRef.current = null;
76
+ prevSequenceRef.current = null;
77
+ return;
78
+ }
68
79
  const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
69
- if (!resolvedTarget) return;
80
+ if (!resolvedTarget) {
81
+ if (registrationRef.current?.isActive) {
82
+ registrationRef.current.unregister();
83
+ registrationRef.current = null;
84
+ }
85
+ prevTargetRef.current = null;
86
+ prevSequenceRef.current = null;
87
+ return;
88
+ }
70
89
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
71
90
  const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
72
91
  if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
73
92
  registrationRef.current.unregister();
74
93
  registrationRef.current = null;
75
94
  }
76
- if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
95
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequenceRef.current, (event, context) => callbackRef.current(event, context), {
77
96
  ...optionsRef.current,
78
97
  target: resolvedTarget
79
98
  });
@@ -85,11 +104,7 @@ function useHotkeySequence(sequence, callback, options = {}) {
85
104
  registrationRef.current = null;
86
105
  }
87
106
  };
88
- }, [
89
- hotkeySequenceString,
90
- mergedOptions.enabled,
91
- sequence
92
- ]);
107
+ }, [hotkeySequenceString]);
93
108
  if (registrationRef.current?.isActive) {
94
109
  registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
95
110
  registrationRef.current.setOptions(optionsWithoutTarget);
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * useHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequence.length === 0) {\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeySequenceString, mergedOptions.enabled, sequence])\n\n // Sync callback and options on EVERY render (outside useEffect)\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,UAAU,oBAAoB;CAGpC,MAAM,kBAAkB,OAA0C,KAAK;CAGvE,MAAM,cAAc,OAAO,SAAS;CACpC,MAAM,aAAa,OAAO,cAAc;CACxC,MAAM,aAAa,OAAO,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,gBAAgB,OAA+C,KAAK;CAC1E,MAAM,kBAAkB,OAAsB,KAAK;CAGnD,MAAM,uBAAuB,qBAAqB,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,iBAAgB;AACd,MAAI,SAAS,WAAW,EACtB;EAIF,MAAM,iBAAiB,MAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,eACH;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,kBACJ,gBAAgB,YAAY,QAC5B,gBAAgB,YAAY;AAG9B,MACE,gBAAgB,SAAS,aACxB,iBAAiB,kBAClB;AACA,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAI5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;AAG1B,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B;EAAC;EAAsB,cAAc;EAAS;EAAS,CAAC;AAG3D,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB"}
1
+ {"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * Each step may include modifiers. You can chain the same modifier across\n * steps (e.g. `Shift+R` then `Shift+T`). Modifier-only keydown events (Shift,\n * Control, Alt, or Meta pressed alone) are ignored while matching—they do not\n * advance the sequence or reset progress.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)\n * and only suppresses firing; the hook updates the existing handle instead of unregistering.\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * // Same modifier on consecutive steps (bare Shift between chords is ignored)\n * useHotkeySequence(['Shift+R', 'Shift+T'], () => {\n * nextAction()\n * })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n const sequenceRef = useRef(sequence)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n sequenceRef.current = sequence\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequenceRef.current.length === 0) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n prevTargetRef.current = null\n prevSequenceRef.current = null\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequenceRef.current,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeySequenceString])\n\n // Sync callback and options on EVERY render (outside useEffect)\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,YAAY,UACX,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;AAG1B,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B,CAAC,qBAAqB,CAAC;AAG1B,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB"}
@@ -0,0 +1,138 @@
1
+ const require_HotkeysProvider = require('./HotkeysProvider.cjs');
2
+ const require_utils = require('./utils.cjs');
3
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
+ let preact_hooks = require("preact/hooks");
5
+
6
+ //#region src/useHotkeySequences.ts
7
+ /**
8
+ * Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
9
+ *
10
+ * Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
11
+ * register variable-length lists without violating the rules of hooks.
12
+ *
13
+ * Options are merged in this order:
14
+ * HotkeysProvider defaults < commonOptions < per-definition options
15
+ *
16
+ * Callbacks and options are synced on every render to avoid stale closures.
17
+ *
18
+ * Definitions with an empty `sequence` are skipped (no registration).
19
+ *
20
+ * @param definitions - Array of sequence definitions to register
21
+ * @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
22
+ * Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
23
+ * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
24
+ * via `setOptions` (no unregister/re-register churn).
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * function VimPalette() {
29
+ * useHotkeySequences([
30
+ * { sequence: ['G', 'G'], callback: () => scrollToTop() },
31
+ * { sequence: ['D', 'D'], callback: () => deleteLine() },
32
+ * { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
33
+ * ])
34
+ * }
35
+ * ```
36
+ *
37
+ * @example
38
+ * ```tsx
39
+ * function DynamicSequences({ items }) {
40
+ * useHotkeySequences(
41
+ * items.map((item) => ({
42
+ * sequence: item.chords,
43
+ * callback: item.action,
44
+ * options: { enabled: item.enabled },
45
+ * })),
46
+ * { preventDefault: true },
47
+ * )
48
+ * }
49
+ * ```
50
+ */
51
+ function useHotkeySequences(definitions, commonOptions = {}) {
52
+ const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequence;
53
+ const manager = (0, _tanstack_hotkeys.getSequenceManager)();
54
+ const registrationsRef = (0, preact_hooks.useRef)(/* @__PURE__ */ new Map());
55
+ const definitionsRef = (0, preact_hooks.useRef)(definitions);
56
+ const sequenceStringsRef = (0, preact_hooks.useRef)([]);
57
+ const commonOptionsRef = (0, preact_hooks.useRef)(commonOptions);
58
+ const defaultOptionsRef = (0, preact_hooks.useRef)(defaultOptions);
59
+ const managerRef = (0, preact_hooks.useRef)(manager);
60
+ const sequenceStrings = definitions.map((def) => (0, _tanstack_hotkeys.formatHotkeySequence)(def.sequence));
61
+ definitionsRef.current = definitions;
62
+ sequenceStringsRef.current = sequenceStrings;
63
+ commonOptionsRef.current = commonOptions;
64
+ defaultOptionsRef.current = defaultOptions;
65
+ managerRef.current = manager;
66
+ (0, preact_hooks.useEffect)(() => {
67
+ const prevRegistrations = registrationsRef.current;
68
+ const nextRegistrations = /* @__PURE__ */ new Map();
69
+ const rows = [];
70
+ for (let i = 0; i < definitionsRef.current.length; i++) {
71
+ const def = definitionsRef.current[i];
72
+ const seqStr = sequenceStringsRef.current[i];
73
+ const seq = def.sequence;
74
+ if (seq.length === 0) continue;
75
+ const mergedOptions = {
76
+ ...defaultOptionsRef.current,
77
+ ...commonOptionsRef.current,
78
+ ...def.options
79
+ };
80
+ const resolvedTarget = require_utils.isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
81
+ if (!resolvedTarget) continue;
82
+ const registrationKey = `${i}:${seqStr}`;
83
+ rows.push({
84
+ registrationKey,
85
+ def,
86
+ seq,
87
+ seqStr,
88
+ mergedOptions,
89
+ resolvedTarget
90
+ });
91
+ }
92
+ const nextKeys = new Set(rows.map((r) => r.registrationKey));
93
+ for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
94
+ for (const row of rows) {
95
+ const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row;
96
+ const existing = prevRegistrations.get(registrationKey);
97
+ if (existing?.handle.isActive && existing.target === resolvedTarget) {
98
+ nextRegistrations.set(registrationKey, existing);
99
+ continue;
100
+ }
101
+ if (existing?.handle.isActive) existing.handle.unregister();
102
+ const handle = managerRef.current.register(seq, def.callback, {
103
+ ...mergedOptions,
104
+ target: resolvedTarget
105
+ });
106
+ nextRegistrations.set(registrationKey, {
107
+ handle,
108
+ target: resolvedTarget
109
+ });
110
+ }
111
+ registrationsRef.current = nextRegistrations;
112
+ });
113
+ (0, preact_hooks.useEffect)(() => {
114
+ return () => {
115
+ for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
116
+ registrationsRef.current = /* @__PURE__ */ new Map();
117
+ };
118
+ }, []);
119
+ for (let i = 0; i < definitions.length; i++) {
120
+ const def = definitions[i];
121
+ const seqStr = sequenceStrings[i];
122
+ const registrationKey = `${i}:${seqStr}`;
123
+ const handle = registrationsRef.current.get(registrationKey)?.handle;
124
+ if (handle?.isActive && def.sequence.length > 0) {
125
+ handle.callback = def.callback;
126
+ const { target: _target, ...optionsWithoutTarget } = {
127
+ ...defaultOptions,
128
+ ...commonOptions,
129
+ ...def.options
130
+ };
131
+ handle.setOptions(optionsWithoutTarget);
132
+ }
133
+ }
134
+ }
135
+
136
+ //#endregion
137
+ exports.useHotkeySequences = useHotkeySequences;
138
+ //# sourceMappingURL=useHotkeySequences.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeySequences.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeySequences.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeySequenceOptions } from './useHotkeySequence'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\n/**\n * A single sequence definition for use with `useHotkeySequences`.\n */\nexport interface UseHotkeySequenceDefinition {\n /** Array of hotkey strings that form the sequence */\n sequence: HotkeySequence\n /** The function to call when the sequence is completed */\n callback: HotkeyCallback\n /** Per-sequence options (merged on top of commonOptions) */\n options?: UseHotkeySequenceOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).\n *\n * Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can\n * register 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 * Definitions with an empty `sequence` are skipped (no registration).\n *\n * @param definitions - Array of sequence definitions to register\n * @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).\n * Per-row `enabled: false` still registers that sequence: `SequenceManager` 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 VimPalette() {\n * useHotkeySequences([\n * { sequence: ['G', 'G'], callback: () => scrollToTop() },\n * { sequence: ['D', 'D'], callback: () => deleteLine() },\n * { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function DynamicSequences({ items }) {\n * useHotkeySequences(\n * items.map((item) => ({\n * sequence: item.chords,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeySequences(\n definitions: Array<UseHotkeySequenceDefinition>,\n commonOptions: UseHotkeySequenceOptions = {},\n): void {\n type RegistrationRecord = {\n handle: SequenceRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkeySequence\n const manager = getSequenceManager()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const definitionsRef = useRef(definitions)\n const sequenceStringsRef = useRef<Array<string>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const sequenceStrings = definitions.map((def) =>\n formatHotkeySequence(def.sequence),\n )\n\n definitionsRef.current = definitions\n sequenceStringsRef.current = sequenceStrings\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 definitionsRef.current)[number]\n seq: HotkeySequence\n seqStr: string\n mergedOptions: UseHotkeySequenceOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < definitionsRef.current.length; i++) {\n const def = definitionsRef.current[i]!\n const seqStr = sequenceStringsRef.current[i]!\n const seq = def.sequence\n if (seq.length === 0) {\n continue\n }\n\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeySequenceOptions\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}:${seqStr}`\n rows.push({\n registrationKey,\n def,\n seq,\n seqStr,\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, seq, mergedOptions, resolvedTarget } = 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(seq, 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 < definitions.length; i++) {\n const def = definitions[i]!\n const seqStr = sequenceStrings[i]!\n const registrationKey = `${i}:${seqStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive && def.sequence.length > 0) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeySequenceOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,SAAgB,mBACd,aACA,gBAA0C,EAAE,EACtC;CAMN,MAAM,iBAAiBA,kDAA0B,CAAC;CAClD,MAAM,qDAA8B;CAEpC,MAAM,4DAA2D,IAAI,KAAK,CAAC;CAC3E,MAAM,0CAAwB,YAAY;CAC1C,MAAM,8CAA2C,EAAE,CAAC;CACpD,MAAM,4CAA0B,cAAc;CAC9C,MAAM,6CAA2B,eAAe;CAChD,MAAM,sCA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