@versini/ui-hooks 1.0.0 → 2.1.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,18 @@
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
+ };
@@ -0,0 +1,61 @@
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) => {
4
+ try {
5
+ return JSON.stringify(t);
6
+ } catch {
7
+ throw new Error(
8
+ "@versini/ui-hooks useLocalStorage: Failed to serialize the value"
9
+ );
10
+ }
11
+ }, m = (t) => {
12
+ try {
13
+ return t && JSON.parse(t);
14
+ } catch {
15
+ return t;
16
+ }
17
+ };
18
+ function N({
19
+ key: t,
20
+ defaultValue: l,
21
+ deserialize: w = m,
22
+ serialize: n = (e) => E(e)
23
+ }) {
24
+ const e = typeof l == "function" ? l() : l, a = s(() => {
25
+ let o;
26
+ try {
27
+ o = typeof window > "u" || !("localStorage" in window) || window.localStorage === null;
28
+ } catch {
29
+ o = !0;
30
+ }
31
+ if (o)
32
+ return window.localStorage.setItem(t, n(e)), e;
33
+ 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;
38
+ }
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());
54
+ },
55
+ [t, a]
56
+ );
57
+ return h(u, d), [c, g, S];
58
+ }
59
+ export {
60
+ N as useLocalStorage
61
+ };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,41 @@
1
+ declare const CUSTOM_EVENT_NAME = "av-local-storage";
2
+ declare global {
3
+ interface WindowEventMap {
4
+ [CUSTOM_EVENT_NAME]: CustomEvent;
5
+ }
6
+ }
7
+ interface StorageProperties<T> {
8
+ /**
9
+ * Storage key.
10
+ */
11
+ key: string;
12
+ /**
13
+ * Default value that will be set if value is not found in storage.
14
+ */
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;
24
+ }
25
+ /**
26
+ *
27
+ * @example
28
+ * import { useLocalStorage } from '@versini/ui-hooks';
29
+ * const [value, setValue] = useLocalStorage({
30
+ * key: 'gpt-model',
31
+ * defaultValue: 'gpt-3',
32
+ * });
33
+ *
34
+ * setValue('gpt-4');
35
+ * setValue((current) => (current === 'gpt-3' ? 'gpt-4' : 'gpt-3'));
36
+ */
37
+ declare function useLocalStorage<T = string>({ key, defaultValue, deserialize, serialize, }: StorageProperties<T>): [T, (val: T | ((prevState: T) => T)) => void, () => void];
38
+
1
39
  /**
2
40
  * React utility to merge refs.
3
41
  *
@@ -91,4 +129,4 @@ type UseUniqueIdOptions = string | number | {
91
129
  };
92
130
  declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
93
131
 
94
- export { useMergeRefs, useUncontrolled, useUniqueId };
132
+ export { useLocalStorage, useMergeRefs, useUncontrolled, useUniqueId };
package/dist/index.js CHANGED
@@ -1,13 +1,16 @@
1
- import { useMergeRefs as t } from "./hooks/useMergeRefs.js";
2
- import { useUncontrolled as m } from "./hooks/useUncontrolled.js";
3
- import { useUniqueId as s } from "./hooks/useUniqueId.js";
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";
4
5
  import "react";
6
+ import "./hooks/useEventListener.js";
5
7
  /*!
6
- @versini/ui-hooks v1.0.0
8
+ @versini/ui-hooks v2.1.0
7
9
  © 2024 gizmette.com
8
10
  */
9
11
  export {
10
- t as useMergeRefs,
11
- m as useUncontrolled,
12
- s as useUniqueId
12
+ m as useLocalStorage,
13
+ f as useMergeRefs,
14
+ u as useUncontrolled,
15
+ i as useUniqueId
13
16
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-hooks",
3
- "version": "1.0.0",
3
+ "version": "2.1.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": "b3908b5958fb8dab384172191bab101229be48bd"
44
+ "gitHead": "f554b1df1fcdd2b47b6e92fd7bbaaf4d91ea53ef"
45
45
  }