@tanstack/react-hotkeys 0.4.2 → 0.5.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
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
3
  const require_useHotkey = require('./useHotkey.cjs');
4
+ const require_useHotkeys = require('./useHotkeys.cjs');
4
5
  const require_useHeldKeys = require('./useHeldKeys.cjs');
5
6
  const require_useHeldKeyCodes = require('./useHeldKeyCodes.cjs');
6
7
  const require_useKeyHold = require('./useKeyHold.cjs');
@@ -14,6 +15,7 @@ exports.useHeldKeys = require_useHeldKeys.useHeldKeys;
14
15
  exports.useHotkey = require_useHotkey.useHotkey;
15
16
  exports.useHotkeyRecorder = require_useHotkeyRecorder.useHotkeyRecorder;
16
17
  exports.useHotkeySequence = require_useHotkeySequence.useHotkeySequence;
18
+ exports.useHotkeys = require_useHotkeys.useHotkeys;
17
19
  exports.useHotkeysContext = require_HotkeysProvider.useHotkeysContext;
18
20
  exports.useKeyHold = require_useKeyHold.useKeyHold;
19
21
  var _tanstack_hotkeys = require("@tanstack/hotkeys");
package/dist/index.d.cts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { UseHotkeyOptions, useHotkey } from "./useHotkey.cjs";
2
2
  import { UseHotkeySequenceOptions, useHotkeySequence } from "./useHotkeySequence.cjs";
3
3
  import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.cjs";
4
+ import { UseHotkeyDefinition, useHotkeys } from "./useHotkeys.cjs";
4
5
  import { useHeldKeys } from "./useHeldKeys.cjs";
5
6
  import { useHeldKeyCodes } from "./useHeldKeyCodes.cjs";
6
7
  import { useKeyHold } from "./useKeyHold.cjs";
7
8
  import { ReactHotkeyRecorder, useHotkeyRecorder } from "./useHotkeyRecorder.cjs";
8
9
  export * from "@tanstack/hotkeys";
9
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, ReactHotkeyRecorder, UseHotkeyOptions, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeysContext, useKeyHold };
10
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, ReactHotkeyRecorder, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeys, useHotkeysContext, useKeyHold };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { UseHotkeyOptions, useHotkey } from "./useHotkey.js";
2
2
  import { UseHotkeySequenceOptions, useHotkeySequence } from "./useHotkeySequence.js";
3
3
  import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.js";
4
+ import { UseHotkeyDefinition, useHotkeys } from "./useHotkeys.js";
4
5
  import { useHeldKeys } from "./useHeldKeys.js";
5
6
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
6
7
  import { useKeyHold } from "./useKeyHold.js";
7
8
  import { ReactHotkeyRecorder, useHotkeyRecorder } from "./useHotkeyRecorder.js";
8
9
  export * from "@tanstack/hotkeys";
