@tanstack/react-hotkeys 0.2.0 → 0.3.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.
@@ -37,27 +37,59 @@ let react = require("react");
37
37
  * ```
38
38
  */
39
39
  function useHotkeySequence(sequence, callback, options = {}) {
40
- const { enabled = true, ...sequenceOptions } = {
40
+ const mergedOptions = {
41
41
  ...require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequence,
42
42
  ...options
43
43
  };
44
- const { timeout, platform } = sequenceOptions;
44
+ const manager = (0, _tanstack_hotkeys.getSequenceManager)();
45
+ const registrationRef = (0, react.useRef)(null);
45
46
  const callbackRef = (0, react.useRef)(callback);
47
+ const optionsRef = (0, react.useRef)(mergedOptions);
48
+ const managerRef = (0, react.useRef)(manager);
46
49
  callbackRef.current = callback;
50
+ optionsRef.current = mergedOptions;
51
+ managerRef.current = manager;
52
+ const prevTargetRef = (0, react.useRef)(null);
53
+ const prevSequenceRef = (0, react.useRef)(null);
54
+ const hotkeySequenceString = (0, _tanstack_hotkeys.formatHotkeySequence)(sequence);
55
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions;
47
56
  (0, react.useEffect)(() => {
48
- if (!enabled || sequence.length === 0) return;
49
- const manager = (0, _tanstack_hotkeys.getSequenceManager)();
50
- const registerOptions = { enabled: true };
51
- if (timeout !== void 0) registerOptions.timeout = timeout;
52
- if (platform !== void 0) registerOptions.platform = platform;
53
- return manager.register(sequence, (event, context) => callbackRef.current(event, context), registerOptions);
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);
59
+ if (!resolvedTarget) return;
60
+ const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
61
+ const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
62
+ if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
63
+ registrationRef.current.unregister();
64
+ registrationRef.current = null;
65
+ }
66
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
67
+ ...optionsRef.current,
68
+ target: resolvedTarget
69
+ });
70
+ prevTargetRef.current = resolvedTarget;
71
+ prevSequenceRef.current = hotkeySequenceString;
72
+ return () => {
73
+ if (registrationRef.current?.isActive) {
74
+ registrationRef.current.unregister();
75
+ registrationRef.current = null;
76
+ }
77
+ };
54
78
  }, [
55
- enabled,
56
- sequence,
57
- sequence.join("|"),
58
- timeout,
59
- platform
79
+ hotkeySequenceString,
80
+ mergedOptions.enabled,
81
+ sequence
60
82
  ]);
83
+ if (registrationRef.current?.isActive) {
84
+ registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
85
+ registrationRef.current.setOptions(optionsWithoutTarget);
86
+ }
87
+ }
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;
61
93
  }
62
94
 
63
95
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'enabled'\n> {\n /** Whether the sequence is enabled. Defaults to true. */\n enabled?: boolean\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 { enabled = true, ...sequenceOptions } = mergedOptions\n\n // Extract options for stable dependencies\n const { timeout, platform } = sequenceOptions\n\n // Use refs to keep callback stable\n const callbackRef = useRef(callback)\n callbackRef.current = callback\n\n // Serialize sequence for dependency comparison\n const sequenceKey = sequence.join('|')\n\n useEffect(() => {\n if (!enabled || sequence.length === 0) {\n return\n }\n\n const manager = getSequenceManager()\n\n // Build options object conditionally to avoid overwriting manager defaults with undefined\n const registerOptions: SequenceOptions = { enabled: true }\n if (timeout !== undefined) registerOptions.timeout = timeout\n if (platform !== undefined) registerOptions.platform = platform\n\n const unregister = manager.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n registerOptions,\n )\n\n return unregister\n }, [enabled, sequence, sequenceKey, timeout, platform])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CAMN,MAAM,EAAE,UAAU,MAAM,GAAG,oBALL;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAKD,MAAM,EAAE,SAAS,aAAa;CAG9B,MAAM,gCAAqB,SAAS;AACpC,aAAY,UAAU;AAKtB,4BAAgB;AACd,MAAI,CAAC,WAAW,SAAS,WAAW,EAClC;EAGF,MAAM,qDAA8B;EAGpC,MAAM,kBAAmC,EAAE,SAAS,MAAM;AAC1D,MAAI,YAAY,OAAW,iBAAgB,UAAU;AACrD,MAAI,aAAa,OAAW,iBAAgB,WAAW;AAQvD,SANmB,QAAQ,SACzB,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD,gBACD;IAGA;EAAC;EAAS;EArBO,SAAS,KAAK,IAAI;EAqBF;EAAS;EAAS,CAAC"}
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,9 +1,13 @@
1
1
  import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
2
2
 
3
3
  //#region src/useHotkeySequence.d.ts
4
- interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'enabled'> {
5
- /** Whether the sequence is enabled. Defaults to true. */
6
- enabled?: boolean;
4
+ interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
5
+ /**
6
+ * The DOM element to attach the event listener to.
7
+ * Can be a React ref, direct DOM element, or null.
8
+ * Defaults to document.
9
+ */
10
+ target?: React.RefObject<HTMLElement | null> | HTMLElement | Document | Window | null;
7
11
  }
8
12
  /**
9
13
  * React hook for registering a keyboard shortcut sequence (Vim-style).
@@ -1,9 +1,13 @@
1
1
  import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
2
2
 
3
3
  //#region src/useHotkeySequence.d.ts
4
- interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'enabled'> {
5
- /** Whether the sequence is enabled. Defaults to true. */
6
- enabled?: boolean;
4
+ interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
5
+ /**
6
+ * The DOM element to attach the event listener to.
7
+ * Can be a React ref, direct DOM element, or null.
8
+ * Defaults to document.
9
+ */
10
+ target?: React.RefObject<HTMLElement | null> | HTMLElement | Document | Window | null;
7
11
  }
8
12
  /**
9
13
  * React hook for registering a keyboard shortcut sequence (Vim-style).
@@ -1,5 +1,5 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
- import { getSequenceManager } from "@tanstack/hotkeys";
2
+ import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
3
3
  import { useEffect, useRef } from "react";
4
4
 
5
5
  //#region src/useHotkeySequence.ts
@@ -36,27 +36,59 @@ import { useEffect, useRef } from "react";
36
36
  * ```
