react-hotkeys-hook 5.2.3 → 5.2.4
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.js +115 -125
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext as T, useContext as M, useState as A, useCallback as
|
|
1
|
+
import { createContext as T, useContext as M, useState as A, useCallback as w, useRef as S, useLayoutEffect as z, useEffect as J } from "react";
|
|
2
2
|
import { jsx as b } from "react/jsx-runtime";
|
|
3
3
|
const j = ["shift", "alt", "meta", "mod", "ctrl", "control"], Q = {
|
|
4
4
|
esc: "escape",
|
|
@@ -24,27 +24,27 @@ function K(e) {
|
|
|
24
24
|
function D(e) {
|
|
25
25
|
return j.includes(e);
|
|
26
26
|
}
|
|
27
|
-
function H(e,
|
|
28
|
-
return e.toLowerCase().split(
|
|
27
|
+
function H(e, r = ",") {
|
|
28
|
+
return e.toLowerCase().split(r);
|
|
29
29
|
}
|
|
30
|
-
function P(e,
|
|
31
|
-
let
|
|
32
|
-
e = e.trim(), e.includes(
|
|
33
|
-
const
|
|
34
|
-
alt:
|
|
35
|
-
ctrl:
|
|
36
|
-
shift:
|
|
37
|
-
meta:
|
|
38
|
-
mod:
|
|
30
|
+
function P(e, r = "+", o = ">", i = !1, c, a) {
|
|
31
|
+
let n = [], y = !1;
|
|
32
|
+
e = e.trim(), e.includes(o) ? (y = !0, n = e.toLocaleLowerCase().split(o).map((f) => K(f))) : n = e.toLocaleLowerCase().split(r).map((f) => K(f));
|
|
33
|
+
const u = {
|
|
34
|
+
alt: n.includes("alt"),
|
|
35
|
+
ctrl: n.includes("ctrl") || n.includes("control"),
|
|
36
|
+
shift: n.includes("shift"),
|
|
37
|
+
meta: n.includes("meta"),
|
|
38
|
+
mod: n.includes("mod"),
|
|
39
39
|
useKey: i
|
|
40
|
-
},
|
|
40
|
+
}, l = n.filter((f) => !j.includes(f));
|
|
41
41
|
return {
|
|
42
|
-
...
|
|
43
|
-
keys:
|
|
44
|
-
description:
|
|
42
|
+
...u,
|
|
43
|
+
keys: l,
|
|
44
|
+
description: c,
|
|
45
45
|
isSequence: y,
|
|
46
46
|
hotkey: e,
|
|
47
|
-
metadata:
|
|
47
|
+
metadata: a
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
typeof document < "u" && (document.addEventListener("keydown", (e) => {
|
|
@@ -62,28 +62,28 @@ const L = /* @__PURE__ */ new Set();
|
|
|
62
62
|
function R(e) {
|
|
63
63
|
return Array.isArray(e);
|
|
64
64
|
}
|
|
65
|
-
function U(e,
|
|
66
|
-
return (R(e) ? e : e.split(
|
|
65
|
+
function U(e, r = ",") {
|
|
66
|
+
return (R(e) ? e : e.split(r)).every((i) => L.has(i.trim().toLowerCase()));
|
|
67
67
|
}
|
|
68
68
|
function _(e) {
|
|
69
|
-
const
|
|
70
|
-
L.has("meta") && L.forEach((
|
|
71
|
-
D(
|
|
72
|
-
}),
|
|
73
|
-
L.add(
|
|
69
|
+
const r = Array.isArray(e) ? e : [e];
|
|
70
|
+
L.has("meta") && L.forEach((o) => {
|
|
71
|
+
D(o) || L.delete(o.toLowerCase());
|
|
72
|
+
}), r.forEach((o) => {
|
|
73
|
+
L.add(o.toLowerCase());
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
76
|
function I(e) {
|
|
77
|
-
const
|
|
78
|
-
e === "meta" ? L.clear() :
|
|
79
|
-
L.delete(
|
|
77
|
+
const r = Array.isArray(e) ? e : [e];
|
|
78
|
+
e === "meta" ? L.clear() : r.forEach((o) => {
|
|
79
|
+
L.delete(o.toLowerCase());
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
|
-
function V(e,
|
|
83
|
-
(typeof
|
|
82
|
+
function V(e, r, o) {
|
|
83
|
+
(typeof o == "function" && o(e, r) || o === !0) && e.preventDefault();
|
|
84
84
|
}
|
|
85
|
-
function X(e,
|
|
86
|
-
return typeof
|
|
85
|
+
function X(e, r, o) {
|
|
86
|
+
return typeof o == "function" ? o(e, r) : o === !0 || o === void 0;
|
|
87
87
|
}
|
|
88
88
|
const Y = [
|
|
89
89
|
"input",
|
|
@@ -102,50 +102,40 @@ const Y = [
|
|
|
102
102
|
function Z(e) {
|
|
103
103
|
return F(e, Y);
|
|
104
104
|
}
|
|
105
|
-
function F(e,
|
|
106
|
-
const { target:
|
|
107
|
-
let
|
|
108
|
-
return ee(
|
|
105
|
+
function F(e, r = !1) {
|
|
106
|
+
const { target: o, composed: i } = e;
|
|
107
|
+
let c, a;
|
|
108
|
+
return ee(o) && i ? (c = e.composedPath()[0] && e.composedPath()[0].tagName, a = e.composedPath()[0] && e.composedPath()[0].role) : (c = o && o.tagName, a = o && o.role), R(r) ? !!(c && r && r.some((n) => n.toLowerCase() === c.toLowerCase() || n === a)) : !!(c && r && r);
|
|
109
109
|
}
|
|
110
110
|
function ee(e) {
|
|
111
111
|
return !!e.tagName && !e.tagName.startsWith("-") && e.tagName.includes("-");
|
|
112
112
|
}
|
|
113
|
-
function te(e,
|
|
114
|
-
return e.length === 0 &&
|
|
113
|
+
function te(e, r) {
|
|
114
|
+
return e.length === 0 && r ? !1 : r ? e.some((o) => r.includes(o)) || e.includes("*") : !0;
|
|
115
115
|
}
|
|
116
|
-
const re = (e,
|
|
117
|
-
const { alt: i, meta:
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (i !== w || o !== h) return !1;
|
|
122
|
-
if (f) {
|
|
123
|
-
if (!l && !a) return !1;
|
|
124
|
-
} else if (u !== l || y !== a) return !1;
|
|
125
|
-
}
|
|
126
|
-
return !0;
|
|
127
|
-
}
|
|
128
|
-
return !1;
|
|
129
|
-
}
|
|
130
|
-
if (!c?.includes(p) && !["ctrl", "control", "unknown", "meta", "alt", "shift", "os"].includes(p))
|
|
116
|
+
const re = (e, r, o = !1) => {
|
|
117
|
+
const { alt: i, meta: c, mod: a, shift: n, ctrl: y, keys: u, useKey: l } = r, { code: f, key: t, ctrlKey: d, metaKey: m, shiftKey: g, altKey: k } = e, p = K(f);
|
|
118
|
+
if (l && u?.length === 1 && u.includes(t.toLowerCase()))
|
|
119
|
+
return !0;
|
|
120
|
+
if (!u?.includes(p) && !["ctrl", "control", "unknown", "meta", "alt", "shift", "os"].includes(p))
|
|
131
121
|
return !1;
|
|
132
|
-
if (!
|
|
133
|
-
if (i !==
|
|
122
|
+
if (!o) {
|
|
123
|
+
if (i !== k && p !== "alt" || n !== g && p !== "shift")
|
|
134
124
|
return !1;
|
|
135
|
-
if (
|
|
136
|
-
if (!
|
|
125
|
+
if (a) {
|
|
126
|
+
if (!m && !d)
|
|
137
127
|
return !1;
|
|
138
|
-
} else if (
|
|
128
|
+
} else if (c !== m && p !== "meta" && p !== "os" || y !== d && p !== "ctrl" && p !== "control")
|
|
139
129
|
return !1;
|
|
140
130
|
}
|
|
141
|
-
return
|
|
142
|
-
}, $ = T(void 0),
|
|
143
|
-
function
|
|
144
|
-
return /* @__PURE__ */ b($.Provider, { value: { addHotkey: e, removeHotkey:
|
|
131
|
+
return u && u.length === 1 && u.includes(p) ? !0 : u && u.length > 0 ? u.includes(p) ? U(u) : !1 : !u || u.length === 0;
|
|
132
|
+
}, $ = T(void 0), oe = () => M($);
|
|
133
|
+
function ne({ addHotkey: e, removeHotkey: r, children: o }) {
|
|
134
|
+
return /* @__PURE__ */ b($.Provider, { value: { addHotkey: e, removeHotkey: r }, children: o });
|
|
145
135
|
}
|
|
146
|
-
function x(e,
|
|
147
|
-
return e &&
|
|
148
|
-
Object.keys(e).reduce((
|
|
136
|
+
function x(e, r) {
|
|
137
|
+
return e && r && typeof e == "object" && typeof r == "object" ? Object.keys(e).length === Object.keys(r).length && // @ts-expect-error TS7053
|
|
138
|
+
Object.keys(e).reduce((o, i) => o && x(e[i], r[i]), !0) : e === r;
|
|
149
139
|
}
|
|
150
140
|
const W = T({
|
|
151
141
|
hotkeys: [],
|
|
@@ -157,46 +147,46 @@ const W = T({
|
|
|
157
147
|
},
|
|
158
148
|
disableScope: () => {
|
|
159
149
|
}
|
|
160
|
-
}), se = () => M(W),
|
|
161
|
-
const [
|
|
162
|
-
i((
|
|
163
|
-
}, []), y =
|
|
164
|
-
i((
|
|
165
|
-
}, []),
|
|
166
|
-
i((
|
|
167
|
-
}, []),
|
|
168
|
-
|
|
169
|
-
}, []),
|
|
170
|
-
|
|
150
|
+
}), se = () => M(W), de = ({ initiallyActiveScopes: e = ["*"], children: r }) => {
|
|
151
|
+
const [o, i] = A(e), [c, a] = A([]), n = w((t) => {
|
|
152
|
+
i((d) => d.includes("*") ? [t] : Array.from(/* @__PURE__ */ new Set([...d, t])));
|
|
153
|
+
}, []), y = w((t) => {
|
|
154
|
+
i((d) => d.filter((m) => m !== t));
|
|
155
|
+
}, []), u = w((t) => {
|
|
156
|
+
i((d) => d.includes(t) ? d.filter((m) => m !== t) : d.includes("*") ? [t] : Array.from(/* @__PURE__ */ new Set([...d, t])));
|
|
157
|
+
}, []), l = w((t) => {
|
|
158
|
+
a((d) => [...d, t]);
|
|
159
|
+
}, []), f = w((t) => {
|
|
160
|
+
a((d) => d.filter((m) => !x(m, t)));
|
|
171
161
|
}, []);
|
|
172
162
|
return /* @__PURE__ */ b(
|
|
173
163
|
W.Provider,
|
|
174
164
|
{
|
|
175
|
-
value: { activeScopes:
|
|
176
|
-
children: /* @__PURE__ */ b(
|
|
165
|
+
value: { activeScopes: o, hotkeys: c, enableScope: n, disableScope: y, toggleScope: u },
|
|
166
|
+
children: /* @__PURE__ */ b(ne, { addHotkey: l, removeHotkey: f, children: r })
|
|
177
167
|
}
|
|
178
168
|
);
|
|
179
169
|
};
|
|
180
170
|
function ie(e) {
|
|
181
|
-
const
|
|
182
|
-
return x(
|
|
171
|
+
const r = S(void 0);
|
|
172
|
+
return x(r.current, e) || (r.current = e), r.current;
|
|
183
173
|
}
|
|
184
174
|
const N = (e) => {
|
|
185
175
|
e.stopPropagation(), e.preventDefault(), e.stopImmediatePropagation();
|
|
186
|
-
},
|
|
187
|
-
function
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
const t = ie(
|
|
191
|
-
return
|
|
192
|
-
if (t?.enabled === !1 || !te(
|
|
176
|
+
}, ce = typeof window < "u" ? z : J;
|
|
177
|
+
function fe(e, r, o, i) {
|
|
178
|
+
const c = S(null), a = S(!1), n = Array.isArray(o) ? Array.isArray(i) ? void 0 : i : o, y = R(e) ? e.join(n?.delimiter) : e, u = Array.isArray(o) ? o : Array.isArray(i) ? i : void 0, l = w(r, u ?? []), f = S(l);
|
|
179
|
+
u ? f.current = l : f.current = r;
|
|
180
|
+
const t = ie(n), { activeScopes: d } = se(), m = oe();
|
|
181
|
+
return ce(() => {
|
|
182
|
+
if (t?.enabled === !1 || !te(d, t?.scopes))
|
|
193
183
|
return;
|
|
194
|
-
let
|
|
184
|
+
let g = [], k;
|
|
195
185
|
const p = (s, B = !1) => {
|
|
196
186
|
if (!(Z(s) && !F(s, t?.enableOnFormTags))) {
|
|
197
|
-
if (
|
|
198
|
-
const v =
|
|
199
|
-
if ((v instanceof Document || v instanceof ShadowRoot) && v.activeElement !==
|
|
187
|
+
if (c.current !== null) {
|
|
188
|
+
const v = c.current.getRootNode();
|
|
189
|
+
if ((v instanceof Document || v instanceof ShadowRoot) && v.activeElement !== c.current && !c.current.contains(v.activeElement)) {
|
|
200
190
|
N(s);
|
|
201
191
|
return;
|
|
202
192
|
}
|
|
@@ -208,7 +198,7 @@ function de(e, n, r, i) {
|
|
|
208
198
|
);
|
|
209
199
|
return;
|
|
210
200
|
}
|
|
211
|
-
const
|
|
201
|
+
const h = P(
|
|
212
202
|
v,
|
|
213
203
|
t?.splitKey,
|
|
214
204
|
t?.sequenceSplitKey,
|
|
@@ -216,38 +206,38 @@ function de(e, n, r, i) {
|
|
|
216
206
|
t?.description,
|
|
217
207
|
t?.metadata
|
|
218
208
|
);
|
|
219
|
-
if (
|
|
220
|
-
|
|
221
|
-
|
|
209
|
+
if (h.isSequence) {
|
|
210
|
+
k = setTimeout(() => {
|
|
211
|
+
g = [];
|
|
222
212
|
}, t?.sequenceTimeoutMs ?? 1e3);
|
|
223
|
-
const C =
|
|
213
|
+
const C = h.useKey ? s.key : K(s.code);
|
|
224
214
|
if (D(C.toLowerCase()))
|
|
225
215
|
return;
|
|
226
|
-
|
|
227
|
-
const G =
|
|
216
|
+
g.push(C);
|
|
217
|
+
const G = h.keys?.[g.length - 1];
|
|
228
218
|
if (C !== G) {
|
|
229
|
-
|
|
219
|
+
g = [], k && clearTimeout(k);
|
|
230
220
|
return;
|
|
231
221
|
}
|
|
232
|
-
|
|
233
|
-
} else if (re(s,
|
|
234
|
-
if (t?.ignoreEventWhen?.(s) || B &&
|
|
222
|
+
g.length === h.keys?.length && (f.current(s, h), k && clearTimeout(k), g = []);
|
|
223
|
+
} else if (re(s, h, t?.ignoreModifiers) || h.keys?.includes("*")) {
|
|
224
|
+
if (t?.ignoreEventWhen?.(s) || B && a.current)
|
|
235
225
|
return;
|
|
236
|
-
if (V(s,
|
|
226
|
+
if (V(s, h, t?.preventDefault), !X(s, h, t?.enabled)) {
|
|
237
227
|
N(s);
|
|
238
228
|
return;
|
|
239
229
|
}
|
|
240
|
-
|
|
230
|
+
f.current(s, h), B || (a.current = !0);
|
|
241
231
|
}
|
|
242
232
|
});
|
|
243
233
|
}
|
|
244
234
|
}, O = (s) => {
|
|
245
235
|
s.code !== void 0 && (_(K(s.code)), (t?.keydown === void 0 && t?.keyup !== !0 || t?.keydown) && p(s));
|
|
246
236
|
}, q = (s) => {
|
|
247
|
-
s.code !== void 0 && (I(K(s.code)),
|
|
248
|
-
}, E =
|
|
249
|
-
return E.addEventListener("keyup", q,
|
|
250
|
-
|
|
237
|
+
s.code !== void 0 && (I(K(s.code)), a.current = !1, t?.keyup && p(s, !0));
|
|
238
|
+
}, E = c.current || n?.document || document;
|
|
239
|
+
return E.addEventListener("keyup", q, n?.eventListenerOptions), E.addEventListener("keydown", O, n?.eventListenerOptions), m && H(y, t?.delimiter).forEach((s) => {
|
|
240
|
+
m.addHotkey(
|
|
251
241
|
P(
|
|
252
242
|
s,
|
|
253
243
|
t?.splitKey,
|
|
@@ -258,8 +248,8 @@ function de(e, n, r, i) {
|
|
|
258
248
|
)
|
|
259
249
|
);
|
|
260
250
|
}), () => {
|
|
261
|
-
E.removeEventListener("keyup", q,
|
|
262
|
-
|
|
251
|
+
E.removeEventListener("keyup", q, n?.eventListenerOptions), E.removeEventListener("keydown", O, n?.eventListenerOptions), m && H(y, t?.delimiter).forEach((s) => {
|
|
252
|
+
m.removeHotkey(
|
|
263
253
|
P(
|
|
264
254
|
s,
|
|
265
255
|
t?.splitKey,
|
|
@@ -269,32 +259,32 @@ function de(e, n, r, i) {
|
|
|
269
259
|
t?.metadata
|
|
270
260
|
)
|
|
271
261
|
);
|
|
272
|
-
}),
|
|
262
|
+
}), g = [], k && clearTimeout(k);
|
|
273
263
|
};
|
|
274
|
-
}, [y, t,
|
|
264
|
+
}, [y, t, d]), c;
|
|
275
265
|
}
|
|
276
266
|
function le(e = !1) {
|
|
277
|
-
const [
|
|
278
|
-
(
|
|
279
|
-
|
|
280
|
-
const t = new Set(
|
|
281
|
-
return t.add(K(e ?
|
|
267
|
+
const [r, o] = A(/* @__PURE__ */ new Set()), [i, c] = A(!1), a = w(
|
|
268
|
+
(l) => {
|
|
269
|
+
l.code !== void 0 && (l.preventDefault(), l.stopPropagation(), o((f) => {
|
|
270
|
+
const t = new Set(f);
|
|
271
|
+
return t.add(K(e ? l.key : l.code)), t;
|
|
282
272
|
}));
|
|
283
273
|
},
|
|
284
274
|
[e]
|
|
285
|
-
),
|
|
286
|
-
typeof document < "u" && (document.removeEventListener("keydown",
|
|
287
|
-
}, [
|
|
288
|
-
|
|
289
|
-
}, [
|
|
290
|
-
|
|
275
|
+
), n = w(() => {
|
|
276
|
+
typeof document < "u" && (document.removeEventListener("keydown", a), c(!1));
|
|
277
|
+
}, [a]), y = w(() => {
|
|
278
|
+
o(/* @__PURE__ */ new Set()), typeof document < "u" && (n(), document.addEventListener("keydown", a), c(!0));
|
|
279
|
+
}, [a, n]), u = w(() => {
|
|
280
|
+
o(/* @__PURE__ */ new Set());
|
|
291
281
|
}, []);
|
|
292
|
-
return [
|
|
282
|
+
return [r, { start: y, stop: n, resetKeys: u, isRecording: i }];
|
|
293
283
|
}
|
|
294
284
|
export {
|
|
295
|
-
|
|
285
|
+
de as HotkeysProvider,
|
|
296
286
|
U as isHotkeyPressed,
|
|
297
|
-
|
|
287
|
+
fe as useHotkeys,
|
|
298
288
|
se as useHotkeysContext,
|
|
299
289
|
le as useRecordHotkeys
|
|
300
290
|
};
|