@tanstack/preact-hotkeys 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/useHotkey.cjs +12 -3
- package/dist/useHotkey.cjs.map +1 -1
- package/dist/useHotkey.d.cts +2 -1
- package/dist/useHotkey.d.ts +2 -1
- package/dist/useHotkey.js +12 -3
- package/dist/useHotkey.js.map +1 -1
- package/dist/useHotkeySequence.cjs +24 -9
- package/dist/useHotkeySequence.cjs.map +1 -1
- package/dist/useHotkeySequence.d.cts +2 -1
- package/dist/useHotkeySequence.d.ts +2 -1
- package/dist/useHotkeySequence.js +24 -9
- package/dist/useHotkeySequence.js.map +1 -1
- package/dist/useHotkeySequences.cjs +138 -0
- package/dist/useHotkeySequences.cjs.map +1 -0
- package/dist/useHotkeySequences.d.cts +63 -0
- package/dist/useHotkeySequences.d.ts +63 -0
- package/dist/useHotkeySequences.js +138 -0
- package/dist/useHotkeySequences.js.map +1 -0
- package/dist/useHotkeys.cjs +7 -8
- package/dist/useHotkeys.cjs.map +1 -1
- package/dist/useHotkeys.d.cts +4 -1
- package/dist/useHotkeys.d.ts +4 -1
- package/dist/useHotkeys.js +7 -8
- package/dist/useHotkeys.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/useHotkey.ts +9 -2
- package/src/useHotkeySequence.ts +19 -4
- package/src/useHotkeySequences.ts +206 -0
- package/src/useHotkeys.ts +7 -14
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { UseHotkeySequenceOptions } from "./useHotkeySequence.cjs";
|
|
2
|
+
import { HotkeyCallback, HotkeySequence } from "@tanstack/hotkeys";
|
|
3
|
+
|
|
4
|
+
//#region src/useHotkeySequences.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A single sequence definition for use with `useHotkeySequences`.
|
|
7
|
+
*/
|
|
8
|
+
interface UseHotkeySequenceDefinition {
|
|
9
|
+
/** Array of hotkey strings that form the sequence */
|
|
10
|
+
sequence: HotkeySequence;
|
|
11
|
+
/** The function to call when the sequence is completed */
|
|
12
|
+
callback: HotkeyCallback;
|
|
13
|
+
/** Per-sequence options (merged on top of commonOptions) */
|
|
14
|
+
options?: UseHotkeySequenceOptions;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
18
|
+
*
|
|
19
|
+
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
|
|
20
|
+
* register variable-length lists without violating the rules of hooks.
|
|
21
|
+
*
|
|
22
|
+
* Options are merged in this order:
|
|
23
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
24
|
+
*
|
|
25
|
+
* Callbacks and options are synced on every render to avoid stale closures.
|
|
26
|
+
*
|
|
27
|
+
* Definitions with an empty `sequence` are skipped (no registration).
|
|
28
|
+
*
|
|
29
|
+
* @param definitions - Array of sequence definitions to register
|
|
30
|
+
* @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
|
|
31
|
+
* Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
|
|
32
|
+
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
33
|
+
* via `setOptions` (no unregister/re-register churn).
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* function VimPalette() {
|
|
38
|
+
* useHotkeySequences([
|
|
39
|
+
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
40
|
+
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
41
|
+
* { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
|
|
42
|
+
* ])
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* function DynamicSequences({ items }) {
|
|
49
|
+
* useHotkeySequences(
|
|
50
|
+
* items.map((item) => ({
|
|
51
|
+
* sequence: item.chords,
|
|
52
|
+
* callback: item.action,
|
|
53
|
+
* options: { enabled: item.enabled },
|
|
54
|
+
* })),
|
|
55
|
+
* { preventDefault: true },
|
|
56
|
+
* )
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare function useHotkeySequences(definitions: Array<UseHotkeySequenceDefinition>, commonOptions?: UseHotkeySequenceOptions): void;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { UseHotkeySequenceDefinition, useHotkeySequences };
|
|
63
|
+
//# sourceMappingURL=useHotkeySequences.d.cts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { UseHotkeySequenceOptions } from "./useHotkeySequence.js";
|
|
2
|
+
import { HotkeyCallback, HotkeySequence } from "@tanstack/hotkeys";
|
|
3
|
+
|
|
4
|
+
//#region src/useHotkeySequences.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A single sequence definition for use with `useHotkeySequences`.
|
|
7
|
+
*/
|
|
8
|
+
interface UseHotkeySequenceDefinition {
|
|
9
|
+
/** Array of hotkey strings that form the sequence */
|
|
10
|
+
sequence: HotkeySequence;
|
|
11
|
+
/** The function to call when the sequence is completed */
|
|
12
|
+
callback: HotkeyCallback;
|
|
13
|
+
/** Per-sequence options (merged on top of commonOptions) */
|
|
14
|
+
options?: UseHotkeySequenceOptions;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
18
|
+
*
|
|
19
|
+
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
|
|
20
|
+
* register variable-length lists without violating the rules of hooks.
|
|
21
|
+
*
|
|
22
|
+
* Options are merged in this order:
|
|
23
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
24
|
+
*
|
|
25
|
+
* Callbacks and options are synced on every render to avoid stale closures.
|
|
26
|
+
*
|
|
27
|
+
* Definitions with an empty `sequence` are skipped (no registration).
|
|
28
|
+
*
|
|
29
|
+
* @param definitions - Array of sequence definitions to register
|
|
30
|
+
* @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
|
|
31
|
+
* Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
|
|
32
|
+
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
33
|
+
* via `setOptions` (no unregister/re-register churn).
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* function VimPalette() {
|
|
38
|
+
* useHotkeySequences([
|
|
39
|
+
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
40
|
+
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
41
|
+
* { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
|
|
42
|
+
* ])
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* function DynamicSequences({ items }) {
|
|
49
|
+
* useHotkeySequences(
|
|
50
|
+
* items.map((item) => ({
|
|
51
|
+
* sequence: item.chords,
|
|
52
|
+
* callback: item.action,
|
|
53
|
+
* options: { enabled: item.enabled },
|
|
54
|
+
* })),
|
|
55
|
+
* { preventDefault: true },
|
|
56
|
+
* )
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare function useHotkeySequences(definitions: Array<UseHotkeySequenceDefinition>, commonOptions?: UseHotkeySequenceOptions): void;
|
|
61
|
+
//#endregion
|
|
62
|
+
export { UseHotkeySequenceDefinition, useHotkeySequences };
|
|
63
|
+
//# sourceMappingURL=useHotkeySequences.d.ts.map
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { isRef } from "./utils.js";
|
|
3
|
+
import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
|
|
4
|
+
import { useEffect, useRef } from "preact/hooks";
|
|
5
|
+
|
|
6
|
+
//#region src/useHotkeySequences.ts
|
|
7
|
+
/**
|
|
8
|
+
* Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).
|
|
9
|
+
*
|
|
10
|
+
* Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can
|
|
11
|
+
* register variable-length lists without violating the rules of hooks.
|
|
12
|
+
*
|
|
13
|
+
* Options are merged in this order:
|
|
14
|
+
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
15
|
+
*
|
|
16
|
+
* Callbacks and options are synced on every render to avoid stale closures.
|
|
17
|
+
*
|
|
18
|
+
* Definitions with an empty `sequence` are skipped (no registration).
|
|
19
|
+
*
|
|
20
|
+
* @param definitions - Array of sequence definitions to register
|
|
21
|
+
* @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).
|
|
22
|
+
* Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row
|
|
23
|
+
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
24
|
+
* via `setOptions` (no unregister/re-register churn).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* function VimPalette() {
|
|
29
|
+
* useHotkeySequences([
|
|
30
|
+
* { sequence: ['G', 'G'], callback: () => scrollToTop() },
|
|
31
|
+
* { sequence: ['D', 'D'], callback: () => deleteLine() },
|
|
32
|
+
* { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },
|
|
33
|
+
* ])
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* function DynamicSequences({ items }) {
|
|
40
|
+
* useHotkeySequences(
|
|
41
|
+
* items.map((item) => ({
|
|
42
|
+
* sequence: item.chords,
|
|
43
|
+
* callback: item.action,
|
|
44
|
+
* options: { enabled: item.enabled },
|
|
45
|
+
* })),
|
|
46
|
+
* { preventDefault: true },
|
|
47
|
+
* )
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function useHotkeySequences(definitions, commonOptions = {}) {
|
|
52
|
+
const defaultOptions = useDefaultHotkeysOptions().hotkeySequence;
|
|
53
|
+
const manager = getSequenceManager();
|
|
54
|
+
const registrationsRef = useRef(/* @__PURE__ */ new Map());
|
|
55
|
+
const definitionsRef = useRef(definitions);
|
|
56
|
+
const sequenceStringsRef = useRef([]);
|
|
57
|
+
const commonOptionsRef = useRef(commonOptions);
|
|
58
|
+
const defaultOptionsRef = useRef(defaultOptions);
|
|
59
|
+
const managerRef = useRef(manager);
|
|
60
|
+
const sequenceStrings = definitions.map((def) => formatHotkeySequence(def.sequence));
|
|
61
|
+
definitionsRef.current = definitions;
|
|
62
|
+
sequenceStringsRef.current = sequenceStrings;
|
|
63
|
+
commonOptionsRef.current = commonOptions;
|
|
64
|
+
defaultOptionsRef.current = defaultOptions;
|
|
65
|
+
managerRef.current = manager;
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
const prevRegistrations = registrationsRef.current;
|
|
68
|
+
const nextRegistrations = /* @__PURE__ */ new Map();
|
|
69
|
+
const rows = [];
|
|
70
|
+
for (let i = 0; i < definitionsRef.current.length; i++) {
|
|
71
|
+
const def = definitionsRef.current[i];
|
|
72
|
+
const seqStr = sequenceStringsRef.current[i];
|
|
73
|
+
const seq = def.sequence;
|
|
74
|
+
if (seq.length === 0) continue;
|
|
75
|
+
const mergedOptions = {
|
|
76
|
+
...defaultOptionsRef.current,
|
|
77
|
+
...commonOptionsRef.current,
|
|
78
|
+
...def.options
|
|
79
|
+
};
|
|
80
|
+
const resolvedTarget = isRef(mergedOptions.target) ? mergedOptions.target.current : mergedOptions.target ?? (typeof document !== "undefined" ? document : null);
|
|
81
|
+
if (!resolvedTarget) continue;
|
|
82
|
+
const registrationKey = `${i}:${seqStr}`;
|
|
83
|
+
rows.push({
|
|
84
|
+
registrationKey,
|
|
85
|
+
def,
|
|
86
|
+
seq,
|
|
87
|
+
seqStr,
|
|
88
|
+
mergedOptions,
|
|
89
|
+
resolvedTarget
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const nextKeys = new Set(rows.map((r) => r.registrationKey));
|
|
93
|
+
for (const [key, record] of prevRegistrations) if (!nextKeys.has(key) && record.handle.isActive) record.handle.unregister();
|
|
94
|
+
for (const row of rows) {
|
|
95
|
+
const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row;
|
|
96
|
+
const existing = prevRegistrations.get(registrationKey);
|
|
97
|
+
if (existing?.handle.isActive && existing.target === resolvedTarget) {
|
|
98
|
+
nextRegistrations.set(registrationKey, existing);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (existing?.handle.isActive) existing.handle.unregister();
|
|
102
|
+
const handle = managerRef.current.register(seq, def.callback, {
|
|
103
|
+
...mergedOptions,
|
|
104
|
+
target: resolvedTarget
|
|
105
|
+
});
|
|
106
|
+
nextRegistrations.set(registrationKey, {
|
|
107
|
+
handle,
|
|
108
|
+
target: resolvedTarget
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
registrationsRef.current = nextRegistrations;
|
|
112
|
+
});
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
return () => {
|
|
115
|
+
for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
|
|
116
|
+
registrationsRef.current = /* @__PURE__ */ new Map();
|
|
117
|
+
};
|
|
118
|
+
}, []);
|
|
119
|
+
for (let i = 0; i < definitions.length; i++) {
|
|
120
|
+
const def = definitions[i];
|
|
121
|
+
const seqStr = sequenceStrings[i];
|
|
122
|
+
const registrationKey = `${i}:${seqStr}`;
|
|
123
|
+
const handle = registrationsRef.current.get(registrationKey)?.handle;
|
|
124
|
+
if (handle?.isActive && def.sequence.length > 0) {
|
|
125
|
+
handle.callback = def.callback;
|
|
126
|
+
const { target: _target, ...optionsWithoutTarget } = {
|
|
127
|
+
...defaultOptions,
|
|
128
|
+
...commonOptions,
|
|
129
|
+
...def.options
|
|
130
|
+
};
|
|
131
|
+
handle.setOptions(optionsWithoutTarget);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
//#endregion
|
|
137
|
+
export { useHotkeySequences };
|
|
138
|
+
//# sourceMappingURL=useHotkeySequences.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHotkeySequences.js","names":[],"sources":["../src/useHotkeySequences.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeySequenceOptions } from './useHotkeySequence'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\n/**\n * A single sequence definition for use with `useHotkeySequences`.\n */\nexport interface UseHotkeySequenceDefinition {\n /** Array of hotkey strings that form the sequence */\n sequence: HotkeySequence\n /** The function to call when the sequence is completed */\n callback: HotkeyCallback\n /** Per-sequence options (merged on top of commonOptions) */\n options?: UseHotkeySequenceOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard shortcut sequences at once (Vim-style).\n *\n * Uses the singleton SequenceManager. Accepts a dynamic array of definitions so you can\n * register variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * Definitions with an empty `sequence` are skipped (no registration).\n *\n * @param definitions - Array of sequence definitions to register\n * @param commonOptions - Shared options applied to all sequences (overridden by per-definition options).\n * Per-row `enabled: false` still registers that sequence: `SequenceManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function VimPalette() {\n * useHotkeySequences([\n * { sequence: ['G', 'G'], callback: () => scrollToTop() },\n * { sequence: ['D', 'D'], callback: () => deleteLine() },\n * { sequence: ['C', 'I', 'W'], callback: () => changeInnerWord(), options: { timeout: 500 } },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function DynamicSequences({ items }) {\n * useHotkeySequences(\n * items.map((item) => ({\n * sequence: item.chords,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeySequences(\n definitions: Array<UseHotkeySequenceDefinition>,\n commonOptions: UseHotkeySequenceOptions = {},\n): void {\n type RegistrationRecord = {\n handle: SequenceRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkeySequence\n const manager = getSequenceManager()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const definitionsRef = useRef(definitions)\n const sequenceStringsRef = useRef<Array<string>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const sequenceStrings = definitions.map((def) =>\n formatHotkeySequence(def.sequence),\n )\n\n definitionsRef.current = definitions\n sequenceStringsRef.current = sequenceStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof definitionsRef.current)[number]\n seq: HotkeySequence\n seqStr: string\n mergedOptions: UseHotkeySequenceOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < definitionsRef.current.length; i++) {\n const def = definitionsRef.current[i]!\n const seqStr = sequenceStringsRef.current[i]!\n const seq = def.sequence\n if (seq.length === 0) {\n continue\n }\n\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeySequenceOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${seqStr}`\n rows.push({\n registrationKey,\n def,\n seq,\n seqStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, seq, mergedOptions, resolvedTarget } = row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(seq, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n\n for (let i = 0; i < definitions.length; i++) {\n const def = definitions[i]!\n const seqStr = sequenceStrings[i]!\n const registrationKey = `${i}:${seqStr}`\n const handle = registrationsRef.current.get(registrationKey)?.handle\n\n if (handle?.isActive && def.sequence.length > 0) {\n handle.callback = def.callback\n const mergedOptions = {\n ...defaultOptions,\n ...commonOptions,\n ...def.options,\n } as UseHotkeySequenceOptions\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n handle.setOptions(optionsWithoutTarget)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,KAAK,eAAe,mBAAmB;GAErE,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,KAAK,IAAI,UAAU;IAC5D,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,YAAY,QAAQ,KAAK;EAC3C,MAAM,MAAM,YAAY;EACxB,MAAM,SAAS,gBAAgB;EAC/B,MAAM,kBAAkB,GAAG,EAAE,GAAG;EAChC,MAAM,SAAS,iBAAiB,QAAQ,IAAI,gBAAgB,EAAE;AAE9D,MAAI,QAAQ,YAAY,IAAI,SAAS,SAAS,GAAG;AAC/C,UAAO,WAAW,IAAI;GAMtB,MAAM,EAAE,QAAQ,SAAS,GAAG,yBALN;IACpB,GAAG;IACH,GAAG;IACH,GAAG,IAAI;IACR;AAED,UAAO,WAAW,qBAAqB"}
|
package/dist/useHotkeys.cjs
CHANGED
|
@@ -17,7 +17,10 @@ let preact_hooks = require("preact/hooks");
|
|
|
17
17
|
* Callbacks and options are synced on every render to avoid stale closures.
|
|
18
18
|
*
|
|
19
19
|
* @param hotkeys - Array of hotkey definitions to register
|
|
20
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
|
|
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).
|
|
21
24
|
*
|
|
22
25
|
* @example
|
|
23
26
|
* ```tsx
|
|
@@ -104,17 +107,13 @@ function useHotkeys(hotkeys, commonOptions = {}) {
|
|
|
104
107
|
});
|
|
105
108
|
}
|
|
106
109
|
registrationsRef.current = nextRegistrations;
|
|
110
|
+
});
|
|
111
|
+
(0, preact_hooks.useEffect)(() => {
|
|
107
112
|
return () => {
|
|
108
113
|
for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
|
|
109
114
|
registrationsRef.current = /* @__PURE__ */ new Map();
|
|
110
115
|
};
|
|
111
|
-
}, [
|
|
112
|
-
return {
|
|
113
|
-
...defaultOptions,
|
|
114
|
-
...commonOptions,
|
|
115
|
-
...def.options
|
|
116
|
-
}.enabled ?? true;
|
|
117
|
-
}).join("\0")]);
|
|
116
|
+
}, []);
|
|
118
117
|
for (let i = 0; i < hotkeys.length; i++) {
|
|
119
118
|
const def = hotkeys[i];
|
|
120
119
|
const hotkeyStr = hotkeyStrings[i];
|
package/dist/useHotkeys.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeys.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { 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 *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n
|
|
1
|
+
{"version":3,"file":"useHotkeys.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { 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 typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n 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 Use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26
|
* Callbacks and options are synced on every render to avoid stale closures.
|
|
27
27
|
*
|
|
28
28
|
* @param hotkeys - Array of hotkey definitions to register
|
|
29
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
|
|
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).
|
|
30
33
|
*
|
|
31
34
|
* @example
|
|
32
35
|
* ```tsx
|
package/dist/useHotkeys.d.ts
CHANGED
|
@@ -26,7 +26,10 @@ interface UseHotkeyDefinition {
|
|
|
26
26
|
* Callbacks and options are synced on every render to avoid stale closures.
|
|
27
27
|
*
|
|
28
28
|
* @param hotkeys - Array of hotkey definitions to register
|
|
29
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
|
|
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).
|
|
30
33
|
*
|
|
31
34
|
* @example
|
|
32
35
|
* ```tsx
|
package/dist/useHotkeys.js
CHANGED
|
@@ -17,7 +17,10 @@ import { useEffect, useRef } from "preact/hooks";
|
|
|
17
17
|
* Callbacks and options are synced on every render to avoid stale closures.
|
|
18
18
|
*
|
|
19
19
|
* @param hotkeys - Array of hotkey definitions to register
|
|
20
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options)
|
|
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).
|
|
21
24
|
*
|
|
22
25
|
* @example
|
|
23
26
|
* ```tsx
|
|
@@ -104,17 +107,13 @@ function useHotkeys(hotkeys, commonOptions = {}) {
|
|
|
104
107
|
});
|
|
105
108
|
}
|
|
106
109
|
registrationsRef.current = nextRegistrations;
|
|
110
|
+
});
|
|
111
|
+
useEffect(() => {
|
|
107
112
|
return () => {
|
|
108
113
|
for (const { handle } of registrationsRef.current.values()) if (handle.isActive) handle.unregister();
|
|
109
114
|
registrationsRef.current = /* @__PURE__ */ new Map();
|
|
110
115
|
};
|
|
111
|
-
}, [
|
|
112
|
-
return {
|
|
113
|
-
...defaultOptions,
|
|
114
|
-
...commonOptions,
|
|
115
|
-
...def.options
|
|
116
|
-
}.enabled ?? true;
|
|
117
|
-
}).join("\0")]);
|
|
116
|
+
}, []);
|
|
118
117
|
for (let i = 0; i < hotkeys.length; i++) {
|
|
119
118
|
const def = hotkeys[i];
|
|
120
119
|
const hotkeyStr = hotkeyStrings[i];
|
package/dist/useHotkeys.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { 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 *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n
|
|
1
|
+
{"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { 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 typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform)) as Hotkey),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,SAAgB,WACd,SACA,gBAAkC,EAAE,EAC9B;CAMN,MAAM,iBAAiB,0BAA0B,CAAC;CAClD,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B,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"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/useHotkey.ts
CHANGED
|
@@ -44,7 +44,8 @@ export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
|
|
|
44
44
|
*
|
|
45
45
|
* @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
|
|
46
46
|
* @param callback - The function to call when the hotkey is pressed
|
|
47
|
-
* @param options - Options for the hotkey behavior
|
|
47
|
+
* @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
|
|
48
|
+
* and only suppresses firing; the hook updates the existing handle instead of unregistering.
|
|
48
49
|
*
|
|
49
50
|
* @example
|
|
50
51
|
* ```tsx
|
|
@@ -136,6 +137,12 @@ export function useHotkey(
|
|
|
136
137
|
|
|
137
138
|
// Skip if no valid target (SSR or ref still null)
|
|
138
139
|
if (!resolvedTarget) {
|
|
140
|
+
if (registrationRef.current?.isActive) {
|
|
141
|
+
registrationRef.current.unregister()
|
|
142
|
+
registrationRef.current = null
|
|
143
|
+
}
|
|
144
|
+
prevTargetRef.current = null
|
|
145
|
+
prevHotkeyRef.current = null
|
|
139
146
|
return
|
|
140
147
|
}
|
|
141
148
|
|
|
@@ -175,7 +182,7 @@ export function useHotkey(
|
|
|
175
182
|
registrationRef.current = null
|
|
176
183
|
}
|
|
177
184
|
}
|
|
178
|
-
}, [hotkeyString
|
|
185
|
+
}, [hotkeyString])
|
|
179
186
|
|
|
180
187
|
// Sync callback and options on EVERY render (outside useEffect)
|
|
181
188
|
// This avoids stale closures - the callback always has access to latest state
|
package/src/useHotkeySequence.ts
CHANGED
|
@@ -41,7 +41,8 @@ export interface UseHotkeySequenceOptions extends Omit<
|
|
|
41
41
|
*
|
|
42
42
|
* @param sequence - Array of hotkey strings that form the sequence
|
|
43
43
|
* @param callback - Function to call when the sequence is completed
|
|
44
|
-
* @param options - Options for the sequence behavior
|
|
44
|
+
* @param options - Options for the sequence behavior. `enabled: false` keeps the registration (visible in devtools)
|
|
45
|
+
* and only suppresses firing; the hook updates the existing handle instead of unregistering.
|
|
45
46
|
*
|
|
46
47
|
* @example
|
|
47
48
|
* ```tsx
|
|
@@ -89,11 +90,13 @@ export function useHotkeySequence(
|
|
|
89
90
|
const callbackRef = useRef(callback)
|
|
90
91
|
const optionsRef = useRef(mergedOptions)
|
|
91
92
|
const managerRef = useRef(manager)
|
|
93
|
+
const sequenceRef = useRef(sequence)
|
|
92
94
|
|
|
93
95
|
// Update refs on every render
|
|
94
96
|
callbackRef.current = callback
|
|
95
97
|
optionsRef.current = mergedOptions
|
|
96
98
|
managerRef.current = manager
|
|
99
|
+
sequenceRef.current = sequence
|
|
97
100
|
|
|
98
101
|
// Track previous target and sequence to detect changes requiring re-registration
|
|
99
102
|
const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
|
|
@@ -106,7 +109,13 @@ export function useHotkeySequence(
|
|
|
106
109
|
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
107
110
|
|
|
108
111
|
useEffect(() => {
|
|
109
|
-
if (
|
|
112
|
+
if (sequenceRef.current.length === 0) {
|
|
113
|
+
if (registrationRef.current?.isActive) {
|
|
114
|
+
registrationRef.current.unregister()
|
|
115
|
+
registrationRef.current = null
|
|
116
|
+
}
|
|
117
|
+
prevTargetRef.current = null
|
|
118
|
+
prevSequenceRef.current = null
|
|
110
119
|
return
|
|
111
120
|
}
|
|
112
121
|
|
|
@@ -118,6 +127,12 @@ export function useHotkeySequence(
|
|
|
118
127
|
|
|
119
128
|
// Skip if no valid target (SSR or ref still null)
|
|
120
129
|
if (!resolvedTarget) {
|
|
130
|
+
if (registrationRef.current?.isActive) {
|
|
131
|
+
registrationRef.current.unregister()
|
|
132
|
+
registrationRef.current = null
|
|
133
|
+
}
|
|
134
|
+
prevTargetRef.current = null
|
|
135
|
+
prevSequenceRef.current = null
|
|
121
136
|
return
|
|
122
137
|
}
|
|
123
138
|
|
|
@@ -140,7 +155,7 @@ export function useHotkeySequence(
|
|
|
140
155
|
// Register if needed (no active registration)
|
|
141
156
|
if (!registrationRef.current || !registrationRef.current.isActive) {
|
|
142
157
|
registrationRef.current = managerRef.current.register(
|
|
143
|
-
|
|
158
|
+
sequenceRef.current,
|
|
144
159
|
(event, context) => callbackRef.current(event, context),
|
|
145
160
|
{
|
|
146
161
|
...optionsRef.current,
|
|
@@ -160,7 +175,7 @@ export function useHotkeySequence(
|
|
|
160
175
|
registrationRef.current = null
|
|
161
176
|
}
|
|
162
177
|
}
|
|
163
|
-
}, [hotkeySequenceString
|
|
178
|
+
}, [hotkeySequenceString])
|
|
164
179
|
|
|
165
180
|
// Sync callback and options on EVERY render (outside useEffect)
|
|
166
181
|
if (registrationRef.current?.isActive) {
|