9
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, ReactHotkeyRecorder, UseHotkeyOptions, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeysContext, useKeyHold };
10
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, ReactHotkeyRecorder, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeySequenceOptions, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeys, useHotkeysContext, useKeyHold };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { HotkeysProvider, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { useHotkey } from "./useHotkey.js";
3
+ import { useHotkeys } from "./useHotkeys.js";
3
4
  import { useHeldKeys } from "./useHeldKeys.js";
4
5
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
5
6
  import { useKeyHold } from "./useKeyHold.js";
@@ -8,4 +9,4 @@ import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
8
9
 
9
10
  export * from "@tanstack/hotkeys"
10
11
 
11
- export { HotkeysProvider, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeysContext, useKeyHold };
12
+ export { HotkeysProvider, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyRecorder, useHotkeySequence, useHotkeys, useHotkeysContext, useKeyHold };
@@ -1,5 +1,6 @@
1
1
  const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
2
  const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
+ const require_utils = require('./utils.cjs');
3
4
  let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
5
  let react = require("react");
5
6
 
@@ -81,7 +82,7 @@ function useHotkey(hotkey, callback, options = {}) {
81
82
  const hotkeyString = typeof hotkey === "string" ? hotkey : (0, _tanstack_hotkeys.formatHotkey)((0, _tanstack_hotkeys.rawHotkeyToParsedHotkey)(hotkey, platform));
82
83
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
83
84
  (0, react.useEffect)(() => {
84
- const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
85
+ const resolvedTarget = require_utils.isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
85
86
  if (!resolvedTarget) return;
86
87
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
87
88
  const hotkeyChanged = prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString;
@@ -107,12 +108,6 @@ function useHotkey(hotkey, callback, options = {}) {
107
108
  registrationRef.current.setOptions(optionsWithoutTarget);
108
109
  }
109
110
  }
110
- /**
111
- * Type guard to check if a value is a React ref-like object.
112
- */
113
- function isRef(value) {
114
- return value !== null && typeof value === "object" && "current" in value;
115
- }
116
111
 
117
112
  //#endregion
118
113
  exports.useHotkey = useHotkey;
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkey.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/useHotkey.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport 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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 React 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\n/**\n * Type guard to check if a value is a React ref-like object.\n */\nfunction isRef(value: unknown): value is React.RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwFA,SAAgB,UACd,QACA,UACA,UAA4B,EAAE,EACxB;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,mDAA4B;CAGlC,MAAM,oCAA0D,KAAK;CAGrE,MAAM,gCAAqB,SAAS;CACpC,MAAM,+BAAoB,cAAc;CACxC,MAAM,+BAAoB,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,kCAA+D,KAAK;CAC1E,MAAM,kCAAsC,KAAK;CAGjD,MAAM,WAAW,cAAc,mDAA4B;CAC3D,MAAM,eACJ,OAAO,WAAW,WACd,4FACsC,QAAQ,SAAS,CAAC;CAG9D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,4BAAgB;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;;;;;;AAO5D,SAAS,MAAM,OAA8D;AAC3E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
1
+ {"version":3,"file":"useHotkey.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkey.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type {\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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 React 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFA,SAAgB,UACd,QACA,UACA,UAA4B,EAAE,EACxB;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,mDAA4B;CAGlC,MAAM,oCAA0D,KAAK;CAGrE,MAAM,gCAAqB,SAAS;CACpC,MAAM,+BAAoB,cAAc;CACxC,MAAM,+BAAoB,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,kCAA+D,KAAK;CAC1E,MAAM,kCAAsC,KAAK;CAGjD,MAAM,WAAW,cAAc,mDAA4B;CAC3D,MAAM,eACJ,OAAO,WAAW,WACd,4FACsC,QAAQ,SAAS,CAAC;CAG9D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,4BAAgB;EAEd,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,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"}
package/dist/useHotkey.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { isRef } from "./utils.js";
2
3
  import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey } from "@tanstack/hotkeys";
3
4
  import { useEffect, useRef } from "react";
4
5
 
@@ -106,12 +107,6 @@ function useHotkey(hotkey, callback, options = {}) {
106
107
  registrationRef.current.setOptions(optionsWithoutTarget);
107
108
  }
108
109
  }
109
- /**
110
- * Type guard to check if a value is a React ref-like object.
111
- */
112
- function isRef(value) {
113
- return value !== null && typeof value === "object" && "current" in value;
114
- }
115
110
 
116
111
  //#endregion
117
112
  export { useHotkey };
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkey.js","names":[],"sources":["../src/useHotkey.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport 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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 React 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\n/**\n * Type guard to check if a value is a React ref-like object.\n */\nfunction isRef(value: unknown): value is React.RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwFA,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;;;;;;AAO5D,SAAS,MAAM,OAA8D;AAC3E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
1
+ {"version":3,"file":"useHotkey.js","names":[],"sources":["../src/useHotkey.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type {\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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 React 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFA,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,5 +1,6 @@
1
1
  const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
2
  const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
+ const require_utils = require('./utils.cjs');
3
4
  let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
5
  let react = require("react");
5
6
 
@@ -46,16 +47,18 @@ function useHotkeySequence(sequence, callback, options = {}) {
46
47
  const callbackRef = (0, react.useRef)(callback);
47
48
  const optionsRef = (0, react.useRef)(mergedOptions);
48
49
  const managerRef = (0, react.useRef)(manager);
50
+ const sequenceRef = (0, react.useRef)(sequence);
49
51
  callbackRef.current = callback;
50
52
  optionsRef.current = mergedOptions;
51
53
  managerRef.current = manager;
54
+ sequenceRef.current = sequence;
52
55
  const prevTargetRef = (0, react.useRef)(null);
53
56
  const prevSequenceRef = (0, react.useRef)(null);
54
57
  const hotkeySequenceString = (0, _tanstack_hotkeys.formatHotkeySequence)(sequence);
55
58
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
56
59
  (0, react.useEffect)(() => {
57
- if (sequence.length === 0) return;
58
- const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
60
+ if (sequenceRef.current.length === 0) return;
61
+ const resolvedTarget = require_utils.isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
59
62
  if (!resolvedTarget) return;
60
63
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
61
64
  const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
@@ -63,7 +66,7 @@ function useHotkeySequence(sequence, callback, options = {}) {
63
66
  registrationRef.current.unregister();
64
67
  registrationRef.current = null;
65
68
  }
66
- if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
69
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequenceRef.current, (event, context) => callbackRef.current(event, context), {
67
70
  ...optionsRef.current,
68
71
  target: resolvedTarget
69
72
  });
@@ -75,22 +78,12 @@ function useHotkeySequence(sequence, callback, options = {}) {
75
78
  registrationRef.current = null;
76
79
  }
77
80
  };
78
- }, [
79
- hotkeySequenceString,
80
- mergedOptions.enabled,
81
- sequence
82
- ]);
81
+ }, [hotkeySequenceString, mergedOptions.enabled]);
83
82
  if (registrationRef.current?.isActive) {
84
83
  registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
85
84
  registrationRef.current.setOptions(optionsWithoutTarget);
86
85
  }
87
86
  }
88
- /**
89
- * Type guard to check if a value is a React ref-like object.
90
- */
91
- function isRef(value) {
92
- return value !== null && typeof value === "object" && "current" in value;
93
- }
94
87
 
95
88
  //#endregion
96
89
  exports.useHotkeySequence = useHotkeySequence;
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 * @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 * 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\n/**\n * Type guard to check if a value is a React ref-like object.\n */\nfunction isRef(value: unknown): value is React.RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,qDAA8B;CAGpC,MAAM,oCAA4D,KAAK;CAGvE,MAAM,gCAAqB,SAAS;CACpC,MAAM,+BAAoB,cAAc;CACxC,MAAM,+BAAoB,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,kCAA+D,KAAK;CAC1E,MAAM,oCAAwC,KAAK;CAGnD,MAAM,mEAA4C,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,4BAAgB;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;;;;;;AAO5D,SAAS,MAAM,OAA8D;AAC3E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
1
+ {"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 * @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 * 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 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 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, mergedOptions.enabled])\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,qDAA8B;CAGpC,MAAM,oCAA4D,KAAK;CAGvE,MAAM,gCAAqB,SAAS;CACpC,MAAM,+BAAoB,cAAc;CACxC,MAAM,+BAAoB,QAAQ;CAClC,MAAM,gCAAqB,SAAS;AAGpC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;AACrB,aAAY,UAAU;CAGtB,MAAM,kCAA+D,KAAK;CAC1E,MAAM,oCAAwC,KAAK;CAGnD,MAAM,mEAA4C,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,4BAAgB;AACd,MAAI,YAAY,QAAQ,WAAW,EACjC;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,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,sBAAsB,cAAc,QAAQ,CAAC;AAGjD,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB"}
@@ -1,4 +1,5 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { isRef } from "./utils.js";
2
3
  import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
3
4
  import { useEffect, useRef } from "react";
4
5
 
