@versini/ui-hooks 3.0.0 → 4.0.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.
- package/dist/hooks/useLocalStorage.js +35 -81
- package/dist/index.d.ts +26 -2
- package/dist/index.js +9 -9
- package/package.json +2 -5
|
@@ -1,91 +1,45 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const d = h(e);
|
|
5
|
-
m(() => {
|
|
6
|
-
d.current = e;
|
|
7
|
-
}, [e]), g(() => {
|
|
8
|
-
const a = window;
|
|
9
|
-
if (!(a && a.addEventListener))
|
|
10
|
-
return;
|
|
11
|
-
const s = (f) => {
|
|
12
|
-
d.current(f);
|
|
13
|
-
};
|
|
14
|
-
return a.addEventListener(r, s, c), () => {
|
|
15
|
-
a.removeEventListener(r, s, c);
|
|
16
|
-
};
|
|
17
|
-
}, [r, t, c]);
|
|
1
|
+
import * as s from "react";
|
|
2
|
+
function g(t, e) {
|
|
3
|
+
window.dispatchEvent(new StorageEvent("storage", { key: t, newValue: e }));
|
|
18
4
|
}
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
), a = u(
|
|
5
|
+
const l = (t, e) => {
|
|
6
|
+
const c = JSON.stringify(
|
|
7
|
+
typeof e == "function" ? e() : e
|
|
8
|
+
);
|
|
9
|
+
window.localStorage.setItem(t, c), g(t, c);
|
|
10
|
+
}, w = (t) => {
|
|
11
|
+
window.localStorage.removeItem(t), g(t, null);
|
|
12
|
+
}, u = (t) => window.localStorage.getItem(t), d = (t) => (window.addEventListener("storage", t), () => window.removeEventListener("storage", t));
|
|
13
|
+
function i({
|
|
14
|
+
key: t,
|
|
15
|
+
initialValue: e
|
|
16
|
+
}) {
|
|
17
|
+
const c = () => u(t), a = s.useSyncExternalStore(
|
|
18
|
+
d,
|
|
19
|
+
c
|
|
20
|
+
), o = s.useCallback(
|
|
36
21
|
(n) => {
|
|
37
|
-
if (t.deserializer)
|
|
38
|
-
return t.deserializer(n);
|
|
39
|
-
if (n === "undefined")
|
|
40
|
-
return;
|
|
41
|
-
const o = e instanceof Function ? e() : e;
|
|
42
|
-
let E;
|
|
43
22
|
try {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
23
|
+
const r = typeof n == "function" ? n(JSON.parse(a)) : n;
|
|
24
|
+
r == null ? w(t) : l(t, r);
|
|
25
|
+
} catch (r) {
|
|
26
|
+
console.warn(r);
|
|
47
27
|
}
|
|
48
|
-
return E;
|
|
49
28
|
},
|
|
50
|
-
[t,
|
|
51
|
-
),
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return o ? a(o) : n;
|
|
58
|
-
} catch (o) {
|
|
59
|
-
return console.warn(`Error reading localStorage key “${r}”:`, o), n;
|
|
60
|
-
}
|
|
61
|
-
}, [e, r, a]), [f, i] = F(() => c ? s() : e instanceof Function ? e() : e), C = v((n) => {
|
|
62
|
-
l && console.warn(
|
|
63
|
-
`Tried setting localStorage key “${r}” even though environment is not a client`
|
|
64
|
-
);
|
|
29
|
+
[t, a]
|
|
30
|
+
), S = s.useCallback(() => {
|
|
31
|
+
o(e);
|
|
32
|
+
}, [e, o]), f = s.useCallback(() => {
|
|
33
|
+
o(null);
|
|
34
|
+
}, [o]);
|
|
35
|
+
return s.useEffect(() => {
|
|
65
36
|
try {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
console.warn(`Error setting localStorage key “${r}”:`, o);
|
|
37
|
+
u(t) === null && typeof e < "u" && l(t, e);
|
|
38
|
+
} catch (n) {
|
|
39
|
+
console.warn(n);
|
|
70
40
|
}
|
|
71
|
-
}),
|
|
72
|
-
l && console.warn(
|
|
73
|
-
`Tried removing localStorage key “${r}” even though environment is not a client`
|
|
74
|
-
);
|
|
75
|
-
const n = e instanceof Function ? e() : e;
|
|
76
|
-
window.localStorage.removeItem(r), i(n), window.dispatchEvent(new StorageEvent("local-storage", { key: r }));
|
|
77
|
-
});
|
|
78
|
-
g(() => {
|
|
79
|
-
i(s());
|
|
80
|
-
}, [r]);
|
|
81
|
-
const w = u(
|
|
82
|
-
(n) => {
|
|
83
|
-
n.key && n.key !== r || i(s());
|
|
84
|
-
},
|
|
85
|
-
[r, s]
|
|
86
|
-
);
|
|
87
|
-
return S("storage", w), S("local-storage", w), [f, C, z];
|
|
41
|
+
}, [t, e]), [a ? JSON.parse(a) : null, o, S, f];
|
|
88
42
|
}
|
|
89
43
|
export {
|
|
90
|
-
|
|
44
|
+
i as useLocalStorage
|
|
91
45
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
interface StorageProperties<T> {
|
|
2
|
+
/**
|
|
3
|
+
* Storage key.
|
|
4
|
+
*/
|
|
5
|
+
key: string;
|
|
6
|
+
/**
|
|
7
|
+
* Default value that will be set if value is not found in storage.
|
|
8
|
+
*/
|
|
9
|
+
initialValue?: T;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { useLocalStorage } from '@versini/ui-hooks';
|
|
15
|
+
* const [value, setValue, resetValue, removeValue] = useLocalStorage({
|
|
16
|
+
* key: 'gpt-model',
|
|
17
|
+
* initialValue: 'gpt-3',
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* setValue('gpt-4'); ==> "gpt-4"
|
|
21
|
+
* setValue((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
|
|
22
|
+
* resetValue(); ==> "gpt-3"
|
|
23
|
+
* removeValue(); ==> null
|
|
24
|
+
*/
|
|
25
|
+
declare function useLocalStorage<T>({ key, initialValue, }: StorageProperties<T>): any[];
|
|
2
26
|
|
|
3
27
|
/**
|
|
4
28
|
* React utility to merge refs.
|
|
@@ -93,4 +117,4 @@ type UseUniqueIdOptions = string | number | {
|
|
|
93
117
|
};
|
|
94
118
|
declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
|
|
95
119
|
|
|
96
|
-
export { useMergeRefs, useUncontrolled, useUniqueId };
|
|
120
|
+
export { useLocalStorage, useMergeRefs, useUncontrolled, useUniqueId };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { useLocalStorage as r } 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 p } from "./hooks/useUniqueId.js";
|
|
5
5
|
/*!
|
|
6
|
-
@versini/ui-hooks
|
|
6
|
+
@versini/ui-hooks v4.0.0
|
|
7
7
|
© 2024 gizmette.com
|
|
8
8
|
*/
|
|
9
9
|
export {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
r as useLocalStorage,
|
|
11
|
+
f as useMergeRefs,
|
|
12
|
+
u as useUncontrolled,
|
|
13
|
+
p as useUniqueId
|
|
14
14
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-hooks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -41,8 +41,5 @@
|
|
|
41
41
|
"react": "18.3.1",
|
|
42
42
|
"react-dom": "18.3.1"
|
|
43
43
|
},
|
|
44
|
-
"
|
|
45
|
-
"usehooks-ts": "3.1.0"
|
|
46
|
-
},
|
|
47
|
-
"gitHead": "e3e5df0ce39be11620614a8f47de214155b589f5"
|
|
44
|
+
"gitHead": "40c5513a982e3c2f8ee1905d4f5d3acbb81062ca"
|
|
48
45
|
}
|