react-hotkeys-hook 5.3.1 → 5.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Johannes Klauss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,29 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hotkeys-hook",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"dev": "vite",
|
|
7
|
-
"build": "tsc && vite build",
|
|
8
|
-
"test": "vitest",
|
|
9
|
-
"prepublishOnly": "vitest run && npm run build"
|
|
10
|
-
},
|
|
3
|
+
"description": "React hook for handling keyboard shortcuts",
|
|
4
|
+
"version": "5.3.3",
|
|
5
|
+
"sideEffects": false,
|
|
11
6
|
"repository": {
|
|
12
7
|
"type": "git",
|
|
13
8
|
"url": "https://github.com/JohannesKlauss/react-keymap-hook.git"
|
|
14
9
|
},
|
|
15
10
|
"homepage": "https://react-hotkeys-hook.vercel.app/",
|
|
16
11
|
"author": "Johannes Klauss",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
".": {
|
|
21
|
-
"types": "./dist/index.d.ts",
|
|
22
|
-
"import": "./dist/index.js"
|
|
23
|
-
}
|
|
24
|
-
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "packages/react-hotkeys-hook/dist/index.js",
|
|
14
|
+
"types": "packages/react-hotkeys-hook/dist/index.d.ts",
|
|
25
15
|
"files": [
|
|
26
|
-
"dist"
|
|
16
|
+
"packages/react-hotkeys-hook/dist"
|
|
27
17
|
],
|
|
28
18
|
"keywords": [
|
|
29
19
|
"react",
|
|
@@ -38,24 +28,22 @@
|
|
|
38
28
|
"hotkeys"
|
|
39
29
|
],
|
|
40
30
|
"license": "MIT",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "npm run -w packages/react-hotkeys-hook build",
|
|
33
|
+
"build:documentation": "npm run -w packages/documentation build",
|
|
34
|
+
"test": "npm run -w packages/react-hotkeys-hook test",
|
|
35
|
+
"prepublishOnly": "npm run test && npm run lint && npm run build",
|
|
36
|
+
"format": "npx @biomejs/biome format --write packages",
|
|
37
|
+
"lint": "npx @biomejs/biome lint --write packages"
|
|
38
|
+
},
|
|
39
|
+
"workspaces": [
|
|
40
|
+
"packages/*"
|
|
41
|
+
],
|
|
41
42
|
"peerDependencies": {
|
|
42
43
|
"react": ">=16.8.0",
|
|
43
44
|
"react-dom": ">=16.8.0"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
|
-
"@
|
|
47
|
-
"@types/react": "19.2.14",
|
|
48
|
-
"@types/react-dom": "19.2.3",
|
|
49
|
-
"@vitejs/plugin-react": "5.2.0",
|
|
50
|
-
"@testing-library/jest-dom": "6.9.1",
|
|
51
|
-
"@testing-library/react": "16.3.2",
|
|
52
|
-
"@testing-library/user-event": "14.6.1",
|
|
53
|
-
"@types/node": "24.12.2",
|
|
54
|
-
"globals": "16.5.0",
|
|
55
|
-
"jsdom": "27.4.0",
|
|
56
|
-
"typescript": "5.9.3",
|
|
57
|
-
"vite": "7.3.2",
|
|
58
|
-
"vitest": "4.1.5",
|
|
59
|
-
"vite-plugin-dts": "4.5.4"
|
|
47
|
+
"@biomejs/biome": "2.5.0"
|
|
60
48
|
}
|
|
61
49
|
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { createContext as e, useCallback as t, useContext as n, useEffect as r, useLayoutEffect as i, useMemo as a, useRef as o, useState as s } from "react";
|
|
2
|
+
import { jsx as c } from "react/jsx-runtime";
|
|
3
|
+
//#region src/lib/parseHotkeys.ts
|
|
4
|
+
var l = [
|
|
5
|
+
"shift",
|
|
6
|
+
"alt",
|
|
7
|
+
"meta",
|
|
8
|
+
"mod",
|
|
9
|
+
"ctrl",
|
|
10
|
+
"control"
|
|
11
|
+
];
|
|
12
|
+
function u() {
|
|
13
|
+
return typeof navigator > "u" ? !1 : /mac/i.test(navigator.userAgent) && !/iphone|ipad|ipod/i.test(navigator.userAgent);
|
|
14
|
+
}
|
|
15
|
+
var d = {
|
|
16
|
+
esc: "escape",
|
|
17
|
+
return: "enter",
|
|
18
|
+
left: "arrowleft",
|
|
19
|
+
right: "arrowright",
|
|
20
|
+
up: "arrowup",
|
|
21
|
+
down: "arrowdown",
|
|
22
|
+
ShiftLeft: "shift",
|
|
23
|
+
ShiftRight: "shift",
|
|
24
|
+
AltLeft: "alt",
|
|
25
|
+
AltRight: "alt",
|
|
26
|
+
MetaLeft: "meta",
|
|
27
|
+
MetaRight: "meta",
|
|
28
|
+
OSLeft: "meta",
|
|
29
|
+
OSRight: "meta",
|
|
30
|
+
ControlLeft: "ctrl",
|
|
31
|
+
ControlRight: "ctrl"
|
|
32
|
+
};
|
|
33
|
+
function f(e) {
|
|
34
|
+
return (d[e.trim()] || e.trim()).toLowerCase().replace(/key|digit|numpad/, "");
|
|
35
|
+
}
|
|
36
|
+
function p(e) {
|
|
37
|
+
return l.includes(e);
|
|
38
|
+
}
|
|
39
|
+
function m(e, t = ",") {
|
|
40
|
+
return e.toLowerCase().split(t);
|
|
41
|
+
}
|
|
42
|
+
function h(e, t = "+", n = ">", r = !1, i, a) {
|
|
43
|
+
let o = [], s = !1;
|
|
44
|
+
e = e.trim(), e.includes(n) ? (s = !0, o = e.toLocaleLowerCase().split(n).map((e) => f(e))) : o = e.toLocaleLowerCase().split(t).map((e) => f(e));
|
|
45
|
+
let c = {
|
|
46
|
+
alt: o.includes("alt"),
|
|
47
|
+
ctrl: o.includes("ctrl") || o.includes("control"),
|
|
48
|
+
shift: o.includes("shift"),
|
|
49
|
+
meta: o.includes("meta"),
|
|
50
|
+
mod: o.includes("mod"),
|
|
51
|
+
useKey: r
|
|
52
|
+
}, u = o.filter((e) => !l.includes(e));
|
|
53
|
+
return {
|
|
54
|
+
...c,
|
|
55
|
+
keys: u,
|
|
56
|
+
description: i,
|
|
57
|
+
isSequence: s,
|
|
58
|
+
hotkey: e,
|
|
59
|
+
metadata: a
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/lib/isHotkeyPressed.ts
|
|
64
|
+
typeof document < "u" && (document.addEventListener("keydown", (e) => {
|
|
65
|
+
e.code !== void 0 && y([f(e.code)]);
|
|
66
|
+
}), document.addEventListener("keyup", (e) => {
|
|
67
|
+
e.code !== void 0 && b([f(e.code)]);
|
|
68
|
+
})), typeof window < "u" && (window.addEventListener("blur", () => {
|
|
69
|
+
g.clear();
|
|
70
|
+
}), window.addEventListener("focus", () => {
|
|
71
|
+
g.clear();
|
|
72
|
+
}), window.addEventListener("contextmenu", () => {
|
|
73
|
+
setTimeout(() => {
|
|
74
|
+
g.clear();
|
|
75
|
+
}, 0);
|
|
76
|
+
})), typeof document < "u" && document.addEventListener("visibilitychange", () => {
|
|
77
|
+
g.clear();
|
|
78
|
+
});
|
|
79
|
+
var g = /* @__PURE__ */ new Set();
|
|
80
|
+
function _(e) {
|
|
81
|
+
return Array.isArray(e);
|
|
82
|
+
}
|
|
83
|
+
function v(e, t = ",") {
|
|
84
|
+
return (_(e) ? e : e.split(t)).every((e) => g.has(e.trim().toLowerCase()));
|
|
85
|
+
}
|
|
86
|
+
function y(e) {
|
|
87
|
+
let t = Array.isArray(e) ? e : [e];
|
|
88
|
+
g.has("meta") && g.forEach((e) => {
|
|
89
|
+
p(e) || g.delete(e.toLowerCase());
|
|
90
|
+
}), t.forEach((e) => {
|
|
91
|
+
g.add(e.toLowerCase());
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function b(e) {
|
|
95
|
+
e === "meta" ? g.clear() : (Array.isArray(e) ? e : [e]).forEach((e) => {
|
|
96
|
+
g.delete(e.toLowerCase());
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/lib/validators.ts
|
|
101
|
+
function x(e, t, n) {
|
|
102
|
+
(typeof n == "function" && n(e, t) || n === !0) && e.preventDefault();
|
|
103
|
+
}
|
|
104
|
+
function S(e, t, n) {
|
|
105
|
+
return typeof n == "function" ? n(e, t) : n === !0 || n === void 0;
|
|
106
|
+
}
|
|
107
|
+
var C = [
|
|
108
|
+
"input",
|
|
109
|
+
"textarea",
|
|
110
|
+
"select",
|
|
111
|
+
"searchbox",
|
|
112
|
+
"slider",
|
|
113
|
+
"spinbutton",
|
|
114
|
+
"menuitem",
|
|
115
|
+
"menuitemcheckbox",
|
|
116
|
+
"menuitemradio",
|
|
117
|
+
"option",
|
|
118
|
+
"radio",
|
|
119
|
+
"textbox"
|
|
120
|
+
];
|
|
121
|
+
function w(e) {
|
|
122
|
+
return T(e, C);
|
|
123
|
+
}
|
|
124
|
+
function T(e, t = !1) {
|
|
125
|
+
let { target: n, composed: r } = e, i, a;
|
|
126
|
+
return E(n) && r ? (i = e.composedPath()[0] && e.composedPath()[0].tagName, a = e.composedPath()[0] && e.composedPath()[0].role) : (i = n && n.tagName, a = n && n.role), _(t) ? !!(i && t?.some((e) => e.toLowerCase() === i.toLowerCase() || e === a)) : !!(i && t && t);
|
|
127
|
+
}
|
|
128
|
+
function E(e) {
|
|
129
|
+
return !!e.tagName && !e.tagName.startsWith("-") && e.tagName.includes("-");
|
|
130
|
+
}
|
|
131
|
+
function D(e, t) {
|
|
132
|
+
return e.length === 0 && t ? !1 : t ? e.some((e) => t.includes(e)) || e.includes("*") : !0;
|
|
133
|
+
}
|
|
134
|
+
var O = (e, t, n = !1) => {
|
|
135
|
+
let { alt: r, meta: i, mod: a, shift: o, ctrl: s, keys: c, useKey: l } = t, { code: d, key: p, ctrlKey: m, metaKey: h, shiftKey: g, altKey: _ } = e, y = f(d), b = typeof e.getModifierState == "function" && e.getModifierState("AltGraph");
|
|
136
|
+
if (!l && !c?.includes(y) && ![
|
|
137
|
+
"ctrl",
|
|
138
|
+
"control",
|
|
139
|
+
"unknown",
|
|
140
|
+
"meta",
|
|
141
|
+
"alt",
|
|
142
|
+
"shift",
|
|
143
|
+
"os"
|
|
144
|
+
].includes(y)) return !1;
|
|
145
|
+
if (!n) {
|
|
146
|
+
if (r !== _ && y !== "alt" || o !== g && y !== "shift") return !1;
|
|
147
|
+
if (a) {
|
|
148
|
+
if (u() ? !h : !m) return !1;
|
|
149
|
+
} else if (i !== h && y !== "meta" && y !== "os" || s !== m && y !== "ctrl" && y !== "control") return !1;
|
|
150
|
+
if (b && !r && !s && !a && ![
|
|
151
|
+
"alt",
|
|
152
|
+
"ctrl",
|
|
153
|
+
"control"
|
|
154
|
+
].includes(y)) return !1;
|
|
155
|
+
}
|
|
156
|
+
return l && c?.length === 1 && c.includes(p.toLowerCase()) || c && c.length === 1 && c.includes(y) ? !0 : c && c.length > 0 ? c.includes(y) ? v(c) : !1 : !c || c.length === 0;
|
|
157
|
+
}, k = e(void 0), A = () => n(k);
|
|
158
|
+
function j({ addHotkey: e, removeHotkey: t, children: n }) {
|
|
159
|
+
return /* @__PURE__ */ c(k.Provider, {
|
|
160
|
+
value: {
|
|
161
|
+
addHotkey: e,
|
|
162
|
+
removeHotkey: t
|
|
163
|
+
},
|
|
164
|
+
children: n
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/lib/deepEqual.ts
|
|
169
|
+
function M(e, t) {
|
|
170
|
+
return e && t && typeof e == "object" && typeof t == "object" ? Object.keys(e).length === Object.keys(t).length && Object.keys(e).reduce((n, r) => n && M(e[r], t[r]), !0) : e === t;
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/lib/HotkeysProvider.tsx
|
|
174
|
+
var N = e({
|
|
175
|
+
hotkeys: [],
|
|
176
|
+
activeScopes: [],
|
|
177
|
+
toggleScope: () => {},
|
|
178
|
+
enableScope: () => {},
|
|
179
|
+
disableScope: () => {}
|
|
180
|
+
}), P = () => n(N), F = ({ initiallyActiveScopes: e = ["*"], children: n }) => {
|
|
181
|
+
let [r, i] = s(e), [a, o] = s([]), l = t((e) => {
|
|
182
|
+
i((t) => t.includes("*") ? [e] : Array.from(new Set([...t, e])));
|
|
183
|
+
}, []), u = t((e) => {
|
|
184
|
+
i((t) => t.filter((t) => t !== e));
|
|
185
|
+
}, []), d = t((e) => {
|
|
186
|
+
i((t) => t.includes(e) ? t.filter((t) => t !== e) : t.includes("*") ? [e] : Array.from(new Set([...t, e])));
|
|
187
|
+
}, []), f = t((e) => {
|
|
188
|
+
o((t) => [...t, e]);
|
|
189
|
+
}, []), p = t((e) => {
|
|
190
|
+
o((t) => t.filter((t) => !M(t, e)));
|
|
191
|
+
}, []);
|
|
192
|
+
return /* @__PURE__ */ c(N.Provider, {
|
|
193
|
+
value: {
|
|
194
|
+
activeScopes: r,
|
|
195
|
+
hotkeys: a,
|
|
196
|
+
enableScope: l,
|
|
197
|
+
disableScope: u,
|
|
198
|
+
toggleScope: d
|
|
199
|
+
},
|
|
200
|
+
children: /* @__PURE__ */ c(j, {
|
|
201
|
+
addHotkey: f,
|
|
202
|
+
removeHotkey: p,
|
|
203
|
+
children: n
|
|
204
|
+
})
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/lib/useDeepEqualMemo.ts
|
|
209
|
+
function I(e) {
|
|
210
|
+
let t = o(void 0);
|
|
211
|
+
return M(t.current, e) || (t.current = e), t.current;
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/lib/useHotkeys.ts
|
|
215
|
+
var L = (e) => {
|
|
216
|
+
e.stopPropagation(), e.preventDefault(), e.stopImmediatePropagation();
|
|
217
|
+
}, R = typeof window < "u" ? i : r;
|
|
218
|
+
function z(e) {
|
|
219
|
+
if (!e) return;
|
|
220
|
+
let { enabled: t, preventDefault: n, ignoreEventWhen: r, ...i } = e;
|
|
221
|
+
return typeof t == "function" ? i : {
|
|
222
|
+
...i,
|
|
223
|
+
enabled: t
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function B(e, n, r, i) {
|
|
227
|
+
let [a, c] = s(null), l = t((e) => (c(e), () => c(null)), []), u = o(!1), d = Array.isArray(r) ? Array.isArray(i) ? void 0 : i : r, g = _(e) ? e.join(d?.delimiter) : e, v = Array.isArray(r) ? r : Array.isArray(i) ? i : void 0, C = t(n, v ?? []), E = o(C);
|
|
228
|
+
v ? E.current = C : E.current = n;
|
|
229
|
+
let k = I(z(d)), j = o(d?.enabled);
|
|
230
|
+
j.current = d?.enabled;
|
|
231
|
+
let M = o(d?.preventDefault);
|
|
232
|
+
M.current = d?.preventDefault;
|
|
233
|
+
let N = o(d?.ignoreEventWhen);
|
|
234
|
+
N.current = d?.ignoreEventWhen;
|
|
235
|
+
let { activeScopes: F } = P(), B = A();
|
|
236
|
+
return R(() => {
|
|
237
|
+
if (j.current === !1 || !D(F, k?.scopes)) return;
|
|
238
|
+
let e = [], t, n = (n, r = !1) => {
|
|
239
|
+
if (!(w(n) && !T(n, k?.enableOnFormTags))) {
|
|
240
|
+
if (a !== null) {
|
|
241
|
+
let e = a.getRootNode();
|
|
242
|
+
if ((e instanceof Document || e instanceof ShadowRoot) && e.activeElement !== a && !a.contains(e.activeElement)) {
|
|
243
|
+
L(n);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
n.target?.isContentEditable && !k?.enableOnContentEditable || m(g, k?.delimiter).forEach((i) => {
|
|
248
|
+
if (i.includes(k?.splitKey ?? "+") && i.includes(k?.sequenceSplitKey ?? ">")) {
|
|
249
|
+
console.warn(`Hotkey ${i} contains both ${k?.splitKey ?? "+"} and ${k?.sequenceSplitKey ?? ">"} which is not supported.`);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
let a = h(i, k?.splitKey, k?.sequenceSplitKey, k?.useKey, k?.description, k?.metadata);
|
|
253
|
+
if (a.isSequence) {
|
|
254
|
+
t = setTimeout(() => {
|
|
255
|
+
e = [];
|
|
256
|
+
}, k?.sequenceTimeoutMs ?? 1e3);
|
|
257
|
+
let r = a.useKey ? n.key : f(n.code);
|
|
258
|
+
if (p(r.toLowerCase())) return;
|
|
259
|
+
if (e.push(r), r !== a.keys?.[e.length - 1]) {
|
|
260
|
+
e = [], t && clearTimeout(t);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
e.length === a.keys?.length && (E.current(n, a), t && clearTimeout(t), e = []);
|
|
264
|
+
} else if (O(n, a, k?.ignoreModifiers) || a.keys?.includes("*")) {
|
|
265
|
+
if (N.current?.(n) || r && u.current || (x(n, a, M.current), !S(n, a, j.current))) return;
|
|
266
|
+
E.current(n, a), r || (u.current = !0);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}, r = (e) => {
|
|
271
|
+
e.code !== void 0 && (y(f(e.code)), (k?.keydown === void 0 && k?.keyup !== !0 || k?.keydown) && n(e));
|
|
272
|
+
}, i = (e) => {
|
|
273
|
+
e.code !== void 0 && (b(f(e.code)), u.current = !1, k?.keyup && n(e, !0));
|
|
274
|
+
}, o = a || d?.document || document;
|
|
275
|
+
return o.addEventListener("keyup", i, d?.eventListenerOptions), o.addEventListener("keydown", r, d?.eventListenerOptions), B && m(g, k?.delimiter).forEach((e) => {
|
|
276
|
+
B.addHotkey(h(e, k?.splitKey, k?.sequenceSplitKey, k?.useKey, k?.description, k?.metadata));
|
|
277
|
+
}), () => {
|
|
278
|
+
o.removeEventListener("keyup", i, d?.eventListenerOptions), o.removeEventListener("keydown", r, d?.eventListenerOptions), B && m(g, k?.delimiter).forEach((e) => {
|
|
279
|
+
B.removeHotkey(h(e, k?.splitKey, k?.sequenceSplitKey, k?.useKey, k?.description, k?.metadata));
|
|
280
|
+
}), e = [], t && clearTimeout(t);
|
|
281
|
+
};
|
|
282
|
+
}, [
|
|
283
|
+
a,
|
|
284
|
+
k,
|
|
285
|
+
F,
|
|
286
|
+
g
|
|
287
|
+
]), l;
|
|
288
|
+
}
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/lib/useRecordHotkeys.ts
|
|
291
|
+
function V(e = !1, n = []) {
|
|
292
|
+
let [i, o] = s(/* @__PURE__ */ new Set()), [c, l] = s(!1), u = a(() => new Set(n.map((e) => e.toLowerCase())), [n]), d = t((t) => {
|
|
293
|
+
if (t.code === void 0) return;
|
|
294
|
+
let n = f(e ? t.key : t.code).toLowerCase();
|
|
295
|
+
u.has(n) || (t.preventDefault(), t.stopPropagation(), o((e) => {
|
|
296
|
+
let t = new Set(e);
|
|
297
|
+
return t.add(n), t;
|
|
298
|
+
}));
|
|
299
|
+
}, [e, u]), p = t(() => {
|
|
300
|
+
l(!1);
|
|
301
|
+
}, []), m = t(() => {
|
|
302
|
+
o(/* @__PURE__ */ new Set()), l(!0);
|
|
303
|
+
}, []), h = t(() => {
|
|
304
|
+
o(/* @__PURE__ */ new Set());
|
|
305
|
+
}, []);
|
|
306
|
+
return r(() => {
|
|
307
|
+
if (typeof document < "u" && c) return document.addEventListener("keydown", d), () => {
|
|
308
|
+
document.removeEventListener("keydown", d);
|
|
309
|
+
};
|
|
310
|
+
}, [c, d]), [i, {
|
|
311
|
+
start: m,
|
|
312
|
+
stop: p,
|
|
313
|
+
resetKeys: h,
|
|
314
|
+
isRecording: c
|
|
315
|
+
}];
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
export { F as HotkeysProvider, v as isHotkeyPressed, B as useHotkeys, P as useHotkeysContext, V as useRecordHotkeys };
|
package/dist/index.js
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
import { createContext as j, useContext as W, useState as A, useCallback as h, useRef as K, useLayoutEffect as X, useEffect as I, useMemo as Y } from "react";
|
|
2
|
-
import { jsx as x } from "react/jsx-runtime";
|
|
3
|
-
const F = ["shift", "alt", "meta", "mod", "ctrl", "control"];
|
|
4
|
-
function Z() {
|
|
5
|
-
return typeof navigator > "u" ? !1 : /mac/i.test(navigator.userAgent) && !/iphone|ipad|ipod/i.test(navigator.userAgent);
|
|
6
|
-
}
|
|
7
|
-
const ee = {
|
|
8
|
-
esc: "escape",
|
|
9
|
-
return: "enter",
|
|
10
|
-
left: "arrowleft",
|
|
11
|
-
right: "arrowright",
|
|
12
|
-
up: "arrowup",
|
|
13
|
-
down: "arrowdown",
|
|
14
|
-
ShiftLeft: "shift",
|
|
15
|
-
ShiftRight: "shift",
|
|
16
|
-
AltLeft: "alt",
|
|
17
|
-
AltRight: "alt",
|
|
18
|
-
MetaLeft: "meta",
|
|
19
|
-
MetaRight: "meta",
|
|
20
|
-
OSLeft: "meta",
|
|
21
|
-
OSRight: "meta",
|
|
22
|
-
ControlLeft: "ctrl",
|
|
23
|
-
ControlRight: "ctrl"
|
|
24
|
-
};
|
|
25
|
-
function L(e) {
|
|
26
|
-
return (ee[e.trim()] || e.trim()).toLowerCase().replace(/key|digit|numpad/, "");
|
|
27
|
-
}
|
|
28
|
-
function $(e) {
|
|
29
|
-
return F.includes(e);
|
|
30
|
-
}
|
|
31
|
-
function R(e, t = ",") {
|
|
32
|
-
return e.toLowerCase().split(t);
|
|
33
|
-
}
|
|
34
|
-
function O(e, t = "+", r = ">", u = !1, s, f) {
|
|
35
|
-
let d = [], l = !1;
|
|
36
|
-
e = e.trim(), e.includes(r) ? (l = !0, d = e.toLocaleLowerCase().split(r).map((y) => L(y))) : d = e.toLocaleLowerCase().split(t).map((y) => L(y));
|
|
37
|
-
const o = {
|
|
38
|
-
alt: d.includes("alt"),
|
|
39
|
-
ctrl: d.includes("ctrl") || d.includes("control"),
|
|
40
|
-
shift: d.includes("shift"),
|
|
41
|
-
meta: d.includes("meta"),
|
|
42
|
-
mod: d.includes("mod"),
|
|
43
|
-
useKey: u
|
|
44
|
-
}, p = d.filter((y) => !F.includes(y));
|
|
45
|
-
return {
|
|
46
|
-
...o,
|
|
47
|
-
keys: p,
|
|
48
|
-
description: s,
|
|
49
|
-
isSequence: l,
|
|
50
|
-
hotkey: e,
|
|
51
|
-
metadata: f
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
typeof document < "u" && (document.addEventListener("keydown", (e) => {
|
|
55
|
-
e.code !== void 0 && z([L(e.code)]);
|
|
56
|
-
}), document.addEventListener("keyup", (e) => {
|
|
57
|
-
e.code !== void 0 && G([L(e.code)]);
|
|
58
|
-
})), typeof window < "u" && (window.addEventListener("blur", () => {
|
|
59
|
-
g.clear();
|
|
60
|
-
}), window.addEventListener("focus", () => {
|
|
61
|
-
g.clear();
|
|
62
|
-
}), window.addEventListener("contextmenu", () => {
|
|
63
|
-
setTimeout(() => {
|
|
64
|
-
g.clear();
|
|
65
|
-
}, 0);
|
|
66
|
-
})), typeof document < "u" && document.addEventListener("visibilitychange", () => {
|
|
67
|
-
g.clear();
|
|
68
|
-
});
|
|
69
|
-
const g = /* @__PURE__ */ new Set();
|
|
70
|
-
function D(e) {
|
|
71
|
-
return Array.isArray(e);
|
|
72
|
-
}
|
|
73
|
-
function te(e, t = ",") {
|
|
74
|
-
return (D(e) ? e : e.split(t)).every((u) => g.has(u.trim().toLowerCase()));
|
|
75
|
-
}
|
|
76
|
-
function z(e) {
|
|
77
|
-
const t = Array.isArray(e) ? e : [e];
|
|
78
|
-
g.has("meta") && g.forEach((r) => {
|
|
79
|
-
$(r) || g.delete(r.toLowerCase());
|
|
80
|
-
}), t.forEach((r) => {
|
|
81
|
-
g.add(r.toLowerCase());
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
function G(e) {
|
|
85
|
-
const t = Array.isArray(e) ? e : [e];
|
|
86
|
-
e === "meta" ? g.clear() : t.forEach((r) => {
|
|
87
|
-
g.delete(r.toLowerCase());
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
function re(e, t, r) {
|
|
91
|
-
(typeof r == "function" && r(e, t) || r === !0) && e.preventDefault();
|
|
92
|
-
}
|
|
93
|
-
function ne(e, t, r) {
|
|
94
|
-
return typeof r == "function" ? r(e, t) : r === !0 || r === void 0;
|
|
95
|
-
}
|
|
96
|
-
const oe = [
|
|
97
|
-
"input",
|
|
98
|
-
"textarea",
|
|
99
|
-
"select",
|
|
100
|
-
"searchbox",
|
|
101
|
-
"slider",
|
|
102
|
-
"spinbutton",
|
|
103
|
-
"menuitem",
|
|
104
|
-
"menuitemcheckbox",
|
|
105
|
-
"menuitemradio",
|
|
106
|
-
"option",
|
|
107
|
-
"radio",
|
|
108
|
-
"textbox"
|
|
109
|
-
];
|
|
110
|
-
function ie(e) {
|
|
111
|
-
return J(e, oe);
|
|
112
|
-
}
|
|
113
|
-
function J(e, t = !1) {
|
|
114
|
-
const { target: r, composed: u } = e;
|
|
115
|
-
let s, f;
|
|
116
|
-
return se(r) && u ? (s = e.composedPath()[0] && e.composedPath()[0].tagName, f = e.composedPath()[0] && e.composedPath()[0].role) : (s = r && r.tagName, f = r && r.role), D(t) ? !!(s && t?.some((d) => d.toLowerCase() === s.toLowerCase() || d === f)) : !!(s && t && t);
|
|
117
|
-
}
|
|
118
|
-
function se(e) {
|
|
119
|
-
return !!e.tagName && !e.tagName.startsWith("-") && e.tagName.includes("-");
|
|
120
|
-
}
|
|
121
|
-
function ue(e, t) {
|
|
122
|
-
return e.length === 0 && t ? !1 : t ? e.some((r) => t.includes(r)) || e.includes("*") : !0;
|
|
123
|
-
}
|
|
124
|
-
const ce = (e, t, r = !1) => {
|
|
125
|
-
const { alt: u, meta: s, mod: f, shift: d, ctrl: l, keys: o, useKey: p } = t, { code: y, key: i, ctrlKey: c, metaKey: n, shiftKey: E, altKey: C } = e, m = L(y);
|
|
126
|
-
if (p && o?.length === 1 && o.includes(i.toLowerCase()))
|
|
127
|
-
return !0;
|
|
128
|
-
if (!o?.includes(m) && !["ctrl", "control", "unknown", "meta", "alt", "shift", "os"].includes(m))
|
|
129
|
-
return !1;
|
|
130
|
-
if (!r) {
|
|
131
|
-
if (u !== C && m !== "alt" || d !== E && m !== "shift")
|
|
132
|
-
return !1;
|
|
133
|
-
if (f) {
|
|
134
|
-
if (Z() ? !n : !c)
|
|
135
|
-
return !1;
|
|
136
|
-
} else if (s !== n && m !== "meta" && m !== "os" || l !== c && m !== "ctrl" && m !== "control")
|
|
137
|
-
return !1;
|
|
138
|
-
}
|
|
139
|
-
return o && o.length === 1 && o.includes(m) ? !0 : o && o.length > 0 ? o.includes(m) ? te(o) : !1 : !o || o.length === 0;
|
|
140
|
-
}, Q = j(void 0), ae = () => W(Q);
|
|
141
|
-
function de({ addHotkey: e, removeHotkey: t, children: r }) {
|
|
142
|
-
return /* @__PURE__ */ x(Q.Provider, { value: { addHotkey: e, removeHotkey: t }, children: r });
|
|
143
|
-
}
|
|
144
|
-
function M(e, t) {
|
|
145
|
-
return e && t && typeof e == "object" && typeof t == "object" ? Object.keys(e).length === Object.keys(t).length && // @ts-expect-error TS7053
|
|
146
|
-
Object.keys(e).reduce((r, u) => r && M(e[u], t[u]), !0) : e === t;
|
|
147
|
-
}
|
|
148
|
-
const U = j({
|
|
149
|
-
hotkeys: [],
|
|
150
|
-
activeScopes: [],
|
|
151
|
-
// This array has to be empty instead of containing '*' as default, to check if the provider is set or not
|
|
152
|
-
toggleScope: () => {
|
|
153
|
-
},
|
|
154
|
-
enableScope: () => {
|
|
155
|
-
},
|
|
156
|
-
disableScope: () => {
|
|
157
|
-
}
|
|
158
|
-
}), fe = () => W(U), ve = ({ initiallyActiveScopes: e = ["*"], children: t }) => {
|
|
159
|
-
const [r, u] = A(e), [s, f] = A([]), d = h((i) => {
|
|
160
|
-
u((c) => c.includes("*") ? [i] : Array.from(/* @__PURE__ */ new Set([...c, i])));
|
|
161
|
-
}, []), l = h((i) => {
|
|
162
|
-
u((c) => c.filter((n) => n !== i));
|
|
163
|
-
}, []), o = h((i) => {
|
|
164
|
-
u((c) => c.includes(i) ? c.filter((n) => n !== i) : c.includes("*") ? [i] : Array.from(/* @__PURE__ */ new Set([...c, i])));
|
|
165
|
-
}, []), p = h((i) => {
|
|
166
|
-
f((c) => [...c, i]);
|
|
167
|
-
}, []), y = h((i) => {
|
|
168
|
-
f((c) => c.filter((n) => !M(n, i)));
|
|
169
|
-
}, []);
|
|
170
|
-
return /* @__PURE__ */ x(
|
|
171
|
-
U.Provider,
|
|
172
|
-
{
|
|
173
|
-
value: { activeScopes: r, hotkeys: s, enableScope: d, disableScope: l, toggleScope: o },
|
|
174
|
-
children: /* @__PURE__ */ x(de, { addHotkey: p, removeHotkey: y, children: t })
|
|
175
|
-
}
|
|
176
|
-
);
|
|
177
|
-
};
|
|
178
|
-
function le(e) {
|
|
179
|
-
const t = K(void 0);
|
|
180
|
-
return M(t.current, e) || (t.current = e), t.current;
|
|
181
|
-
}
|
|
182
|
-
const ye = (e) => {
|
|
183
|
-
e.stopPropagation(), e.preventDefault(), e.stopImmediatePropagation();
|
|
184
|
-
}, me = typeof window < "u" ? X : I;
|
|
185
|
-
function pe(e) {
|
|
186
|
-
if (!e) return;
|
|
187
|
-
const { enabled: t, preventDefault: r, ignoreEventWhen: u, ...s } = e;
|
|
188
|
-
return typeof t == "function" ? s : { ...s, enabled: t };
|
|
189
|
-
}
|
|
190
|
-
function we(e, t, r, u) {
|
|
191
|
-
const [s, f] = A(null), d = h((w) => (f(w), () => f(null)), []), l = K(!1), o = Array.isArray(r) ? Array.isArray(u) ? void 0 : u : r, p = D(e) ? e.join(o?.delimiter) : e, y = Array.isArray(r) ? r : Array.isArray(u) ? u : void 0, i = h(t, y ?? []), c = K(i);
|
|
192
|
-
y ? c.current = i : c.current = t;
|
|
193
|
-
const n = le(pe(o)), E = K(o?.enabled);
|
|
194
|
-
E.current = o?.enabled;
|
|
195
|
-
const C = K(o?.preventDefault);
|
|
196
|
-
C.current = o?.preventDefault;
|
|
197
|
-
const m = K(o?.ignoreEventWhen);
|
|
198
|
-
m.current = o?.ignoreEventWhen;
|
|
199
|
-
const { activeScopes: T } = fe(), b = ae();
|
|
200
|
-
return me(() => {
|
|
201
|
-
if (E.current === !1 || !ue(T, n?.scopes))
|
|
202
|
-
return;
|
|
203
|
-
let w = [], S;
|
|
204
|
-
const q = (a, _ = !1) => {
|
|
205
|
-
if (!(ie(a) && !J(a, n?.enableOnFormTags))) {
|
|
206
|
-
if (s !== null) {
|
|
207
|
-
const k = s.getRootNode();
|
|
208
|
-
if ((k instanceof Document || k instanceof ShadowRoot) && k.activeElement !== s && !s.contains(k.activeElement)) {
|
|
209
|
-
ye(a);
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
a.target?.isContentEditable && !n?.enableOnContentEditable || R(p, n?.delimiter).forEach((k) => {
|
|
214
|
-
if (k.includes(n?.splitKey ?? "+") && k.includes(n?.sequenceSplitKey ?? ">")) {
|
|
215
|
-
console.warn(
|
|
216
|
-
`Hotkey ${k} contains both ${n?.splitKey ?? "+"} and ${n?.sequenceSplitKey ?? ">"} which is not supported.`
|
|
217
|
-
);
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
const v = O(
|
|
221
|
-
k,
|
|
222
|
-
n?.splitKey,
|
|
223
|
-
n?.sequenceSplitKey,
|
|
224
|
-
n?.useKey,
|
|
225
|
-
n?.description,
|
|
226
|
-
n?.metadata
|
|
227
|
-
);
|
|
228
|
-
if (v.isSequence) {
|
|
229
|
-
S = setTimeout(() => {
|
|
230
|
-
w = [];
|
|
231
|
-
}, n?.sequenceTimeoutMs ?? 1e3);
|
|
232
|
-
const P = v.useKey ? a.key : L(a.code);
|
|
233
|
-
if ($(P.toLowerCase()))
|
|
234
|
-
return;
|
|
235
|
-
w.push(P);
|
|
236
|
-
const V = v.keys?.[w.length - 1];
|
|
237
|
-
if (P !== V) {
|
|
238
|
-
w = [], S && clearTimeout(S);
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
w.length === v.keys?.length && (c.current(a, v), S && clearTimeout(S), w = []);
|
|
242
|
-
} else if (ce(a, v, n?.ignoreModifiers) || v.keys?.includes("*")) {
|
|
243
|
-
if (m.current?.(a) || _ && l.current || (re(a, v, C.current), !ne(a, v, E.current)))
|
|
244
|
-
return;
|
|
245
|
-
c.current(a, v), _ || (l.current = !0);
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
}, B = (a) => {
|
|
250
|
-
a.code !== void 0 && (z(L(a.code)), (n?.keydown === void 0 && n?.keyup !== !0 || n?.keydown) && q(a));
|
|
251
|
-
}, N = (a) => {
|
|
252
|
-
a.code !== void 0 && (G(L(a.code)), l.current = !1, n?.keyup && q(a, !0));
|
|
253
|
-
}, H = s || o?.document || document;
|
|
254
|
-
return H.addEventListener("keyup", N, o?.eventListenerOptions), H.addEventListener("keydown", B, o?.eventListenerOptions), b && R(p, n?.delimiter).forEach((a) => {
|
|
255
|
-
b.addHotkey(
|
|
256
|
-
O(
|
|
257
|
-
a,
|
|
258
|
-
n?.splitKey,
|
|
259
|
-
n?.sequenceSplitKey,
|
|
260
|
-
n?.useKey,
|
|
261
|
-
n?.description,
|
|
262
|
-
n?.metadata
|
|
263
|
-
)
|
|
264
|
-
);
|
|
265
|
-
}), () => {
|
|
266
|
-
H.removeEventListener("keyup", N, o?.eventListenerOptions), H.removeEventListener("keydown", B, o?.eventListenerOptions), b && R(p, n?.delimiter).forEach((a) => {
|
|
267
|
-
b.removeHotkey(
|
|
268
|
-
O(
|
|
269
|
-
a,
|
|
270
|
-
n?.splitKey,
|
|
271
|
-
n?.sequenceSplitKey,
|
|
272
|
-
n?.useKey,
|
|
273
|
-
n?.description,
|
|
274
|
-
n?.metadata
|
|
275
|
-
)
|
|
276
|
-
);
|
|
277
|
-
}), w = [], S && clearTimeout(S);
|
|
278
|
-
};
|
|
279
|
-
}, [s, n, T, p]), d;
|
|
280
|
-
}
|
|
281
|
-
function ke(e = !1, t = []) {
|
|
282
|
-
const [r, u] = A(/* @__PURE__ */ new Set()), [s, f] = A(!1), d = Y(() => new Set(t.map((i) => i.toLowerCase())), [t]), l = h(
|
|
283
|
-
(i) => {
|
|
284
|
-
if (i.code === void 0)
|
|
285
|
-
return;
|
|
286
|
-
const c = L(e ? i.key : i.code).toLowerCase();
|
|
287
|
-
d.has(c) || (i.preventDefault(), i.stopPropagation(), u((n) => {
|
|
288
|
-
const E = new Set(n);
|
|
289
|
-
return E.add(c), E;
|
|
290
|
-
}));
|
|
291
|
-
},
|
|
292
|
-
[e, d]
|
|
293
|
-
), o = h(() => {
|
|
294
|
-
f(!1);
|
|
295
|
-
}, []), p = h(() => {
|
|
296
|
-
u(/* @__PURE__ */ new Set()), f(!0);
|
|
297
|
-
}, []), y = h(() => {
|
|
298
|
-
u(/* @__PURE__ */ new Set());
|
|
299
|
-
}, []);
|
|
300
|
-
return I(() => {
|
|
301
|
-
if (typeof document < "u" && s)
|
|
302
|
-
return document.addEventListener("keydown", l), () => {
|
|
303
|
-
document.removeEventListener("keydown", l);
|
|
304
|
-
};
|
|
305
|
-
}, [s, l]), [r, { start: p, stop: o, resetKeys: y, isRecording: s }];
|
|
306
|
-
}
|
|
307
|
-
export {
|
|
308
|
-
ve as HotkeysProvider,
|
|
309
|
-
te as isHotkeyPressed,
|
|
310
|
-
we as useHotkeys,
|
|
311
|
-
fe as useHotkeysContext,
|
|
312
|
-
ke as useRecordHotkeys
|
|
313
|
-
};
|