@versini/ui-hooks 4.1.2 → 4.2.0

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
+ };
@@ -1,22 +1,22 @@
1
- import { useRef as U, useState as c, useEffect as V } from "react";
1
+ import { useState as f, useEffect as U } from "react";
2
2
  function w({
3
3
  value: e,
4
- defaultValue: n,
4
+ defaultValue: o,
5
5
  finalValue: u,
6
6
  onChange: t = () => {
7
7
  },
8
- initialControlledDelay: o = 0
8
+ initialControlledDelay: r = 0
9
9
  }) {
10
- const s = U(!1), [f, i] = c(), [l, d] = c(
11
- n !== void 0 ? n : u
12
- ), m = (r) => {
13
- d(r), t == null || t(r);
10
+ const [c, i] = f(!1), [n, d] = f(
11
+ o !== void 0 ? o : u
12
+ ), m = (s) => {
13
+ d(s), t == null || t(s);
14
14
  };
15
- return V(() => {
16
- (async () => e !== void 0 && (!s.current && o > 0 && (await new Promise(
17
- (r) => setTimeout(r, o)
18
- ), s.current = !0), i(e)))();
19
- }, [e, o]), e !== void 0 ? [f, t, !0] : [l, m, !1];
15
+ return U(() => {
16
+ (async () => e !== void 0 && !c && r > 0 && (await new Promise(
17
+ (s) => setTimeout(s, r)
18
+ ), i(!0)))();
19
+ }, [e, r, c]), e !== void 0 ? !c && r > 0 ? ["", t, !0] : [e, t, !0] : [n, m, !1];
20
20
  }
21
21
  export {
22
22
  w as useUncontrolled
package/dist/index.d.ts CHANGED
@@ -78,29 +78,6 @@ type ObserverRect = Omit<DOMRectReadOnly, "toJSON">;
78
78
  */
79
79
  declare function useResizeObserver<T extends HTMLElement = any>(options?: ResizeObserverOptions): readonly [react.RefObject<T>, ObserverRect];
80
80
 
81
- /**
82
- * MIT License
83
- *
84
- * Copyright (c) 2021 Vitaly Rtishchev
85
- *
86
- * Permission is hereby granted, free of charge, to any person obtaining a copy
87
- * of this software and associated documentation files (the "Software"), to deal
88
- * in the Software without restriction, including without limitation the rights
89
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
90
- * copies of the Software, and to permit persons to whom the Software is
91
- * furnished to do so, subject to the following conditions:
92
- *
93
- * The above copyright notice and this permission notice shall be included in all
94
- * copies or substantial portions of the Software.
95
- *
96
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
99
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
101
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
102
- * SOFTWARE.
103
- */
104
81
  interface UseUncontrolledInput<T> {
105
82
  /** Value for controlled state */
106
83
  value?: T;
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import { useResizeObserver as p } from "./hooks/useResizeObserver.js";
5
5
  import { useUncontrolled as n } from "./hooks/useUncontrolled.js";
6
6
  import { useUniqueId as l } from "./hooks/useUniqueId.js";
7
7
  /*!
8
- @versini/ui-hooks v4.1.2
8
+ @versini/ui-hooks v4.2.0
9
9
  © 2024 gizmette.com
10
10
  */
11
11
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-hooks",
3
- "version": "4.1.2",
3
+ "version": "4.2.0",
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": "d377204b52ba01d6b10692aabc5c0a72c0cf5d8e"
44
+ "gitHead": "6a6a35ebe0703c747695dd047bd90c48a059bea4"
45
45
  }