@versini/ui-hooks 4.1.3 → 4.2.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.
@@ -0,0 +1,56 @@
1
+ import { useEffect as y } from "react";
2
+ function E(t) {
3
+ const e = t.toLowerCase().split("+").map((s) => s.trim()), r = {
4
+ alt: e.includes("alt"),
5
+ ctrl: e.includes("ctrl"),
6
+ meta: e.includes("meta"),
7
+ mod: e.includes("mod"),
8
+ shift: e.includes("shift")
9
+ }, a = ["alt", "ctrl", "meta", "shift", "mod"], n = e.find((s) => !a.includes(s));
10
+ return {
11
+ ...r,
12
+ key: n
13
+ };
14
+ }
15
+ function p(t, e) {
16
+ const { alt: r, ctrl: a, meta: n, mod: s, shift: i, key: f } = t, { altKey: c, ctrlKey: o, metaKey: u, shiftKey: d, key: m } = e;
17
+ if (r !== c)
18
+ return !1;
19
+ if (s) {
20
+ if (!o && !u)
21
+ return !1;
22
+ } else if (a !== o || n !== u)
23
+ return !1;
24
+ return i !== d ? !1 : !!(f && (m.toLowerCase() === f.toLowerCase() || e.code.replace("Key", "").toLowerCase() === f.toLowerCase()));
25
+ }
26
+ function l(t) {
27
+ return (e) => p(E(t), e);
28
+ }
29
+ function h(t) {
30
+ return (e) => {
31
+ const r = "nativeEvent" in e ? e.nativeEvent : e;
32
+ t.forEach(([a, n, s = { preventDefault: !0 }]) => {
33
+ l(a)(r) && (s.preventDefault && e.preventDefault(), n(r));
34
+ });
35
+ };
36
+ }
37
+ function k(t, e, r = !1) {
38
+ return t.target instanceof HTMLElement ? (r || !t.target.isContentEditable) && !e.includes(t.target.tagName) : !0;
39
+ }
40
+ function w(t, e = ["INPUT", "TEXTAREA", "SELECT"], r = !1) {
41
+ y(() => {
42
+ const a = (n) => {
43
+ t.forEach(
44
+ ([s, i, f = { preventDefault: !0 }]) => {
45
+ l(s)(n) && k(n, e, r) && (f.preventDefault && n.preventDefault(), i(n));
46
+ }
47
+ );
48
+ };
49
+ return document.documentElement.addEventListener("keydown", a), () => document.documentElement.removeEventListener("keydown", a);
50
+ }, [t, e, r]);
51
+ }
52
+ export {
53
+ h as getHotkeyHandler,
54
+ k as shouldFireEvent,
55
+ w as useHotkeys
56
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,16 @@
1
1
  import * as react from 'react';
2
2
 
3
+ interface HotkeyItemOptions {
4
+ preventDefault?: boolean;
5
+ }
6
+
7
+ type HotkeyItem = [
8
+ string,
9
+ (event: KeyboardEvent) => void,
10
+ HotkeyItemOptions?
11
+ ];
12
+ declare function useHotkeys(hotkeys: HotkeyItem[], tagsToIgnore?: string[], triggerOnContentEditable?: boolean): void;
13
+
3
14
  /**
4
15
  * Custom hook that returns a function indicating whether the component
5
16
  * is mounted or not.
@@ -129,4 +140,4 @@ type UseUniqueIdOptions = string | number | {
129
140
  };
130
141
  declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
131
142
 
132
- export { useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
143
+ export { useHotkeys, useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
package/dist/index.js CHANGED
@@ -1,18 +1,20 @@
1
- import { useIsMounted as r } from "./hooks/useIsMounted.js";
2
- import { useLocalStorage as t } from "./hooks/useLocalStorage.js";
3
- import { useMergeRefs as f } from "./hooks/useMergeRefs.js";
4
- import { useResizeObserver as p } from "./hooks/useResizeObserver.js";
5
- import { useUncontrolled as n } from "./hooks/useUncontrolled.js";
6
- import { useUniqueId as l } from "./hooks/useUniqueId.js";
1
+ import { useHotkeys as r } from "./hooks/useHotkeys.js";
2
+ import { useIsMounted as t } from "./hooks/useIsMounted.js";
3
+ import { useLocalStorage as f } from "./hooks/useLocalStorage.js";
4
+ import { useMergeRefs as p } from "./hooks/useMergeRefs.js";
5
+ import { useResizeObserver as n } from "./hooks/useResizeObserver.js";
6
+ import { useUncontrolled as l } from "./hooks/useUncontrolled.js";
7
+ import { useUniqueId as c } from "./hooks/useUniqueId.js";
7
8
  /*!
8
- @versini/ui-hooks v4.1.3
9
+ @versini/ui-hooks v4.2.1
9
10
  © 2024 gizmette.com
10
11
  */
11
12
  export {
12
- r as useIsMounted,
13
- t as useLocalStorage,
14
- f as useMergeRefs,
15
- p as useResizeObserver,
16
- n as useUncontrolled,
17
- l as useUniqueId
13
+ r as useHotkeys,
14
+ t as useIsMounted,
15
+ f as useLocalStorage,
16
+ p as useMergeRefs,
17
+ n as useResizeObserver,
18
+ l as useUncontrolled,
19
+ c as useUniqueId
18
20
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-hooks",
3
- "version": "4.1.3",
3
+ "version": "4.2.1",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -41,5 +41,5 @@
41
41
  "react": "18.3.1",
42
42
  "react-dom": "18.3.1"
43
43
  },
44
- "gitHead": "5398941244aa5c6642426325c804d7cb9529b688"
44
+ "gitHead": "b4cbab083052f7f45b00e98c2992fffa31d05c1f"
45
45
  }