@tanstack/solid-hotkeys 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ const require_HotkeysProvider = require('./HotkeysProvider.cjs');
2
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
3
+ let solid_js = require("solid-js");
4
+
5
+ //#region src/createHotkeys.ts
6
+ /**
7
+ * SolidJS primitive for registering multiple keyboard hotkeys at once.
8
+ *
9
+ * Uses the singleton HotkeyManager for efficient event handling.
10
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
11
+ * with variable-length lists.
12
+ *
13
+ * Options are merged in this order:
14
+ * HotkeysProvider defaults < commonOptions < per-definition options
15
+ *
16
+ * @param hotkeys - Array of hotkey definitions, or accessor returning them
17
+ * @param commonOptions - Shared options applied to all hotkeys, or accessor
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * function Editor() {
22
+ * createHotkeys([
23
+ * { hotkey: 'Mod+S', callback: () => save() },
24
+ * { hotkey: 'Mod+Z', callback: () => undo() },
25
+ * { hotkey: 'Escape', callback: () => close() },
26
+ * ])
27
+ * }
28
+ * ```
29
+ *
30
+ * @example
31
+ * ```tsx
32
+ * function MenuShortcuts(props) {
33
+ * createHotkeys(
34
+ * () => props.items.map((item) => ({
35
+ * hotkey: item.shortcut,
36
+ * callback: item.action,
37
+ * options: { enabled: item.enabled },
38
+ * })),
39
+ * { preventDefault: true },
40
+ * )
41
+ * }
42
+ * ```
43
+ */
44
+ function createHotkeys(hotkeys, commonOptions = {}) {
45
+ const defaultOptions = require_HotkeysProvider.useDefaultHotkeysOptions();
46
+ const manager = (0, _tanstack_hotkeys.getHotkeyManager)();
47
+ const registrations = /* @__PURE__ */ new Map();
48
+ (0, solid_js.onCleanup)(() => {
49
+ for (const { handle } of registrations.values()) if (handle.isActive) handle.unregister();
50
+ registrations.clear();
51
+ });
52
+ (0, solid_js.createEffect)(() => {
53
+ const resolvedHotkeys = typeof hotkeys === "function" ? hotkeys() : hotkeys;
54
+ const resolvedCommonOptions = typeof commonOptions === "function" ? commonOptions() : commonOptions;
55
+ const nextKeys = /* @__PURE__ */ new Set();
56
+ const prepared = [];
57
+ for (let i = 0; i < resolvedHotkeys.length; i++) {
58
+ const def = resolvedHotkeys[i];
59
+ const resolvedDefOptions = def.options ?? {};
60
+ const mergedOptions = {
61
+ ...defaultOptions.hotkey,
62
+ ...resolvedCommonOptions,
63
+ ...resolvedDefOptions
64
+ };
65
+ const platform = mergedOptions.platform ?? (0, _tanstack_hotkeys.detectPlatform)();
66
+ const hotkeyString = typeof def.hotkey === "string" ? def.hotkey : (0, _tanstack_hotkeys.formatHotkey)((0, _tanstack_hotkeys.rawHotkeyToParsedHotkey)(def.hotkey, platform));
67
+ const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
68
+ if (!resolvedTarget) continue;
69
+ const registrationKey = `${i}:${hotkeyString}`;
70
+ nextKeys.add(registrationKey);
71
+ prepared.push({
72
+ registrationKey,
73
+ def,
74
+ mergedOptions,
75
+ hotkeyString,
76
+ resolvedTarget
77
+ });
78
+ }
79
+ for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
80
+ if (record.handle.isActive) record.handle.unregister();
81
+ registrations.delete(key);
82
+ }
83
+ for (const p of prepared) {
84
+ const existing = registrations.get(p.registrationKey);
85
+ if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
86
+ existing.handle.callback = p.def.callback;
87
+ const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
88
+ existing.handle.setOptions(optionsWithoutTarget);
89
+ continue;
90
+ }
91
+ if (existing) {
92
+ if (existing.handle.isActive) existing.handle.unregister();
93
+ registrations.delete(p.registrationKey);
94
+ }
95
+ const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
96
+ const handle = manager.register(p.hotkeyString, p.def.callback, {
97
+ ...optionsWithoutTarget,
98
+ target: p.resolvedTarget
99
+ });
100
+ registrations.set(p.registrationKey, {
101
+ handle,
102
+ target: p.resolvedTarget
103
+ });
104
+ }
105
+ });
106
+ }
107
+
108
+ //#endregion
109
+ exports.createHotkeys = createHotkeys;
110
+ //# sourceMappingURL=createHotkeys.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createHotkeys.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeyOptions } from './createHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `createHotkeys`.\n */\nexport interface CreateHotkeyDefinition {\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?: CreateHotkeyOptions\n}\n\n/**\n * SolidJS primitive 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.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * @param hotkeys - Array of hotkey definitions, or accessor returning them\n * @param commonOptions - Shared options applied to all hotkeys, or accessor\n *\n * @example\n * ```tsx\n * function Editor() {\n * createHotkeys([\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(props) {\n * createHotkeys(\n * () => props.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 createHotkeys(\n hotkeys:\n | Array<CreateHotkeyDefinition>\n | (() => Array<CreateHotkeyDefinition>),\n commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getHotkeyManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n // Clean up only when this component/scope disposes — not before every effect\n // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive\n // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and\n // re-registered them, flooding the manager store and could freeze the tab.\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeyDefinition\n mergedOptions: CreateHotkeyOptions\n hotkeyString: Hotkey\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolvedHotkeys.length; i++) {\n const def = resolvedHotkeys[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkey,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeyOptions\n\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(\n rawHotkeyToParsedHotkey(def.hotkey, platform),\n ) as Hotkey)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n hotkeyString,\n resolvedTarget,\n })\n }\n\n // Drop stale keys first so list index shifts don't briefly double-register\n // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.hotkeyString, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,SAAgB,cACd,SAGA,gBAAmE,EAAE,EAC/D;CAMN,MAAM,iBAAiBA,kDAA0B;CACjD,MAAM,mDAA4B;CAElC,MAAM,gCAAgB,IAAI,KAAiC;AAM3D,+BAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,kCAAmB;EACjB,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EACpE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,gBA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QAAQ,EAAE;IACX,CAAC;;GAEJ"}
@@ -0,0 +1,57 @@
1
+ import { CreateHotkeyOptions } from "./createHotkey.cjs";
2
+ import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
3
+
4
+ //#region src/createHotkeys.d.ts
5
+ /**
6
+ * A single hotkey definition for use with `createHotkeys`.
7
+ */
8
+ interface CreateHotkeyDefinition {
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?: CreateHotkeyOptions;
15
+ }
16
+ /**
17
+ * SolidJS primitive 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.
22
+ *
23
+ * Options are merged in this order:
24
+ * HotkeysProvider defaults < commonOptions < per-definition options
25
+ *
26
+ * @param hotkeys - Array of hotkey definitions, or accessor returning them
27
+ * @param commonOptions - Shared options applied to all hotkeys, or accessor
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * function Editor() {
32
+ * createHotkeys([
33
+ * { hotkey: 'Mod+S', callback: () => save() },
34
+ * { hotkey: 'Mod+Z', callback: () => undo() },
35
+ * { hotkey: 'Escape', callback: () => close() },
36
+ * ])
37
+ * }
38
+ * ```
39
+ *
40
+ * @example
41
+ * ```tsx
42
+ * function MenuShortcuts(props) {
43
+ * createHotkeys(
44
+ * () => props.items.map((item) => ({
45
+ * hotkey: item.shortcut,
46
+ * callback: item.action,
47
+ * options: { enabled: item.enabled },
48
+ * })),
49
+ * { preventDefault: true },
50
+ * )
51
+ * }
52
+ * ```
53
+ */
54
+ declare function createHotkeys(hotkeys: Array<CreateHotkeyDefinition> | (() => Array<CreateHotkeyDefinition>), commonOptions?: CreateHotkeyOptions | (() => CreateHotkeyOptions)): void;
55
+ //#endregion
56
+ export { CreateHotkeyDefinition, createHotkeys };
57
+ //# sourceMappingURL=createHotkeys.d.cts.map
@@ -0,0 +1,57 @@
1
+ import { CreateHotkeyOptions } from "./createHotkey.js";
2
+ import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
3
+
4
+ //#region src/createHotkeys.d.ts
5
+ /**
6
+ * A single hotkey definition for use with `createHotkeys`.
7
+ */
8
+ interface CreateHotkeyDefinition {
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?: CreateHotkeyOptions;
15
+ }
16
+ /**
17
+ * SolidJS primitive 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.
22
+ *
23
+ * Options are merged in this order:
24
+ * HotkeysProvider defaults < commonOptions < per-definition options
25
+ *
26
+ * @param hotkeys - Array of hotkey definitions, or accessor returning them
27
+ * @param commonOptions - Shared options applied to all hotkeys, or accessor
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * function Editor() {
32
+ * createHotkeys([
33
+ * { hotkey: 'Mod+S', callback: () => save() },
34
+ * { hotkey: 'Mod+Z', callback: () => undo() },
35
+ * { hotkey: 'Escape', callback: () => close() },
36
+ * ])
37
+ * }
38
+ * ```
39
+ *
40
+ * @example
41
+ * ```tsx
42
+ * function MenuShortcuts(props) {
43
+ * createHotkeys(
44
+ * () => props.items.map((item) => ({
45
+ * hotkey: item.shortcut,
46
+ * callback: item.action,
47
+ * options: { enabled: item.enabled },
48
+ * })),
49
+ * { preventDefault: true },
50
+ * )
51
+ * }
52
+ * ```
53
+ */
54
+ declare function createHotkeys(hotkeys: Array<CreateHotkeyDefinition> | (() => Array<CreateHotkeyDefinition>), commonOptions?: CreateHotkeyOptions | (() => CreateHotkeyOptions)): void;
55
+ //#endregion
56
+ export { CreateHotkeyDefinition, createHotkeys };
57
+ //# sourceMappingURL=createHotkeys.d.ts.map
@@ -0,0 +1,110 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey } from "@tanstack/hotkeys";
3
+ import { createEffect, onCleanup } from "solid-js";
4
+
5
+ //#region src/createHotkeys.ts
6
+ /**
7
+ * SolidJS primitive for registering multiple keyboard hotkeys at once.
8
+ *
9
+ * Uses the singleton HotkeyManager for efficient event handling.
10
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
11
+ * with variable-length lists.
12
+ *
13
+ * Options are merged in this order:
14
+ * HotkeysProvider defaults < commonOptions < per-definition options
15
+ *
16
+ * @param hotkeys - Array of hotkey definitions, or accessor returning them
17
+ * @param commonOptions - Shared options applied to all hotkeys, or accessor
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * function Editor() {
22
+ * createHotkeys([
23
+ * { hotkey: 'Mod+S', callback: () => save() },
24
+ * { hotkey: 'Mod+Z', callback: () => undo() },
25
+ * { hotkey: 'Escape', callback: () => close() },
26
+ * ])
27
+ * }
28
+ * ```
29
+ *
30
+ * @example
31
+ * ```tsx
32
+ * function MenuShortcuts(props) {
33
+ * createHotkeys(
34
+ * () => props.items.map((item) => ({
35
+ * hotkey: item.shortcut,
36
+ * callback: item.action,
37
+ * options: { enabled: item.enabled },
38
+ * })),
39
+ * { preventDefault: true },
40
+ * )
41
+ * }
42
+ * ```
43
+ */
44
+ function createHotkeys(hotkeys, commonOptions = {}) {
45
+ const defaultOptions = useDefaultHotkeysOptions();
46
+ const manager = getHotkeyManager();
47
+ const registrations = /* @__PURE__ */ new Map();
48
+ onCleanup(() => {
49
+ for (const { handle } of registrations.values()) if (handle.isActive) handle.unregister();
50
+ registrations.clear();
51
+ });
52
+ createEffect(() => {
53
+ const resolvedHotkeys = typeof hotkeys === "function" ? hotkeys() : hotkeys;
54
+ const resolvedCommonOptions = typeof commonOptions === "function" ? commonOptions() : commonOptions;
55
+ const nextKeys = /* @__PURE__ */ new Set();
56
+ const prepared = [];
57
+ for (let i = 0; i < resolvedHotkeys.length; i++) {
58
+ const def = resolvedHotkeys[i];
59
+ const resolvedDefOptions = def.options ?? {};
60
+ const mergedOptions = {
61
+ ...defaultOptions.hotkey,
62
+ ...resolvedCommonOptions,
63
+ ...resolvedDefOptions
64
+ };
65
+ const platform = mergedOptions.platform ?? detectPlatform();
66
+ const hotkeyString = typeof def.hotkey === "string" ? def.hotkey : formatHotkey(rawHotkeyToParsedHotkey(def.hotkey, platform));
67
+ const resolvedTarget = "target" in mergedOptions ? mergedOptions.target ?? null : typeof document !== "undefined" ? document : null;
68
+ if (!resolvedTarget) continue;
69
+ const registrationKey = `${i}:${hotkeyString}`;
70
+ nextKeys.add(registrationKey);
71
+ prepared.push({
72
+ registrationKey,
73
+ def,
74
+ mergedOptions,
75
+ hotkeyString,
76
+ resolvedTarget
77
+ });
78
+ }
79
+ for (const [key, record] of [...registrations.entries()]) if (!nextKeys.has(key)) {
80
+ if (record.handle.isActive) record.handle.unregister();
81
+ registrations.delete(key);
82
+ }
83
+ for (const p of prepared) {
84
+ const existing = registrations.get(p.registrationKey);
85
+ if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
86
+ existing.handle.callback = p.def.callback;
87
+ const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
88
+ existing.handle.setOptions(optionsWithoutTarget);
89
+ continue;
90
+ }
91
+ if (existing) {
92
+ if (existing.handle.isActive) existing.handle.unregister();
93
+ registrations.delete(p.registrationKey);
94
+ }
95
+ const { target: _target, ...optionsWithoutTarget } = p.mergedOptions;
96
+ const handle = manager.register(p.hotkeyString, p.def.callback, {
97
+ ...optionsWithoutTarget,
98
+ target: p.resolvedTarget
99
+ });
100
+ registrations.set(p.registrationKey, {
101
+ handle,
102
+ target: p.resolvedTarget
103
+ });
104
+ }
105
+ });
106
+ }
107
+
108
+ //#endregion
109
+ export { createHotkeys };
110
+ //# sourceMappingURL=createHotkeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createHotkeys.js","names":[],"sources":["../src/createHotkeys.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport {\n detectPlatform,\n formatHotkey,\n getHotkeyManager,\n rawHotkeyToParsedHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { CreateHotkeyOptions } from './createHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `createHotkeys`.\n */\nexport interface CreateHotkeyDefinition {\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?: CreateHotkeyOptions\n}\n\n/**\n * SolidJS primitive 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.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * @param hotkeys - Array of hotkey definitions, or accessor returning them\n * @param commonOptions - Shared options applied to all hotkeys, or accessor\n *\n * @example\n * ```tsx\n * function Editor() {\n * createHotkeys([\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(props) {\n * createHotkeys(\n * () => props.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 createHotkeys(\n hotkeys:\n | Array<CreateHotkeyDefinition>\n | (() => Array<CreateHotkeyDefinition>),\n commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions()\n const manager = getHotkeyManager()\n\n const registrations = new Map<string, RegistrationRecord>()\n\n // Clean up only when this component/scope disposes — not before every effect\n // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive\n // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and\n // re-registered them, flooding the manager store and could freeze the tab.\n onCleanup(() => {\n for (const { handle } of registrations.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrations.clear()\n })\n\n createEffect(() => {\n const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys\n const resolvedCommonOptions =\n typeof commonOptions === 'function' ? commonOptions() : commonOptions\n\n const nextKeys = new Set<string>()\n const prepared: Array<{\n registrationKey: string\n def: CreateHotkeyDefinition\n mergedOptions: CreateHotkeyOptions\n hotkeyString: Hotkey\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < resolvedHotkeys.length; i++) {\n const def = resolvedHotkeys[i]!\n const resolvedDefOptions = def.options ?? {}\n\n const mergedOptions = {\n ...defaultOptions.hotkey,\n ...resolvedCommonOptions,\n ...resolvedDefOptions,\n } as CreateHotkeyOptions\n\n const platform = mergedOptions.platform ?? detectPlatform()\n const hotkeyString: Hotkey =\n typeof def.hotkey === 'string'\n ? def.hotkey\n : (formatHotkey(\n rawHotkeyToParsedHotkey(def.hotkey, platform),\n ) as Hotkey)\n\n const resolvedTarget =\n 'target' in mergedOptions\n ? (mergedOptions.target ?? null)\n : typeof document !== 'undefined'\n ? document\n : null\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyString}`\n nextKeys.add(registrationKey)\n prepared.push({\n registrationKey,\n def,\n mergedOptions,\n hotkeyString,\n resolvedTarget,\n })\n }\n\n // Drop stale keys first so list index shifts don't briefly double-register\n // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).\n for (const [key, record] of [...registrations.entries()]) {\n if (!nextKeys.has(key)) {\n if (record.handle.isActive) {\n record.handle.unregister()\n }\n registrations.delete(key)\n }\n }\n\n for (const p of prepared) {\n const existing = registrations.get(p.registrationKey)\n if (existing?.handle.isActive && existing.target === p.resolvedTarget) {\n existing.handle.callback = p.def.callback\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n continue\n }\n\n if (existing) {\n if (existing.handle.isActive) {\n existing.handle.unregister()\n }\n registrations.delete(p.registrationKey)\n }\n\n const { target: _target, ...optionsWithoutTarget } = p.mergedOptions\n const handle = manager.register(p.hotkeyString, p.def.callback, {\n ...optionsWithoutTarget,\n target: p.resolvedTarget,\n })\n registrations.set(p.registrationKey, {\n handle,\n target: p.resolvedTarget,\n })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,SAAgB,cACd,SAGA,gBAAmE,EAAE,EAC/D;CAMN,MAAM,iBAAiB,0BAA0B;CACjD,MAAM,UAAU,kBAAkB;CAElC,MAAM,gCAAgB,IAAI,KAAiC;AAM3D,iBAAgB;AACd,OAAK,MAAM,EAAE,YAAY,cAAc,QAAQ,CAC7C,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,gBAAc,OAAO;GACrB;AAEF,oBAAmB;EACjB,MAAM,kBAAkB,OAAO,YAAY,aAAa,SAAS,GAAG;EACpE,MAAM,wBACJ,OAAO,kBAAkB,aAAa,eAAe,GAAG;EAE1D,MAAM,2BAAW,IAAI,KAAa;EAClC,MAAM,WAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;GAC/C,MAAM,MAAM,gBAAgB;GAC5B,MAAM,qBAAqB,IAAI,WAAW,EAAE;GAE5C,M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  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
3
  const require_createHotkey = require('./createHotkey.cjs');
