@versini/ui-hooks 2.1.0 → 2.1.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/dist/hooks/useEventCallback.js +12 -0
- package/dist/hooks/useLocalStorage.js +33 -35
- package/dist/index.js +10 -9
- package/package.json +2 -2
|
@@ -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
|
|
2
|
-
import {
|
|
3
|
-
|
|
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
|
-
},
|
|
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
|
|
19
|
+
function V({
|
|
19
20
|
key: t,
|
|
20
|
-
defaultValue:
|
|
21
|
-
deserialize:
|
|
22
|
-
serialize:
|
|
21
|
+
defaultValue: n,
|
|
22
|
+
deserialize: g = p,
|
|
23
|
+
serialize: c = (o) => h(o)
|
|
23
24
|
}) {
|
|
24
|
-
const
|
|
25
|
-
let
|
|
25
|
+
const o = typeof n == "function" ? n() : n, a = u(() => {
|
|
26
|
+
let r;
|
|
26
27
|
try {
|
|
27
|
-
|
|
28
|
+
r = typeof window > "u" || !("localStorage" in window) || window.localStorage === null;
|
|
28
29
|
} catch {
|
|
29
|
-
|
|
30
|
+
r = !0;
|
|
30
31
|
}
|
|
31
|
-
if (
|
|
32
|
-
return window.localStorage.setItem(t,
|
|
32
|
+
if (r)
|
|
33
|
+
return window.localStorage.setItem(t, c(o)), o;
|
|
33
34
|
try {
|
|
34
|
-
const
|
|
35
|
-
return
|
|
36
|
-
} catch (
|
|
37
|
-
return console.warn(`Error reading localStorage key “${t}”:`,
|
|
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
|
-
}, [
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
|
55
|
+
return E(s, f), [l, S, d];
|
|
58
56
|
}
|
|
59
57
|
export {
|
|
60
|
-
|
|
58
|
+
V as useLocalStorage
|
|
61
59
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { useLocalStorage as
|
|
2
|
-
import { useMergeRefs as
|
|
3
|
-
import { useUncontrolled as
|
|
4
|
-
import { useUniqueId as
|
|
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.
|
|
9
|
+
@versini/ui-hooks v2.1.1
|
|
9
10
|
© 2024 gizmette.com
|
|
10
11
|
*/
|
|
11
12
|
export {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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.
|
|
3
|
+
"version": "2.1.1",
|
|
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": "
|
|
44
|
+
"gitHead": "36efe490aaadd28598f457a90c6876ed30ed927b"
|
|
45
45
|
}
|