@sohanemon/utils 3.0.4 → 3.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.
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +10 -23
- package/package.json +1 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { EffectCallback } from 'react';
|
|
2
2
|
export declare const useClickOutside: (callback?: () => void) => any;
|
|
3
3
|
export declare function useMediaQuery(tailwindBreakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | `(${string})`): any;
|
|
4
|
-
export declare const useSwiperRef: () => any[];
|
|
5
4
|
export declare function useEffectOnce(effect: EffectCallback): void;
|
|
6
5
|
export declare function useUpdateEffect(effect: EffectCallback, deps: any[]): void;
|
|
6
|
+
export declare function useDebounce<T>(state: T, delay?: number): T;
|
|
7
7
|
export declare const useIsomorphicEffect: any;
|
|
8
8
|
export declare function useTimeout(callback: () => void, delay?: number | null): void;
|
|
9
9
|
export declare function useWindowEvent<K extends string = keyof WindowEventMap>(type: K, listener: K extends keyof WindowEventMap ? (this: Window, ev: WindowEventMap[K]) => void : (this: Window, ev: CustomEvent) => void, options?: boolean | AddEventListenerOptions): void;
|
package/dist/hooks/index.js
CHANGED
|
@@ -55,29 +55,6 @@ export function useMediaQuery(tailwindBreakpoint) {
|
|
|
55
55
|
}, [parsedQuery]);
|
|
56
56
|
return matches;
|
|
57
57
|
}
|
|
58
|
-
export const useSwiperRef = () => {
|
|
59
|
-
const [navigationElement, setNavigationElement] = useState(null);
|
|
60
|
-
const ref = useRef(null);
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
setNavigationElement(ref.current);
|
|
63
|
-
}, []);
|
|
64
|
-
return [navigationElement, ref];
|
|
65
|
-
};
|
|
66
|
-
// call the hook
|
|
67
|
-
// const [nextEl, nextRef] = useSwiperRef();
|
|
68
|
-
// const [prevEl, prevRef] = useSwiperRef();
|
|
69
|
-
// add to navigation module
|
|
70
|
-
// <Swiper
|
|
71
|
-
// modules={[Navigation]}
|
|
72
|
-
// navigation={{
|
|
73
|
-
// prevEl,
|
|
74
|
-
// nextEl,
|
|
75
|
-
// }}
|
|
76
|
-
// >
|
|
77
|
-
// ...
|
|
78
|
-
// </Swiper>;
|
|
79
|
-
// add ref to any element which may trigger
|
|
80
|
-
// <button ref={nextRef}>...</button>;
|
|
81
58
|
export function useEffectOnce(effect) {
|
|
82
59
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
83
60
|
useEffect(effect, []);
|
|
@@ -94,6 +71,16 @@ export function useUpdateEffect(effect, deps) {
|
|
|
94
71
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
95
72
|
}, deps);
|
|
96
73
|
}
|
|
74
|
+
export function useDebounce(state, delay = 500) {
|
|
75
|
+
const [debouncedState, setDebouncedState] = useState(state);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const timer = setTimeout(() => setDebouncedState(state), delay);
|
|
78
|
+
return () => {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
};
|
|
81
|
+
}, [state, delay]);
|
|
82
|
+
return debouncedState;
|
|
83
|
+
}
|
|
97
84
|
export const useIsomorphicEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
98
85
|
export function useTimeout(callback, delay = 1000) {
|
|
99
86
|
const savedCallback = useRef(callback);
|