@@ -45,15 +46,17 @@ function useHotkeySequence(sequence, callback, options = {}) {
45
46
  const callbackRef = useRef(callback);
46
47
  const optionsRef = useRef(mergedOptions);
47
48
  const managerRef = useRef(manager);
49
+ const sequenceRef = useRef(sequence);
48
50
  callbackRef.current = callback;
49
51
  optionsRef.current = mergedOptions;
50
52
  managerRef.current = manager;
53
+ sequenceRef.current = sequence;
51
54
  const prevTargetRef = useRef(null);
52
55
  const prevSequenceRef = useRef(null);
53
56
  const hotkeySequenceString = formatHotkeySequence(sequence);
54
57
  const { target: _target, ...optionsWithoutTarget } = mergedOptions;
55
58
  useEffect(() => {
56
- if (sequence.length === 0) return;
59
+ if (sequenceRef.current.length === 0) return;
57
60
  const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
58
61
  if (!resolvedTarget) return;
59
62
  const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
@@ -62,7 +65,7 @@ function useHotkeySequence(sequence, callback, options = {}) {
62
65
  registrationRef.current.unregister();
63
66
  registrationRef.current = null;
64
67
  }
65
- if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
68
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequenceRef.current, (event, context) => callbackRef.current(event, context), {
66
69
  ...optionsRef.current,
67
70
  target: resolvedTarget
68
71
  });
@@ -74,22 +77,12 @@ function useHotkeySequence(sequence, callback, options = {}) {
74
77
  registrationRef.current = null;
75
78
  }
76
79
  };
77
- }, [
78
- hotkeySequenceString,
79
- mergedOptions.enabled,
80
- sequence
81
- ]);
80
+ }, [hotkeySequenceString, mergedOptions.enabled]);
82
81
  if (registrationRef.current?.isActive) {
83
82
  registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
84
83
  registrationRef.current.setOptions(optionsWithoutTarget);
85
84
  }
86
85
  }
87
- /**
88
- * Type guard to check if a value is a React ref-like object.
89
- */
90
- function isRef(value) {
91
- return value !== null && typeof value === "object" && "current" in value;
92
- }
93
86
 
94
87
  //#endregion
