react-hotkeys-hook 4.4.1 → 4.4.2

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/README.md CHANGED
@@ -80,7 +80,7 @@ export const ExampleComponent = () => {
80
80
 
81
81
  #### Changing a scope's active state
82
82
 
83
- You can change the active state of a scope using the `deactivateScope`, `activateScope` and `toggleScope` functions
83
+ You can change the active state of a scope using the `disableScope`, `enableScope` and `toggleScope` functions
84
84
  returned by the `useHotkeysContext()` hook. Note that you have to have your app wrapped in a `<HotkeysProvider>` component.
85
85
 
86
86
  ```jsx harmony
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "react-hotkeys-hook",
3
3
  "description": "React hook for handling keyboard shortcuts",
4
- "version": "4.4.1",
5
- "repository": "https://JohannesKlauss@github.com/JohannesKlauss/react-keymap-hook.git",
4
+ "version": "4.4.2",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/JohannesKlauss/react-keymap-hook.git"
8
+ },
6
9
  "homepage": "https://johannesklauss.github.io/react-hotkeys-hook/",
7
10
  "author": "Johannes Klauss",
8
11
  "main": "dist/index.js",
@@ -75,25 +78,25 @@
75
78
  }
76
79
  },
77
80
  "devDependencies": {
78
- "@babel/core": "7.21.8",
81
+ "@babel/core": "7.23.2",
79
82
  "@babel/plugin-proposal-class-properties": "7.18.6",
80
- "@babel/plugin-transform-react-jsx": "7.21.5",
81
- "@babel/preset-env": "7.21.5",
82
- "@babel/preset-react": "7.18.6",
83
- "@babel/preset-typescript": "7.21.5",
84
- "@testing-library/jest-dom": "5.16.5",
83
+ "@babel/plugin-transform-react-jsx": "7.22.15",
84
+ "@babel/preset-env": "7.23.2",
85
+ "@babel/preset-react": "7.22.15",
86
+ "@babel/preset-typescript": "7.23.2",
87
+ "@testing-library/jest-dom": "5.17.0",
85
88
  "@testing-library/react": "14.0.0",
86
89
  "@testing-library/user-event": "14.4.3",
87
- "@types/jest": "29.5.2",
88
- "@types/react": "18.2.14",
89
- "@types/react-dom": "18.2.6",
90
+ "@types/jest": "29.5.6",
91
+ "@types/react": "18.2.33",
92
+ "@types/react-dom": "18.2.14",
90
93
  "@typescript-eslint/eslint-plugin": "5.60.0",
91
94
  "@typescript-eslint/parser": "5.60.0",
92
- "eslint": "^8.34.0",
95
+ "eslint": "8.56.0",
93
96
  "eslint-plugin-prettier": "4.2.1",
94
- "eslint-plugin-react": "^7.32.2",
95
- "jest": "29.5.0",
96
- "jest-environment-jsdom": "29.5.0",
97
+ "eslint-plugin-react": "7.33.2",
98
+ "jest": "29.7.0",
99
+ "jest-environment-jsdom": "29.7.0",
97
100
  "prettier": "2.8.8",
98
101
  "react": "18.2.0",
99
102
  "react-dom": "18.2.0",
package/src/validators.ts CHANGED
@@ -58,6 +58,10 @@ export const isHotkeyMatchingKeyboardEvent = (e: KeyboardEvent, hotkey: Hotkey,
58
58
  const keyCode = mapKey(code)
59
59
  const pressedKey = pressedKeyUppercase.toLowerCase()
60
60
 
61
+ if (!keys?.includes(keyCode) && !['ctrl', 'control', 'unknown', 'meta', 'alt', 'shift', 'os'].includes(keyCode)) {
62
+ return false;
63
+ }
64
+
61
65
  if (!ignoreModifiers) {
62
66
  // We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
63
67
  if (alt === !altKey && pressedKey !== 'alt') {
@@ -1,14 +0,0 @@
1
- import { ReactNode } from 'react';
2
- import { Hotkey } from './types';
3
- declare type BoundHotkeysProxyProviderType = {
4
- addHotkey: (hotkey: Hotkey) => void;
5
- removeHotkey: (hotkey: Hotkey) => void;
6
- };
7
- export declare const useBoundHotkeysProxy: () => BoundHotkeysProxyProviderType | undefined;
8
- interface Props {
9
- children: ReactNode;
10
- addHotkey: (hotkey: Hotkey) => void;
11
- removeHotkey: (hotkey: Hotkey) => void;
12
- }
13
- export default function BoundHotkeysProxyProviderProvider({ addHotkey, removeHotkey, children }: Props): import("react").JSX.Element;
14
- export {};
@@ -1,16 +0,0 @@
1
- import { Hotkey } from './types';
2
- import { ReactNode } from 'react';
3
- export declare type HotkeysContextType = {
4
- hotkeys: ReadonlyArray<Hotkey>;
5
- enabledScopes: string[];
6
- toggleScope: (scope: string) => void;
7
- enableScope: (scope: string) => void;
8
- disableScope: (scope: string) => void;
9
- };
10
- export declare const useHotkeysContext: () => HotkeysContextType;
11
- interface Props {
12
- initiallyActiveScopes?: string[];
13
- children: ReactNode;
14
- }
15
- export declare const HotkeysProvider: ({ initiallyActiveScopes, children }: Props) => import("react").JSX.Element;
16
- export {};
@@ -1 +0,0 @@
1
- export default function deepEqual(x: any, y: any): boolean;
package/dist/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import useHotkeys from './useHotkeys';
2
- import type { Options, Keys, HotkeyCallback } from './types';
3
- import { HotkeysProvider, useHotkeysContext } from './HotkeysProvider';
4
- import { isHotkeyPressed } from './isHotkeyPressed';
5
- import useRecordHotkeys from './useRecordHotkeys';
6
- export { useHotkeys, useRecordHotkeys, useHotkeysContext, isHotkeyPressed, HotkeysProvider, Options, Keys, HotkeyCallback, };
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
-
2
- 'use strict'
3
-
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./react-hotkeys-hook.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./react-hotkeys-hook.cjs.development.js')
8
- }
@@ -1,4 +0,0 @@
1
- export declare function isReadonlyArray(value: unknown): value is readonly unknown[];
2
- export declare function isHotkeyPressed(key: string | readonly string[], splitKey?: string): boolean;
3
- export declare function pushToCurrentlyPressedKeys(key: string | string[]): void;
4
- export declare function removeFromCurrentlyPressedKeys(key: string | string[]): void;
@@ -1,5 +0,0 @@
1
- import { Hotkey } from './types';
2
- export declare function mapKey(key: string): string;
3
- export declare function isHotkeyModifier(key: string): boolean;
4
- export declare function parseKeysHookInput(keys: string, splitKey?: string): string[];
5
- export declare function parseHotkey(hotkey: string, combinationKey?: string, description?: string): Hotkey;
@@ -1,523 +0,0 @@
1
- 'use strict';
2
-
3
- var react = require('react');
4
- var jsxRuntime = require('react/jsx-runtime');
5
-
6
- function _extends() {
7
- _extends = Object.assign ? Object.assign.bind() : function (target) {
8
- for (var i = 1; i < arguments.length; i++) {
9
- var source = arguments[i];
10
- for (var key in source) {
11
- if (Object.prototype.hasOwnProperty.call(source, key)) {
12
- target[key] = source[key];
13
- }
14
- }
15
- }
16
- return target;
17
- };
18
- return _extends.apply(this, arguments);
19
- }
20
-
21
- var reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl'];
22
- var mappedKeys = {
23
- esc: 'escape',
24
- "return": 'enter',
25
- '.': 'period',
26
- ',': 'comma',
27
- '-': 'slash',
28
- ' ': 'space',
29
- '`': 'backquote',
30
- '#': 'backslash',
31
- '+': 'bracketright',
32
- ShiftLeft: 'shift',
33
- ShiftRight: 'shift',
34
- AltLeft: 'alt',
35
- AltRight: 'alt',
36
- MetaLeft: 'meta',
37
- MetaRight: 'meta',
38
- OSLeft: 'meta',
39
- OSRight: 'meta',
40
- ControlLeft: 'ctrl',
41
- ControlRight: 'ctrl'
42
- };
43
- function mapKey(key) {
44
- return (mappedKeys[key] || key).trim().toLowerCase().replace(/key|digit|numpad|arrow/, '');
45
- }
46
- function isHotkeyModifier(key) {
47
- return reservedModifierKeywords.includes(key);
48
- }
49
- function parseKeysHookInput(keys, splitKey) {
50
- if (splitKey === void 0) {
51
- splitKey = ',';
52
- }
53
- return keys.split(splitKey);
54
- }
55
- function parseHotkey(hotkey, combinationKey, description) {
56
- if (combinationKey === void 0) {
57
- combinationKey = '+';
58
- }
59
- var keys = hotkey.toLocaleLowerCase().split(combinationKey).map(function (k) {
60
- return mapKey(k);
61
- });
62
- var modifiers = {
63
- alt: keys.includes('alt'),
64
- ctrl: keys.includes('ctrl') || keys.includes('control'),
65
- shift: keys.includes('shift'),
66
- meta: keys.includes('meta'),
67
- mod: keys.includes('mod')
68
- };
69
- var singleCharKeys = keys.filter(function (k) {
70
- return !reservedModifierKeywords.includes(k);
71
- });
72
- return _extends({}, modifiers, {
73
- keys: singleCharKeys,
74
- description: description
75
- });
76
- }
77
-
78
- (function () {
79
- if (typeof document !== 'undefined') {
80
- document.addEventListener('keydown', function (e) {
81
- if (e.key === undefined) {
82
- // Synthetic event (e.g., Chrome autofill). Ignore.
83
- return;
84
- }
85
- pushToCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)]);
86
- });
87
- document.addEventListener('keyup', function (e) {
88
- if (e.key === undefined) {
89
- // Synthetic event (e.g., Chrome autofill). Ignore.
90
- return;
91
- }
92
- removeFromCurrentlyPressedKeys([mapKey(e.key), mapKey(e.code)]);
93
- });
94
- }
95
- if (typeof window !== 'undefined') {
96
- window.addEventListener('blur', function () {
97
- currentlyPressedKeys.clear();
98
- });
99
- }
100
- })();
101
- var currentlyPressedKeys = /*#__PURE__*/new Set();
102
- // https://github.com/microsoft/TypeScript/issues/17002
103
- function isReadonlyArray(value) {
104
- return Array.isArray(value);
105
- }
106
- function isHotkeyPressed(key, splitKey) {
107
- if (splitKey === void 0) {
108
- splitKey = ',';
109
- }
110
- var hotkeyArray = isReadonlyArray(key) ? key : key.split(splitKey);
111
- return hotkeyArray.every(function (hotkey) {
112
- return currentlyPressedKeys.has(hotkey.trim().toLowerCase());
113
- });
114
- }
115
- function pushToCurrentlyPressedKeys(key) {
116
- var hotkeyArray = Array.isArray(key) ? key : [key];
117
- /*
118
- Due to a weird behavior on macOS we need to clear the set if the user pressed down the meta key and presses another key.
119
- https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser
120
- Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.
121
- */
122
- if (currentlyPressedKeys.has('meta')) {
123
- currentlyPressedKeys.forEach(function (key) {
124
- return !isHotkeyModifier(key) && currentlyPressedKeys["delete"](key.toLowerCase());
125
- });
126
- }
127
- hotkeyArray.forEach(function (hotkey) {
128
- return currentlyPressedKeys.add(hotkey.toLowerCase());
129
- });
130
- }
131
- function removeFromCurrentlyPressedKeys(key) {
132
- var hotkeyArray = Array.isArray(key) ? key : [key];
133
- /*
134
- Due to a weird behavior on macOS we need to clear the set if the user pressed down the meta key and presses another key.
135
- https://stackoverflow.com/questions/11818637/why-does-javascript-drop-keyup-events-when-the-metakey-is-pressed-on-mac-browser
136
- Otherwise the set will hold all ever pressed keys while the meta key is down which leads to wrong results.
137
- */
138
- if (key === 'meta') {
139
- currentlyPressedKeys.clear();
140
- } else {
141
- hotkeyArray.forEach(function (hotkey) {
142
- return currentlyPressedKeys["delete"](hotkey.toLowerCase());
143
- });
144
- }
145
- }
146
-
147
- function maybePreventDefault(e, hotkey, preventDefault) {
148
- if (typeof preventDefault === 'function' && preventDefault(e, hotkey) || preventDefault === true) {
149
- e.preventDefault();
150
- }
151
- }
152
- function isHotkeyEnabled(e, hotkey, enabled) {
153
- if (typeof enabled === 'function') {
154
- return enabled(e, hotkey);
155
- }
156
- return enabled === true || enabled === undefined;
157
- }
158
- function isKeyboardEventTriggeredByInput(ev) {
159
- return isHotkeyEnabledOnTag(ev, ['input', 'textarea', 'select']);
160
- }
161
- function isHotkeyEnabledOnTag(_ref, enabledOnTags) {
162
- var target = _ref.target;
163
- if (enabledOnTags === void 0) {
164
- enabledOnTags = false;
165
- }
166
- var targetTagName = target && target.tagName;
167
- if (isReadonlyArray(enabledOnTags)) {
168
- return Boolean(targetTagName && enabledOnTags && enabledOnTags.some(function (tag) {
169
- return tag.toLowerCase() === targetTagName.toLowerCase();
170
- }));
171
- }
172
- return Boolean(targetTagName && enabledOnTags && enabledOnTags === true);
173
- }
174
- function isScopeActive(activeScopes, scopes) {
175
- if (activeScopes.length === 0 && scopes) {
176
- console.warn('A hotkey has the "scopes" option set, however no active scopes were found. If you want to use the global scopes feature, you need to wrap your app in a <HotkeysProvider>');
177
- return true;
178
- }
179
- if (!scopes) {
180
- return true;
181
- }
182
- return activeScopes.some(function (scope) {
183
- return scopes.includes(scope);
184
- }) || activeScopes.includes('*');
185
- }
186
- var isHotkeyMatchingKeyboardEvent = function isHotkeyMatchingKeyboardEvent(e, hotkey, ignoreModifiers) {
187
- if (ignoreModifiers === void 0) {
188
- ignoreModifiers = false;
189
- }
190
- var alt = hotkey.alt,
191
- meta = hotkey.meta,
192
- mod = hotkey.mod,
193
- shift = hotkey.shift,
194
- ctrl = hotkey.ctrl,
195
- keys = hotkey.keys;
196
- var pressedKeyUppercase = e.key,
197
- code = e.code,
198
- ctrlKey = e.ctrlKey,
199
- metaKey = e.metaKey,
200
- shiftKey = e.shiftKey,
201
- altKey = e.altKey;
202
- var keyCode = mapKey(code);
203
- var pressedKey = pressedKeyUppercase.toLowerCase();
204
- if (!ignoreModifiers) {
205
- // We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
206
- if (alt === !altKey && pressedKey !== 'alt') {
207
- return false;
208
- }
209
- if (shift === !shiftKey && pressedKey !== 'shift') {
210
- return false;
211
- }
212
- // Mod is a special key name that is checking for meta on macOS and ctrl on other platforms
213
- if (mod) {
214
- if (!metaKey && !ctrlKey) {
215
- return false;
216
- }
217
- } else {
218
- if (meta === !metaKey && pressedKey !== 'meta' && pressedKey !== 'os') {
219
- return false;
220
- }
221
- if (ctrl === !ctrlKey && pressedKey !== 'ctrl' && pressedKey !== 'control') {
222
- return false;
223
- }
224
- }
225
- }
226
- // All modifiers are correct, now check the key
227
- // If the key is set, we check for the key
228
- if (keys && keys.length === 1 && (keys.includes(pressedKey) || keys.includes(keyCode))) {
229
- return true;
230
- } else if (keys) {
231
- // Check if all keys are present in pressedDownKeys set
232
- return isHotkeyPressed(keys);
233
- } else if (!keys) {
234
- // If the key is not set, we only listen for modifiers, that check went alright, so we return true
235
- return true;
236
- }
237
- // There is nothing that matches.
238
- return false;
239
- };
240
-
241
- var BoundHotkeysProxyProvider = /*#__PURE__*/react.createContext(undefined);
242
- var useBoundHotkeysProxy = function useBoundHotkeysProxy() {
243
- return react.useContext(BoundHotkeysProxyProvider);
244
- };
245
- function BoundHotkeysProxyProviderProvider(_ref) {
246
- var addHotkey = _ref.addHotkey,
247
- removeHotkey = _ref.removeHotkey,
248
- children = _ref.children;
249
- return /*#__PURE__*/jsxRuntime.jsx(BoundHotkeysProxyProvider.Provider, {
250
- value: {
251
- addHotkey: addHotkey,
252
- removeHotkey: removeHotkey
253
- },
254
- children: children
255
- });
256
- }
257
-
258
- function deepEqual(x, y) {
259
- //@ts-ignore
260
- return x && y && typeof x === 'object' && typeof y === 'object' ? Object.keys(x).length === Object.keys(y).length &&
261
- //@ts-ignore
262
- Object.keys(x).reduce(function (isEqual, key) {
263
- return isEqual && deepEqual(x[key], y[key]);
264
- }, true) : x === y;
265
- }
266
-
267
- var HotkeysContext = /*#__PURE__*/react.createContext({
268
- hotkeys: [],
269
- enabledScopes: [],
270
- toggleScope: function toggleScope() {},
271
- enableScope: function enableScope() {},
272
- disableScope: function disableScope() {}
273
- });
274
- var useHotkeysContext = function useHotkeysContext() {
275
- return react.useContext(HotkeysContext);
276
- };
277
- var HotkeysProvider = function HotkeysProvider(_ref) {
278
- var _ref$initiallyActiveS = _ref.initiallyActiveScopes,
279
- initiallyActiveScopes = _ref$initiallyActiveS === void 0 ? ['*'] : _ref$initiallyActiveS,
280
- children = _ref.children;
281
- var _useState = react.useState((initiallyActiveScopes == null ? void 0 : initiallyActiveScopes.length) > 0 ? initiallyActiveScopes : ['*']),
282
- internalActiveScopes = _useState[0],
283
- setInternalActiveScopes = _useState[1];
284
- var _useState2 = react.useState([]),
285
- boundHotkeys = _useState2[0],
286
- setBoundHotkeys = _useState2[1];
287
- var enableScope = react.useCallback(function (scope) {
288
- setInternalActiveScopes(function (prev) {
289
- if (prev.includes('*')) {
290
- return [scope];
291
- }
292
- return Array.from(new Set([].concat(prev, [scope])));
293
- });
294
- }, []);
295
- var disableScope = react.useCallback(function (scope) {
296
- setInternalActiveScopes(function (prev) {
297
- if (prev.filter(function (s) {
298
- return s !== scope;
299
- }).length === 0) {
300
- return ['*'];
301
- } else {
302
- return prev.filter(function (s) {
303
- return s !== scope;
304
- });
305
- }
306
- });
307
- }, []);
308
- var toggleScope = react.useCallback(function (scope) {
309
- setInternalActiveScopes(function (prev) {
310
- if (prev.includes(scope)) {
311
- if (prev.filter(function (s) {
312
- return s !== scope;
313
- }).length === 0) {
314
- return ['*'];
315
- } else {
316
- return prev.filter(function (s) {
317
- return s !== scope;
318
- });
319
- }
320
- } else {
321
- if (prev.includes('*')) {
322
- return [scope];
323
- }
324
- return Array.from(new Set([].concat(prev, [scope])));
325
- }
326
- });
327
- }, []);
328
- var addBoundHotkey = react.useCallback(function (hotkey) {
329
- setBoundHotkeys(function (prev) {
330
- return [].concat(prev, [hotkey]);
331
- });
332
- }, []);
333
- var removeBoundHotkey = react.useCallback(function (hotkey) {
334
- setBoundHotkeys(function (prev) {
335
- return prev.filter(function (h) {
336
- return !deepEqual(h, hotkey);
337
- });
338
- });
339
- }, []);
340
- return /*#__PURE__*/jsxRuntime.jsx(HotkeysContext.Provider, {
341
- value: {
342
- enabledScopes: internalActiveScopes,
343
- hotkeys: boundHotkeys,
344
- enableScope: enableScope,
345
- disableScope: disableScope,
346
- toggleScope: toggleScope
347
- },
348
- children: /*#__PURE__*/jsxRuntime.jsx(BoundHotkeysProxyProviderProvider, {
349
- addHotkey: addBoundHotkey,
350
- removeHotkey: removeBoundHotkey,
351
- children: children
352
- })
353
- });
354
- };
355
-
356
- function useDeepEqualMemo(value) {
357
- var ref = react.useRef(undefined);
358
- if (!deepEqual(ref.current, value)) {
359
- ref.current = value;
360
- }
361
- return ref.current;
362
- }
363
-
364
- var stopPropagation = function stopPropagation(e) {
365
- e.stopPropagation();
366
- e.preventDefault();
367
- e.stopImmediatePropagation();
368
- };
369
- var useSafeLayoutEffect = typeof window !== 'undefined' ? react.useLayoutEffect : react.useEffect;
370
- function useHotkeys(keys, callback, options, dependencies) {
371
- var ref = react.useRef(null);
372
- var hasTriggeredRef = react.useRef(false);
373
- var _options = !(options instanceof Array) ? options : !(dependencies instanceof Array) ? dependencies : undefined;
374
- var _keys = isReadonlyArray(keys) ? keys.join(_options == null ? void 0 : _options.splitKey) : keys;
375
- var _deps = options instanceof Array ? options : dependencies instanceof Array ? dependencies : undefined;
376
- var memoisedCB = react.useCallback(callback, _deps != null ? _deps : []);
377
- var cbRef = react.useRef(memoisedCB);
378
- if (_deps) {
379
- cbRef.current = memoisedCB;
380
- } else {
381
- cbRef.current = callback;
382
- }
383
- var memoisedOptions = useDeepEqualMemo(_options);
384
- var _useHotkeysContext = useHotkeysContext(),
385
- enabledScopes = _useHotkeysContext.enabledScopes;
386
- var proxy = useBoundHotkeysProxy();
387
- useSafeLayoutEffect(function () {
388
- if ((memoisedOptions == null ? void 0 : memoisedOptions.enabled) === false || !isScopeActive(enabledScopes, memoisedOptions == null ? void 0 : memoisedOptions.scopes)) {
389
- return;
390
- }
391
- var listener = function listener(e, isKeyUp) {
392
- var _e$target;
393
- if (isKeyUp === void 0) {
394
- isKeyUp = false;
395
- }
396
- if (isKeyboardEventTriggeredByInput(e) && !isHotkeyEnabledOnTag(e, memoisedOptions == null ? void 0 : memoisedOptions.enableOnFormTags)) {
397
- return;
398
- }
399
- if (memoisedOptions != null && memoisedOptions.ignoreEventWhen != null && memoisedOptions.ignoreEventWhen(e)) {
400
- return;
401
- }
402
- // TODO: SINCE THE EVENT IS NOW ATTACHED TO THE REF, THE ACTIVE ELEMENT CAN NEVER BE INSIDE THE REF. THE HOTKEY ONLY TRIGGERS IF THE
403
- // REF IS THE ACTIVE ELEMENT. THIS IS A PROBLEM SINCE FOCUSED SUB COMPONENTS WON'T TRIGGER THE HOTKEY.
404
- if (ref.current !== null && document.activeElement !== ref.current && !ref.current.contains(document.activeElement)) {
405
- stopPropagation(e);
406
- return;
407
- }
408
- if ((_e$target = e.target) != null && _e$target.isContentEditable && !(memoisedOptions != null && memoisedOptions.enableOnContentEditable)) {
409
- return;
410
- }
411
- parseKeysHookInput(_keys, memoisedOptions == null ? void 0 : memoisedOptions.splitKey).forEach(function (key) {
412
- var _hotkey$keys;
413
- var hotkey = parseHotkey(key, memoisedOptions == null ? void 0 : memoisedOptions.combinationKey);
414
- if (isHotkeyMatchingKeyboardEvent(e, hotkey, memoisedOptions == null ? void 0 : memoisedOptions.ignoreModifiers) || (_hotkey$keys = hotkey.keys) != null && _hotkey$keys.includes('*')) {
415
- if (isKeyUp && hasTriggeredRef.current) {
416
- return;
417
- }
418
- maybePreventDefault(e, hotkey, memoisedOptions == null ? void 0 : memoisedOptions.preventDefault);
419
- if (!isHotkeyEnabled(e, hotkey, memoisedOptions == null ? void 0 : memoisedOptions.enabled)) {
420
- stopPropagation(e);
421
- return;
422
- }
423
- // Execute the user callback for that hotkey
424
- cbRef.current(e, hotkey);
425
- if (!isKeyUp) {
426
- hasTriggeredRef.current = true;
427
- }
428
- }
429
- });
430
- };
431
- var handleKeyDown = function handleKeyDown(event) {
432
- if (event.key === undefined) {
433
- // Synthetic event (e.g., Chrome autofill). Ignore.
434
- return;
435
- }
436
- pushToCurrentlyPressedKeys(mapKey(event.code));
437
- if ((memoisedOptions == null ? void 0 : memoisedOptions.keydown) === undefined && (memoisedOptions == null ? void 0 : memoisedOptions.keyup) !== true || memoisedOptions != null && memoisedOptions.keydown) {
438
- listener(event);
439
- }
440
- };
441
- var handleKeyUp = function handleKeyUp(event) {
442
- if (event.key === undefined) {
443
- // Synthetic event (e.g., Chrome autofill). Ignore.
444
- return;
445
- }
446
- removeFromCurrentlyPressedKeys(mapKey(event.code));
447
- hasTriggeredRef.current = false;
448
- if (memoisedOptions != null && memoisedOptions.keyup) {
449
- listener(event, true);
450
- }
451
- };
452
- var domNode = ref.current || (_options == null ? void 0 : _options.document) || document;
453
- // @ts-ignore
454
- domNode.addEventListener('keyup', handleKeyUp);
455
- // @ts-ignore
456
- domNode.addEventListener('keydown', handleKeyDown);
457
- if (proxy) {
458
- parseKeysHookInput(_keys, memoisedOptions == null ? void 0 : memoisedOptions.splitKey).forEach(function (key) {
459
- return proxy.addHotkey(parseHotkey(key, memoisedOptions == null ? void 0 : memoisedOptions.combinationKey, memoisedOptions == null ? void 0 : memoisedOptions.description));
460
- });
461
- }
462
- return function () {
463
- // @ts-ignore
464
- domNode.removeEventListener('keyup', handleKeyUp);
465
- // @ts-ignore
466
- domNode.removeEventListener('keydown', handleKeyDown);
467
- if (proxy) {
468
- parseKeysHookInput(_keys, memoisedOptions == null ? void 0 : memoisedOptions.splitKey).forEach(function (key) {
469
- return proxy.removeHotkey(parseHotkey(key, memoisedOptions == null ? void 0 : memoisedOptions.combinationKey, memoisedOptions == null ? void 0 : memoisedOptions.description));
470
- });
471
- }
472
- };
473
- }, [_keys, memoisedOptions, enabledScopes]);
474
- return ref;
475
- }
476
-
477
- function useRecordHotkeys() {
478
- var _useState = react.useState(new Set()),
479
- keys = _useState[0],
480
- setKeys = _useState[1];
481
- var _useState2 = react.useState(false),
482
- isRecording = _useState2[0],
483
- setIsRecording = _useState2[1];
484
- var handler = react.useCallback(function (event) {
485
- if (event.key === undefined) {
486
- // Synthetic event (e.g., Chrome autofill). Ignore.
487
- return;
488
- }
489
- event.preventDefault();
490
- event.stopPropagation();
491
- setKeys(function (prev) {
492
- var newKeys = new Set(prev);
493
- newKeys.add(mapKey(event.code));
494
- return newKeys;
495
- });
496
- }, []);
497
- var stop = react.useCallback(function () {
498
- if (typeof document !== 'undefined') {
499
- document.removeEventListener('keydown', handler);
500
- setIsRecording(false);
501
- }
502
- }, [handler]);
503
- var start = react.useCallback(function () {
504
- setKeys(new Set());
505
- if (typeof document !== 'undefined') {
506
- stop();
507
- document.addEventListener('keydown', handler);
508
- setIsRecording(true);
509
- }
510
- }, [handler, stop]);
511
- return [keys, {
512
- start: start,
513
- stop: stop,
514
- isRecording: isRecording
515
- }];
516
- }
517
-
518
- exports.HotkeysProvider = HotkeysProvider;
519
- exports.isHotkeyPressed = isHotkeyPressed;
520
- exports.useHotkeys = useHotkeys;
521
- exports.useHotkeysContext = useHotkeysContext;
522
- exports.useRecordHotkeys = useRecordHotkeys;
523
- //# sourceMappingURL=react-hotkeys-hook.cjs.development.js.map