@sohanemon/utils 4.0.26 → 4.0.27
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 +12 -6
- package/dist/hooks/index.js +41 -26
- package/package.json +1 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React from 'react';
|
|
2
2
|
export * from './action';
|
|
3
|
-
export declare const useClickOutside: (callback?: () => void) =>
|
|
3
|
+
export declare const useClickOutside: (callback?: () => void) => React.MutableRefObject<HTMLDivElement>;
|
|
4
4
|
export declare function useMediaQuery(tailwindBreakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | `(${string})`): boolean;
|
|
5
|
-
export declare function useEffectOnce(effect: EffectCallback): void;
|
|
6
|
-
export declare function useUpdateEffect(effect: EffectCallback, deps: any[]): void;
|
|
5
|
+
export declare function useEffectOnce(effect: React.EffectCallback): void;
|
|
6
|
+
export declare function useUpdateEffect(effect: React.EffectCallback, deps: any[]): void;
|
|
7
7
|
export declare function useDebounce<T>(state: T, delay?: number): T;
|
|
8
|
-
export declare const useIsomorphicEffect: typeof useLayoutEffect;
|
|
8
|
+
export declare const useIsomorphicEffect: typeof React.useLayoutEffect;
|
|
9
9
|
export declare function useTimeout(callback: () => void, delay?: number | null): void;
|
|
10
10
|
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;
|
|
11
|
-
type LocalStorageValue<T> = [T, Dispatch<SetStateAction<T>>];
|
|
11
|
+
type LocalStorageValue<T> = [T, React.Dispatch<React.SetStateAction<T>>];
|
|
12
12
|
export declare const useLocalStorage: <T extends Record<string, any>>(key: string, defaultValue: T) => LocalStorageValue<T>;
|
|
13
13
|
export declare const useUrlParams: <T extends string | number | boolean>(key: string, defaultValue: T) => [T, (value: T) => void];
|
|
14
14
|
export declare const useQuerySelector: <T extends Element>(selector: string) => T | null;
|
|
@@ -20,3 +20,9 @@ export declare function useCopyToClipboard({ timeout }: {
|
|
|
20
20
|
isCopied: Boolean;
|
|
21
21
|
copy: (value: string) => void;
|
|
22
22
|
};
|
|
23
|
+
type CalculationProps = {
|
|
24
|
+
blockIds: string[];
|
|
25
|
+
dynamic?: boolean;
|
|
26
|
+
margin?: number;
|
|
27
|
+
};
|
|
28
|
+
export declare const useHeightCalculation: ({ blockIds, margin, dynamic, }: CalculationProps) => number;
|
package/dist/hooks/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
3
2
|
import { copyToClipboard } from '../functions';
|
|
3
|
+
import * as React from 'react';
|
|
4
4
|
export * from './action';
|
|
5
5
|
export const useClickOutside = (callback = () => alert('clicked outside')) => {
|
|
6
|
-
const ref = useRef(null);
|
|
6
|
+
const ref = React.useRef(null);
|
|
7
7
|
const listener = (e) => {
|
|
8
8
|
if (ref.current && !ref.current.contains(e.target)) {
|
|
9
9
|
callback();
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
|
-
useEffect(() => {
|
|
12
|
+
React.useEffect(() => {
|
|
13
13
|
document.addEventListener('mousedown', listener);
|
|
14
14
|
document.addEventListener('touchstart', listener);
|
|
15
15
|
return () => {
|
|
@@ -20,7 +20,7 @@ export const useClickOutside = (callback = () => alert('clicked outside')) => {
|
|
|
20
20
|
return ref;
|
|
21
21
|
};
|
|
22
22
|
export function useMediaQuery(tailwindBreakpoint) {
|
|
23
|
-
const parsedQuery = useMemo(() => {
|
|
23
|
+
const parsedQuery = React.useMemo(() => {
|
|
24
24
|
switch (tailwindBreakpoint) {
|
|
25
25
|
case 'sm':
|
|
26
26
|
return '(min-width: 640px)';
|
|
@@ -42,12 +42,12 @@ export function useMediaQuery(tailwindBreakpoint) {
|
|
|
42
42
|
}
|
|
43
43
|
return false;
|
|
44
44
|
};
|
|
45
|
-
const [matches, setMatches] = useState(getMatches(parsedQuery));
|
|
45
|
+
const [matches, setMatches] = React.useState(getMatches(parsedQuery));
|
|
46
46
|
function handleChange() {
|
|
47
47
|
setMatches(getMatches(parsedQuery));
|
|
48
48
|
}
|
|
49
49
|
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
|
|
50
|
-
useEffect(() => {
|
|
50
|
+
React.useEffect(() => {
|
|
51
51
|
const matchMedia = window.matchMedia(parsedQuery);
|
|
52
52
|
handleChange();
|
|
53
53
|
matchMedia.addEventListener('change', handleChange);
|
|
@@ -58,12 +58,12 @@ export function useMediaQuery(tailwindBreakpoint) {
|
|
|
58
58
|
return matches;
|
|
59
59
|
}
|
|
60
60
|
export function useEffectOnce(effect) {
|
|
61
|
-
useEffect(effect, []);
|
|
61
|
+
React.useEffect(effect, []);
|
|
62
62
|
}
|
|
63
63
|
export function useUpdateEffect(effect, deps) {
|
|
64
|
-
const isInitialMount = useRef(true);
|
|
64
|
+
const isInitialMount = React.useRef(true);
|
|
65
65
|
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
|
|
66
|
-
useEffect(() => {
|
|
66
|
+
React.useEffect(() => {
|
|
67
67
|
if (isInitialMount.current) {
|
|
68
68
|
isInitialMount.current = false;
|
|
69
69
|
}
|
|
@@ -73,8 +73,8 @@ export function useUpdateEffect(effect, deps) {
|
|
|
73
73
|
}, deps);
|
|
74
74
|
}
|
|
75
75
|
export function useDebounce(state, delay = 500) {
|
|
76
|
-
const [debouncedState, setDebouncedState] = useState(state);
|
|
77
|
-
useEffect(() => {
|
|
76
|
+
const [debouncedState, setDebouncedState] = React.useState(state);
|
|
77
|
+
React.useEffect(() => {
|
|
78
78
|
const timer = setTimeout(() => setDebouncedState(state), delay);
|
|
79
79
|
return () => {
|
|
80
80
|
clearTimeout(timer);
|
|
@@ -82,13 +82,13 @@ export function useDebounce(state, delay = 500) {
|
|
|
82
82
|
}, [state, delay]);
|
|
83
83
|
return debouncedState;
|
|
84
84
|
}
|
|
85
|
-
export const useIsomorphicEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
85
|
+
export const useIsomorphicEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
86
86
|
export function useTimeout(callback, delay = 1000) {
|
|
87
|
-
const savedCallback = useRef(callback);
|
|
87
|
+
const savedCallback = React.useRef(callback);
|
|
88
88
|
useIsomorphicEffect(() => {
|
|
89
89
|
savedCallback.current = callback;
|
|
90
90
|
}, [callback]);
|
|
91
|
-
useEffect(() => {
|
|
91
|
+
React.useEffect(() => {
|
|
92
92
|
if (!delay && delay !== 0) {
|
|
93
93
|
return;
|
|
94
94
|
}
|
|
@@ -98,16 +98,16 @@ export function useTimeout(callback, delay = 1000) {
|
|
|
98
98
|
}
|
|
99
99
|
export function useWindowEvent(type, listener, options) {
|
|
100
100
|
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
|
|
101
|
-
useEffect(() => {
|
|
101
|
+
React.useEffect(() => {
|
|
102
102
|
window.addEventListener(type, listener, options);
|
|
103
103
|
return () => window.removeEventListener(type, listener, options);
|
|
104
104
|
}, [type, listener]);
|
|
105
105
|
}
|
|
106
106
|
// Custom hook for using local storage with a specified key and default value
|
|
107
107
|
export const useLocalStorage = (key, defaultValue) => {
|
|
108
|
-
const [storedValue, setStoredValue] = useState(defaultValue);
|
|
108
|
+
const [storedValue, setStoredValue] = React.useState(defaultValue);
|
|
109
109
|
// Use effect to retrieve the stored value from local storage on component mount
|
|
110
|
-
useEffect(() => {
|
|
110
|
+
React.useEffect(() => {
|
|
111
111
|
const value = localStorage.getItem(key);
|
|
112
112
|
if (value) {
|
|
113
113
|
setStoredValue(JSON.parse(value));
|
|
@@ -131,9 +131,9 @@ export const useLocalStorage = (key, defaultValue) => {
|
|
|
131
131
|
};
|
|
132
132
|
// Custom hook for using URL parameters with a specified key and default value
|
|
133
133
|
export const useUrlParams = (key, defaultValue) => {
|
|
134
|
-
const [value, setValue] = useState(defaultValue);
|
|
134
|
+
const [value, setValue] = React.useState(defaultValue);
|
|
135
135
|
// Use effect to retrieve the value from URL parameters on component mount
|
|
136
|
-
useEffect(() => {
|
|
136
|
+
React.useEffect(() => {
|
|
137
137
|
const params = new URLSearchParams(window.location.search);
|
|
138
138
|
const value = params.get(key);
|
|
139
139
|
if (value !== null) {
|
|
@@ -150,9 +150,9 @@ export const useUrlParams = (key, defaultValue) => {
|
|
|
150
150
|
return [value, updateValue];
|
|
151
151
|
};
|
|
152
152
|
export const useQuerySelector = (selector) => {
|
|
153
|
-
const [element, setElement] = useState(null);
|
|
154
|
-
const elementRef = useRef(null);
|
|
155
|
-
useLayoutEffect(() => {
|
|
153
|
+
const [element, setElement] = React.useState(null);
|
|
154
|
+
const elementRef = React.useRef(null);
|
|
155
|
+
React.useLayoutEffect(() => {
|
|
156
156
|
const referenceElement = document.querySelector(selector);
|
|
157
157
|
if (!referenceElement)
|
|
158
158
|
return;
|
|
@@ -175,14 +175,14 @@ export const useQuerySelector = (selector) => {
|
|
|
175
175
|
return element;
|
|
176
176
|
};
|
|
177
177
|
export function useIsClient() {
|
|
178
|
-
const [isClient, setIsClient] = useState(false);
|
|
179
|
-
useEffect(() => {
|
|
178
|
+
const [isClient, setIsClient] = React.useState(false);
|
|
179
|
+
React.useEffect(() => {
|
|
180
180
|
setIsClient(true);
|
|
181
181
|
}, []);
|
|
182
182
|
return isClient;
|
|
183
183
|
}
|
|
184
184
|
export function useLockScroll() {
|
|
185
|
-
useLayoutEffect(() => {
|
|
185
|
+
React.useLayoutEffect(() => {
|
|
186
186
|
const originalStyle = window.getComputedStyle(document.body).overflow;
|
|
187
187
|
document.body.style.overflow = 'hidden';
|
|
188
188
|
return () => {
|
|
@@ -191,7 +191,7 @@ export function useLockScroll() {
|
|
|
191
191
|
}, []);
|
|
192
192
|
}
|
|
193
193
|
export function useCopyToClipboard({ timeout = 2000 }) {
|
|
194
|
-
const [isCopied, setIsCopied] = useState(false);
|
|
194
|
+
const [isCopied, setIsCopied] = React.useState(false);
|
|
195
195
|
const copy = (value) => {
|
|
196
196
|
copyToClipboard(value, () => {
|
|
197
197
|
setIsCopied(true);
|
|
@@ -202,3 +202,18 @@ export function useCopyToClipboard({ timeout = 2000 }) {
|
|
|
202
202
|
};
|
|
203
203
|
return { isCopied, copy };
|
|
204
204
|
}
|
|
205
|
+
export const useHeightCalculation = ({ blockIds = [], margin = 0, dynamic = false, }) => {
|
|
206
|
+
const [height, setTableHeight] = React.useState(500);
|
|
207
|
+
const handleResize = () => {
|
|
208
|
+
const blcokHeight = blockIds.reduce((prevHeight, id) => prevHeight + (document.getElementById(id)?.clientHeight || 0), 0);
|
|
209
|
+
setTableHeight(window.innerHeight - blcokHeight - margin);
|
|
210
|
+
};
|
|
211
|
+
useIsomorphicEffect(() => {
|
|
212
|
+
handleResize();
|
|
213
|
+
if (!dynamic)
|
|
214
|
+
return;
|
|
215
|
+
window.addEventListener('resize', handleResize);
|
|
216
|
+
return () => window.removeEventListener('resize', handleResize);
|
|
217
|
+
}, []);
|
|
218
|
+
return height;
|
|
219
|
+
};
|