react-simplikit 0.0.2 → 0.0.3
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/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/components/ImpressionArea/index.d.ts +48 -0
- package/dist/components/ImpressionArea/index.js +276 -0
- package/dist/components/Separated/index.d.ts +38 -0
- package/dist/components/Separated/index.js +40 -0
- package/dist/components/SwitchCase/index.d.ts +46 -0
- package/dist/components/SwitchCase/index.js +35 -0
- package/dist/hooks/useAsyncEffect/index.d.ts +25 -0
- package/dist/hooks/useAsyncEffect/index.js +43 -0
- package/dist/hooks/useBooleanState/index.d.ts +19 -0
- package/dist/hooks/useBooleanState/index.js +45 -0
- package/dist/hooks/useCallbackOnce/index.d.ts +41 -0
- package/dist/hooks/useCallbackOnce/index.js +59 -0
- package/dist/hooks/useDebounce/index.d.ts +47 -0
- package/dist/hooks/useDebounce/index.js +123 -0
- package/dist/hooks/useImpressionRef/index.d.ts +38 -0
- package/dist/hooks/useImpressionRef/index.js +237 -0
- package/dist/hooks/useInputState/index.d.ts +27 -0
- package/dist/hooks/useInputState/index.js +45 -0
- package/dist/hooks/useIntersectionObserver/index.d.ts +33 -0
- package/dist/hooks/useIntersectionObserver/index.js +91 -0
- package/dist/hooks/useInterval/index.d.ts +37 -0
- package/dist/hooks/useInterval/index.js +64 -0
- package/dist/hooks/useLoading/index.d.ts +37 -0
- package/dist/hooks/useLoading/index.js +61 -0
- package/dist/hooks/useOutsideClickEffect/index.d.ts +27 -0
- package/dist/hooks/useOutsideClickEffect/index.js +70 -0
- package/dist/hooks/usePreservedCallback/index.d.ts +30 -0
- package/dist/hooks/usePreservedCallback/index.js +41 -0
- package/dist/hooks/usePreservedReference/index.d.ts +42 -0
- package/dist/hooks/usePreservedReference/index.js +44 -0
- package/dist/hooks/usePrevious/index.d.ts +21 -0
- package/dist/hooks/usePrevious/index.js +47 -0
- package/dist/hooks/useRefEffect/index.d.ts +31 -0
- package/dist/hooks/useRefEffect/index.js +68 -0
- package/dist/hooks/useStorageState/index.d.ts +50 -0
- package/dist/hooks/useStorageState/index.js +168 -0
- package/dist/hooks/useThrottle/index.d.ts +47 -0
- package/dist/hooks/useThrottle/index.js +147 -0
- package/dist/hooks/useTimeout/index.d.ts +30 -0
- package/dist/hooks/useTimeout/index.js +53 -0
- package/dist/hooks/useToggle/index.d.ts +30 -0
- package/dist/hooks/useToggle/index.js +36 -0
- package/dist/hooks/useVisibilityEvent/index.d.ts +27 -0
- package/dist/hooks/useVisibilityEvent/index.js +46 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +761 -0
- package/dist/utils/buildContext/index.d.ts +38 -0
- package/dist/utils/buildContext/index.js +55 -0
- package/dist/utils/mergeRefs/index.d.ts +37 -0
- package/dist/utils/mergeRefs/index.js +45 -0
- package/esm/components/ImpressionArea/index.d.mts +48 -0
- package/esm/components/ImpressionArea/index.mjs +249 -0
- package/esm/components/Separated/index.d.mts +38 -0
- package/esm/components/Separated/index.mjs +13 -0
- package/esm/components/SwitchCase/index.d.mts +46 -0
- package/esm/components/SwitchCase/index.mjs +8 -0
- package/esm/hooks/useAsyncEffect/index.d.mts +25 -0
- package/esm/hooks/useAsyncEffect/index.mjs +16 -0
- package/esm/hooks/useBooleanState/index.d.mts +19 -0
- package/esm/hooks/useBooleanState/index.mjs +18 -0
- package/esm/hooks/useCallbackOnce/index.d.mts +41 -0
- package/esm/hooks/useCallbackOnce/index.mjs +32 -0
- package/esm/hooks/useDebounce/index.d.mts +47 -0
- package/esm/hooks/useDebounce/index.mjs +96 -0
- package/esm/hooks/useImpressionRef/index.d.mts +38 -0
- package/esm/hooks/useImpressionRef/index.mjs +210 -0
- package/esm/hooks/useInputState/index.d.mts +27 -0
- package/esm/hooks/useInputState/index.mjs +18 -0
- package/esm/hooks/useIntersectionObserver/index.d.mts +33 -0
- package/esm/hooks/useIntersectionObserver/index.mjs +64 -0
- package/esm/hooks/useInterval/index.d.mts +37 -0
- package/esm/hooks/useInterval/index.mjs +37 -0
- package/esm/hooks/useLoading/index.d.mts +37 -0
- package/esm/hooks/useLoading/index.mjs +34 -0
- package/esm/hooks/useOutsideClickEffect/index.d.mts +27 -0
- package/esm/hooks/useOutsideClickEffect/index.mjs +43 -0
- package/esm/hooks/usePreservedCallback/index.d.mts +30 -0
- package/esm/hooks/usePreservedCallback/index.mjs +14 -0
- package/esm/hooks/usePreservedReference/index.d.mts +42 -0
- package/esm/hooks/usePreservedReference/index.mjs +17 -0
- package/esm/hooks/usePrevious/index.d.mts +21 -0
- package/esm/hooks/usePrevious/index.mjs +20 -0
- package/esm/hooks/useRefEffect/index.d.mts +31 -0
- package/esm/hooks/useRefEffect/index.mjs +41 -0
- package/esm/hooks/useStorageState/index.d.mts +50 -0
- package/esm/hooks/useStorageState/index.mjs +141 -0
- package/esm/hooks/useThrottle/index.d.mts +47 -0
- package/esm/hooks/useThrottle/index.mjs +120 -0
- package/esm/hooks/useTimeout/index.d.mts +30 -0
- package/esm/hooks/useTimeout/index.mjs +26 -0
- package/esm/hooks/useToggle/index.d.mts +30 -0
- package/esm/hooks/useToggle/index.mjs +9 -0
- package/esm/hooks/useVisibilityEvent/index.d.mts +27 -0
- package/esm/hooks/useVisibilityEvent/index.mjs +19 -0
- package/esm/index.d.mts +26 -0
- package/esm/index.mjs +711 -0
- package/esm/utils/buildContext/index.d.mts +38 -0
- package/esm/utils/buildContext/index.mjs +28 -0
- package/esm/utils/mergeRefs/index.d.mts +37 -0
- package/esm/utils/mergeRefs/index.mjs +18 -0
- package/package.json +79 -7
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useIntersectionObserver/index.ts
|
|
21
|
+
var useIntersectionObserver_exports = {};
|
|
22
|
+
__export(useIntersectionObserver_exports, {
|
|
23
|
+
useIntersectionObserver: () => useIntersectionObserver
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useIntersectionObserver_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useIntersectionObserver/useIntersectionObserver.ts
|
|
28
|
+
var import_react3 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
function usePreservedCallback(callback) {
|
|
33
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
callbackRef.current = callback;
|
|
36
|
+
}, [callback]);
|
|
37
|
+
return (0, import_react.useCallback)((...args) => {
|
|
38
|
+
return callbackRef.current(...args);
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/hooks/useRefEffect/useRefEffect.ts
|
|
43
|
+
var import_react2 = require("react");
|
|
44
|
+
function useRefEffect(callback, deps) {
|
|
45
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
46
|
+
const cleanupCallbackRef = (0, import_react2.useRef)(() => {
|
|
47
|
+
});
|
|
48
|
+
const effect = (0, import_react2.useCallback)(
|
|
49
|
+
(element) => {
|
|
50
|
+
cleanupCallbackRef.current();
|
|
51
|
+
cleanupCallbackRef.current = () => {
|
|
52
|
+
};
|
|
53
|
+
if (element == null) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const cleanup = preservedCallback(element);
|
|
57
|
+
if (typeof cleanup === "function") {
|
|
58
|
+
cleanupCallbackRef.current = cleanup;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
62
|
+
[preservedCallback, ...deps]
|
|
63
|
+
);
|
|
64
|
+
return effect;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/hooks/useIntersectionObserver/useIntersectionObserver.ts
|
|
68
|
+
function useIntersectionObserver(callback, options) {
|
|
69
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
70
|
+
const observer = (0, import_react3.useMemo)(() => {
|
|
71
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return new IntersectionObserver(([entry]) => {
|
|
75
|
+
preservedCallback(entry);
|
|
76
|
+
}, options);
|
|
77
|
+
}, [preservedCallback, options]);
|
|
78
|
+
return useRefEffect(
|
|
79
|
+
(element) => {
|
|
80
|
+
observer?.observe(element);
|
|
81
|
+
return () => {
|
|
82
|
+
observer?.unobserve(element);
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
[preservedCallback, options]
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
useIntersectionObserver
|
|
91
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
type IntervalOptions = number | {
|
|
2
|
+
delay: number;
|
|
3
|
+
immediate?: boolean;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* @description
|
|
8
|
+
* `useInterval` is a React hook that executes a function at a specified interval.
|
|
9
|
+
* It is useful for timers, polling data, and other recurring tasks.
|
|
10
|
+
*
|
|
11
|
+
* @param {() => void} callback - The function to be executed periodically.
|
|
12
|
+
* @param {number | { delay: number; enabled?: boolean; immediate?: boolean }} options - Configures the interval behavior.
|
|
13
|
+
* @param {number} options.delay - The interval duration in milliseconds. If `null`, the interval will not run.
|
|
14
|
+
* @param {boolean} [options.immediate=false] - If `true`, executes immediately before starting the interval.
|
|
15
|
+
* @param {boolean} [options.enabled=true] - If `false`, the interval will not run.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* import { useInterval } from 'react-simplikit';
|
|
19
|
+
* import { useState } from 'react';
|
|
20
|
+
*
|
|
21
|
+
* function Timer() {
|
|
22
|
+
* const [time, setTime] = useState(0);
|
|
23
|
+
*
|
|
24
|
+
* useInterval(() => {
|
|
25
|
+
* setTime(prev => prev + 1);
|
|
26
|
+
* }, 1000);
|
|
27
|
+
*
|
|
28
|
+
* return (
|
|
29
|
+
* <div>
|
|
30
|
+
* <p>{time} seconds</p>
|
|
31
|
+
* </div>
|
|
32
|
+
* );
|
|
33
|
+
* }
|
|
34
|
+
*/
|
|
35
|
+
declare function useInterval(callback: () => void, options: IntervalOptions): void;
|
|
36
|
+
|
|
37
|
+
export { useInterval };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useInterval/index.ts
|
|
21
|
+
var useInterval_exports = {};
|
|
22
|
+
__export(useInterval_exports, {
|
|
23
|
+
useInterval: () => useInterval
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useInterval_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useInterval/useInterval.ts
|
|
28
|
+
var import_react2 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
function usePreservedCallback(callback) {
|
|
33
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
callbackRef.current = callback;
|
|
36
|
+
}, [callback]);
|
|
37
|
+
return (0, import_react.useCallback)((...args) => {
|
|
38
|
+
return callbackRef.current(...args);
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/hooks/useInterval/useInterval.ts
|
|
43
|
+
function useInterval(callback, options) {
|
|
44
|
+
const delay = typeof options === "number" ? options : options.delay;
|
|
45
|
+
const immediate = typeof options === "number" ? false : options.immediate;
|
|
46
|
+
const enabled = typeof options === "number" ? true : options.enabled ?? true;
|
|
47
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
48
|
+
(0, import_react2.useEffect)(() => {
|
|
49
|
+
if (immediate === true && enabled) {
|
|
50
|
+
preservedCallback();
|
|
51
|
+
}
|
|
52
|
+
}, [immediate, preservedCallback, enabled]);
|
|
53
|
+
(0, import_react2.useEffect)(() => {
|
|
54
|
+
if (!enabled) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const id = window.setInterval(preservedCallback, delay);
|
|
58
|
+
return () => window.clearInterval(id);
|
|
59
|
+
}, [delay, preservedCallback, enabled]);
|
|
60
|
+
}
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
useInterval
|
|
64
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* `useLoading` is a React hook that simplifies managing the loading state of a `Promise`.
|
|
4
|
+
* It provides a state to track whether an asynchronous operation is in progress and a function to handle the loading state automatically.
|
|
5
|
+
*
|
|
6
|
+
* @returns A tuple `[boolean, <T>(promise: Promise<T>) => Promise<T>]`:
|
|
7
|
+
*
|
|
8
|
+
* - `boolean`: Represents the current loading state.
|
|
9
|
+
* - The initial value is `false`.
|
|
10
|
+
* - It is set to `true` when an asynchronous task is in progress.
|
|
11
|
+
*
|
|
12
|
+
* - `<T>(promise: Promise<T>) => Promise<T>`: A function that executes asynchronous tasks while managing the loading state.
|
|
13
|
+
* - This function takes a `Promise` as an argument and automatically resets the `isLoading` state to `false` when the `Promise` completes.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* function ConfirmButton() {
|
|
17
|
+
* const [loading, startLoading] = useLoading();
|
|
18
|
+
*
|
|
19
|
+
* const handleSubmit = useCallback(async () => {
|
|
20
|
+
* try {
|
|
21
|
+
* const result = await startLoading(postConfirmation());
|
|
22
|
+
* router.push(`/success?id=${result.id}`);
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* console.error('Error:', error);
|
|
25
|
+
* }
|
|
26
|
+
* }, [startLoading]);
|
|
27
|
+
*
|
|
28
|
+
* return (
|
|
29
|
+
* <button disabled={loading} onClick={handleSubmit}>
|
|
30
|
+
* {loading ? 'Loading...' : 'Confirm'}
|
|
31
|
+
* </button>
|
|
32
|
+
* );
|
|
33
|
+
* }
|
|
34
|
+
*/
|
|
35
|
+
declare function useLoading(): [boolean, <T>(promise: Promise<T>) => Promise<T>];
|
|
36
|
+
|
|
37
|
+
export { useLoading };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useLoading/index.ts
|
|
21
|
+
var useLoading_exports = {};
|
|
22
|
+
__export(useLoading_exports, {
|
|
23
|
+
useLoading: () => useLoading
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useLoading_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useLoading/useLoading.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
function useLoading() {
|
|
30
|
+
const [loading, setLoading] = (0, import_react.useState)(false);
|
|
31
|
+
const ref = useIsMountedRef();
|
|
32
|
+
const startTransition = (0, import_react.useCallback)(
|
|
33
|
+
async (promise) => {
|
|
34
|
+
try {
|
|
35
|
+
setLoading(true);
|
|
36
|
+
const data = await promise;
|
|
37
|
+
return data;
|
|
38
|
+
} finally {
|
|
39
|
+
if (ref.isMounted) {
|
|
40
|
+
setLoading(false);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
[ref.isMounted]
|
|
45
|
+
);
|
|
46
|
+
return (0, import_react.useMemo)(() => [loading, startTransition], [loading, startTransition]);
|
|
47
|
+
}
|
|
48
|
+
function useIsMountedRef() {
|
|
49
|
+
const ref = (0, import_react.useRef)({ isMounted: true }).current;
|
|
50
|
+
(0, import_react.useEffect)(() => {
|
|
51
|
+
ref.isMounted = true;
|
|
52
|
+
return () => {
|
|
53
|
+
ref.isMounted = false;
|
|
54
|
+
};
|
|
55
|
+
}, [ref]);
|
|
56
|
+
return ref;
|
|
57
|
+
}
|
|
58
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
+
0 && (module.exports = {
|
|
60
|
+
useLoading
|
|
61
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type OneOrMore<T> = T | T[];
|
|
2
|
+
/**
|
|
3
|
+
* @description
|
|
4
|
+
* `useOutsideClickEffect` is a React hook that triggers a callback when a click event occurs outside the specified container(s).
|
|
5
|
+
* It is useful for closing modals, dropdowns, tooltips, and other UI components when clicking outside.
|
|
6
|
+
*
|
|
7
|
+
* @param {HTMLElement | HTMLElement[] | null} container - A single HTML element, an array of HTML elements, or `null`.
|
|
8
|
+
* If `null`, no event listener is attached.
|
|
9
|
+
* @param {() => void} callback - A function that is executed when clicking outside the specified container(s).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* import { useOutsideClickEffect } from 'react-simplikit';
|
|
13
|
+
* import { useState } from 'react';
|
|
14
|
+
*
|
|
15
|
+
* function Example() {
|
|
16
|
+
* const [wrapperEl, setWrapperEl] = useState<HTMLDivElement | null>(null);
|
|
17
|
+
*
|
|
18
|
+
* useOutsideClickEffect(wrapperEl, () => {
|
|
19
|
+
* console.log('Outside clicked!');
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* return <div ref={setWrapperEl}>Content</div>;
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
declare function useOutsideClickEffect(container: OneOrMore<HTMLElement | null>, callback: () => void): void;
|
|
26
|
+
|
|
27
|
+
export { useOutsideClickEffect };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useOutsideClickEffect/index.ts
|
|
21
|
+
var useOutsideClickEffect_exports = {};
|
|
22
|
+
__export(useOutsideClickEffect_exports, {
|
|
23
|
+
useOutsideClickEffect: () => useOutsideClickEffect
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useOutsideClickEffect_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/useOutsideClickEffect/useOutsideClickEffect.ts
|
|
28
|
+
var import_react2 = require("react");
|
|
29
|
+
|
|
30
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
function usePreservedCallback(callback) {
|
|
33
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
callbackRef.current = callback;
|
|
36
|
+
}, [callback]);
|
|
37
|
+
return (0, import_react.useCallback)((...args) => {
|
|
38
|
+
return callbackRef.current(...args);
|
|
39
|
+
}, []);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/hooks/useOutsideClickEffect/useOutsideClickEffect.ts
|
|
43
|
+
function useOutsideClickEffect(container, callback) {
|
|
44
|
+
const containers = (0, import_react2.useRef)([]);
|
|
45
|
+
const handleDocumentClick = usePreservedCallback(({ target }) => {
|
|
46
|
+
if (target === null) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (containers.current.length === 0) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (containers.current.some((x) => x.contains(target))) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
callback();
|
|
56
|
+
});
|
|
57
|
+
(0, import_react2.useEffect)(() => {
|
|
58
|
+
containers.current = [container].flat(1).filter((item) => item != null);
|
|
59
|
+
}, [container]);
|
|
60
|
+
(0, import_react2.useEffect)(() => {
|
|
61
|
+
document.addEventListener("click", handleDocumentClick);
|
|
62
|
+
return () => {
|
|
63
|
+
document.removeEventListener("click", handleDocumentClick);
|
|
64
|
+
};
|
|
65
|
+
}, [handleDocumentClick]);
|
|
66
|
+
}
|
|
67
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
+
0 && (module.exports = {
|
|
69
|
+
useOutsideClickEffect
|
|
70
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* `usePreservedCallback` is a React hook that maintains a stable reference to a callback function
|
|
4
|
+
* while ensuring it always has access to the latest state or props. This prevents unnecessary re-renders
|
|
5
|
+
* and simplifies dependency management when passing callbacks to child components or handling event listeners.
|
|
6
|
+
*
|
|
7
|
+
* @param {(...args: any[]) => any} callback - The function to preserve.
|
|
8
|
+
* It always references the latest state or props, even when the component re-renders.
|
|
9
|
+
*
|
|
10
|
+
* @returns {(...args: any[]) => any} A function with the same signature as the input callback.
|
|
11
|
+
* The returned function maintains a stable reference while accessing the latest state or props.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { usePreservedCallback } from 'react-simplikit';
|
|
15
|
+
* import { useState } from 'react';
|
|
16
|
+
*
|
|
17
|
+
* function Counter() {
|
|
18
|
+
* const [count, setCount] = useState(0);
|
|
19
|
+
*
|
|
20
|
+
* const handleClick = usePreservedCallback(() => {
|
|
21
|
+
* console.log(`Current count: ${count}`);
|
|
22
|
+
* setCount(prev => prev + 1);
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* return <button onClick={handleClick}>Click me</button>;
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
declare function usePreservedCallback<Arguments extends any[] = any[], ReturnValue = unknown>(callback: (...args: Arguments) => ReturnValue): (...args: Arguments) => ReturnValue;
|
|
29
|
+
|
|
30
|
+
export { usePreservedCallback };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/usePreservedCallback/index.ts
|
|
21
|
+
var usePreservedCallback_exports = {};
|
|
22
|
+
__export(usePreservedCallback_exports, {
|
|
23
|
+
usePreservedCallback: () => usePreservedCallback
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(usePreservedCallback_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
function usePreservedCallback(callback) {
|
|
30
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
31
|
+
(0, import_react.useEffect)(() => {
|
|
32
|
+
callbackRef.current = callback;
|
|
33
|
+
}, [callback]);
|
|
34
|
+
return (0, import_react.useCallback)((...args) => {
|
|
35
|
+
return callbackRef.current(...args);
|
|
36
|
+
}, []);
|
|
37
|
+
}
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
usePreservedCallback
|
|
41
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type NotNullishValue = {};
|
|
2
|
+
/**
|
|
3
|
+
* @description
|
|
4
|
+
* `usePreservedReference` is a React hook that helps maintain the reference of a value
|
|
5
|
+
* when it hasn't changed, while ensuring you can safely use the latest state.
|
|
6
|
+
* It prevents unnecessary re-renders while always allowing access to the latest data.
|
|
7
|
+
*
|
|
8
|
+
* @param {T} value - The value to maintain the reference for. It returns a new reference
|
|
9
|
+
* if the state value changes after comparison.
|
|
10
|
+
* @param {(a: T, b: T) => boolean} [areValuesEqual] - An optional function to determine
|
|
11
|
+
* if two values are equal. By default, it uses `JSON.stringify` for comparison.
|
|
12
|
+
*
|
|
13
|
+
* @returns {T} Returns the same reference if the value is considered equal to the previous one,
|
|
14
|
+
* otherwise returns a new reference.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* import { usePreservedReference } from 'react-simplikit';
|
|
18
|
+
* import { useState } from 'react';
|
|
19
|
+
*
|
|
20
|
+
* function ExampleComponent() {
|
|
21
|
+
* const [state, setState] = useState({ key: 'value' });
|
|
22
|
+
*
|
|
23
|
+
* const preservedState = usePreservedReference(state);
|
|
24
|
+
*
|
|
25
|
+
* return <div>{preservedState.key}</div>;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { usePreservedReference } from 'react-simplikit';
|
|
30
|
+
* import { useState } from 'react';
|
|
31
|
+
*
|
|
32
|
+
* function ExampleComponent() {
|
|
33
|
+
* const [state, setState] = useState({ key: 'value' });
|
|
34
|
+
*
|
|
35
|
+
* const preservedState = usePreservedReference(state, (a, b) => a.key === b.key);
|
|
36
|
+
*
|
|
37
|
+
* return <div>{preservedState.key}</div>;
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
declare function usePreservedReference<T extends NotNullishValue>(value: T, areValuesEqual?: (a: T, b: T) => boolean): T;
|
|
41
|
+
|
|
42
|
+
export { usePreservedReference };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/usePreservedReference/index.ts
|
|
21
|
+
var usePreservedReference_exports = {};
|
|
22
|
+
__export(usePreservedReference_exports, {
|
|
23
|
+
usePreservedReference: () => usePreservedReference
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(usePreservedReference_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/usePreservedReference/usePreservedReference.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
30
|
+
const ref = (0, import_react.useRef)(value);
|
|
31
|
+
return (0, import_react.useMemo)(() => {
|
|
32
|
+
if (!areValuesEqual(ref.current, value)) {
|
|
33
|
+
ref.current = value;
|
|
34
|
+
}
|
|
35
|
+
return ref.current;
|
|
36
|
+
}, [areValuesEqual, value]);
|
|
37
|
+
}
|
|
38
|
+
function areDeeplyEqual(x, y) {
|
|
39
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
40
|
+
}
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
usePreservedReference
|
|
44
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* Returns the previous value of the input state.
|
|
4
|
+
* If a re-render occurs but the state value does not change, the previous value remains unchanged.
|
|
5
|
+
* If the state is an object or requires custom change detection, a `compare` function can be provided.
|
|
6
|
+
* By default, state changes are detected using `prev === next`.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of the state.
|
|
9
|
+
* @param {T} state - The state whose previous value is to be tracked.
|
|
10
|
+
* @param {(prev: T | undefined, next: T) => boolean} [compare] - An optional comparison function to determine if the state has changed.
|
|
11
|
+
*
|
|
12
|
+
* @returns {T | undefined} The previous value of the state.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const [count, setCount] = useState(0);
|
|
16
|
+
* // initial value of previousCount is `0`
|
|
17
|
+
* const previousCount = usePrevious(count);
|
|
18
|
+
*/
|
|
19
|
+
declare function usePrevious<T>(state: T, compare?: (prev: T, next: T) => boolean): T;
|
|
20
|
+
|
|
21
|
+
export { usePrevious };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/usePrevious/index.ts
|
|
21
|
+
var usePrevious_exports = {};
|
|
22
|
+
__export(usePrevious_exports, {
|
|
23
|
+
usePrevious: () => usePrevious
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(usePrevious_exports);
|
|
26
|
+
|
|
27
|
+
// src/hooks/usePrevious/usePrevious.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var strictEquals = (prev, next) => prev === next;
|
|
30
|
+
function usePrevious(state, compare = strictEquals) {
|
|
31
|
+
const prevRef = (0, import_react.useRef)(state);
|
|
32
|
+
const currentRef = (0, import_react.useRef)(state);
|
|
33
|
+
const isFirstRender = (0, import_react.useRef)(true);
|
|
34
|
+
(0, import_react.useEffect)(() => {
|
|
35
|
+
isFirstRender.current = false;
|
|
36
|
+
}, []);
|
|
37
|
+
if (isFirstRender.current || compare(currentRef.current, state)) {
|
|
38
|
+
return prevRef.current;
|
|
39
|
+
}
|
|
40
|
+
prevRef.current = currentRef.current;
|
|
41
|
+
currentRef.current = state;
|
|
42
|
+
return prevRef.current;
|
|
43
|
+
}
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
usePrevious
|
|
47
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { DependencyList } from 'react';
|
|
2
|
+
|
|
3
|
+
type CleanupCallback = () => void;
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useRefEffect` is a custom hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
|
|
7
|
+
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
|
|
8
|
+
*
|
|
9
|
+
* @param callback - A callback function that is executed when the element is set. This function can return a cleanup function.
|
|
10
|
+
* @param deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
|
|
11
|
+
*
|
|
12
|
+
* @returns A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { useRefEffect } from 'react-simplikit';
|
|
16
|
+
*
|
|
17
|
+
* function Component() {
|
|
18
|
+
* const ref = useRefEffect<HTMLDivElement>(element => {
|
|
19
|
+
* console.log('Element mounted:', element);
|
|
20
|
+
*
|
|
21
|
+
* return () => {
|
|
22
|
+
* console.log('Element unmounted:', element);
|
|
23
|
+
* };
|
|
24
|
+
* }, []);
|
|
25
|
+
*
|
|
26
|
+
* return <div ref={ref}>Basic Example</div>;
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
declare function useRefEffect<Element extends HTMLElement = HTMLElement>(callback: (element: Element) => CleanupCallback | void, deps: DependencyList): (element: Element | null) => void;
|
|
30
|
+
|
|
31
|
+
export { useRefEffect };
|