@hrnec06/react_utils 1.3.5 → 1.4.1
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/package.json +1 -1
- package/src/hooks/useKeyListener.ts +8 -5
- package/src/index.ts +4 -0
- package/src/lib/utils.ts +8 -0
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { KeyboardButton } from "@hrnec06/util";
|
|
1
2
|
import { useEffect, useState } from "react";
|
|
2
3
|
|
|
3
4
|
interface KeyListenerOptions {
|
|
@@ -5,9 +6,11 @@ interface KeyListenerOptions {
|
|
|
5
6
|
shift?: boolean,
|
|
6
7
|
alt?: boolean
|
|
7
8
|
}
|
|
8
|
-
export default function useKeyListener(key: string, options?: KeyListenerOptions) {
|
|
9
|
+
export default function useKeyListener(key: string | KeyboardButton, options?: KeyListenerOptions, element?: EventTarget) {
|
|
9
10
|
const [pressing, setPressing] = useState(false);
|
|
10
11
|
|
|
12
|
+
element ||= window;
|
|
13
|
+
|
|
11
14
|
const keyMatching = (e: KeyboardEvent, strict: boolean): boolean => {
|
|
12
15
|
if (strict && options?.ctrl && !e.ctrlKey)
|
|
13
16
|
return false;
|
|
@@ -34,12 +37,12 @@ export default function useKeyListener(key: string, options?: KeyListenerOptions
|
|
|
34
37
|
setPressing(false);
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
element.addEventListener('keydown', downListener as EventListenerOrEventListenerObject);
|
|
41
|
+
element.addEventListener('keyup', upListener as EventListenerOrEventListenerObject);
|
|
39
42
|
|
|
40
43
|
return () => {
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
element.removeEventListener('keydown', downListener as EventListenerOrEventListenerObject);
|
|
45
|
+
element.removeEventListener('keyup', upListener as EventListenerOrEventListenerObject);
|
|
43
46
|
}
|
|
44
47
|
}, []);
|
|
45
48
|
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,8 @@ import useLazySignal, { LazySignal } from "./hooks/useLazySignal";
|
|
|
14
14
|
import Debugger from './components/Debugger/Debugger';
|
|
15
15
|
import ResizeableBox from "./components/ResizeableBox/ResizeableBox";
|
|
16
16
|
|
|
17
|
+
import * as util from './lib/utils';
|
|
18
|
+
|
|
17
19
|
export {
|
|
18
20
|
useKeyListener,
|
|
19
21
|
useListener,
|
|
@@ -32,4 +34,6 @@ export {
|
|
|
32
34
|
|
|
33
35
|
Debugger,
|
|
34
36
|
ResizeableBox,
|
|
37
|
+
|
|
38
|
+
util
|
|
35
39
|
};
|
package/src/lib/utils.ts
ADDED