95
88
  export { useHotkeySequence };
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 * @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 * 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\n/**\n * Type guard to check if a value is a React ref-like object.\n */\nfunction isRef(value: unknown): value is React.RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,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;;;;;;AAO5D,SAAS,MAAM,OAA8D;AAC3E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
1
+ {"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\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 React ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | React.RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * React 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 * @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 * 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 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 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, mergedOptions.enabled])\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,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;CAClC,MAAM,cAAc,OAAO,SAAS;AAGpC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;AACrB,aAAY,UAAU;CAGtB,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,YAAY,QAAQ,WAAW,EACjC;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,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,sBAAsB,cAAc,QAAQ,CAAC;AAGjD,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_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
+ const require_utils = require('./utils.cjs');
4
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
5
+ let react = require("react");
6
+
7
+ //#region src/useHotkeys.ts
8
+ /**
9
+ * React hook for registering multiple keyboard hotkeys at once.
10
+ *
11
+ * Uses the singleton HotkeyManager for efficient event handling.
12
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
13
+ * with variable-length lists without violating the rules of hooks.
14
+ *
15
+ * Options are merged in this order:
16
+ * HotkeysProvider defaults < commonOptions < per-definition options
17
+ *
18
+ * Callbacks and options are synced on every render to avoid stale closures.
19
+ *
20
+ * @param hotkeys - Array of hotkey definitions to register
21
+ * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * function Editor() {
26
+ * useHotkeys([
27
+ * { hotkey: 'Mod+S', callback: () => save() },
28
+ * { hotkey: 'Mod+Z', callback: () => undo() },
29
+ * { hotkey: 'Escape', callback: () => close() },
30
+ * ])
31
+ * }
32
+ * ```
33
+ *
34
+ * @example
35
+ * ```tsx
36
+ * function MenuShortcuts({ items }) {
37
+ * // Dynamic hotkeys from data -- safe because it's a single hook call
38
+ * useHotkeys(
39
+ * items.map((item) => ({
40
+ * hotkey: item.shortcut,
41
+ * callback: item.action,
42
+ * options: { enabled: item.enabled },
43
+ * })),
44
+ * { preventDefault: true },
45
+ * )
46
+ * }
47
+ * ```
48
+ */
49
+ function useHotkeys(hotkeys, commonOptions = {}) {
50
+ const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions().hotkey;
51
+ const manager = (0, _tanstack_hotkeys.getHotkeyManager)();
52
+ const platform = commonOptions.platform ?? defaultOptions?.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
53
+ const registrationsRef = (0, react.useRef)(/* @__PURE__ */ new Map());
54
+ const hotkeysRef = (0, react.useRef)(hotkeys);
55
+ const hotkeyStringsRef = (0, react.useRef)([]);
56
+ const commonOptionsRef = (0, react.useRef)(commonOptions);
57
+ const defaultOptionsRef = (0, react.useRef)(defaultOptions);
58
+ const managerRef = (0, react.useRef)(manager);
59
+ const hotkeyStrings = hotkeys.map((def) => typeof def.hotkey === "string" ? def.hotkey : (0, _tanstack_hotkeys.formatHotkey)((0, _tanstack_hotkeys.rawHotkeyToParsedHotkey)(def.hotkey, platform)));
60
+ hotkeysRef.current = hotkeys;
61
+ hotkeyStringsRef.current = hotkeyStrings;
62
+ commonOptionsRef.current = commonOptions;
63
+ defaultOptionsRef.current = defaultOptions;
64
+ managerRef.current = manager;
65
+ (0, react.useEffect)(() => {
66
+ const prevRegistrations = registrationsRef.current;
67
+ const nextRegistrations = /* @__PURE__ */ new Map();
68
+ const rows = [];
69
+ for (let i = 0; i < hotkeysRef.current.length; i++) {
70
+ const def = hotkeysRef.current[i];
71
+ const hotkeyStr = hotkeyStringsRef.current[i];
72
+ const mergedOptions = {
73
+ ...defaultOptionsRef.current,
74
+ ...commonOptionsRef.current,
75
+ ...def.options
76
+ };
77
+ const resolvedTarget = require_utils.isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
78
+ if (!resolvedTarget) continue;
79
+ const registrationKey = `${i}:${hotkeyStr}`;
80
+ rows.push({
81
+ registrationKey,
82
+ def,
83
+ hotkeyStr,
84
+ mergedOptions,
85
+ resolvedTarget
86
+ });
87
+ }
88
+ const nextKeys = new Set(rows.map((r) => r.registrationKey));
89
+ for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
90
+ for (const row of rows) {
91
+ const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } = row;
92
+ const existing = prevRegistrations.get(registrationKey);
93
+ if (existing?.handle.isActive && existing.target === resolvedTarget) {
94
+ nextRegistrations.set(registrationKey, existing);
95
+ continue;
96
+ }
97
+ if (existing?.handle.isActive) existing.handle.unregister();
98
+ const handle = managerRef.current.register(hotkeyStr, def.callback, {
99
+ ...mergedOptions,
100
+ target: resolvedTarget
101
+ });
102
+ nextRegistrations.set(registrationKey, {
103
+ handle,
104
+ target: resolvedTarget
105
+ });
106
+ }
107
+ registrationsRef.current = nextRegistrations;
108
+ return () => {
109
+ for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
110
+ registrationsRef.current = /* @__PURE__ */ new Map();
111
+ };
112
+ }, [hotkeyStrings.join("\0"), hotkeys.map((def) => {
113
+ return {
114
+ ...defaultOptions,
115
+ ...commonOptions,
116
+ ...def.options
117
+ }.enabled ?? true;
118
+ }).join("\0")]);
119
+ for (let i = 0; i < hotkeys.length; i++) {
120
+ const def = hotkeys[i];
121
+ const hotkeyStr = hotkeyStrings[i];
122
+ const registrationKey = `${i}:${hotkeyStr}`;
123
+ const handle = registrationsRef.current.get(registrationKey)?.handle;
124
+ if (handle?.isActive) {
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.useHotkeys = useHotkeys;
138
+ //# sourceMappingURL=useHotkeys.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeys.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * React hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n // Stable serialized keys for effect dependencies\n const hotkeyKey = hotkeyStrings.join('\\0')\n const enabledKey = hotkeys\n .map((def) => {\n const merged = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n }\n return merged.enabled ?? true\n })\n .join('\\0')\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [hotkeyKey, enabledKey])\n\n // Sync callbacks and options on EVERY render (outside useEffect)\n for (let i = 0; i < hotkeys.length; i++) {\n const def = hotkeys[i]!\n const hotkeyStr = hotkeyStrings[i]!\n const registrationKey = `${i}:${hotkeyStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeyOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsEA,SAAgB,WACd,SACA,gBAAkC,EAAE,EAC9B;CAMN,MAAM,iBAAiBA,kDAA0B,CAAC;CAClD,MAAM,mDAA4B;CAClC,MAAM,WACJ,cAAc,YAAY,gBAAgB,mDAA4B;CAExE,MAAM,qDAA2D,IAAI,KAAK,CAAC;CAC3E,MAAM,+BAAoB,QAAQ;CAClC,MAAM,qCAAyC,EAAE,CAAC;CAClD,MAAM,qCAA0B,cAAc;CAC9C,MAAM,sCAA2B,eAAe;CAChD,MAAM,+BAAoB,QAAQ;CAElC,MAAM,gBAAgB,QAAQ,KAAK,QACjC,OAAO,IAAI,WAAW,WAClB,IAAI,4FACkC,IAAI,QAAQ,SAAS,CAAC,CACjE;AAED,YAAW,UAAU;AACrB,kBAAiB,UAAU;AAC3B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAerB,4BAAgB;EACd,MAAM,oBAAoB,iBAAiB;EAC3C,MAAM,oCAAoB,IAAI,KAAiC;EAE/D,MAAM,OAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,QAAQ,QAAQ,KAAK;GAClD,MAAM,MAAM,WAAW,QAAQ;GAC/B,MAAM,YAAY,iBAAiB,QAAQ;GAC3C,MAAM,gBAAgB;IACpB,GAAG,kBAAkB;IACrB,GAAG,iBAAiB;IACpB,GAAG,IAAI;IACR;GAED,MAAM,iBAAiBC,oBAAM,cAAc,OAAO,GAC9C,cAAc,OAAO,UACpB,cAAc,WACd,OAAO,aAAa,cAAc,WAAW;AAElD,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,QAAK,KAAK;IACR;IACA;IACA;IACA;IACA;IACD,CAAC;;EAGJ,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,gBAAgB,CAAC;AAE5D,OAAK,MAAM,CAAC,KAAK,WAAW,kBAC1B,KAAI,CAAC,SAAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,WAAW,eAAe,mBACtD;GAEF,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AACnE,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,WAAW,IAAI,UAAU;IAClE,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;AAE3B,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,CA9Fe,cAAc,KAAK,KAAK,EACvB,QAChB,KAAK,QAAQ;AAMZ,SALe;GACb,GAAG;GACH,GAAG;GACH,GAAG,IAAI;GACR,CACa,WAAW;GACzB,CACD,KAAK,KAAK,CAoFa,CAAC;AAG3B,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"}
@@ -0,0 +1,60 @@
1
+ import { UseHotkeyOptions } from "./useHotkey.cjs";
2
+ import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
3
+
4
+ //#region src/useHotkeys.d.ts
5
+ /**
6
+ * A single hotkey definition for use with `useHotkeys`.
7
+ */
8
+ interface UseHotkeyDefinition {
9
+ /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
10
+ hotkey: RegisterableHotkey;
11
+ /** The function to call when the hotkey is pressed */
12
+ callback: HotkeyCallback;
13
+ /** Per-hotkey options (merged on top of commonOptions) */
14
+ options?: UseHotkeyOptions;
15
+ }
16
+ /**
17
+ * React hook for registering multiple keyboard hotkeys at once.
18
+ *
19
+ * Uses the singleton HotkeyManager for efficient event handling.
20
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
21
+ * with variable-length lists without violating the rules of hooks.
22
+ *
23
+ * Options are merged in this order:
24
+ * HotkeysProvider defaults < commonOptions < per-definition options
25
+ *
26
+ * Callbacks and options are synced on every render to avoid stale closures.
27
+ *
28
+ * @param hotkeys - Array of hotkey definitions to register
29
+ * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
30
+ *
31
+ * @example
32
+ * ```tsx
33
+ * function Editor() {
34
+ * useHotkeys([
35
+ * { hotkey: 'Mod+S', callback: () => save() },
36
+ * { hotkey: 'Mod+Z', callback: () => undo() },
37
+ * { hotkey: 'Escape', callback: () => close() },
38
+ * ])
39
+ * }
40
+ * ```
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * function MenuShortcuts({ items }) {
45
+ * // Dynamic hotkeys from data -- safe because it's a single hook call
46
+ * useHotkeys(
47
+ * items.map((item) => ({
48
+ * hotkey: item.shortcut,
49
+ * callback: item.action,
50
+ * options: { enabled: item.enabled },
51
+ * })),
52
+ * { preventDefault: true },
53
+ * )
54
+ * }
55
+ * ```
56
+ */
57
+ declare function useHotkeys(hotkeys: Array<UseHotkeyDefinition>, commonOptions?: UseHotkeyOptions): void;
58
+ //#endregion
59
+ export { UseHotkeyDefinition, useHotkeys };
60
+ //# sourceMappingURL=useHotkeys.d.cts.map
@@ -0,0 +1,60 @@
1
+ import { UseHotkeyOptions } from "./useHotkey.js";
2
+ import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
3
+
4
+ //#region src/useHotkeys.d.ts
5
+ /**
6
+ * A single hotkey definition for use with `useHotkeys`.
7
+ */
8
+ interface UseHotkeyDefinition {
9
+ /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
10
+ hotkey: RegisterableHotkey;
11
+ /** The function to call when the hotkey is pressed */
12
+ callback: HotkeyCallback;
13
+ /** Per-hotkey options (merged on top of commonOptions) */
14
+ options?: UseHotkeyOptions;
15
+ }
16
+ /**
17
+ * React hook for registering multiple keyboard hotkeys at once.
18
+ *
19
+ * Uses the singleton HotkeyManager for efficient event handling.
20
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
21
+ * with variable-length lists without violating the rules of hooks.
22
+ *
23
+ * Options are merged in this order:
24
+ * HotkeysProvider defaults < commonOptions < per-definition options
25
+ *
26
+ * Callbacks and options are synced on every render to avoid stale closures.
27
+ *
28
+ * @param hotkeys - Array of hotkey definitions to register
29
+ * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
30
+ *
31
+ * @example
32
+ * ```tsx
33
+ * function Editor() {
34
+ * useHotkeys([
35
+ * { hotkey: 'Mod+S', callback: () => save() },
36
+ * { hotkey: 'Mod+Z', callback: () => undo() },
37
+ * { hotkey: 'Escape', callback: () => close() },
38
+ * ])
39
+ * }
40
+ * ```
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * function MenuShortcuts({ items }) {
45
+ * // Dynamic hotkeys from data -- safe because it's a single hook call
46
+ * useHotkeys(
47
+ * items.map((item) => ({
48
+ * hotkey: item.shortcut,
49
+ * callback: item.action,
50
+ * options: { enabled: item.enabled },
51
+ * })),
52
+ * { preventDefault: true },
53
+ * )
54
+ * }
55
+ * ```
56
+ */
57
+ declare function useHotkeys(hotkeys: Array<UseHotkeyDefinition>, commonOptions?: UseHotkeyOptions): void;
58
+ //#endregion
59
+ export { UseHotkeyDefinition, useHotkeys };
60
+ //# sourceMappingURL=useHotkeys.d.ts.map
@@ -0,0 +1,137 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { isRef } from "./utils.js";
3
+ import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey } from "@tanstack/hotkeys";
4
+ import { useEffect, useRef } from "react";
5
+
6
+ //#region src/useHotkeys.ts
7
+ /**
8
+ * React hook for registering multiple keyboard hotkeys at once.
9
+ *
10
+ * Uses the singleton HotkeyManager for efficient event handling.
11
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
12
+ * with variable-length lists without violating the rules of hooks.
13
+ *
14
+ * Options are merged in this order:
15
+ * HotkeysProvider defaults < commonOptions < per-definition options
16
+ *
17
+ * Callbacks and options are synced on every render to avoid stale closures.
18
+ *
19
+ * @param hotkeys - Array of hotkey definitions to register
20
+ * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * function Editor() {
25
+ * useHotkeys([
26
+ * { hotkey: 'Mod+S', callback: () => save() },
27
+ * { hotkey: 'Mod+Z', callback: () => undo() },
28
+ * { hotkey: 'Escape', callback: () => close() },
29
+ * ])
30
+ * }
31
+ * ```
32
+ *
33
+ * @example
34
+ * ```tsx
35
+ * function MenuShortcuts({ items }) {
36
+ * // Dynamic hotkeys from data -- safe because it's a single hook call
37
+ * useHotkeys(
38
+ * items.map((item) => ({
39
+ * hotkey: item.shortcut,
40
+ * callback: item.action,
41
+ * options: { enabled: item.enabled },
42
+ * })),
43
+ * { preventDefault: true },
44
+ * )
45
+ * }
46
+ * ```
47
+ */
48
+ function useHotkeys(hotkeys, commonOptions = {}) {
49
+ const defaultOptions = useDefaultHotkeysOptions().hotkey;
50
+ const manager = getHotkeyManager();
51
+ const platform = commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform();
52
+ const registrationsRef = useRef(/* @__PURE__ */ new Map());
53
+ const hotkeysRef = useRef(hotkeys);
54
+ const hotkeyStringsRef = useRef([]);
55
+ const commonOptionsRef = useRef(commonOptions);
56
+ const defaultOptionsRef = useRef(defaultOptions);
57
+ const managerRef = useRef(manager);
58
+ const hotkeyStrings = hotkeys.map((def) => typeof def.hotkey === "string" ? def.hotkey : formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)));
59
+ hotkeysRef.current = hotkeys;
60
+ hotkeyStringsRef.current = hotkeyStrings;
61
+ commonOptionsRef.current = commonOptions;
62
+ defaultOptionsRef.current = defaultOptions;
63
+ managerRef.current = manager;
64
+ useEffect(() => {
65
+ const prevRegistrations = registrationsRef.current;
66
+ const nextRegistrations = /* @__PURE__ */ new Map();
67
+ const rows = [];
68
+ for (let i = 0; i < hotkeysRef.current.length; i++) {
69
+ const def = hotkeysRef.current[i];
70
+ const hotkeyStr = hotkeyStringsRef.current[i];
71
+ const mergedOptions = {
72
+ ...defaultOptionsRef.current,
73
+ ...commonOptionsRef.current,
74
+ ...def.options
75
+ };
76
+ const resolvedTarget = isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
77
+ if (!resolvedTarget) continue;
78
+ const registrationKey = `${i}:${hotkeyStr}`;
79
+ rows.push({
80
+ registrationKey,
81
+ def,
82
+ hotkeyStr,
83
+ mergedOptions,
84
+ resolvedTarget
85
+ });
86
+ }
87
+ const nextKeys = new Set(rows.map((r) => r.registrationKey));
88
+ for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
89
+ for (const row of rows) {
90
+ const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } = row;
91
+ const existing = prevRegistrations.get(registrationKey);
92
+ if (existing?.handle.isActive && existing.target === resolvedTarget) {
93
+ nextRegistrations.set(registrationKey, existing);
94
+ continue;
95
+ }
96
+ if (existing?.handle.isActive) existing.handle.unregister();
97
+ const handle = managerRef.current.register(hotkeyStr, def.callback, {
98
+ ...mergedOptions,
99
+ target: resolvedTarget
100
+ });
101
+ nextRegistrations.set(registrationKey, {
102
+ handle,
103
+ target: resolvedTarget
104
+ });
105
+ }
106
+ registrationsRef.current = nextRegistrations;
107
+ return () => {
108
+ for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
109
+ registrationsRef.current = /* @__PURE__ */ new Map();
110
+ };
111
+ }, [hotkeyStrings.join("\0"), hotkeys.map((def) => {
112
+ return {
113
+ ...defaultOptions,
114
+ ...commonOptions,
115
+ ...def.options
116
+ }.enabled ?? true;
117
+ }).join("\0")]);
118
+ for (let i = 0; i < hotkeys.length; i++) {
119
+ const def = hotkeys[i];
120
+ const hotkeyStr = hotkeyStrings[i];
121
+ const registrationKey = `${i}:${hotkeyStr}`;
122
+ const handle = registrationsRef.current.get(registrationKey)?.handle;
123
+ if (handle?.isActive) {
124
+ handle.callback = def.callback;
125
+ const { target: _target, ...optionsWithoutTarget } = {
126
+ ...defaultOptions,
127
+ ...commonOptions,
128
+ ...def.options
129
+ };
130
+ handle.setOptions(optionsWithoutTarget);
131
+ }
132
+ }
133
+ }
134
+
135
+ //#endregion
136
+ export { useHotkeys };
137
+ //# sourceMappingURL=useHotkeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * React hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n // Stable serialized keys for effect dependencies\n const hotkeyKey = hotkeyStrings.join('\\0')\n const enabledKey = hotkeys\n .map((def) => {\n const merged = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n }\n return merged.enabled ?? true\n })\n .join('\\0')\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [hotkeyKey, enabledKey])\n\n // Sync callbacks and options on EVERY render (outside useEffect)\n for (let i = 0; i < hotkeys.length; i++) {\n const def = hotkeys[i]!\n const hotkeyStr = hotkeyStrings[i]!\n const registrationKey = `${i}:${hotkeyStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeyOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsEA,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,mBAAmB,uBAAwC,IAAI,KAAK,CAAC;CAC3E,MAAM,aAAa,OAAO,QAAQ;CAClC,MAAM,mBAAmB,OAAsB,EAAE,CAAC;CAClD,MAAM,mBAAmB,OAAO,cAAc;CAC9C,MAAM,oBAAoB,OAAO,eAAe;CAChD,MAAM,aAAa,OAAO,QAAQ;CAElC,MAAM,gBAAgB,QAAQ,KAAK,QACjC,OAAO,IAAI,WAAW,WAClB,IAAI,SACH,aAAa,wBAAwB,IAAI,QAAQ,SAAS,CAAC,CACjE;AAED,YAAW,UAAU;AACrB,kBAAiB,UAAU;AAC3B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAerB,iBAAgB;EACd,MAAM,oBAAoB,iBAAiB;EAC3C,MAAM,oCAAoB,IAAI,KAAiC;EAE/D,MAAM,OAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,QAAQ,QAAQ,KAAK;GAClD,MAAM,MAAM,WAAW,QAAQ;GAC/B,MAAM,YAAY,iBAAiB,QAAQ;GAC3C,MAAM,gBAAgB;IACpB,GAAG,kBAAkB;IACrB,GAAG,iBAAiB;IACpB,GAAG,IAAI;IACR;GAED,MAAM,iBAAiB,MAAM,cAAc,OAAO,GAC9C,cAAc,OAAO,UACpB,cAAc,WACd,OAAO,aAAa,cAAc,WAAW;AAElD,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,QAAK,KAAK;IACR;IACA;IACA;IACA;IACA;IACD,CAAC;;EAGJ,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,gBAAgB,CAAC;AAE5D,OAAK,MAAM,CAAC,KAAK,WAAW,kBAC1B,KAAI,CAAC,SAAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,WAAW,eAAe,mBACtD;GAEF,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AACnE,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,WAAW,IAAI,UAAU;IAClE,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;AAE3B,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,CA9Fe,cAAc,KAAK,KAAK,EACvB,QAChB,KAAK,QAAQ;AAMZ,SALe;GACb,GAAG;GACH,GAAG;GACH,GAAG,IAAI;GACR,CACa,WAAW;GACzB,CACD,KAAK,KAAK,CAoFa,CAAC;AAG3B,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/dist/utils.cjs ADDED
@@ -0,0 +1,12 @@
1
+
2
+ //#region src/utils.ts
3
+ /**
4
+ * Type guard to check if a value is a React ref-like object.
5
+ */
6
+ function isRef(value) {
7
+ return value !== null && typeof value === "object" && "current" in value;
8
+ }
9
+
10
+ //#endregion
11
+ exports.isRef = isRef;
12
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import type React from 'react'\n\n/**\n * Type guard to check if a value is a React ref-like object.\n */\nexport function isRef(\n value: unknown,\n): value is React.RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;AAKA,SAAgB,MACd,OAC8C;AAC9C,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
package/dist/utils.js ADDED
@@ -0,0 +1,11 @@
1
+ //#region src/utils.ts
2
+ /**
3
+ * Type guard to check if a value is a React ref-like object.
4
+ */
5
+ function isRef(value) {
6
+ return value !== null && typeof value === "object" && "current" in value;
7
+ }
8
+
9
+ //#endregion
10
+ export { isRef };
11
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import type React from 'react'\n\n/**\n * Type guard to check if a value is a React ref-like object.\n */\nexport function isRef(\n value: unknown,\n): value is React.RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;AAKA,SAAgB,MACd,OAC8C;AAC9C,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-hotkeys",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "React adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -43,10 +43,10 @@
43
43
  "@tanstack/hotkeys": "0.4.2"
