@versini/ui-hooks 2.1.0 → 2.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,12 @@
1
+ import { useRef as t, useEffect as a, useCallback as c } from "react";
2
+ function o(r) {
3
+ const e = t(() => {
4
+ throw new Error("Cannot call an event handler while rendering.");
5
+ });
6
+ return a(() => {
7
+ e.current = r;
8
+ }, [r]), c((...n) => e.current(...n), [e]);
9
+ }
10
+ export {
11
+ o as useEventCallback
12
+ };
@@ -1,6 +1,7 @@
1
- import { useCallback as s, useState as f } from "react";
2
- import { useEventListener as h } from "./useEventListener.js";
3
- const u = "av-local-storage", E = (t) => {
1
+ import { useCallback as u, useState as m } from "react";
2
+ import { useEventCallback as w } from "./useEventCallback.js";
3
+ import { useEventListener as E } from "./useEventListener.js";
4
+ const s = "av-local-storage", h = (t) => {
4
5
  try {
5
6
  return JSON.stringify(t);
6
7
  } catch {
@@ -8,54 +9,51 @@ const u = "av-local-storage", E = (t) => {
8
9
  "@versini/ui-hooks useLocalStorage: Failed to serialize the value"
9
10
  );
10
11
  }
11
- }, m = (t) => {
12
+ }, p = (t) => {
12
13
  try {
13
14
  return t && JSON.parse(t);
14
15
  } catch {
15
16
  return t;
16
17
  }
17
18
  };
18
- function N({
19
+ function V({
19
20
  key: t,
20
- defaultValue: l,
21
- deserialize: w = m,
22
- serialize: n = (e) => E(e)
21
+ defaultValue: n,
22
+ deserialize: g = p,
23
+ serialize: c = (o) => h(o)
23
24
  }) {
24
- const e = typeof l == "function" ? l() : l, a = s(() => {
25
- let o;
25
+ const o = typeof n == "function" ? n() : n, a = u(() => {
26
+ let r;
26
27
  try {
27
- o = typeof window > "u" || !("localStorage" in window) || window.localStorage === null;
28
+ r = typeof window > "u" || !("localStorage" in window) || window.localStorage === null;
28
29
  } catch {
29
- o = !0;
30
+ r = !0;
30
31
  }
31
- if (o)
32
- return window.localStorage.setItem(t, n(e)), e;
32
+ if (r)
33
+ return window.localStorage.setItem(t, c(o)), o;
33
34
  try {
34
- const r = window.localStorage.getItem(t);
35
- return r !== null ? w(r) : (window.localStorage.setItem(t, n(e)), e);
36
- } catch (r) {
37
- return console.warn(`Error reading localStorage key “${t}”:`, r), e;
35
+ const e = window.localStorage.getItem(t);
36
+ return e !== null ? g(e) : (window.localStorage.setItem(t, c(o)), o);
37
+ } catch (e) {
38
+ return console.warn(`Error reading localStorage key “${t}”:`, e), o;
38
39
  }
39
- }, [e, w, t, n]), [c, i] = f(a()), g = s(
40
- (o) => {
41
- try {
42
- const r = o instanceof Function ? o(c) : o;
43
- if (r === void 0)
44
- return;
45
- window.localStorage.setItem(t, n(r)), i(r), window.dispatchEvent(new Event(u));
46
- } catch (r) {
47
- console.warn(`Error setting localStorage key “${t}”:`, r);
48
- }
49
- },
50
- [t, n, c]
51
- ), S = s(() => (window.localStorage.removeItem(t), i(a()), window.dispatchEvent(new Event(u)), c), [t, a, c]), d = s(
52
- (o) => {
53
- o != null && o.key && o.key !== t || i(a());
40
+ }, [o, t]), [l, i] = m(a()), S = w((r) => {
41
+ try {
42
+ const e = r instanceof Function ? r(l) : r;
43
+ if (e === void 0)
44
+ return;
45
+ window.localStorage.setItem(t, c(e)), i(e), window.dispatchEvent(new Event(s));
46
+ } catch (e) {
47
+ console.warn(`Error setting localStorage key “${t}”:`, e);
48
+ }
49
+ }), d = w(() => (window.localStorage.removeItem(t), i(a()), window.dispatchEvent(new Event(s)), l)), f = u(
50
+ (r) => {
51
+ r != null && r.key && r.key !== t || i(a());
54
52
  },
55
53
  [t, a]
56
54
  );
57
- return h(u, d), [c, g, S];
55
+ return E(s, f), [l, S, d];
58
56
  }
59
57
  export {
60
- N as useLocalStorage
58
+ V as useLocalStorage
61
59
  };
package/dist/index.js CHANGED
@@ -1,16 +1,17 @@
1
- import { useLocalStorage as m } from "./hooks/useLocalStorage.js";
2
- import { useMergeRefs as f } from "./hooks/useMergeRefs.js";
3
- import { useUncontrolled as u } from "./hooks/useUncontrolled.js";
4
- import { useUniqueId as i } from "./hooks/useUniqueId.js";
1
+ import { useLocalStorage as p } from "./hooks/useLocalStorage.js";
2
+ import { useMergeRefs as s } from "./hooks/useMergeRefs.js";
3
+ import { useUncontrolled as i } from "./hooks/useUncontrolled.js";
4
+ import { useUniqueId as l } from "./hooks/useUniqueId.js";
5
5
  import "react";
6
+ import "./hooks/useEventCallback.js";
6
7
  import "./hooks/useEventListener.js";
7
8
  /*!
8
- @versini/ui-hooks v2.1.0
9
+ @versini/ui-hooks v2.2.0
9
10
  © 2024 gizmette.com
10
11
  */
11
12
  export {
12
- m as useLocalStorage,
13
- f as useMergeRefs,
14
- u as useUncontrolled,
15
- i as useUniqueId
13
+ p as useLocalStorage,
14
+ s as useMergeRefs,
15
+ i as useUncontrolled,
16
+ l as useUniqueId
16
17
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-hooks",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -41,5 +41,5 @@
41
41
  "react": "18.2.0",
42
42
  "react-dom": "18.2.0"
43
43
  },
44
- "gitHead": "f554b1df1fcdd2b47b6e92fd7bbaaf4d91ea53ef"
44
+ "gitHead": "cd36ec402376c003c3e08dada4a9770a65ca9c46"
45
45
  }