@tanstack/preact-hotkeys 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +15 -1
  2. package/dist/HotkeysProvider.d.ts +6 -13
  3. package/dist/HotkeysProvider.js +1 -2
  4. package/dist/index.d.ts +2 -1
  5. package/dist/index.js +2 -1
  6. package/dist/useHeldKeyCodes.d.ts +2 -4
  7. package/dist/useHeldKeyCodes.js +3 -3
  8. package/dist/useHeldKeys.d.ts +2 -4
  9. package/dist/useHeldKeys.js +3 -3
  10. package/dist/useHotkey.d.ts +3 -6
  11. package/dist/useHotkey.js +12 -8
  12. package/dist/useHotkeyHint.d.ts +11 -0
  13. package/dist/useHotkeyHint.js +17 -0
  14. package/dist/useHotkeyRecorder.d.ts +3 -6
  15. package/dist/useHotkeyRecorder.js +1 -2
  16. package/dist/useHotkeyRegistrations.d.ts +3 -6
  17. package/dist/useHotkeyRegistrations.js +6 -4
  18. package/dist/useHotkeySequence.d.ts +3 -6
  19. package/dist/useHotkeySequence.js +10 -7
  20. package/dist/useHotkeySequenceRecorder.d.ts +3 -6
  21. package/dist/useHotkeySequenceRecorder.js +1 -2
  22. package/dist/useHotkeySequences.d.ts +3 -6
  23. package/dist/useHotkeySequences.js +4 -17
  24. package/dist/useHotkeys.d.ts +3 -6
  25. package/dist/useHotkeys.js +4 -17
  26. package/dist/useKeyHold.d.ts +2 -5
  27. package/dist/useKeyHold.js +1 -2
  28. package/dist/utils.js +1 -2
  29. package/package.json +11 -16
  30. package/dist/HotkeysProvider.cjs +0 -26
  31. package/dist/HotkeysProvider.cjs.map +0 -1
  32. package/dist/HotkeysProvider.d.cts +0 -29
  33. package/dist/HotkeysProvider.js.map +0 -1
  34. package/dist/index.cjs +0 -33
  35. package/dist/index.d.cts +0 -13
  36. package/dist/useHeldKeyCodes.cjs +0 -37
  37. package/dist/useHeldKeyCodes.cjs.map +0 -1
  38. package/dist/useHeldKeyCodes.d.cts +0 -31
  39. package/dist/useHeldKeyCodes.js.map +0 -1
  40. package/dist/useHeldKeys.cjs +0 -33
  41. package/dist/useHeldKeys.cjs.map +0 -1
  42. package/dist/useHeldKeys.d.cts +0 -27
  43. package/dist/useHeldKeys.js.map +0 -1
  44. package/dist/useHotkey.cjs +0 -121
  45. package/dist/useHotkey.cjs.map +0 -1
  46. package/dist/useHotkey.d.cts +0 -75
  47. package/dist/useHotkey.js.map +0 -1
  48. package/dist/useHotkeyRecorder.cjs +0 -71
  49. package/dist/useHotkeyRecorder.cjs.map +0 -1
  50. package/dist/useHotkeyRecorder.d.cts +0 -57
  51. package/dist/useHotkeyRecorder.js.map +0 -1
  52. package/dist/useHotkeyRegistrations.cjs +0 -26
  53. package/dist/useHotkeyRegistrations.cjs.map +0 -1
  54. package/dist/useHotkeyRegistrations.d.cts +0 -26
  55. package/dist/useHotkeyRegistrations.js.map +0 -1
  56. package/dist/useHotkeySequence.cjs +0 -116
  57. package/dist/useHotkeySequence.cjs.map +0 -1
  58. package/dist/useHotkeySequence.d.cts +0 -59
  59. package/dist/useHotkeySequence.js.map +0 -1
  60. package/dist/useHotkeySequenceRecorder.cjs +0 -39
  61. package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
  62. package/dist/useHotkeySequenceRecorder.d.cts +0 -19
  63. package/dist/useHotkeySequenceRecorder.js.map +0 -1
  64. package/dist/useHotkeySequences.cjs +0 -138
  65. package/dist/useHotkeySequences.cjs.map +0 -1
  66. package/dist/useHotkeySequences.d.cts +0 -63
  67. package/dist/useHotkeySequences.js.map +0 -1
  68. package/dist/useHotkeys.cjs +0 -136
  69. package/dist/useHotkeys.cjs.map +0 -1
  70. package/dist/useHotkeys.d.cts +0 -63
  71. package/dist/useHotkeys.js.map +0 -1
  72. package/dist/useKeyHold.cjs +0 -53
  73. package/dist/useKeyHold.cjs.map +0 -1
  74. package/dist/useKeyHold.d.cts +0 -47
  75. package/dist/useKeyHold.js.map +0 -1
  76. package/dist/utils.cjs +0 -12
  77. package/dist/utils.cjs.map +0 -1
  78. package/dist/utils.js.map +0 -1
  79. package/src/HotkeysProvider.tsx +0 -56
  80. package/src/index.ts +0 -17
  81. package/src/useHeldKeyCodes.ts +0 -33
  82. package/src/useHeldKeys.ts +0 -29
  83. package/src/useHotkey.ts +0 -188
  84. package/src/useHotkeyRecorder.ts +0 -101
  85. package/src/useHotkeyRegistrations.ts +0 -45
  86. package/src/useHotkeySequence.ts +0 -188
  87. package/src/useHotkeySequenceRecorder.ts +0 -64
  88. package/src/useHotkeySequences.ts +0 -206
  89. package/src/useHotkeys.ts +0 -207
  90. package/src/useKeyHold.ts +0 -52
  91. package/src/utils.ts +0 -8
