tona-hooks 1.0.19 → 1.0.21
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/index.d.ts +12 -2
- package/dist/index.js +63 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dispatch, EffectCallback, useEffect } from "preact/hooks";
|
|
2
|
-
import { SetStateAction } from "preact/compat";
|
|
2
|
+
import { Dispatch as Dispatch$1, SetStateAction } from "preact/compat";
|
|
3
3
|
import { RefObject } from "preact";
|
|
4
4
|
|
|
5
5
|
//#region src/use-ajax-complete.d.ts
|
|
@@ -27,6 +27,16 @@ declare function useEventCallback<Args extends unknown[], R>(fn: ((...args: Args
|
|
|
27
27
|
//#region src/use-isomorphic-layout-effect.d.ts
|
|
28
28
|
declare const useIsomorphicLayoutEffect: typeof useEffect;
|
|
29
29
|
//#endregion
|
|
30
|
+
//#region src/use-local-storage.d.ts
|
|
31
|
+
type parserOptions<T> = {
|
|
32
|
+
raw: true;
|
|
33
|
+
} | {
|
|
34
|
+
raw: false;
|
|
35
|
+
serializer: (value: T) => string;
|
|
36
|
+
deserializer: (value: string) => T;
|
|
37
|
+
};
|
|
38
|
+
declare const useLocalStorage: <T>(key: string, initialValue?: T, options?: parserOptions<T>) => [T | undefined, Dispatch$1<SetStateAction<T | undefined>>, () => void];
|
|
39
|
+
//#endregion
|
|
30
40
|
//#region src/use-query-dom.d.ts
|
|
31
41
|
interface UseQueryDomOptions<T> {
|
|
32
42
|
selector: string;
|
|
@@ -68,4 +78,4 @@ interface State {
|
|
|
68
78
|
}
|
|
69
79
|
declare const useWindowScroll: () => State;
|
|
70
80
|
//#endregion
|
|
71
|
-
export { useAjaxComplete, useEffectOnce, useEventCallback, useIsomorphicLayoutEffect, useQueryDOM, useRafState, useScroll, useUnmount, useWindowScroll };
|
|
81
|
+
export { useAjaxComplete, useEffectOnce, useEventCallback, useIsomorphicLayoutEffect, useLocalStorage, useQueryDOM, useRafState, useScroll, useUnmount, useWindowScroll };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
|
|
2
|
-
import { useCallback as useCallback$1, useRef as useRef$1 } from "preact/compat";
|
|
2
|
+
import { useCallback as useCallback$1, useLayoutEffect as useLayoutEffect$1, useRef as useRef$1, useState as useState$1 } from "preact/compat";
|
|
3
3
|
|
|
4
4
|
//#region src/use-ajax-complete.ts
|
|
5
5
|
/**
|
|
@@ -50,6 +50,67 @@ function useEventCallback(fn) {
|
|
|
50
50
|
return useCallback$1((...args) => ref.current?.(...args), [ref]);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/misc/util.ts
|
|
55
|
+
const noop = () => {};
|
|
56
|
+
function on(obj, ...args) {
|
|
57
|
+
if (obj?.addEventListener) obj.addEventListener(...args);
|
|
58
|
+
}
|
|
59
|
+
function off(obj, ...args) {
|
|
60
|
+
if (obj?.removeEventListener) obj.removeEventListener(...args);
|
|
61
|
+
}
|
|
62
|
+
const isBrowser = typeof window !== "undefined";
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/use-local-storage.ts
|
|
66
|
+
const useLocalStorage = (key, initialValue, options) => {
|
|
67
|
+
if (!isBrowser) return [
|
|
68
|
+
initialValue,
|
|
69
|
+
noop,
|
|
70
|
+
noop
|
|
71
|
+
];
|
|
72
|
+
if (!key) throw new Error("useLocalStorage key may not be falsy");
|
|
73
|
+
const deserializer = options ? options.raw ? (value) => value : options.deserializer : JSON.parse;
|
|
74
|
+
const initializer = useRef$1((key$1) => {
|
|
75
|
+
try {
|
|
76
|
+
const serializer = options ? options.raw ? String : options.serializer : JSON.stringify;
|
|
77
|
+
const localStorageValue = localStorage.getItem(key$1);
|
|
78
|
+
if (localStorageValue !== null) return deserializer(localStorageValue);
|
|
79
|
+
else {
|
|
80
|
+
initialValue && localStorage.setItem(key$1, serializer(initialValue));
|
|
81
|
+
return initialValue;
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
return initialValue;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const [state, setState] = useState$1(() => initializer.current(key));
|
|
88
|
+
useLayoutEffect$1(() => setState(initializer.current(key)), [key]);
|
|
89
|
+
return [
|
|
90
|
+
state,
|
|
91
|
+
useCallback$1((valOrFunc) => {
|
|
92
|
+
try {
|
|
93
|
+
const newState = typeof valOrFunc === "function" ? valOrFunc(state) : valOrFunc;
|
|
94
|
+
if (typeof newState === "undefined") return;
|
|
95
|
+
let value;
|
|
96
|
+
if (options) if (options.raw) if (typeof newState === "string") value = newState;
|
|
97
|
+
else value = JSON.stringify(newState);
|
|
98
|
+
else if (options.serializer) value = options.serializer(newState);
|
|
99
|
+
else value = JSON.stringify(newState);
|
|
100
|
+
else value = JSON.stringify(newState);
|
|
101
|
+
localStorage.setItem(key, value);
|
|
102
|
+
setState(deserializer(value));
|
|
103
|
+
} catch {}
|
|
104
|
+
}, [key, setState]),
|
|
105
|
+
useCallback$1(() => {
|
|
106
|
+
try {
|
|
107
|
+
localStorage.removeItem(key);
|
|
108
|
+
setState(void 0);
|
|
109
|
+
} catch {}
|
|
110
|
+
}, [key, setState])
|
|
111
|
+
];
|
|
112
|
+
};
|
|
113
|
+
|
|
53
114
|
//#endregion
|
|
54
115
|
//#region src/use-query-dom.ts
|
|
55
116
|
function useQueryDOM({ selector, observe = false, queryFn, ajaxUrl }) {
|
|
@@ -176,16 +237,6 @@ const useRafState = (initialState) => {
|
|
|
176
237
|
return [state, setRafState];
|
|
177
238
|
};
|
|
178
239
|
|
|
179
|
-
//#endregion
|
|
180
|
-
//#region src/misc/util.ts
|
|
181
|
-
function on(obj, ...args) {
|
|
182
|
-
if (obj?.addEventListener) obj.addEventListener(...args);
|
|
183
|
-
}
|
|
184
|
-
function off(obj, ...args) {
|
|
185
|
-
if (obj?.removeEventListener) obj.removeEventListener(...args);
|
|
186
|
-
}
|
|
187
|
-
const isBrowser = typeof window !== "undefined";
|
|
188
|
-
|
|
189
240
|
//#endregion
|
|
190
241
|
//#region src/use-scroll.ts
|
|
191
242
|
const useScroll = (ref) => {
|
|
@@ -242,4 +293,4 @@ const useWindowScroll = () => {
|
|
|
242
293
|
};
|
|
243
294
|
|
|
244
295
|
//#endregion
|
|
245
|
-
export { useAjaxComplete, useEffectOnce, useEventCallback, useIsomorphicLayoutEffect, useQueryDOM, useRafState, useScroll, useUnmount, useWindowScroll };
|
|
296
|
+
export { useAjaxComplete, useEffectOnce, useEventCallback, useIsomorphicLayoutEffect, useLocalStorage, useQueryDOM, useRafState, useScroll, useUnmount, useWindowScroll };
|