37
37
  */
38
38
  function useHotkeySequence(sequence, callback, options = {}) {
39
- const { enabled = true, ...sequenceOptions } = {
39
+ const mergedOptions = {
40
40
  ...useDefaultHotkeysOptions().hotkeySequence,
41
41
  ...options
42
42
  };
43
- const { timeout, platform } = sequenceOptions;
43
+ const manager = getSequenceManager();
44
+ const registrationRef = useRef(null);
44
45
  const callbackRef = useRef(callback);
46
+ const optionsRef = useRef(mergedOptions);
47
+ const managerRef = useRef(manager);
45
48
  callbackRef.current = callback;
49
+ optionsRef.current = mergedOptions;
50
+ managerRef.current = manager;
51
+ const prevTargetRef = useRef(null);
52
+ const prevSequenceRef = useRef(null);
53
+ const hotkeySequenceString = formatHotkeySequence(sequence);
54
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions;
46
55
  useEffect(() => {
47
- if (!enabled || sequence.length === 0) return;
48
- const manager = getSequenceManager();
49
- const registerOptions = { enabled: true };
50
- if (timeout !== void 0) registerOptions.timeout = timeout;
51
- if (platform !== void 0) registerOptions.platform = platform;
52
- return manager.register(sequence, (event, context) => callbackRef.current(event, context), registerOptions);
56
+ if (sequence.length === 0) return;
57
+ const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
58
+ if (!resolvedTarget) return;
59
+ const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
60
+ const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
61
+ if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
62
+ registrationRef.current.unregister();
63
+ registrationRef.current = null;
64
+ }
65
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
66
+ ...optionsRef.current,
67
+ target: resolvedTarget
68
+ });
69
+ prevTargetRef.current = resolvedTarget;
70
+ prevSequenceRef.current = hotkeySequenceString;
71
+ return () => {
72
+ if (registrationRef.current?.isActive) {
73
+ registrationRef.current.unregister();
74
+ registrationRef.current = null;
75
+ }
76
+ };
53
77
  }, [
54
- enabled,
55
- sequence,
56
- sequence.join("|"),
57
- timeout,
58
- platform
78
+ hotkeySequenceString,
79
+ mergedOptions.enabled,
80
+ sequence
59
81
  ]);