44
44
  },
45
45
  "devDependencies": {
46
- "@eslint-react/eslint-plugin": "^2.13.0",
46
+ "@eslint-react/eslint-plugin": "^3.0.0",
47
47
  "@testing-library/react": "^16.3.2",
48
48
  "@types/react": "^19.2.14",
49
- "@vitejs/plugin-react": "^5.1.4",
49
+ "@vitejs/plugin-react": "^6.0.1",
50
50
  "eslint-plugin-react-compiler": "19.1.0-rc.2",
51
51
  "eslint-plugin-react-hooks": "^7.0.1",
52
52
  "react": "^19.2.4",
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export * from './HotkeysProvider'
6
6
 
7
7
  // React-specific exports
8
8
  export * from './useHotkey'
9
+ export * from './useHotkeys'
9
10
  export * from './useHeldKeys'
10
11
  export * from './useHeldKeyCodes'
11
12
  export * from './useKeyHold'
package/src/useHotkey.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  rawHotkeyToParsedHotkey,
7
7
  } from '@tanstack/hotkeys'
8
8
  import { useDefaultHotkeysOptions } from './HotkeysProvider'
9
+ import { isRef } from './utils'
9
10
  import type {
10
11
  Hotkey,
11
12
  HotkeyCallback,
@@ -182,10 +183,3 @@ export function useHotkey(
182
183
  registrationRef.current.setOptions(optionsWithoutTarget)
183
184
  }
184
185
  }
