@tanstack/react-hotkeys 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +121 -45
  3. package/dist/HotkeysProvider.cjs +23 -0
  4. package/dist/HotkeysProvider.cjs.map +1 -0
  5. package/dist/HotkeysProvider.d.cts +27 -0
  6. package/dist/HotkeysProvider.d.ts +27 -0
  7. package/dist/HotkeysProvider.js +19 -0
  8. package/dist/HotkeysProvider.js.map +1 -0
  9. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  10. package/dist/index.cjs +25 -0
  11. package/dist/index.d.cts +9 -0
  12. package/dist/index.d.ts +9 -0
  13. package/dist/index.js +11 -0
  14. package/dist/useHeldKeyCodes.cjs +38 -0
  15. package/dist/useHeldKeyCodes.cjs.map +1 -0
  16. package/dist/useHeldKeyCodes.d.cts +31 -0
  17. package/dist/useHeldKeyCodes.d.ts +31 -0
  18. package/dist/useHeldKeyCodes.js +37 -0
  19. package/dist/useHeldKeyCodes.js.map +1 -0
  20. package/dist/useHeldKeys.cjs +34 -0
  21. package/dist/useHeldKeys.cjs.map +1 -0
  22. package/dist/useHeldKeys.d.cts +27 -0
  23. package/dist/useHeldKeys.d.ts +27 -0
  24. package/dist/useHeldKeys.js +33 -0
  25. package/dist/useHeldKeys.js.map +1 -0
  26. package/dist/useHotkey.cjs +119 -0
  27. package/dist/useHotkey.cjs.map +1 -0
  28. package/dist/useHotkey.d.cts +73 -0
  29. package/dist/useHotkey.d.ts +73 -0
  30. package/dist/useHotkey.js +118 -0
  31. package/dist/useHotkey.js.map +1 -0
  32. package/dist/useHotkeyRecorder.cjs +72 -0
  33. package/dist/useHotkeyRecorder.cjs.map +1 -0
  34. package/dist/useHotkeyRecorder.d.cts +57 -0
  35. package/dist/useHotkeyRecorder.d.ts +57 -0
  36. package/dist/useHotkeyRecorder.js +71 -0
  37. package/dist/useHotkeyRecorder.js.map +1 -0
  38. package/dist/useHotkeySequence.cjs +65 -0
  39. package/dist/useHotkeySequence.cjs.map +1 -0
  40. package/dist/useHotkeySequence.d.cts +43 -0
  41. package/dist/useHotkeySequence.d.ts +43 -0
  42. package/dist/useHotkeySequence.js +64 -0
  43. package/dist/useHotkeySequence.js.map +1 -0
  44. package/dist/useKeyHold.cjs +54 -0
  45. package/dist/useKeyHold.cjs.map +1 -0
  46. package/dist/useKeyHold.d.cts +47 -0
  47. package/dist/useKeyHold.d.ts +47 -0
  48. package/dist/useKeyHold.js +53 -0
  49. package/dist/useKeyHold.js.map +1 -0
  50. package/package.json +66 -7
  51. package/src/HotkeysProvider.tsx +51 -0
  52. package/src/index.ts +13 -0
  53. package/src/useHeldKeyCodes.ts +33 -0
  54. package/src/useHeldKeys.ts +29 -0
  55. package/src/useHotkey.ts +191 -0
  56. package/src/useHotkeyRecorder.ts +101 -0
  57. package/src/useHotkeySequence.ts +92 -0
  58. package/src/useKeyHold.ts +52 -0
