@versini/ui-hooks 3.0.1 → 4.0.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.
@@ -1,59 +1,42 @@
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) => {
5
- try {
6
- return JSON.stringify(t);
7
- } catch {
8
- throw new Error(
9
- "@versini/ui-hooks useLocalStorage: Failed to serialize the value"
10
- );
11
- }
12
- }, p = (t) => {
13
- try {
14
- return t && JSON.parse(t);
15
- } catch {
16
- return t;
17
- }
18
- };
19
- function V({
1
+ import { useSyncExternalStore as w, useCallback as a, useEffect as d } from "react";
2
+ function S(t, e) {
3
+ window.dispatchEvent(new StorageEvent("storage", { key: t, newValue: e }));
4
+ }
5
+ const g = (t, e) => {
6
+ const c = JSON.stringify(
7
+ typeof e == "function" ? e() : e
8
+ );
9
+ window.localStorage.setItem(t, c), S(t, c);
10
+ }, i = (t) => {
11
+ window.localStorage.removeItem(t), S(t, null);
12
+ }, l = (t) => window.localStorage.getItem(t), m = (t) => (window.addEventListener("storage", t), () => window.removeEventListener("storage", t));
13
+ function E({
20
14
  key: t,
21
- defaultValue: n,
22
- deserialize: g = p,
23
- serialize: c = (o) => h(o)
15
+ initialValue: e
24
16
  }) {
25
- const o = typeof n == "function" ? n() : n, a = u(() => {
26
- let r;
27
- try {
28
- r = typeof window > "u" || !("localStorage" in window) || window.localStorage === null;
29
- } catch {
30
- r = !0;
31
- }
32
- if (r)
33
- return window.localStorage.setItem(t, c(o)), o;
34
- try {
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;
39
- }
40
- }, [o, t]), [l, i] = m(a()), S = w((r) => {
17
+ const s = w(m, () => l(t)), o = a(
18
+ (n) => {
19
+ try {
20
+ const r = typeof n == "function" ? n(JSON.parse(s)) : n;
21
+ r == null ? i(t) : g(t, r);
22
+ } catch (r) {
23
+ console.warn(r);
24
+ }
25
+ },
26
+ [t, s]
27
+ ), u = a(() => {
28
+ o(e);
29
+ }, [e, o]), f = a(() => {
30
+ o(null);
31
+ }, [o]);
32
+ return d(() => {
41
33
  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);
34
+ l(t) === null && typeof e < "u" && g(t, e);
35
+ } catch (n) {
36
+ console.warn(n);
48
37
  }
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());
52
- },
53
- [t, a]
54
- );
55
- return E(s, f), [l, S, d];
38
+ }, [t, e]), [s ? JSON.parse(s) : null, o, u, f];
56
39
  }
57
40
  export {
58
- V as useLocalStorage
41
+ E as useLocalStorage
59
42
  };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- declare const CUSTOM_EVENT_NAME = "av-local-storage";
2
- declare global {
3
- interface WindowEventMap {
4
- [CUSTOM_EVENT_NAME]: CustomEvent;
5
- }
6
- }
7
1
  interface StorageProperties<T> {
8
2
  /**
9
3
  * Storage key.
@@ -12,29 +6,23 @@ interface StorageProperties<T> {
12
6
  /**
13
7
  * Default value that will be set if value is not found in storage.
14
8
  */
15
- defaultValue?: T;
16
- /**
17
- * Function to serialize value into string to be saved in storage.
18
- */
19
- serialize?: (value: T) => string;
20
- /**
21
- * Function to deserialize string value from storage to value.
22
- */
23
- deserialize?: (value: string | undefined) => T;
9
+ initialValue?: T;
24
10
  }
25
11
  /**
26
12
  *
27
13
  * @example
28
14
  * import { useLocalStorage } from '@versini/ui-hooks';
29
- * const [value, setValue] = useLocalStorage({
15
+ * const [value, setValue, resetValue, removeValue] = useLocalStorage({
30
16
  * key: 'gpt-model',
31
- * defaultValue: 'gpt-3',
17
+ * initialValue: 'gpt-3',
32
18
  * });
33
19
  *
34
- * setValue('gpt-4');
20
+ * setValue('gpt-4'); ==> "gpt-4"
35
21
  * setValue((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
22
+ * resetValue(); ==> "gpt-3"
23
+ * removeValue(); ==> null
36
24
  */
37
- declare function useLocalStorage<T = string>({ key, defaultValue, deserialize, serialize, }: StorageProperties<T>): [T, (val: T | ((prevState: T) => T)) => void, () => void];
25
+ declare function useLocalStorage<T>({ key, initialValue, }: StorageProperties<T>): any[];
38
26
 
39
27
  /**
40
28
  * React utility to merge refs.
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import { useMergeRefs as f } from "./hooks/useMergeRefs.js";
3
3
  import { useUncontrolled as u } from "./hooks/useUncontrolled.js";
4
4
  import { useUniqueId as p } from "./hooks/useUniqueId.js";
5
5
  /*!
6
- @versini/ui-hooks v3.0.1
6
+ @versini/ui-hooks v4.0.1
7
7
  © 2024 gizmette.com
8
8
  */
9
9
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-hooks",
3
- "version": "3.0.1",
3
+ "version": "4.0.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": "9e9b109b35d7ee476d6ff96cdd70bc9ef276ddb7"
44
+ "gitHead": "5ddede585a2476c9eddc74162cf10c30d0f1fe88"
45
45
  }
@@ -1,12 +0,0 @@
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,18 +0,0 @@
1
- import { useRef as E, useEffect as i } from "react";
2
- function o(n, c, r, e) {
3
- const s = E(c);
4
- i(() => {
5
- s.current = c;
6
- }, [c]), i(() => {
7
- const t = (r == null ? void 0 : r.current) ?? window;
8
- if (!(t && t.addEventListener))
9
- return;
10
- const u = (f) => s.current(f);
11
- return t.addEventListener(n, u, e), () => {
12
- t.removeEventListener(n, u, e);
13
- };
14
- }, [n, r, e]);
15
- }
16
- export {
17
- o as useEventListener
18
- };