82
+ if (registrationRef.current?.isActive) {
83
+ registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
84
+ registrationRef.current.setOptions(optionsWithoutTarget);
85
+ }
86
+ }
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;
60
92
  }
61
93
 
62
94
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'enabled'\n> {\n /** Whether the sequence is enabled. Defaults to true. */\n enabled?: boolean\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 { enabled = true, ...sequenceOptions } = mergedOptions\n\n // Extract options for stable dependencies\n const { timeout, platform } = sequenceOptions\n\n // Use refs to keep callback stable\n const callbackRef = useRef(callback)\n callbackRef.current = callback\n\n // Serialize sequence for dependency comparison\n const sequenceKey = sequence.join('|')\n\n useEffect(() => {\n if (!enabled || sequence.length === 0) {\n return\n }\n\n const manager = getSequenceManager()\n\n // Build options object conditionally to avoid overwriting manager defaults with undefined\n const registerOptions: SequenceOptions = { enabled: true }\n if (timeout !== undefined) registerOptions.timeout = timeout\n if (platform !== undefined) registerOptions.platform = platform\n\n const unregister = manager.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n registerOptions,\n )\n\n return unregister\n }, [enabled, sequence, sequenceKey, timeout, platform])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CAMN,MAAM,EAAE,UAAU,MAAM,GAAG,oBALL;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAKD,MAAM,EAAE,SAAS,aAAa;CAG9B,MAAM,cAAc,OAAO,SAAS;AACpC,aAAY,UAAU;AAKtB,iBAAgB;AACd,MAAI,CAAC,WAAW,SAAS,WAAW,EAClC;EAGF,MAAM,UAAU,oBAAoB;EAGpC,MAAM,kBAAmC,EAAE,SAAS,MAAM;AAC1D,MAAI,YAAY,OAAW,iBAAgB,UAAU;AACrD,MAAI,aAAa,OAAW,iBAAgB,WAAW;AAQvD,SANmB,QAAQ,SACzB,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD,gBACD;IAGA;EAAC;EAAS;EArBO,SAAS,KAAK,IAAI;EAqBF;EAAS;EAAS,CAAC"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-hotkeys",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "React adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@tanstack/react-store": "^0.9.1",
43
- "@tanstack/hotkeys": "0.2.0"
43
+ "@tanstack/hotkeys": "0.3.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@eslint-react/eslint-plugin": "^2.13.0",
@@ -1,18 +1,29 @@
1
1
  import { useEffect, useRef } from 'react'
2
- import { getSequenceManager } from '@tanstack/hotkeys'
2
+ import { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'
3
3
  import { useDefaultHotkeysOptions } from './HotkeysProvider'
4
4
  import type {
5
5
  HotkeyCallback,
6
+ HotkeyCallbackContext,
6
7
  HotkeySequence,
7
8
  SequenceOptions,
9
+ SequenceRegistrationHandle,
8
10
  } from '@tanstack/hotkeys'
9
11
 
10
12
  export interface UseHotkeySequenceOptions extends Omit<
11
13
  SequenceOptions,
12
- 'enabled'
14
+ 'target'
13
15
  > {
14
- /** Whether the sequence is enabled. Defaults to true. */
15
- enabled?: boolean
16
+ /**
17
+ * The DOM element to attach the event listener to.
18
+ * Can be a React ref, direct DOM element, or null.
19
+ * Defaults to document.
20
+ */
21
+ target?:
22
+ | React.RefObject<HTMLElement | null>
23
+ | HTMLElement
24
+ | Document
25
+ | Window
26
+ | null
16
27
  }
17
28
 
18
29
  /**
@@ -57,36 +68,101 @@ export function useHotkeySequence(
57
68
  ...options,
58
69
  } as UseHotkeySequenceOptions
59
70
 
60
- const { enabled = true, ...sequenceOptions } = mergedOptions
71
+ const manager = getSequenceManager()
61
72
 
62
- // Extract options for stable dependencies
63
- const { timeout, platform } = sequenceOptions
73
+ // Stable ref for registration handle
74
+ const registrationRef = useRef<SequenceRegistrationHandle | null>(null)
64
75
 
65
- // Use refs to keep callback stable
76
+ // Refs to capture current values for use in effect without adding dependencies
66
77
  const callbackRef = useRef(callback)
78
+ const optionsRef = useRef(mergedOptions)
79
+ const managerRef = useRef(manager)
80
+
81
+ // Update refs on every render
67
82
  callbackRef.current = callback
83
+ optionsRef.current = mergedOptions
84
+ managerRef.current = manager
85
+
86
+ // Track previous target and sequence to detect changes requiring re-registration
87
+ const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
88
+ const prevSequenceRef = useRef<string | null>(null)
68
89
 
69
- // Serialize sequence for dependency comparison
70
- const sequenceKey = sequence.join('|')
90
+ // Normalize to hotkey sequence string (join with spaces)
91
+ const hotkeySequenceString = formatHotkeySequence(sequence)
92
+
93
+ // Extract options without target (target is handled separately)
94
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions
71
95
 
72
96
  useEffect(() => {
73
- if (!enabled || sequence.length === 0) {
97
+ if (sequence.length === 0) {
98
+ return
99
+ }
100
+
101
+ // Resolve target inside the effect so refs are already attached after mount
102
+ const resolvedTarget = isRef(optionsRef.current.target)
103
+ ? optionsRef.current.target.current
104
+ : (optionsRef.current.target ??
105
+ (typeof document !== 'undefined' ? document : null))
106
+
107
+ // Skip if no valid target (SSR or ref still null)
108
+ if (!resolvedTarget) {
74
109
  return
75
110
  }
76
111
 
77
- const manager = getSequenceManager()
112
+ // Check if we need to re-register (target or sequence changed)
113
+ const targetChanged =
114
+ prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget
115
+ const sequenceChanged =
116
+ prevSequenceRef.current !== null &&
117
+ prevSequenceRef.current !== hotkeySequenceString
118
+
119
+ // If we have an active registration and target/sequence changed, unregister first
120
+ if (
121
+ registrationRef.current?.isActive &&
122
+ (targetChanged || sequenceChanged)
123
+ ) {
124
+ registrationRef.current.unregister()
125
+ registrationRef.current = null
126
+ }
127
+
128
+ // Register if needed (no active registration)
129
+ if (!registrationRef.current || !registrationRef.current.isActive) {
130
+ registrationRef.current = managerRef.current.register(
131
+ sequence,
132
+ (event, context) => callbackRef.current(event, context),
133
+ {
134
+ ...optionsRef.current,
135
+ target: resolvedTarget,
136
+ },
137
+ )
138
+ }
139
+
140
+ // Update tracking refs
141
+ prevTargetRef.current = resolvedTarget
142
+ prevSequenceRef.current = hotkeySequenceString
78
143
 
79
- // Build options object conditionally to avoid overwriting manager defaults with undefined
80
- const registerOptions: SequenceOptions = { enabled: true }
81
- if (timeout !== undefined) registerOptions.timeout = timeout
82
- if (platform !== undefined) registerOptions.platform = platform
144
+ // Cleanup on unmount
145
+ return () => {
146
+ if (registrationRef.current?.isActive) {
147
+ registrationRef.current.unregister()
148
+ registrationRef.current = null
149
+ }
150
+ }
151
+ }, [hotkeySequenceString, mergedOptions.enabled, sequence])
83
152
 
84
- const unregister = manager.register(
85
- sequence,
86
- (event, context) => callbackRef.current(event, context),
87
- registerOptions,
88
- )
153
+ // Sync callback and options on EVERY render (outside useEffect)
154
+ if (registrationRef.current?.isActive) {
155
+ registrationRef.current.callback = (
156
+ event: KeyboardEvent,
157
+ context: HotkeyCallbackContext,
158
+ ) => callbackRef.current(event, context)
159
+ registrationRef.current.setOptions(optionsWithoutTarget)
160
+ }
161
+ }
89
162
 
90
- return unregister
91
- }, [enabled, sequence, sequenceKey, timeout, platform])
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
92
168
  }