@@ -0,0 +1,33 @@
1
+ import { getKeyStateTracker } from "@tanstack/hotkeys";
2
+ import { useStore } from "@tanstack/react-store";
3
+
4
+ //#region src/useHeldKeys.ts
5
+ /**
6
+ * React hook that returns an array of currently held keyboard keys.
7
+ *
8
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
9
+ * to the global KeyStateTracker and updates whenever keys are pressed
10
+ * or released.
11
+ *
12
+ * @returns Array of currently held key names
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * function KeyDisplay() {
17
+ * const heldKeys = useHeldKeys()
18
+ *
19
+ * return (
20
+ * <div>
21
+ * Currently pressed: {heldKeys.join(' + ') || 'None'}
22
+ * </div>
23
+ * )
24
+ * }
25
+ * ```
26
+ */
27
+ function useHeldKeys() {
28
+ return useStore(getKeyStateTracker().store, (state) => state.heldKeys);
29
+ }
30
+
31
+ //#endregion
32
+ export { useHeldKeys };
33
+ //# sourceMappingURL=useHeldKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHeldKeys.js","names":[],"sources":["../src/useHeldKeys.ts"],"sourcesContent":["import { useStore } from '@tanstack/react-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\n\n/**\n * React hook that returns an array of currently held keyboard keys.\n *\n * This hook uses `useStore` from `@tanstack/react-store` to subscribe\n * to the global KeyStateTracker and updates whenever keys are pressed\n * or released.\n *\n * @returns Array of currently held key names\n *\n * @example\n * ```tsx\n * function KeyDisplay() {\n * const heldKeys = useHeldKeys()\n *\n * return (\n * <div>\n * Currently pressed: {heldKeys.join(' + ') || 'None'}\n * </div>\n * )\n * }\n * ```\n */\nexport function useHeldKeys(): Array<string> {\n const tracker = getKeyStateTracker()\n return useStore(tracker.store, (state) => state.heldKeys)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,cAA6B;AAE3C,QAAO,SADS,oBAAoB,CACZ,QAAQ,UAAU,MAAM,SAAS"}
@@ -0,0 +1,119 @@
1
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
+ let react = require("react");
5
+
6
+ //#region src/useHotkey.ts
7
+ /**
8
+ * React hook for registering a keyboard hotkey.
9
+ *
10
+ * Uses the singleton HotkeyManager for efficient event handling.
11
+ * The callback receives both the keyboard event and a context object
12
+ * containing the hotkey string and parsed hotkey.
13
+ *
14
+ * This hook syncs the callback and options on every render to avoid
15
+ * stale closures. This means
16
+ * callbacks that reference React state will always have access to
17
+ * the latest values.
18
+ *
19
+ * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
20
+ * @param callback - The function to call when the hotkey is pressed
21
+ * @param options - Options for the hotkey behavior
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * function SaveButton() {
26
+ * const [count, setCount] = useState(0)
27
+ *
28
+ * // Callback always has access to latest count value
29
+ * useHotkey('Mod+S', (event, { hotkey }) => {
30
+ * console.log(`Save triggered, count is ${count}`)
31
+ * handleSave()
32
+ * })
33
+ *
34
+ * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
35
+ * }
36
+ * ```
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ * function Modal({ isOpen, onClose }) {
41
+ * // enabled option is synced on every render
42
+ * useHotkey('Escape', () => {
43
+ * onClose()
44
+ * }, { enabled: isOpen })
45
+ *
46
+ * if (!isOpen) return null
47
+ * return <div className="modal">...</div>
48
+ * }
49
+ * ```
50
+ *
51
+ * @example
52
+ * ```tsx
53
+ * function Editor() {
54
+ * const editorRef = useRef<HTMLDivElement>(null)
55
+ *
56
+ * // Scoped to a specific element
57
+ * useHotkey('Mod+S', () => {
58
+ * save()
59
+ * }, { target: editorRef })
60
+ *
61
+ * return <div ref={editorRef}>...</div>
62
+ * }
63
+ * ```
64
+ */
65
+ function useHotkey(hotkey, callback, options = {}) {
66
+ const mergedOptions = {
67
+ ...require_HotkeysProvider.useDefaultHotkeysOptions().hotkey,
68
+ ...options
69
+ };
70
+ const manager = (0, _tanstack_hotkeys.getHotkeyManager)();
71
+ const registrationRef = (0, react.useRef)(null);
72
+ const callbackRef = (0, react.useRef)(callback);
73
+ const optionsRef = (0, react.useRef)(mergedOptions);
74
+ const managerRef = (0, react.useRef)(manager);
75
+ callbackRef.current = callback;
76
+ optionsRef.current = mergedOptions;
77
+ managerRef.current = manager;
78
+ const prevTargetRef = (0, react.useRef)(null);
79
+ const prevHotkeyRef = (0, react.useRef)(null);
80
+ const platform = mergedOptions.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
81
+ const hotkeyString = typeof hotkey === "string" ? hotkey : (0, _tanstack_hotkeys.formatHotkey)((0, _tanstack_hotkeys.rawHotkeyToParsedHotkey)(hotkey, platform));
82
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions;
83
+ (0, react.useEffect)(() => {
84
+ const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
85
+ if (!resolvedTarget) return;
86
+ const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
87
+ const hotkeyChanged = prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString;
88
+ if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
89
+ registrationRef.current.unregister();
90
+ registrationRef.current = null;
91
+ }
92
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(hotkeyString, callbackRef.current, {
93
+ ...optionsRef.current,
94
+ target: resolvedTarget
95
+ });
96
+ prevTargetRef.current = resolvedTarget;
97
+ prevHotkeyRef.current = hotkeyString;
98
+ return () => {
99
+ if (registrationRef.current?.isActive) {
100
+ registrationRef.current.unregister();
101
+ registrationRef.current = null;
102
+ }
103
+ };
104
+ }, [hotkeyString, options.enabled]);
105
+ if (registrationRef.current?.isActive) {
106
+ registrationRef.current.callback = callback;
107
+ registrationRef.current.setOptions(optionsWithoutTarget);
108
+ }
109
+ }
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
+
117
+ //#endregion
118
+ exports.useHotkey = useHotkey;
119
+ //# sourceMappingURL=useHotkey.cjs.map
@@ -0,0 +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"}
@@ -0,0 +1,73 @@
1
+ import { HotkeyCallback, HotkeyOptions, RegisterableHotkey } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useHotkey.d.ts
4
+ interface UseHotkeyOptions extends Omit<HotkeyOptions, '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;
11
+ }
12
+ /**
13
+ * React hook for registering a keyboard hotkey.
14
+ *
15
+ * Uses the singleton HotkeyManager for efficient event handling.
16
+ * The callback receives both the keyboard event and a context object
17
+ * containing the hotkey string and parsed hotkey.
18
+ *
19
+ * This hook syncs the callback and options on every render to avoid
20
+ * stale closures. This means
21
+ * callbacks that reference React state will always have access to
22
+ * the latest values.
23
+ *
24
+ * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
25
+ * @param callback - The function to call when the hotkey is pressed
26
+ * @param options - Options for the hotkey behavior
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * function SaveButton() {
31
+ * const [count, setCount] = useState(0)
32
+ *
33
+ * // Callback always has access to latest count value
34
+ * useHotkey('Mod+S', (event, { hotkey }) => {
35
+ * console.log(`Save triggered, count is ${count}`)
36
+ * handleSave()
37
+ * })
38
+ *
39
+ * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
40
+ * }
41
+ * ```
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * function Modal({ isOpen, onClose }) {
46
+ * // enabled option is synced on every render
47
+ * useHotkey('Escape', () => {
48
+ * onClose()
49
+ * }, { enabled: isOpen })
50
+ *
51
+ * if (!isOpen) return null
52
+ * return <div className="modal">...</div>
53
+ * }
54
+ * ```
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * function Editor() {
59
+ * const editorRef = useRef<HTMLDivElement>(null)
60
+ *
61
+ * // Scoped to a specific element
62
+ * useHotkey('Mod+S', () => {
63
+ * save()
64
+ * }, { target: editorRef })
65
+ *
66
+ * return <div ref={editorRef}>...</div>
67
+ * }
68
+ * ```
69
+ */
70
+ declare function useHotkey(hotkey: RegisterableHotkey, callback: HotkeyCallback, options?: UseHotkeyOptions): void;
71
+ //#endregion
72
+ export { UseHotkeyOptions, useHotkey };
73
+ //# sourceMappingURL=useHotkey.d.cts.map
@@ -0,0 +1,73 @@
1
+ import { HotkeyCallback, HotkeyOptions, RegisterableHotkey } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useHotkey.d.ts
4
+ interface UseHotkeyOptions extends Omit<HotkeyOptions, '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;
11
+ }
12
+ /**
13
+ * React hook for registering a keyboard hotkey.
14
+ *
15
+ * Uses the singleton HotkeyManager for efficient event handling.
16
+ * The callback receives both the keyboard event and a context object
17
+ * containing the hotkey string and parsed hotkey.
18
+ *
19
+ * This hook syncs the callback and options on every render to avoid
20
+ * stale closures. This means
21
+ * callbacks that reference React state will always have access to
22
+ * the latest values.
23
+ *
24
+ * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
25
+ * @param callback - The function to call when the hotkey is pressed
26
+ * @param options - Options for the hotkey behavior
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * function SaveButton() {
31
+ * const [count, setCount] = useState(0)
32
+ *
33
+ * // Callback always has access to latest count value
34
+ * useHotkey('Mod+S', (event, { hotkey }) => {
35
+ * console.log(`Save triggered, count is ${count}`)
36
+ * handleSave()
37
+ * })
38
+ *
39
+ * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
40
+ * }
41
+ * ```
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * function Modal({ isOpen, onClose }) {
46
+ * // enabled option is synced on every render
47
+ * useHotkey('Escape', () => {
48
+ * onClose()
49
+ * }, { enabled: isOpen })
50
+ *
51
+ * if (!isOpen) return null
52
+ * return <div className="modal">...</div>
53
+ * }
54
+ * ```
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * function Editor() {
59
+ * const editorRef = useRef<HTMLDivElement>(null)
60
+ *
61
+ * // Scoped to a specific element
62
+ * useHotkey('Mod+S', () => {
63
+ * save()
64
+ * }, { target: editorRef })
65
+ *
66
+ * return <div ref={editorRef}>...</div>
67
+ * }
68
+ * ```
69
+ */
70
+ declare function useHotkey(hotkey: RegisterableHotkey, callback: HotkeyCallback, options?: UseHotkeyOptions): void;
71
+ //#endregion
72
+ export { UseHotkeyOptions, useHotkey };
73
+ //# sourceMappingURL=useHotkey.d.ts.map
@@ -0,0 +1,118 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey } from "@tanstack/hotkeys";
3
+ import { useEffect, useRef } from "react";
4
+
5
+ //#region src/useHotkey.ts
6
+ /**
7
+ * React hook for registering a keyboard hotkey.
8
+ *
9
+ * Uses the singleton HotkeyManager for efficient event handling.
10
+ * The callback receives both the keyboard event and a context object
11
+ * containing the hotkey string and parsed hotkey.
12
+ *
13
+ * This hook syncs the callback and options on every render to avoid
14
+ * stale closures. This means
15
+ * callbacks that reference React state will always have access to
16
+ * the latest values.
17
+ *
18
+ * @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
19
+ * @param callback - The function to call when the hotkey is pressed
20
+ * @param options - Options for the hotkey behavior
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * function SaveButton() {
25
+ * const [count, setCount] = useState(0)
26
+ *
27
+ * // Callback always has access to latest count value
28
+ * useHotkey('Mod+S', (event, { hotkey }) => {
29
+ * console.log(`Save triggered, count is ${count}`)
30
+ * handleSave()
31
+ * })
32
+ *
33
+ * return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
34
+ * }
35
+ * ```
36
+ *
37
+ * @example
38
+ * ```tsx
39
+ * function Modal({ isOpen, onClose }) {
40
+ * // enabled option is synced on every render
41
+ * useHotkey('Escape', () => {
42
+ * onClose()
43
+ * }, { enabled: isOpen })
44
+ *
45
+ * if (!isOpen) return null
46
+ * return <div className="modal">...</div>
47
+ * }
48
+ * ```
49
+ *
50
+ * @example
51
+ * ```tsx
52
+ * function Editor() {
53
+ * const editorRef = useRef<HTMLDivElement>(null)
54
+ *
55
+ * // Scoped to a specific element
56
+ * useHotkey('Mod+S', () => {
57
+ * save()
58
+ * }, { target: editorRef })
59
+ *
60
+ * return <div ref={editorRef}>...</div>
61
+ * }
62
+ * ```
63
+ */
64
+ function useHotkey(hotkey, callback, options = {}) {
65
+ const mergedOptions = {
66
+ ...useDefaultHotkeysOptions().hotkey,
67
+ ...options
68
+ };
69
+ const manager = getHotkeyManager();
70
+ const registrationRef = useRef(null);
71
+ const callbackRef = useRef(callback);
72
+ const optionsRef = useRef(mergedOptions);
73
+ const managerRef = useRef(manager);
74
+ callbackRef.current = callback;
75
+ optionsRef.current = mergedOptions;
76
+ managerRef.current = manager;
77
+ const prevTargetRef = useRef(null);
78
+ const prevHotkeyRef = useRef(null);
79
+ const platform = mergedOptions.platform ?? detectPlatform();
80
+ const hotkeyString = typeof hotkey === "string" ? hotkey : formatHotkey(rawHotkeyToParsedHotkey(hotkey, platform));
81
+ const { target: _target, ...optionsWithoutTarget } = mergedOptions;
82
+ useEffect(() => {
83
+ const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
84
+ if (!resolvedTarget) return;
85
+ const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
86
+ const hotkeyChanged = prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString;
87
+ if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
88
+ registrationRef.current.unregister();
89
+ registrationRef.current = null;
90
+ }
91
+ if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(hotkeyString, callbackRef.current, {
92
+ ...optionsRef.current,
93
+ target: resolvedTarget
94
+ });
95
+ prevTargetRef.current = resolvedTarget;
96
+ prevHotkeyRef.current = hotkeyString;
97
+ return () => {
98
+ if (registrationRef.current?.isActive) {
99
+ registrationRef.current.unregister();
100
+ registrationRef.current = null;
101
+ }
102
+ };
103
+ }, [hotkeyString, options.enabled]);
104
+ if (registrationRef.current?.isActive) {
105
+ registrationRef.current.callback = callback;
106
+ registrationRef.current.setOptions(optionsWithoutTarget);
107
+ }
108
+ }
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
+
116
+ //#endregion
117
+ export { useHotkey };
118
+ //# sourceMappingURL=useHotkey.js.map
@@ -0,0 +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"}
@@ -0,0 +1,72 @@
1
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
+ let react = require("react");
5
+ let _tanstack_react_store = require("@tanstack/react-store");
6
+
7
+ //#region src/useHotkeyRecorder.ts
8
+ /**
9
+ * React hook for recording keyboard shortcuts.
10
+ *
11
+ * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`
12
+ * class, managing all the complexity of capturing keyboard events, converting them
13
+ * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete
14
+ * to clear.
15
+ *
16
+ * @param options - Configuration options for the recorder
17
+ * @returns An object with recording state and control functions
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * function ShortcutSettings() {
22
+ * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
23
+ *
24
+ * const recorder = useHotkeyRecorder({
25
+ * onRecord: (hotkey) => {
26
+ * setShortcut(hotkey)
27
+ * },
28
+ * onCancel: () => {
29
+ * console.log('Recording cancelled')
30
+ * },
31
+ * })
32
+ *
33
+ * return (
34
+ * <div>
35
+ * <button onClick={recorder.startRecording}>
36
+ * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}
37
+ * </button>
38
+ * {recorder.recordedHotkey && (
39
+ * <div>Recording: {recorder.recordedHotkey}</div>
40
+ * )}
41
+ * </div>
42
+ * )
43
+ * }
44
+ * ```
45
+ */
46
+ function useHotkeyRecorder(options) {
47
+ const mergedOptions = {
48
+ ...require_HotkeysProvider.useDefaultHotkeysOptions().hotkeyRecorder,
49
+ ...options
50
+ };
51
+ const recorderRef = (0, react.useRef)(null);
52
+ if (!recorderRef.current) recorderRef.current = new _tanstack_hotkeys.HotkeyRecorder(mergedOptions);
53
+ recorderRef.current.setOptions(mergedOptions);
54
+ const isRecording = (0, _tanstack_react_store.useStore)(recorderRef.current.store, (state) => state.isRecording);
55
+ const recordedHotkey = (0, _tanstack_react_store.useStore)(recorderRef.current.store, (state) => state.recordedHotkey);
56
+ (0, react.useEffect)(() => {
57
+ return () => {
58
+ recorderRef.current?.destroy();
59
+ };
60
+ }, []);
61
+ return {
62
+ isRecording,
63
+ recordedHotkey,
64
+ startRecording: () => recorderRef.current?.start(),
65
+ stopRecording: () => recorderRef.current?.stop(),
66
+ cancelRecording: () => recorderRef.current?.cancel()
67
+ };
68
+ }
69
+
70
+ //#endregion
71
+ exports.useHotkeyRecorder = useHotkeyRecorder;
72
+ //# sourceMappingURL=useHotkeyRecorder.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeyRecorder.cjs","names":["useDefaultHotkeysOptions","HotkeyRecorder"],"sources":["../src/useHotkeyRecorder.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport { HotkeyRecorder } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { Hotkey, HotkeyRecorderOptions } from '@tanstack/hotkeys'\n\nexport interface ReactHotkeyRecorder {\n /** Whether recording is currently active */\n isRecording: boolean\n /** The currently recorded hotkey (for live preview) */\n recordedHotkey: Hotkey | null\n /** Start recording a new hotkey */\n startRecording: () => void\n /** Stop recording (same as cancel) */\n stopRecording: () => void\n /** Cancel recording without saving */\n cancelRecording: () => void\n}\n\n/**\n * React hook for recording keyboard shortcuts.\n *\n * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`\n * class, managing all the complexity of capturing keyboard events, converting them\n * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete\n * to clear.\n *\n * @param options - Configuration options for the recorder\n * @returns An object with recording state and control functions\n *\n * @example\n * ```tsx\n * function ShortcutSettings() {\n * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')\n *\n * const recorder = useHotkeyRecorder({\n * onRecord: (hotkey) => {\n * setShortcut(hotkey)\n * },\n * onCancel: () => {\n * console.log('Recording cancelled')\n * },\n * })\n *\n * return (\n * <div>\n * <button onClick={recorder.startRecording}>\n * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}\n * </button>\n * {recorder.recordedHotkey && (\n * <div>Recording: {recorder.recordedHotkey}</div>\n * )}\n * </div>\n * )\n * }\n * ```\n */\nexport function useHotkeyRecorder(\n options: HotkeyRecorderOptions,\n): ReactHotkeyRecorder {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeyRecorder,\n ...options,\n } as HotkeyRecorderOptions\n\n const recorderRef = useRef<HotkeyRecorder | null>(null)\n\n // Create recorder instance once\n if (!recorderRef.current) {\n recorderRef.current = new HotkeyRecorder(mergedOptions)\n }\n\n // Sync options on every render (same pattern as useHotkey)\n // This ensures callbacks always have access to latest values\n recorderRef.current.setOptions(mergedOptions)\n\n // Subscribe to recorder state using useStore (same pattern as useHeldKeys)\n const isRecording = useStore(\n recorderRef.current.store,\n (state) => state.isRecording,\n )\n const recordedHotkey = useStore(\n recorderRef.current.store,\n (state) => state.recordedHotkey,\n )\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n recorderRef.current?.destroy()\n }\n }, [])\n\n return {\n isRecording,\n recordedHotkey,\n startRecording: () => recorderRef.current?.start(),\n stopRecording: () => recorderRef.current?.stop(),\n cancelRecording: () => recorderRef.current?.cancel(),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAgB,kBACd,SACqB;CACrB,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,gCAA4C,KAAK;AAGvD,KAAI,CAAC,YAAY,QACf,aAAY,UAAU,IAAIC,iCAAe,cAAc;AAKzD,aAAY,QAAQ,WAAW,cAAc;CAG7C,MAAM,kDACJ,YAAY,QAAQ,QACnB,UAAU,MAAM,YAClB;CACD,MAAM,qDACJ,YAAY,QAAQ,QACnB,UAAU,MAAM,eAClB;AAGD,4BAAgB;AACd,eAAa;AACX,eAAY,SAAS,SAAS;;IAE/B,EAAE,CAAC;AAEN,QAAO;EACL;EACA;EACA,sBAAsB,YAAY,SAAS,OAAO;EAClD,qBAAqB,YAAY,SAAS,MAAM;EAChD,uBAAuB,YAAY,SAAS,QAAQ;EACrD"}
@@ -0,0 +1,57 @@
1
+ import { Hotkey, HotkeyRecorderOptions } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useHotkeyRecorder.d.ts
4
+ interface ReactHotkeyRecorder {
5
+ /** Whether recording is currently active */
6
+ isRecording: boolean;
7
+ /** The currently recorded hotkey (for live preview) */
8
+ recordedHotkey: Hotkey | null;
9
+ /** Start recording a new hotkey */
10
+ startRecording: () => void;
11
+ /** Stop recording (same as cancel) */
12
+ stopRecording: () => void;
13
+ /** Cancel recording without saving */
14
+ cancelRecording: () => void;
15
+ }
16
+ /**
17
+ * React hook for recording keyboard shortcuts.
18
+ *
19
+ * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`
20
+ * class, managing all the complexity of capturing keyboard events, converting them
21
+ * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete
22
+ * to clear.
23
+ *
24
+ * @param options - Configuration options for the recorder
25
+ * @returns An object with recording state and control functions
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * function ShortcutSettings() {
30
+ * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
31
+ *
32
+ * const recorder = useHotkeyRecorder({
33
+ * onRecord: (hotkey) => {
34
+ * setShortcut(hotkey)
35
+ * },
36
+ * onCancel: () => {
37
+ * console.log('Recording cancelled')
38
+ * },
39
+ * })
40
+ *
41
+ * return (
42
+ * <div>
43
+ * <button onClick={recorder.startRecording}>
44
+ * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}
45
+ * </button>
46
+ * {recorder.recordedHotkey && (
47
+ * <div>Recording: {recorder.recordedHotkey}</div>
48
+ * )}
49
+ * </div>
50
+ * )
51
+ * }
52
+ * ```
53
+ */
54
+ declare function useHotkeyRecorder(options: HotkeyRecorderOptions): ReactHotkeyRecorder;
55
+ //#endregion
56
+ export { ReactHotkeyRecorder, useHotkeyRecorder };
57
+ //# sourceMappingURL=useHotkeyRecorder.d.cts.map
@@ -0,0 +1,57 @@
1
+ import { Hotkey, HotkeyRecorderOptions } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useHotkeyRecorder.d.ts
4
+ interface ReactHotkeyRecorder {
5
+ /** Whether recording is currently active */
6
+ isRecording: boolean;
7
+ /** The currently recorded hotkey (for live preview) */
8
+ recordedHotkey: Hotkey | null;
9
+ /** Start recording a new hotkey */
10
+ startRecording: () => void;
11
+ /** Stop recording (same as cancel) */
12
+ stopRecording: () => void;
13
+ /** Cancel recording without saving */
14
+ cancelRecording: () => void;
15
+ }
16
+ /**
17
+ * React hook for recording keyboard shortcuts.
18
+ *
19
+ * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`
20
+ * class, managing all the complexity of capturing keyboard events, converting them
21
+ * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete
22
+ * to clear.
23
+ *
24
+ * @param options - Configuration options for the recorder
25
+ * @returns An object with recording state and control functions
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * function ShortcutSettings() {
30
+ * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
31
+ *
32
+ * const recorder = useHotkeyRecorder({
33
+ * onRecord: (hotkey) => {
34
+ * setShortcut(hotkey)
35
+ * },
36
+ * onCancel: () => {
37
+ * console.log('Recording cancelled')
38
+ * },
39
+ * })
40
+ *
41
+ * return (
42
+ * <div>
43
+ * <button onClick={recorder.startRecording}>
44
+ * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}
45
+ * </button>
46
+ * {recorder.recordedHotkey && (
47
+ * <div>Recording: {recorder.recordedHotkey}</div>
48
+ * )}
49
+ * </div>
50
+ * )
51
+ * }
52
+ * ```
53
+ */
54
+ declare function useHotkeyRecorder(options: HotkeyRecorderOptions): ReactHotkeyRecorder;
55
+ //#endregion
56
+ export { ReactHotkeyRecorder, useHotkeyRecorder };
57
+ //# sourceMappingURL=useHotkeyRecorder.d.ts.map