@hrnec06/react_utils 1.4.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hrnec06/react_utils",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "A debugger component for react.",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -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
- window.addEventListener('keydown', downListener);
38
- window.addEventListener('keyup', upListener);
40
+ element.addEventListener('keydown', downListener as EventListenerOrEventListenerObject);
41
+ element.addEventListener('keyup', upListener as EventListenerOrEventListenerObject);
39
42
 
40
43
  return () => {
41
- window.removeEventListener('keydown', downListener);
42
- window.removeEventListener('keyup', upListener);
44
+ element.removeEventListener('keydown', downListener as EventListenerOrEventListenerObject);
45
+ element.removeEventListener('keyup', upListener as EventListenerOrEventListenerObject);
43
46
  }
44
47
  }, []);
45
48