4
+ const require_createHotkeys = require('./createHotkeys.cjs');
4
5
  const require_createHeldKeys = require('./createHeldKeys.cjs');
5
6
  const require_createHeldKeyCodes = require('./createHeldKeyCodes.cjs');
6
7
  const require_createKeyHold = require('./createKeyHold.cjs');
@@ -13,6 +14,7 @@ exports.createHeldKeys = require_createHeldKeys.createHeldKeys;
13
14
  exports.createHotkey = require_createHotkey.createHotkey;
14
15
  exports.createHotkeyRecorder = require_createHotkeyRecorder.createHotkeyRecorder;
15
16
  exports.createHotkeySequence = require_createHotkeySequence.createHotkeySequence;
17
+ exports.createHotkeys = require_createHotkeys.createHotkeys;
16
18
  exports.createKeyHold = require_createKeyHold.createKeyHold;
17
19
  exports.useDefaultHotkeysOptions = require_HotkeysProvider.useDefaultHotkeysOptions;
18
20
  exports.useHotkeysContext = require_HotkeysProvider.useHotkeysContext;
package/dist/index.d.cts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { CreateHotkeyOptions, createHotkey } from "./createHotkey.cjs";
2
2
  import { CreateHotkeySequenceOptions, createHotkeySequence } from "./createHotkeySequence.cjs";