185
-
186
- /**
187
- * Type guard to check if a value is a React ref-like object.
188
- */
189
- function isRef(value: unknown): value is React.RefObject<HTMLElement | null> {
190
- return value !== null && typeof value === 'object' && 'current' in value
191
- }
@@ -1,6 +1,7 @@
1
1
  import { useEffect, useRef } from 'react'
2
2
  import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
3
3
  import { useDefaultHotkeysOptions } from './HotkeysProvider'
4
+ import { isRef } from './utils'
4
5
  import type {
5
6
  HotkeyCallback,
6
7
  HotkeyCallbackContext,
@@ -77,11 +78,13 @@ export function useHotkeySequence(
77
78
  const callbackRef = useRef(callback)
78
79
  const optionsRef = useRef(mergedOptions)
79
80
  const managerRef = useRef(manager)
81
+ const sequenceRef = useRef(sequence)
80
82
 
81
83
  // Update refs on every render
82
84
  callbackRef.current = callback
83
85
  optionsRef.current = mergedOptions
84
86
  managerRef.current = manager
87
+ sequenceRef.current = sequence
85
88
 
86
89
  // Track previous target and sequence to detect changes requiring re-registration
87
90
  const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
@@ -94,7 +97,7 @@ export function useHotkeySequence(
94
97
  const { target: _target, ...optionsWithoutTarget } = mergedOptions
95
98
 
96
99
  useEffect(() => {
97
- if (sequence.length === 0) {
100
+ if (sequenceRef.current.length === 0) {
98
101
  return
99
102
  }
100
103
 
@@ -128,7 +131,7 @@ export function useHotkeySequence(
128
131
  // Register if needed (no active registration)
129
132
  if (!registrationRef.current || !registrationRef.current.isActive) {
130
133
  registrationRef.current = managerRef.current.register(
131
- sequence,
134
+ sequenceRef.current,
132
135
  (event, context) => callbackRef.current(event, context),
133
136
  {
134
137
  ...optionsRef.current,
@@ -148,7 +151,7 @@ export function useHotkeySequence(
148
151
  registrationRef.current = null
149
152
  }
150
153
  }
151
- }, [hotkeySequenceString, mergedOptions.enabled, sequence])
154
+ }, [hotkeySequenceString, mergedOptions.enabled])
152
155
 
153
156
  // Sync callback and options on EVERY render (outside useEffect)
154
157
  if (registrationRef.current?.isActive) {
@@ -159,10 +162,3 @@ export function useHotkeySequence(
159
162
  registrationRef.current.setOptions(optionsWithoutTarget)
160
163
  }
161
164
  }
162
-
163
- /**
164
- * Type guard to check if a value is a React ref-like object.
165
- */
166
- function isRef(value: unknown): value is React.RefObject<HTMLElement | null> {
167
- return value !== null && typeof value === 'object' && 'current' in value
168
- }
@@ -0,0 +1,219 @@
1
+ import { useEffect, useRef } from 'react'
2
+ import {
3
+ detectPlatform,
4
+ formatHotkey,
5
+ getHotkeyManager,
6
+ rawHotkeyToParsedHotkey,
7
+ } from '@tanstack/hotkeys'
8
+ import { useDefaultHotkeysOptions } from './HotkeysProvider'
9
+ import { isRef } from './utils'
10
+ import type { UseHotkeyOptions } from './useHotkey'
11
+ import type {
12
+ Hotkey,
13
+ HotkeyCallback,
14
+ HotkeyRegistrationHandle,
15
+ RegisterableHotkey,
16
+ } from '@tanstack/hotkeys'
17
+
18
+ /**
19
+ * A single hotkey definition for use with `useHotkeys`.
20
+ */
21
+ export interface UseHotkeyDefinition {
22
+ /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
23
+ hotkey: RegisterableHotkey
24
+ /** The function to call when the hotkey is pressed */
25
+ callback: HotkeyCallback
26
+ /** Per-hotkey options (merged on top of commonOptions) */
27
+ options?: UseHotkeyOptions
28
+ }
29
+
30
+ /**
31
+ * React hook for registering multiple keyboard hotkeys at once.
32
+ *
33
+ * Uses the singleton HotkeyManager for efficient event handling.
34
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
35
+ * with variable-length lists without violating the rules of hooks.
36
+ *
37
+ * Options are merged in this order:
38
+ * HotkeysProvider defaults < commonOptions < per-definition options
39
+ *
40
+ * Callbacks and options are synced on every render to avoid stale closures.
41
+ *
42
+ * @param hotkeys - Array of hotkey definitions to register
43
+ * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
44
+ *
45
+ * @example
46
+ * ```tsx
47
+ * function Editor() {
48
+ * useHotkeys([
49
+ * { hotkey: 'Mod+S', callback: () => save() },
50
+ * { hotkey: 'Mod+Z', callback: () => undo() },
51
+ * { hotkey: 'Escape', callback: () => close() },
52
+ * ])
53
+ * }
54
+ * ```
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * function MenuShortcuts({ items }) {
59
+ * // Dynamic hotkeys from data -- safe because it's a single hook call
60
+ * useHotkeys(
61
+ * items.map((item) => ({
62
+ * hotkey: item.shortcut,
63
+ * callback: item.action,
64
+ * options: { enabled: item.enabled },
65
+ * })),
66
+ * { preventDefault: true },
67
+ * )
68
+ * }
69
+ * ```
70
+ */
71
+ export function useHotkeys(
72
+ hotkeys: Array<UseHotkeyDefinition>,
73
+ commonOptions: UseHotkeyOptions = {},
74
+ ): void {
75
+ type RegistrationRecord = {
76
+ handle: HotkeyRegistrationHandle
77
+ target: Document | HTMLElement | Window
78
+ }
79
+
80
+ const defaultOptions = useDefaultHotkeysOptions().hotkey
81
+ const manager = getHotkeyManager()
82
+ const platform =
83
+ commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()
84
+
85
+ const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())
86
+ const hotkeysRef = useRef(hotkeys)
87
+ const hotkeyStringsRef = useRef<Array<Hotkey>>([])
88
+ const commonOptionsRef = useRef(commonOptions)
89
+ const defaultOptionsRef = useRef(defaultOptions)
90
+ const managerRef = useRef(manager)
91
+
92
+ const hotkeyStrings = hotkeys.map((def) =>
93
+ typeof def.hotkey === 'string'
94
+ ? def.hotkey
95
+ : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),
96
+ )
97
+
98
+ hotkeysRef.current = hotkeys
99
+ hotkeyStringsRef.current = hotkeyStrings
100
+ commonOptionsRef.current = commonOptions
101
+ defaultOptionsRef.current = defaultOptions
102
+ managerRef.current = manager
103
+
104
+ // Stable serialized keys for effect dependencies
105
+ const hotkeyKey = hotkeyStrings.join('\0')
106
+ const enabledKey = hotkeys
107
+ .map((def) => {
108
+ const merged = {
109
+ ...defaultOptions,
110
+ ...commonOptions,
111
+ ...def.options,
112
+ }
113
+ return merged.enabled ?? true
114
+ })
115
+ .join('\0')
116
+
117
+ useEffect(() => {
118
+ const prevRegistrations = registrationsRef.current
119
+ const nextRegistrations = new Map<string, RegistrationRecord>()
120
+
121
+ const rows: Array<{
122
+ registrationKey: string
123
+ def: (typeof hotkeysRef.current)[number]
124
+ hotkeyStr: Hotkey
125
+ mergedOptions: UseHotkeyOptions
126
+ resolvedTarget: Document | HTMLElement | Window
127
+ }> = []
128
+
129
+ for (let i = 0; i < hotkeysRef.current.length; i++) {
130
+ const def = hotkeysRef.current[i]!
131
+ const hotkeyStr = hotkeyStringsRef.current[i]!
132
+ const mergedOptions = {
133
+ ...defaultOptionsRef.current,
134
+ ...commonOptionsRef.current,
135
+ ...def.options,
136
+ } as UseHotkeyOptions
137
+
138
+ const resolvedTarget = isRef(mergedOptions.target)
139
+ ? mergedOptions.target.current
140
+ : (mergedOptions.target ??
141
+ (typeof document !== 'undefined' ? document : null))
142
+
143
+ if (!resolvedTarget) {
144
+ continue
145
+ }
146
+
147
+ const registrationKey = `${i}:${hotkeyStr}`
148
+ rows.push({
149
+ registrationKey,
150
+ def,
151
+ hotkeyStr,
152
+ mergedOptions,
153
+ resolvedTarget,
154
+ })
155
+ }
156
+
157
+ const nextKeys = new Set(rows.map((r) => r.registrationKey))
158
+
159
+ for (const [key, record] of prevRegistrations) {
160
+ if (!nextKeys.has(key) && record.handle.isActive) {
161
+ record.handle.unregister()
162
+ }
163
+ }
164
+
165
+ for (const row of rows) {
166
+ const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =
167
+ row
168
+
169
+ const existing = prevRegistrations.get(registrationKey)
170
+ if (existing?.handle.isActive && existing.target === resolvedTarget) {
171
+ nextRegistrations.set(registrationKey, existing)
172
+ continue
173
+ }
174
+
175
+ if (existing?.handle.isActive) {
176
+ existing.handle.unregister()
177
+ }
178
+
179
+ const handle = managerRef.current.register(hotkeyStr, def.callback, {
180
+ ...mergedOptions,
181
+ target: resolvedTarget,
182
+ })
183
+ nextRegistrations.set(registrationKey, {
184
+ handle,
185
+ target: resolvedTarget,
186
+ })
187
+ }
188
+
189
+ registrationsRef.current = nextRegistrations
190
+
191
+ return () => {
192
+ for (const { handle } of registrationsRef.current.values()) {
193
+ if (handle.isActive) {
194
+ handle.unregister()
195
+ }
196
+ }
197
+ registrationsRef.current = new Map()
198
+ }
199
+ }, [hotkeyKey, enabledKey])
200
+
201
+ // Sync callbacks and options on EVERY render (outside useEffect)
202
+ for (let i = 0; i < hotkeys.length; i++) {
203
+ const def = hotkeys[i]!
204
+ const hotkeyStr = hotkeyStrings[i]!
205
+ const registrationKey = `${i}:${hotkeyStr}`
206
+ const handle = registrationsRef.current.get(registrationKey)?.handle
207
+
208
+ if (handle?.isActive) {
209
+ handle.callback = def.callback
210
+ const mergedOptions = {
211
+ ...defaultOptions,
212
+ ...commonOptions,
213
+ ...def.options,
214
+ } as UseHotkeyOptions
215
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions
216
+ handle.setOptions(optionsWithoutTarget)
217
+ }
218
+ }
219
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type React from 'react'
2
+
3
+ /**
4
+ * Type guard to check if a value is a React ref-like object.
5
+ */
6
+ export function isRef(
7
+ value: unknown,
8
+ ): value is React.RefObject<HTMLElement | null> {
9
+ return value !== null && typeof value === 'object' && 'current' in value
10
+ }