@@ -1,136 +0,0 @@
1
- const require_HotkeysProvider = require('./HotkeysProvider.cjs');
2
- const require_utils = require('./utils.cjs');
3
- let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
- let preact_hooks = require("preact/hooks");
5
-
6
- //#region src/useHotkeys.ts
7
- /**
8
- * Preact 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
- * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row
22
- * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
23
- * via `setOptions` (no unregister/re-register churn).
24
- *
25
- * @example
26
- * ```tsx
27
- * function Editor() {
28
- * useHotkeys([
29
- * { hotkey: 'Mod+S', callback: () => save() },
30
- * { hotkey: 'Mod+Z', callback: () => undo() },
31
- * { hotkey: 'Escape', callback: () => close() },
32
- * ])
33
- * }
34
- * ```
35
- *
36
- * @example
37
- * ```tsx
38
- * function MenuShortcuts({ items }) {
39
- * // Dynamic hotkeys from data -- safe because it's a single hook call
40
- * useHotkeys(
41
- * items.map((item) => ({
42
- * hotkey: item.shortcut,
43
- * callback: item.action,
44
- * options: { enabled: item.enabled },
45
- * })),
46
- * { preventDefault: true },
47
- * )
48
- * }
49
- * ```
50
- */
51
- function useHotkeys(hotkeys, commonOptions = {}) {
52
- const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions().hotkey;
53
- const manager = (0, _tanstack_hotkeys.getHotkeyManager)();
54
- const platform = commonOptions.platform ?? defaultOptions?.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
55
- const registrationsRef = (0, preact_hooks.useRef)(/* @__PURE__ */ new Map());
56
- const hotkeysRef = (0, preact_hooks.useRef)(hotkeys);
57
- const hotkeyStringsRef = (0, preact_hooks.useRef)([]);
58
- const commonOptionsRef = (0, preact_hooks.useRef)(commonOptions);
59
- const defaultOptionsRef = (0, preact_hooks.useRef)(defaultOptions);
60
- const managerRef = (0, preact_hooks.useRef)(manager);
61
- const hotkeyStrings = hotkeys.map((def) => (0, _tanstack_hotkeys.normalizeRegisterableHotkey)(def.hotkey, platform));
62
- hotkeysRef.current = hotkeys;
63
- hotkeyStringsRef.current = hotkeyStrings;
64
- commonOptionsRef.current = commonOptions;
65
- defaultOptionsRef.current = defaultOptions;
66
- managerRef.current = manager;
67
- (0, preact_hooks.useEffect)(() => {
68
- const prevRegistrations = registrationsRef.current;
69
- const nextRegistrations = /* @__PURE__ */ new Map();
70
- const rows = [];
71
- for (let i = 0; i < hotkeysRef.current.length; i++) {
72
- const def = hotkeysRef.current[i];
73
- const hotkeyStr = hotkeyStringsRef.current[i];
74
- const mergedOptions = {
75
- ...defaultOptionsRef.current,
76
- ...commonOptionsRef.current,
77
- ...def.options
78
- };
79
- const resolvedTarget = require_utils.isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
80
- if (!resolvedTarget) continue;
81
- const registrationKey = `${i}:${hotkeyStr}`;
82
- rows.push({
83
- registrationKey,
84
- def,
85
- hotkeyStr,
86
- mergedOptions,
87
- resolvedTarget
88
- });
89
- }
90
- const nextKeys = new Set(rows.map((r) => r.registrationKey));
91
- for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
92
- for (const row of rows) {
93
- const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } = row;
94
- const existing = prevRegistrations.get(registrationKey);
95
- if (existing?.handle.isActive && existing.target === resolvedTarget) {
96
- nextRegistrations.set(registrationKey, existing);
97
- continue;
98
- }
99
- if (existing?.handle.isActive) existing.handle.unregister();
100
- const handle = managerRef.current.register(hotkeyStr, def.callback, {
101
- ...mergedOptions,
102
- target: resolvedTarget
103
- });
104
- nextRegistrations.set(registrationKey, {
105
- handle,
106
- target: resolvedTarget
107
- });
108
- }
109
- registrationsRef.current = nextRegistrations;
110
- });
111
- (0, preact_hooks.useEffect)(() => {
112
- return () => {
113
- for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
114
- registrationsRef.current = /* @__PURE__ */ new Map();
115
- };
116
- }, []);
117
- for (let i = 0; i < hotkeys.length; i++) {
118
- const def = hotkeys[i];
119
- const hotkeyStr = hotkeyStrings[i];
120
- const registrationKey = `${i}:${hotkeyStr}`;
121
- const handle = registrationsRef.current.get(registrationKey)?.handle;
122
- if (handle?.isActive) {
123
- handle.callback = def.callback;
124
- const { target: _target, ...optionsWithoutTarget } = {
125
- ...defaultOptions,
126
- ...commonOptions,
127
- ...def.options
128
- };
129
- handle.setOptions(optionsWithoutTarget);
130
- }
131
- }
132
- }
133
-
134
- //#endregion
135
- exports.useHotkeys = useHotkeys;
136
- //# sourceMappingURL=useHotkeys.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useHotkeys.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n getHotkeyManager,\n normalizeRegisterableHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).\n * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n normalizeRegisterableHotkey(def.hotkey, platform),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n\n for (let i = 0; i < hotkeys.length; i++) {\n const def = hotkeys[i]!\n const hotkeyStr = hotkeyStrings[i]!\n const registrationKey = `${i}:${hotkeyStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeyOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,WACd,SACA,gBAAkC,EAAE,EAC9B;CAMN,MAAM,iBAAiBA,kDAA0B,CAAC;CAClD,MAAM,mDAA4B;CAClC,MAAM,WACJ,cAAc,YAAY,gBAAgB,mDAA4B;CAExE,MAAM,4DAA2D,IAAI,KAAK,CAAC;CAC3E,MAAM,sCAAoB,QAAQ;CAClC,MAAM,4CAAyC,EAAE,CAAC;CAClD,MAAM,4CAA0B,cAAc;CAC9C,MAAM,6CAA2B,eAAe;CAChD,MAAM,sCAAoB,QAAQ;CAElC,MAAM,gBAAgB,QAAQ,KAAK,2DACL,IAAI,QAAQ,SAAS,CAClD;AAED,YAAW,UAAU;AACrB,kBAAiB,UAAU;AAC3B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAErB,mCAAgB;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;GAC3B;AAEF,mCAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC;AAEN,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,MAAM,QAAQ;EACpB,MAAM,YAAY,cAAc;EAChC,MAAM,kBAAkB,GAAG,EAAE,GAAG;EAChC,MAAM,SAAS,iBAAiB,QAAQ,IAAI,gBAAgB,EAAE;AAE9D,MAAI,QAAQ,UAAU;AACpB,UAAO,WAAW,IAAI;GAMtB,MAAM,EAAE,QAAQ,SAAS,GAAG,yBALN;IACpB,GAAG;IACH,GAAG;IACH,GAAG,IAAI;IACR;AAED,UAAO,WAAW,qBAAqB"}
@@ -1,63 +0,0 @@
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
- * Preact 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
- * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row
31
- * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
32
- * via `setOptions` (no unregister/re-register churn).
33
- *
34
- * @example
35
- * ```tsx
36
- * function Editor() {
37
- * useHotkeys([
38
- * { hotkey: 'Mod+S', callback: () => save() },
39
- * { hotkey: 'Mod+Z', callback: () => undo() },
40
- * { hotkey: 'Escape', callback: () => close() },
41
- * ])
42
- * }
43
- * ```
44
- *
45
- * @example
46
- * ```tsx
47
- * function MenuShortcuts({ items }) {
48
- * // Dynamic hotkeys from data -- safe because it's a single hook call
49
- * useHotkeys(
50
- * items.map((item) => ({
51
- * hotkey: item.shortcut,
52
- * callback: item.action,
53
- * options: { enabled: item.enabled },
54
- * })),
55
- * { preventDefault: true },
56
- * )
57
- * }
58
- * ```
59
- */
60
- declare function useHotkeys(hotkeys: Array<UseHotkeyDefinition>, commonOptions?: UseHotkeyOptions): void;
61
- //#endregion
62
- export { UseHotkeyDefinition, useHotkeys };
63
- //# sourceMappingURL=useHotkeys.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n getHotkeyManager,\n normalizeRegisterableHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).\n * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n normalizeRegisterableHotkey(def.hotkey, platform),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n\n for (let i = 0; i < hotkeys.length; i++) {\n const def = hotkeys[i]!\n const hotkeyStr = hotkeyStrings[i]!\n const registrationKey = `${i}:${hotkeyStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeyOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,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,4BAA4B,IAAI,QAAQ,SAAS,CAClD;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;GAC3B;AAEF,iBAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC;AAEN,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,MAAM,QAAQ;EACpB,MAAM,YAAY,cAAc;EAChC,MAAM,kBAAkB,GAAG,EAAE,GAAG;EAChC,MAAM,SAAS,iBAAiB,QAAQ,IAAI,gBAAgB,EAAE;AAE9D,MAAI,QAAQ,UAAU;AACpB,UAAO,WAAW,IAAI;GAMtB,MAAM,EAAE,QAAQ,SAAS,GAAG,yBALN;IACpB,GAAG;IACH,GAAG;IACH,GAAG,IAAI;IACR;AAED,UAAO,WAAW,qBAAqB"}
@@ -1,53 +0,0 @@
1
- let _tanstack_hotkeys = require("@tanstack/hotkeys");
2
- let _tanstack_preact_store = require("@tanstack/preact-store");
3
-
4
- //#region src/useKeyHold.ts
5
- /**
6
- * Preact hook that returns whether a specific key is currently being held.
7
- *
8
- * This hook uses `useSelector` from `@tanstack/preact-store` to subscribe
9
- * to the global KeyStateTracker and uses a selector to determine if
10
- * the specified key is held.
11
- *
12
- * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
13
- * @returns True if the key is currently held down
14
- *
15
- * @example
16
- * ```tsx
17
- * function ShiftIndicator() {
18
- * const isShiftHeld = useKeyHold('Shift')
19
- *
20
- * return (
21
- * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
22
- * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
23
- * </div>
24
- * )
25
- * }
26
- * ```
27
- *
28
- * @example
29
- * ```tsx
30
- * function ModifierIndicators() {
31
- * const ctrl = useKeyHold('Control')
32
- * const shift = useKeyHold('Shift')
33
- * const alt = useKeyHold('Alt')
34
- *
35
- * return (
36
- * <div>
37
- * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
38
- * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
39
- * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
40
- * </div>
41
- * )
42
- * }
43
- * ```
44
- */
45
- function useKeyHold(key) {
46
- const tracker = (0, _tanstack_hotkeys.getKeyStateTracker)();
47
- const normalizedKey = key.toLowerCase();
48
- return (0, _tanstack_preact_store.useSelector)(tracker.store, (state) => state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey));
49
- }
50
-
51
- //#endregion
52
- exports.useKeyHold = useKeyHold;
53
- //# sourceMappingURL=useKeyHold.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useKeyHold.cjs","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useSelector } from '@tanstack/preact-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { IndividualKey } from '@tanstack/hotkeys'\n\n/**\n * Preact hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useSelector` from `@tanstack/preact-store` to subscribe\n * to the global KeyStateTracker and uses a selector to determine if\n * the specified key is held.\n *\n * @param key - The key to check (e.g., 'Shift', 'Control', 'A')\n * @returns True if the key is currently held down\n *\n * @example\n * ```tsx\n * function ShiftIndicator() {\n * const isShiftHeld = useKeyHold('Shift')\n *\n * return (\n * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>\n * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * function ModifierIndicators() {\n * const ctrl = useKeyHold('Control')\n * const shift = useKeyHold('Shift')\n * const alt = useKeyHold('Alt')\n *\n * return (\n * <div>\n * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>\n * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>\n * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>\n * </div>\n * )\n * }\n * ```\n */\nexport function useKeyHold(key: IndividualKey): boolean {\n const tracker = getKeyStateTracker()\n const normalizedKey = key.toLowerCase()\n\n return useSelector(tracker.store, (state) =>\n state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,WAAW,KAA6B;CACtD,MAAM,qDAA8B;CACpC,MAAM,gBAAgB,IAAI,aAAa;AAEvC,gDAAmB,QAAQ,QAAQ,UACjC,MAAM,SAAS,MAAM,YAAY,QAAQ,aAAa,KAAK,cAAc,CAC1E"}
@@ -1,47 +0,0 @@
1
- import { IndividualKey } from "@tanstack/hotkeys";
2
-
3
- //#region src/useKeyHold.d.ts
4
- /**
5
- * Preact hook that returns whether a specific key is currently being held.
6
- *
7
- * This hook uses `useSelector` from `@tanstack/preact-store` to subscribe
8
- * to the global KeyStateTracker and uses a selector to determine if
9
- * the specified key is held.
10
- *
11
- * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
12
- * @returns True if the key is currently held down
13
- *
14
- * @example
15
- * ```tsx
16
- * function ShiftIndicator() {
17
- * const isShiftHeld = useKeyHold('Shift')
18
- *
19
- * return (
20
- * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
21
- * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
22
- * </div>
23
- * )
24
- * }
25
- * ```
26
- *
27
- * @example
28
- * ```tsx
29
- * function ModifierIndicators() {
30
- * const ctrl = useKeyHold('Control')
31
- * const shift = useKeyHold('Shift')
32
- * const alt = useKeyHold('Alt')
33
- *
34
- * return (
35
- * <div>
36
- * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
37
- * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
38
- * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
39
- * </div>
40
- * )
41
- * }
42
- * ```
43
- */
44
- declare function useKeyHold(key: IndividualKey): boolean;
45
- //#endregion
46
- export { useKeyHold };
47
- //# sourceMappingURL=useKeyHold.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useKeyHold.js","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useSelector } from '@tanstack/preact-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { IndividualKey } from '@tanstack/hotkeys'\n\n/**\n * Preact hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useSelector` from `@tanstack/preact-store` to subscribe\n * to the global KeyStateTracker and uses a selector to determine if\n * the specified key is held.\n *\n * @param key - The key to check (e.g., 'Shift', 'Control', 'A')\n * @returns True if the key is currently held down\n *\n * @example\n * ```tsx\n * function ShiftIndicator() {\n * const isShiftHeld = useKeyHold('Shift')\n *\n * return (\n * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>\n * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * function ModifierIndicators() {\n * const ctrl = useKeyHold('Control')\n * const shift = useKeyHold('Shift')\n * const alt = useKeyHold('Alt')\n *\n * return (\n * <div>\n * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>\n * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>\n * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>\n * </div>\n * )\n * }\n * ```\n */\nexport function useKeyHold(key: IndividualKey): boolean {\n const tracker = getKeyStateTracker()\n const normalizedKey = key.toLowerCase()\n\n return useSelector(tracker.store, (state) =>\n state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,WAAW,KAA6B;CACtD,MAAM,UAAU,oBAAoB;CACpC,MAAM,gBAAgB,IAAI,aAAa;AAEvC,QAAO,YAAY,QAAQ,QAAQ,UACjC,MAAM,SAAS,MAAM,YAAY,QAAQ,aAAa,KAAK,cAAc,CAC1E"}
package/dist/utils.cjs DELETED
@@ -1,12 +0,0 @@
1
-
2
- //#region src/utils.ts
3
- /**
4
- * Type guard to check if a value is a Preact 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
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.cjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import type { RefObject } from 'preact'\n\n/**\n * Type guard to check if a value is a Preact ref-like object.\n */\nexport function isRef(value: unknown): value is RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;AAKA,SAAgB,MAAM,OAAwD;AAC5E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
package/dist/utils.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import type { RefObject } from 'preact'\n\n/**\n * Type guard to check if a value is a Preact ref-like object.\n */\nexport function isRef(value: unknown): value is RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;AAKA,SAAgB,MAAM,OAAwD;AAC5E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
@@ -1,56 +0,0 @@
1
- import { createContext } from 'preact'
2
- import { useContext, useMemo } from 'preact/hooks'
3
- import type { ComponentChildren } from 'preact'
4
- import type {
5
- HotkeyRecorderOptions,
6
- HotkeySequenceRecorderOptions,
7
- } from '@tanstack/hotkeys'
8
- import type { UseHotkeyOptions } from './useHotkey'
9
- import type { UseHotkeySequenceOptions } from './useHotkeySequence'
10
-
11
- export interface HotkeysProviderOptions {
12
- hotkey?: Partial<UseHotkeyOptions>
13
- hotkeyRecorder?: Partial<HotkeyRecorderOptions>
14
- hotkeySequenceRecorder?: Partial<HotkeySequenceRecorderOptions>
15
- hotkeySequence?: Partial<UseHotkeySequenceOptions>
16
- }
17
-
18
- interface HotkeysContextValue {
19
- defaultOptions: HotkeysProviderOptions
20
- }
21
-
22
- const HotkeysContext = createContext<HotkeysContextValue | null>(null)
23
-
24
- export interface HotkeysProviderProps {
25
- children: ComponentChildren
26
- defaultOptions?: HotkeysProviderOptions
27
- }
28
-
29
- const DEFAULT_OPTIONS: HotkeysProviderOptions = {}
30
-
31
- export function HotkeysProvider({
32
- children,
33
- defaultOptions = DEFAULT_OPTIONS,
34
- }: HotkeysProviderProps) {
35
- const contextValue: HotkeysContextValue = useMemo(
36
- () => ({
37
- defaultOptions,
38
- }),
39
- [defaultOptions],
40
- )
41
-
42
- return (
43
- <HotkeysContext.Provider value={contextValue}>
44
- {children}
45
- </HotkeysContext.Provider>
46
- )
47
- }
48
-
49
- export function useHotkeysContext() {
50
- return useContext(HotkeysContext)
51
- }
52
-
53
- export function useDefaultHotkeysOptions() {
54
- const context = useContext(HotkeysContext)
55
- return context?.defaultOptions ?? {}
56
- }
package/src/index.ts DELETED
@@ -1,17 +0,0 @@
1
- // Re-export everything from the core package
2
- export * from '@tanstack/hotkeys'
3
-
4
- // provider
5
- export * from './HotkeysProvider'
6
-
7
- // Preact-specific exports
8
- export * from './useHotkey'
9
- export * from './useHotkeys'
10
- export * from './useHeldKeys'
11
- export * from './useHeldKeyCodes'
12
- export * from './useKeyHold'
13
- export * from './useHotkeySequence'
14
- export * from './useHotkeySequences'
15
- export * from './useHotkeyRecorder'
16
- export * from './useHotkeyRegistrations'
17
- export * from './useHotkeySequenceRecorder'
@@ -1,33 +0,0 @@
1
- import { useSelector } from '@tanstack/preact-store'
2
- import { getKeyStateTracker } from '@tanstack/hotkeys'
3
-
4
- /**
5
- * Preact hook that returns a map of currently held key names to their physical `event.code` values.
6
- *
7
- * This is useful for debugging which physical key was pressed (e.g. distinguishing
8
- * left vs right Shift via "ShiftLeft" / "ShiftRight").
9
- *
10
- * @returns Record mapping normalized key names to their `event.code` values
11
- *
12
- * @example
13
- * ```tsx
14
- * function KeyDebugDisplay() {
15
- * const heldKeys = useHeldKeys()
16
- * const heldCodes = useHeldKeyCodes()
17
- *
18
- * return (
19
- * <div>
20
- * {heldKeys.map((key) => (
21
- * <kbd key={key}>
22
- * {key} <small>{heldCodes[key]}</small>
23
- * </kbd>
24
- * ))}
25
- * </div>
26
- * )
27
- * }
28
- * ```
29
- */
30
- export function useHeldKeyCodes(): Record<string, string> {
31
- const tracker = getKeyStateTracker()
32
- return useSelector(tracker.store, (state) => state.heldCodes)
33
- }
@@ -1,29 +0,0 @@
1
- import { useSelector } from '@tanstack/preact-store'
2
- import { getKeyStateTracker } from '@tanstack/hotkeys'
3
-
4
- /**
5
- * Preact hook that returns an array of currently held keyboard keys.
6
- *
7
- * This hook uses `useSelector` from `@tanstack/preact-store` to subscribe
8
- * to the global KeyStateTracker and updates whenever keys are pressed
9
- * or released.
10
- *
11
- * @returns Array of currently held key names
12
- *
13
- * @example
14
- * ```tsx
15
- * function KeyDisplay() {
16
- * const heldKeys = useHeldKeys()
17
- *
18
- * return (
19
- * <div>
20
- * Currently pressed: {heldKeys.join(' + ') || 'None'}
21
- * </div>
22
- * )
23
- * }
24
- * ```
25
- */
26
- export function useHeldKeys(): Array<string> {
27
- const tracker = getKeyStateTracker()
28
- return useSelector(tracker.store, (state) => state.heldKeys)
29
- }