@versini/ui-hooks 2.0.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/useEventListener.js +18 -0
- package/dist/hooks/useLocalStorage.js +59 -0
- package/dist/index.d.ts +39 -1
- package/dist/index.js +11 -7
- 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
|
+
};
|
|
@@ -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,59 @@
|
|
|
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({
|
|
20
|
+
key: t,
|
|
21
|
+
defaultValue: n,
|
|
22
|
+
deserialize: g = p,
|
|
23
|
+
serialize: c = (o) => h(o)
|
|
24
|
+
}) {
|
|
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) => {
|
|
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());
|
|
52
|
+
},
|
|
53
|
+
[t, a]
|
|
54
|
+
);
|
|
55
|
+
return E(s, f), [l, S, d];
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
V as useLocalStorage
|
|
59
|
+
};
|
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,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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";
|
|
4
5
|
import "react";
|
|
6
|
+
import "./hooks/useEventCallback.js";
|
|
7
|
+
import "./hooks/useEventListener.js";
|
|
5
8
|
/*!
|
|
6
|
-
@versini/ui-hooks v2.
|
|
9
|
+
@versini/ui-hooks v2.1.1
|
|
7
10
|
© 2024 gizmette.com
|
|
8
11
|
*/
|
|
9
12
|
export {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
p as useLocalStorage,
|
|
14
|
+
s as useMergeRefs,
|
|
15
|
+
i as useUncontrolled,
|
|
16
|
+
l as useUniqueId
|
|
13
17
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-hooks",
|
|
3
|
-
"version": "2.
|
|
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
|
}
|