@sohanemon/utils 4.0.17 → 4.0.19
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/components/media-wrapper.d.ts +1 -1
- package/dist/hooks/index.d.ts +10 -9
- package/dist/hooks/index.js +34 -6
- package/package.json +12 -22
|
@@ -6,5 +6,5 @@ type MediaWrapperProps = React.ComponentProps<'div'> & {
|
|
|
6
6
|
fallback?: React.ElementType;
|
|
7
7
|
classNameFallback?: string;
|
|
8
8
|
};
|
|
9
|
-
export declare function MediaWrapper({ breakpoint, as, fallback, classNameFallback, className: classNameOriginal, ...props }: MediaWrapperProps):
|
|
9
|
+
export declare function MediaWrapper({ breakpoint, as, fallback, classNameFallback, className: classNameOriginal, ...props }: MediaWrapperProps): import("react/jsx-runtime").JSX.Element;
|
|
10
10
|
export {};
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type Dispatch, type EffectCallback, type SetStateAction } from 'react';
|
|
2
|
-
export declare const useClickOutside: (callback?: () => void) =>
|
|
3
|
-
export declare function useMediaQuery(tailwindBreakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | `(${string})`):
|
|
1
|
+
import { type Dispatch, type EffectCallback, type SetStateAction, useEffect } from 'react';
|
|
2
|
+
export declare const useClickOutside: (callback?: () => void) => import("react").MutableRefObject<HTMLDivElement>;
|
|
3
|
+
export declare function useMediaQuery(tailwindBreakpoint: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | `(${string})`): boolean;
|
|
4
4
|
export declare function useEffectOnce(effect: EffectCallback): void;
|
|
5
5
|
export declare function useUpdateEffect(effect: EffectCallback, deps: any[]): void;
|
|
6
6
|
export declare function useDebounce<T>(state: T, delay?: number): T;
|
|
7
|
-
export declare const useIsomorphicEffect:
|
|
7
|
+
export declare const useIsomorphicEffect: typeof useEffect;
|
|
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;
|
|
10
10
|
type LocalStorageValue<T> = [T, Dispatch<SetStateAction<T>>];
|
|
@@ -21,9 +21,10 @@ interface UseAsyncOptions<T extends (...args: any) => any> {
|
|
|
21
21
|
mode?: 'onLoad' | 'onTrigger';
|
|
22
22
|
}
|
|
23
23
|
export declare const useAsync: <T extends (...args: any) => any>(fn: T, opts?: UseAsyncOptions<T>) => {
|
|
24
|
-
execute:
|
|
25
|
-
isLoading:
|
|
26
|
-
result:
|
|
27
|
-
error:
|
|
24
|
+
execute: (args: Parameters<T>[0]) => Promise<void>;
|
|
25
|
+
isLoading: boolean;
|
|
26
|
+
result: Awaited<ReturnType<T>>;
|
|
27
|
+
error: Error;
|
|
28
28
|
};
|
|
29
|
-
export
|
|
29
|
+
export declare const useQuerySelector: <T extends Element>(selector: string) => T | null;
|
|
30
|
+
export default useQuerySelector;
|
package/dist/hooks/index.js
CHANGED
|
@@ -145,7 +145,7 @@ export const useUrlParams = (key, defaultValue) => {
|
|
|
145
145
|
return [value, updateValue];
|
|
146
146
|
};
|
|
147
147
|
export const useAsync = (fn, opts = {}) => {
|
|
148
|
-
const { initialArgs, callback = {}, mode =
|
|
148
|
+
const { initialArgs, callback = {}, mode = 'onTrigger' } = opts;
|
|
149
149
|
const { onSuccess, onError, onExecute, onSettle } = callback;
|
|
150
150
|
const [isLoading, setIsLoading] = useState(false);
|
|
151
151
|
const [result, setValue] = useState(null);
|
|
@@ -157,10 +157,12 @@ export const useAsync = (fn, opts = {}) => {
|
|
|
157
157
|
setError(null);
|
|
158
158
|
onExecute?.();
|
|
159
159
|
try {
|
|
160
|
-
startTransition(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
startTransition(() => {
|
|
161
|
+
(async () => {
|
|
162
|
+
const response = await fn(args);
|
|
163
|
+
setValue(response);
|
|
164
|
+
onSuccess?.(response);
|
|
165
|
+
})();
|
|
164
166
|
});
|
|
165
167
|
}
|
|
166
168
|
catch (error) {
|
|
@@ -172,7 +174,7 @@ export const useAsync = (fn, opts = {}) => {
|
|
|
172
174
|
onSettle?.();
|
|
173
175
|
}
|
|
174
176
|
}, [fn, onExecute, onSuccess, onError, onSettle]);
|
|
175
|
-
|
|
177
|
+
useIsomorphicEffect(() => {
|
|
176
178
|
if (mode === 'onLoad') {
|
|
177
179
|
execute(initialArgs);
|
|
178
180
|
}
|
|
@@ -184,3 +186,29 @@ export const useAsync = (fn, opts = {}) => {
|
|
|
184
186
|
error,
|
|
185
187
|
};
|
|
186
188
|
};
|
|
189
|
+
export const useQuerySelector = (selector) => {
|
|
190
|
+
const [element, setElement] = useState(null);
|
|
191
|
+
const elementRef = useRef(null);
|
|
192
|
+
useLayoutEffect(() => {
|
|
193
|
+
const referenceElement = document.querySelector(selector);
|
|
194
|
+
if (!referenceElement)
|
|
195
|
+
return;
|
|
196
|
+
if (elementRef.current !== referenceElement) {
|
|
197
|
+
elementRef.current = referenceElement;
|
|
198
|
+
setElement(referenceElement);
|
|
199
|
+
}
|
|
200
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
201
|
+
// Only update state if the element reference changes
|
|
202
|
+
if (elementRef.current !== referenceElement) {
|
|
203
|
+
elementRef.current = referenceElement;
|
|
204
|
+
setElement(referenceElement);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
resizeObserver.observe(referenceElement);
|
|
208
|
+
return () => {
|
|
209
|
+
resizeObserver.disconnect();
|
|
210
|
+
};
|
|
211
|
+
}, [selector]);
|
|
212
|
+
return element;
|
|
213
|
+
};
|
|
214
|
+
export default useQuerySelector;
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sohanemon/utils",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.19",
|
|
4
|
+
"author": "Sohan Emon <sohanemon@outlook.com>",
|
|
4
5
|
"description": "",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"source": "./src/index.ts",
|
|
@@ -14,39 +15,28 @@
|
|
|
14
15
|
},
|
|
15
16
|
"typesVersions": {
|
|
16
17
|
"*": {
|
|
17
|
-
"core": [
|
|
18
|
-
|
|
19
|
-
]
|
|
20
|
-
"hooks": [
|
|
21
|
-
"dist/hooks/index.d.ts"
|
|
22
|
-
],
|
|
23
|
-
"components": [
|
|
24
|
-
"dist/components/index.d.ts"
|
|
25
|
-
]
|
|
18
|
+
"core": ["dist/index.d.ts"],
|
|
19
|
+
"hooks": ["dist/hooks/index.d.ts"],
|
|
20
|
+
"components": ["dist/components/index.d.ts"]
|
|
26
21
|
}
|
|
27
22
|
},
|
|
28
|
-
"files": [
|
|
29
|
-
"dist",
|
|
30
|
-
"README.md"
|
|
31
|
-
],
|
|
23
|
+
"files": ["dist", "README.md"],
|
|
32
24
|
"scripts": {
|
|
33
25
|
"build": "tsc",
|
|
34
26
|
"build:watch": "tsc --watch",
|
|
35
27
|
"export": "tsc && npm publish"
|
|
36
28
|
},
|
|
37
|
-
"keywords": [
|
|
38
|
-
"utils",
|
|
39
|
-
"cn"
|
|
40
|
-
],
|
|
41
|
-
"author": "sohanemon",
|
|
29
|
+
"keywords": ["utils", "cn"],
|
|
42
30
|
"license": "ISC",
|
|
43
31
|
"devDependencies": {
|
|
44
|
-
"typescript": "^5.
|
|
32
|
+
"typescript": "^5.5.4"
|
|
45
33
|
},
|
|
46
34
|
"dependencies": {
|
|
47
35
|
"@iconify/react": "^4.1.1",
|
|
48
|
-
"
|
|
49
|
-
"react": "^18.
|
|
36
|
+
"@types/node": "^22.4.0",
|
|
37
|
+
"@types/react": "^18.3.3",
|
|
38
|
+
"clsx": "^2.1.1",
|
|
39
|
+
"react": "^18.3.1",
|
|
50
40
|
"tailwind-merge": "^1.14.0"
|
|
51
41
|
}
|
|
52
42
|
}
|