3
3
  import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.cjs";
4
+ import { CreateHotkeyDefinition, createHotkeys } from "./createHotkeys.cjs";
4
5
  import { createHeldKeys } from "./createHeldKeys.cjs";
5
6
  import { createHeldKeyCodes } from "./createHeldKeyCodes.cjs";
6
7
  import { createKeyHold } from "./createKeyHold.cjs";
7
8
  import { SolidHotkeyRecorder, createHotkeyRecorder } from "./createHotkeyRecorder.cjs";
8
9
  export * from "@tanstack/hotkeys";
9
- export { CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
10
+ export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { CreateHotkeyOptions, createHotkey } from "./createHotkey.js";
2
2
  import { CreateHotkeySequenceOptions, createHotkeySequence } from "./createHotkeySequence.js";
3
3
  import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.js";
4
+ import { CreateHotkeyDefinition, createHotkeys } from "./createHotkeys.js";
4
5
  import { createHeldKeys } from "./createHeldKeys.js";
5
6
  import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
6
7
  import { createKeyHold } from "./createKeyHold.js";
7
8
  import { SolidHotkeyRecorder, createHotkeyRecorder } from "./createHotkeyRecorder.js";
8
9
  export * from "@tanstack/hotkeys";
9
- export { CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
10
+ export { CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeySequenceOptions, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { HotkeysProvider, useDefaultHotkeysOptions, useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { createHotkey } from "./createHotkey.js";
3
+ import { createHotkeys } from "./createHotkeys.js";
3
4
  import { createHeldKeys } from "./createHeldKeys.js";
4
5
  import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
5
6
  import { createKeyHold } from "./createKeyHold.js";
@@ -8,4 +9,4 @@ import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
8
9
 
9
10
  export * from "@tanstack/hotkeys"
10
11
 
11
- export { HotkeysProvider, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
12
+ export { HotkeysProvider, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyRecorder, createHotkeySequence, createHotkeys, createKeyHold, useDefaultHotkeysOptions, useHotkeysContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/solid-hotkeys",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "SolidJS adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "@solidjs/testing-library": "^0.8.10",
50
50
  "@testing-library/jest-dom": "^6.9.1",
51
51
  "solid-js": "^1.9.11",
52
- "vite-plugin-solid": "^2.11.10"
52
+ "vite-plugin-solid": "^2.11.11"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "solid-js": ">=1.7.0"
@@ -0,0 +1,188 @@
1
+ import { createEffect, onCleanup } from 'solid-js'
2
+ import {
3
+ detectPlatform,
4
+ formatHotkey,
5
+ getHotkeyManager,
6
+ rawHotkeyToParsedHotkey,
7
+ } from '@tanstack/hotkeys'
8
+ import { useDefaultHotkeysOptions } from './HotkeysProvider'
9
+ import type { CreateHotkeyOptions } from './createHotkey'
10
+ import type {
11
+ Hotkey,
12
+ HotkeyCallback,
13
+ HotkeyRegistrationHandle,
14
+ RegisterableHotkey,
15
+ } from '@tanstack/hotkeys'
16
+
17
+ /**
18
+ * A single hotkey definition for use with `createHotkeys`.
19
+ */
20
+ export interface CreateHotkeyDefinition {
21
+ /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
22
+ hotkey: RegisterableHotkey
23
+ /** The function to call when the hotkey is pressed */
24
+ callback: HotkeyCallback
25
+ /** Per-hotkey options (merged on top of commonOptions) */
26
+ options?: CreateHotkeyOptions
27
+ }
28
+
29
+ /**
30
+ * SolidJS primitive for registering multiple keyboard hotkeys at once.
31
+ *
32
+ * Uses the singleton HotkeyManager for efficient event handling.
33
+ * Accepts a dynamic array of hotkey definitions, making it safe to use
34
+ * with variable-length lists.
35
+ *
36
+ * Options are merged in this order:
37
+ * HotkeysProvider defaults < commonOptions < per-definition options
38
+ *
39
+ * @param hotkeys - Array of hotkey definitions, or accessor returning them
40
+ * @param commonOptions - Shared options applied to all hotkeys, or accessor
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * function Editor() {
45
+ * createHotkeys([
46
+ * { hotkey: 'Mod+S', callback: () => save() },
47
+ * { hotkey: 'Mod+Z', callback: () => undo() },
48
+ * { hotkey: 'Escape', callback: () => close() },
49
+ * ])
50
+ * }
51
+ * ```
52
+ *
53
+ * @example
54
+ * ```tsx
55
+ * function MenuShortcuts(props) {
56
+ * createHotkeys(
57
+ * () => props.items.map((item) => ({
58
+ * hotkey: item.shortcut,
59
+ * callback: item.action,
60
+ * options: { enabled: item.enabled },
61
+ * })),
62
+ * { preventDefault: true },
63
+ * )
64
+ * }
65
+ * ```
66
+ */
67
+ export function createHotkeys(
68
+ hotkeys:
69
+ | Array<CreateHotkeyDefinition>
70
+ | (() => Array<CreateHotkeyDefinition>),
71
+ commonOptions: CreateHotkeyOptions | (() => CreateHotkeyOptions) = {},
72
+ ): void {
73
+ type RegistrationRecord = {
74
+ handle: HotkeyRegistrationHandle
75
+ target: Document | HTMLElement | Window
76
+ }
77
+
78
+ const defaultOptions = useDefaultHotkeysOptions()
79
+ const manager = getHotkeyManager()
80
+
81
+ const registrations = new Map<string, RegistrationRecord>()
82
+
83
+ // Clean up only when this component/scope disposes — not before every effect
84
+ // re-run. An inner `onCleanup` inside `createEffect` runs whenever reactive
85
+ // deps change (e.g. dynamic hotkey list), which unregistered every hotkey and
86
+ // re-registered them, flooding the manager store and could freeze the tab.
87
+ onCleanup(() => {
88
+ for (const { handle } of registrations.values()) {
89
+ if (handle.isActive) {
90
+ handle.unregister()
91
+ }
92
+ }
93
+ registrations.clear()
94
+ })
95
+
96
+ createEffect(() => {
97
+ const resolvedHotkeys = typeof hotkeys === 'function' ? hotkeys() : hotkeys
98
+ const resolvedCommonOptions =
99
+ typeof commonOptions === 'function' ? commonOptions() : commonOptions
100
+
101
+ const nextKeys = new Set<string>()
102
+ const prepared: Array<{
103
+ registrationKey: string
104
+ def: CreateHotkeyDefinition
105
+ mergedOptions: CreateHotkeyOptions
106
+ hotkeyString: Hotkey
107
+ resolvedTarget: Document | HTMLElement | Window
108
+ }> = []
109
+
110
+ for (let i = 0; i < resolvedHotkeys.length; i++) {
111
+ const def = resolvedHotkeys[i]!
112
+ const resolvedDefOptions = def.options ?? {}
113
+
114
+ const mergedOptions = {
115
+ ...defaultOptions.hotkey,
116
+ ...resolvedCommonOptions,
117
+ ...resolvedDefOptions,
118
+ } as CreateHotkeyOptions
119
+
120
+ const platform = mergedOptions.platform ?? detectPlatform()
121
+ const hotkeyString: Hotkey =
122
+ typeof def.hotkey === 'string'
123
+ ? def.hotkey
124
+ : (formatHotkey(
125
+ rawHotkeyToParsedHotkey(def.hotkey, platform),
126
+ ) as Hotkey)
127
+
128
+ const resolvedTarget =
129
+ 'target' in mergedOptions
130
+ ? (mergedOptions.target ?? null)
131
+ : typeof document !== 'undefined'
132
+ ? document
133
+ : null
134
+
135
+ if (!resolvedTarget) {
136
+ continue
137
+ }
138
+
139
+ const registrationKey = `${i}:${hotkeyString}`
140
+ nextKeys.add(registrationKey)
141
+ prepared.push({
142
+ registrationKey,
143
+ def,
144
+ mergedOptions,
145
+ hotkeyString,
146
+ resolvedTarget,
147
+ })
148
+ }
149
+
150
+ // Drop stale keys first so list index shifts don't briefly double-register
151
+ // the same hotkey+target (conflictBehavior: 'warn' + devtools can loop).
152
+ for (const [key, record] of [...registrations.entries()]) {
153
+ if (!nextKeys.has(key)) {
154
+ if (record.handle.isActive) {
155
+ record.handle.unregister()
156
+ }
157
+ registrations.delete(key)
158
+ }
159
+ }
160
+
161
+ for (const p of prepared) {
162
+ const existing = registrations.get(p.registrationKey)
163
+ if (existing?.handle.isActive && existing.target === p.resolvedTarget) {
164
+ existing.handle.callback = p.def.callback
165
+ const { target: _target, ...optionsWithoutTarget } = p.mergedOptions
166
+ existing.handle.setOptions(optionsWithoutTarget)
167
+ continue
168
+ }
169
+
170
+ if (existing) {
171
+ if (existing.handle.isActive) {
172
+ existing.handle.unregister()
173
+ }
174
+ registrations.delete(p.registrationKey)
175
+ }
176
+
177
+ const { target: _target, ...optionsWithoutTarget } = p.mergedOptions
178
+ const handle = manager.register(p.hotkeyString, p.def.callback, {
179
+ ...optionsWithoutTarget,
180
+ target: p.resolvedTarget,
181
+ })
182
+ registrations.set(p.registrationKey, {
183
+ handle,
184
+ target: p.resolvedTarget,
185
+ })
186
+ }
187
+ })
188
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export * from './HotkeysProvider'
6
6
 
7
7
  // SolidJS-specific primitives
8
8
  export * from './createHotkey'
9
+ export * from './createHotkeys'
9
10
  export * from './createHeldKeys'
10
11
  export * from './createHeldKeyCodes'
11
12
  export * from './createKeyHold'