react-simplikit 0.0.48 → 0.0.50
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/ImpressionArea/index.cjs +7 -4
- package/dist/components/ImpressionArea/index.d.cts +1 -1
- package/dist/components/Separated/index.cjs +1 -1
- package/dist/hooks/useCallbackOncePerRender/index.cjs +6 -3
- package/dist/hooks/useCounter/index.cjs +12 -14
- package/dist/hooks/useDebounce/index.cjs +14 -8
- package/dist/hooks/useDebouncedCallback/index.cjs +7 -4
- package/dist/hooks/useDoubleClick/index.cjs +6 -3
- package/dist/hooks/useImpressionRef/index.cjs +7 -4
- package/dist/hooks/useIntersectionObserver/index.cjs +6 -3
- package/dist/hooks/useInterval/index.cjs +24 -15
- package/dist/hooks/useIsClient/index.cjs +40 -0
- package/dist/hooks/useIsClient/index.d.cts +40 -0
- package/dist/hooks/useList/index.cjs +104 -0
- package/dist/hooks/useList/index.d.cts +50 -0
- package/dist/hooks/useLoading/index.cjs +9 -6
- package/dist/hooks/useLongPress/index.cjs +6 -3
- package/dist/hooks/useOutsideClickEffect/index.cjs +6 -3
- package/dist/hooks/usePreservedCallback/index.cjs +6 -3
- package/dist/hooks/usePrevious/index.d.cts +3 -3
- package/dist/hooks/useRefEffect/index.cjs +6 -3
- package/dist/hooks/useRefEffect/index.d.cts +3 -3
- package/dist/hooks/useSet/index.cjs +105 -0
- package/dist/hooks/useSet/index.d.cts +39 -0
- package/dist/hooks/useThrottle/index.cjs +6 -3
- package/dist/hooks/useThrottledCallback/index.cjs +172 -0
- package/dist/hooks/useThrottledCallback/index.d.cts +30 -0
- package/dist/hooks/useTimeout/index.cjs +8 -5
- package/dist/hooks/useTimeout/index.d.cts +1 -1
- package/dist/index.cjs +264 -110
- package/dist/index.d.cts +4 -0
- package/esm/components/ImpressionArea/index.d.ts +1 -1
- package/esm/components/ImpressionArea/index.js +7 -4
- package/esm/components/Separated/index.js +1 -1
- package/esm/hooks/useCallbackOncePerRender/index.js +6 -3
- package/esm/hooks/useCounter/index.js +12 -14
- package/esm/hooks/useDebounce/index.js +14 -8
- package/esm/hooks/useDebouncedCallback/index.js +7 -4
- package/esm/hooks/useDoubleClick/index.js +6 -3
- package/esm/hooks/useImpressionRef/index.js +7 -4
- package/esm/hooks/useIntersectionObserver/index.js +6 -3
- package/esm/hooks/useInterval/index.js +24 -15
- package/esm/hooks/useIsClient/index.d.ts +40 -0
- package/esm/hooks/useIsClient/index.js +14 -0
- package/esm/hooks/useList/index.d.ts +50 -0
- package/esm/hooks/useList/index.js +78 -0
- package/esm/hooks/useLoading/index.js +9 -6
- package/esm/hooks/useLongPress/index.js +6 -3
- package/esm/hooks/useOutsideClickEffect/index.js +6 -3
- package/esm/hooks/usePreservedCallback/index.js +6 -3
- package/esm/hooks/usePrevious/index.d.ts +3 -3
- package/esm/hooks/useRefEffect/index.d.ts +3 -3
- package/esm/hooks/useRefEffect/index.js +6 -3
- package/esm/hooks/useSet/index.d.ts +39 -0
- package/esm/hooks/useSet/index.js +79 -0
- package/esm/hooks/useThrottle/index.js +6 -3
- package/esm/hooks/useThrottledCallback/index.d.ts +30 -0
- package/esm/hooks/useThrottledCallback/index.js +146 -0
- package/esm/hooks/useTimeout/index.d.ts +1 -1
- package/esm/hooks/useTimeout/index.js +8 -5
- package/esm/index.d.ts +4 -0
- package/esm/index.js +234 -84
- package/package.json +1 -1
|
@@ -91,9 +91,12 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
|
|
|
91
91
|
var import_react = require("react");
|
|
92
92
|
function usePreservedCallback(callback) {
|
|
93
93
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
94
|
-
(0, import_react.useEffect)(
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
(0, import_react.useEffect)(
|
|
95
|
+
function syncCallbackRef() {
|
|
96
|
+
callbackRef.current = callback;
|
|
97
|
+
},
|
|
98
|
+
[callback]
|
|
99
|
+
);
|
|
97
100
|
return (0, import_react.useCallback)((...args) => {
|
|
98
101
|
return callbackRef.current(...args);
|
|
99
102
|
}, []);
|
|
@@ -109,7 +112,7 @@ function useDebouncedCallback({
|
|
|
109
112
|
const handleChange = usePreservedCallback(onChange);
|
|
110
113
|
const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
|
|
111
114
|
} });
|
|
112
|
-
(0, import_react2.useEffect)(()
|
|
115
|
+
(0, import_react2.useEffect)(function clearDebouncedOnUnmount() {
|
|
113
116
|
const current = ref.current;
|
|
114
117
|
return () => {
|
|
115
118
|
current.clearPreviousDebounce();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ElementType, ReactNode, Ref } from 'react';
|
|
2
2
|
import { UseImpressionRefOptions } from '../../hooks/useImpressionRef/index.cjs';
|
|
3
3
|
|
|
4
|
-
type Element<T extends ElementType,
|
|
4
|
+
type Element<T extends ElementType, IntrinsicElementsOf = T extends keyof React.JSX.IntrinsicElements ? React.JSX.IntrinsicElements[T] : HTMLElement> = IntrinsicElementsOf extends React.ClassAttributes<infer E extends HTMLElement> ? E : HTMLElement;
|
|
5
5
|
type Props<Tag extends ElementType> = React.ComponentPropsWithoutRef<Tag> & UseImpressionRefOptions & {
|
|
6
6
|
as?: Tag;
|
|
7
7
|
children?: ReactNode;
|
|
@@ -33,7 +33,7 @@ function Separated({ children, by: separator }) {
|
|
|
33
33
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: childrenArray.map((child, i, { length }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react.Fragment, { children: [
|
|
34
34
|
child,
|
|
35
35
|
i + 1 !== length && separator
|
|
36
|
-
] }, i)) });
|
|
36
|
+
] }, child.key ?? i)) });
|
|
37
37
|
}
|
|
38
38
|
// Annotate the CommonJS export names for ESM import in node:
|
|
39
39
|
0 && (module.exports = {
|
|
@@ -32,9 +32,12 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -27,27 +27,25 @@ module.exports = __toCommonJS(useCounter_exports);
|
|
|
27
27
|
|
|
28
28
|
// src/hooks/useCounter/useCounter.ts
|
|
29
29
|
var import_react = require("react");
|
|
30
|
+
var validateValue = (value, { min, max }) => {
|
|
31
|
+
if (min !== void 0 && value < min) {
|
|
32
|
+
return min;
|
|
33
|
+
}
|
|
34
|
+
if (max !== void 0 && value > max) {
|
|
35
|
+
return max;
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
};
|
|
30
39
|
function useCounter(initialValue = 0, { min, max, step = 1 } = {}) {
|
|
31
|
-
const
|
|
32
|
-
let validatedValue = value;
|
|
33
|
-
if (min !== void 0 && validatedValue < min) {
|
|
34
|
-
validatedValue = min;
|
|
35
|
-
}
|
|
36
|
-
if (max !== void 0 && validatedValue > max) {
|
|
37
|
-
validatedValue = max;
|
|
38
|
-
}
|
|
39
|
-
return validatedValue;
|
|
40
|
-
};
|
|
41
|
-
const [count, setCountState] = (0, import_react.useState)(() => validateValue(initialValue));
|
|
42
|
-
const validateValueMemoized = (0, import_react.useCallback)(validateValue, [min, max]);
|
|
40
|
+
const [count, setCountState] = (0, import_react.useState)(() => validateValue(initialValue, { min, max }));
|
|
43
41
|
const setCount = (0, import_react.useCallback)(
|
|
44
42
|
(value) => {
|
|
45
43
|
setCountState((prev) => {
|
|
46
44
|
const nextValue = typeof value === "function" ? value(prev) : value;
|
|
47
|
-
return
|
|
45
|
+
return validateValue(nextValue, { min, max });
|
|
48
46
|
});
|
|
49
47
|
},
|
|
50
|
-
[
|
|
48
|
+
[min, max]
|
|
51
49
|
);
|
|
52
50
|
const increment = (0, import_react.useCallback)(() => {
|
|
53
51
|
setCount((prev) => prev + step);
|
|
@@ -33,9 +33,12 @@ var import_react3 = require("react");
|
|
|
33
33
|
var import_react = require("react");
|
|
34
34
|
function usePreservedCallback(callback) {
|
|
35
35
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
36
|
-
(0, import_react.useEffect)(
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
(0, import_react.useEffect)(
|
|
37
|
+
function syncCallbackRef() {
|
|
38
|
+
callbackRef.current = callback;
|
|
39
|
+
},
|
|
40
|
+
[callback]
|
|
41
|
+
);
|
|
39
42
|
return (0, import_react.useCallback)((...args) => {
|
|
40
43
|
return callbackRef.current(...args);
|
|
41
44
|
}, []);
|
|
@@ -111,11 +114,14 @@ function useDebounce(callback, wait, options = {}) {
|
|
|
111
114
|
const debounced = (0, import_react3.useMemo)(() => {
|
|
112
115
|
return debounce(preservedCallback, wait, { edges });
|
|
113
116
|
}, [preservedCallback, wait, edges]);
|
|
114
|
-
(0, import_react2.useEffect)(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
(0, import_react2.useEffect)(
|
|
118
|
+
function cancelDebouncedOnUnmount() {
|
|
119
|
+
return () => {
|
|
120
|
+
debounced.cancel();
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
[debounced]
|
|
124
|
+
);
|
|
119
125
|
return debounced;
|
|
120
126
|
}
|
|
121
127
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -85,9 +85,12 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
|
|
|
85
85
|
var import_react = require("react");
|
|
86
86
|
function usePreservedCallback(callback) {
|
|
87
87
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
88
|
-
(0, import_react.useEffect)(
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
(0, import_react.useEffect)(
|
|
89
|
+
function syncCallbackRef() {
|
|
90
|
+
callbackRef.current = callback;
|
|
91
|
+
},
|
|
92
|
+
[callback]
|
|
93
|
+
);
|
|
91
94
|
return (0, import_react.useCallback)((...args) => {
|
|
92
95
|
return callbackRef.current(...args);
|
|
93
96
|
}, []);
|
|
@@ -103,7 +106,7 @@ function useDebouncedCallback({
|
|
|
103
106
|
const handleChange = usePreservedCallback(onChange);
|
|
104
107
|
const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
|
|
105
108
|
} });
|
|
106
|
-
(0, import_react2.useEffect)(()
|
|
109
|
+
(0, import_react2.useEffect)(function clearDebouncedOnUnmount() {
|
|
107
110
|
const current = ref.current;
|
|
108
111
|
return () => {
|
|
109
112
|
current.clearPreviousDebounce();
|
|
@@ -32,9 +32,12 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -88,9 +88,12 @@ function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
|
|
|
88
88
|
var import_react = require("react");
|
|
89
89
|
function usePreservedCallback(callback) {
|
|
90
90
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
91
|
-
(0, import_react.useEffect)(
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
(0, import_react.useEffect)(
|
|
92
|
+
function syncCallbackRef() {
|
|
93
|
+
callbackRef.current = callback;
|
|
94
|
+
},
|
|
95
|
+
[callback]
|
|
96
|
+
);
|
|
94
97
|
return (0, import_react.useCallback)((...args) => {
|
|
95
98
|
return callbackRef.current(...args);
|
|
96
99
|
}, []);
|
|
@@ -106,7 +109,7 @@ function useDebouncedCallback({
|
|
|
106
109
|
const handleChange = usePreservedCallback(onChange);
|
|
107
110
|
const ref = (0, import_react2.useRef)({ value: false, clearPreviousDebounce: () => {
|
|
108
111
|
} });
|
|
109
|
-
(0, import_react2.useEffect)(()
|
|
112
|
+
(0, import_react2.useEffect)(function clearDebouncedOnUnmount() {
|
|
110
113
|
const current = ref.current;
|
|
111
114
|
return () => {
|
|
112
115
|
current.clearPreviousDebounce();
|
|
@@ -32,9 +32,12 @@ var import_react3 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -32,9 +32,12 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -46,18 +49,24 @@ function useInterval(callback, options) {
|
|
|
46
49
|
const immediate = typeof options === "number" ? false : options.immediate;
|
|
47
50
|
const enabled = typeof options === "number" ? true : options.enabled ?? true;
|
|
48
51
|
const preservedCallback = usePreservedCallback(callback);
|
|
49
|
-
(0, import_react2.useEffect)(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
(0, import_react2.useEffect)(
|
|
53
|
+
function runImmediateCallback() {
|
|
54
|
+
if (immediate === true && enabled) {
|
|
55
|
+
preservedCallback();
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
[immediate, preservedCallback, enabled]
|
|
59
|
+
);
|
|
60
|
+
(0, import_react2.useEffect)(
|
|
61
|
+
function startInterval() {
|
|
62
|
+
if (!enabled) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const id = setInterval(preservedCallback, delay);
|
|
66
|
+
return () => clearInterval(id);
|
|
67
|
+
},
|
|
68
|
+
[delay, preservedCallback, enabled]
|
|
69
|
+
);
|
|
61
70
|
}
|
|
62
71
|
// Annotate the CommonJS export names for ESM import in node:
|
|
63
72
|
0 && (module.exports = {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/hooks/useIsClient/index.ts
|
|
22
|
+
var useIsClient_exports = {};
|
|
23
|
+
__export(useIsClient_exports, {
|
|
24
|
+
useIsClient: () => useIsClient
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(useIsClient_exports);
|
|
27
|
+
|
|
28
|
+
// src/hooks/useIsClient/useIsClient.ts
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
function useIsClient() {
|
|
31
|
+
const [isClient, setIsClient] = (0, import_react.useState)(false);
|
|
32
|
+
(0, import_react.useEffect)(function syncClientState() {
|
|
33
|
+
setIsClient(true);
|
|
34
|
+
}, []);
|
|
35
|
+
return isClient;
|
|
36
|
+
}
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
useIsClient
|
|
40
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* `useIsClient` is a React hook that returns `true` only in the client-side environment.
|
|
4
|
+
* It is primarily used to differentiate between client-side and server-side rendering (SSR).
|
|
5
|
+
* The state is set to `true` only after the component is mounted in the client-side environment.
|
|
6
|
+
*
|
|
7
|
+
* @returns {boolean} Returns `true` in a client-side environment, and `false` otherwise.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* function ClientSideContent() {
|
|
11
|
+
* const isClient = useIsClient();
|
|
12
|
+
*
|
|
13
|
+
* if (!isClient) {
|
|
14
|
+
* return <div>Loading...</div>; // Rendered on the server side
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* return <div>Client-side rendered content</div>; // Rendered on the client side
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* function ClientOnlyMap() {
|
|
22
|
+
* const isClient = useIsClient();
|
|
23
|
+
*
|
|
24
|
+
* if (!isClient) return null;
|
|
25
|
+
*
|
|
26
|
+
* return <div id="map" />;
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* function ClientTheme() {
|
|
31
|
+
* const isClient = useIsClient();
|
|
32
|
+
*
|
|
33
|
+
* const theme = isClient ? localStorage.getItem('theme') : 'light';
|
|
34
|
+
*
|
|
35
|
+
* return <div>Current theme: {theme}</div>;
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
declare function useIsClient(): boolean;
|
|
39
|
+
|
|
40
|
+
export { useIsClient };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/hooks/useList/index.ts
|
|
22
|
+
var useList_exports = {};
|
|
23
|
+
__export(useList_exports, {
|
|
24
|
+
useList: () => useList
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(useList_exports);
|
|
27
|
+
|
|
28
|
+
// src/hooks/useList/useList.ts
|
|
29
|
+
var import_react3 = require("react");
|
|
30
|
+
|
|
31
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
32
|
+
var import_react = require("react");
|
|
33
|
+
function usePreservedCallback(callback) {
|
|
34
|
+
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
41
|
+
return (0, import_react.useCallback)((...args) => {
|
|
42
|
+
return callbackRef.current(...args);
|
|
43
|
+
}, []);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/hooks/usePreservedReference/usePreservedReference.ts
|
|
47
|
+
var import_react2 = require("react");
|
|
48
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
49
|
+
const ref = (0, import_react2.useRef)(value);
|
|
50
|
+
return (0, import_react2.useMemo)(() => {
|
|
51
|
+
if (!areValuesEqual(ref.current, value)) {
|
|
52
|
+
ref.current = value;
|
|
53
|
+
}
|
|
54
|
+
return ref.current;
|
|
55
|
+
}, [areValuesEqual, value]);
|
|
56
|
+
}
|
|
57
|
+
function areDeeplyEqual(x, y) {
|
|
58
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/hooks/useList/useList.ts
|
|
62
|
+
function useList(initialState = []) {
|
|
63
|
+
const [list, setList] = (0, import_react3.useState)(initialState);
|
|
64
|
+
const preservedInitialState = usePreservedReference(initialState);
|
|
65
|
+
const push = usePreservedCallback((value) => {
|
|
66
|
+
setList((prev) => [...prev, value]);
|
|
67
|
+
});
|
|
68
|
+
const insertAt = usePreservedCallback((index, value) => {
|
|
69
|
+
setList((prev) => {
|
|
70
|
+
const next = [...prev];
|
|
71
|
+
next.splice(index, 0, value);
|
|
72
|
+
return next;
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
const updateAt = usePreservedCallback((index, value) => {
|
|
76
|
+
setList((prev) => {
|
|
77
|
+
const next = [...prev];
|
|
78
|
+
next[index] = value;
|
|
79
|
+
return next;
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
const removeAt = usePreservedCallback((index) => {
|
|
83
|
+
setList((prev) => {
|
|
84
|
+
const next = [...prev];
|
|
85
|
+
next.splice(index, 1);
|
|
86
|
+
return next;
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
const setAll = usePreservedCallback((values) => {
|
|
90
|
+
setList(values);
|
|
91
|
+
});
|
|
92
|
+
const reset = usePreservedCallback(() => {
|
|
93
|
+
setList(preservedInitialState);
|
|
94
|
+
});
|
|
95
|
+
const actions = (0, import_react3.useMemo)(
|
|
96
|
+
() => ({ push, insertAt, updateAt, removeAt, setAll, reset }),
|
|
97
|
+
[push, insertAt, updateAt, removeAt, setAll, reset]
|
|
98
|
+
);
|
|
99
|
+
return [list, actions];
|
|
100
|
+
}
|
|
101
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
102
|
+
0 && (module.exports = {
|
|
103
|
+
useList
|
|
104
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
type ListActions<T> = {
|
|
2
|
+
push: (value: T) => void;
|
|
3
|
+
insertAt: (index: number, value: T) => void;
|
|
4
|
+
updateAt: (index: number, value: T) => void;
|
|
5
|
+
removeAt: (index: number) => void;
|
|
6
|
+
setAll: (values: T[]) => void;
|
|
7
|
+
reset: () => void;
|
|
8
|
+
};
|
|
9
|
+
type UseListReturn<T> = [ReadonlyArray<T>, ListActions<T>];
|
|
10
|
+
/**
|
|
11
|
+
* @description
|
|
12
|
+
* A React hook that manages an array as state.
|
|
13
|
+
* Provides efficient state management and stable action functions.
|
|
14
|
+
*
|
|
15
|
+
* @param {T[]} initialState - Initial array state
|
|
16
|
+
*
|
|
17
|
+
* @returns {UseListReturn<T>} A tuple containing the array state and actions to manipulate it
|
|
18
|
+
* - `push` - Appends a value to the end of the list
|
|
19
|
+
* - `insertAt` - Inserts a value at the specified index
|
|
20
|
+
* - `updateAt` - Updates the value at the specified index
|
|
21
|
+
* - `removeAt` - Removes the value at the specified index
|
|
22
|
+
* - `setAll` - Replaces the entire list with a new array
|
|
23
|
+
* - `reset` - Resets the list to its initial state
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* const [list, actions] = useList<string>(['apple', 'banana']);
|
|
28
|
+
*
|
|
29
|
+
* // Add an item
|
|
30
|
+
* actions.push('cherry');
|
|
31
|
+
*
|
|
32
|
+
* // Insert at index
|
|
33
|
+
* actions.insertAt(1, 'grape');
|
|
34
|
+
*
|
|
35
|
+
* // Update at index
|
|
36
|
+
* actions.updateAt(0, 'orange');
|
|
37
|
+
*
|
|
38
|
+
* // Remove at index
|
|
39
|
+
* actions.removeAt(2);
|
|
40
|
+
*
|
|
41
|
+
* // Replace all
|
|
42
|
+
* actions.setAll(['kiwi', 'mango']);
|
|
43
|
+
*
|
|
44
|
+
* // Reset to initial state
|
|
45
|
+
* actions.reset();
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function useList<T>(initialState?: T[]): UseListReturn<T>;
|
|
49
|
+
|
|
50
|
+
export { useList };
|
|
@@ -48,12 +48,15 @@ function useLoading() {
|
|
|
48
48
|
}
|
|
49
49
|
function useIsMountedRef() {
|
|
50
50
|
const ref = (0, import_react.useRef)({ isMounted: true }).current;
|
|
51
|
-
(0, import_react.useEffect)(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
(0, import_react.useEffect)(
|
|
52
|
+
function trackMountedState() {
|
|
53
|
+
ref.isMounted = true;
|
|
54
|
+
return () => {
|
|
55
|
+
ref.isMounted = false;
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
[ref]
|
|
59
|
+
);
|
|
57
60
|
return ref;
|
|
58
61
|
}
|
|
59
62
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -32,9 +32,12 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -32,9 +32,12 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -29,9 +29,12 @@ module.exports = __toCommonJS(usePreservedCallback_exports);
|
|
|
29
29
|
var import_react = require("react");
|
|
30
30
|
function usePreservedCallback(callback) {
|
|
31
31
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
32
|
-
(0, import_react.useEffect)(
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
(0, import_react.useEffect)(
|
|
33
|
+
function syncCallbackRef() {
|
|
34
|
+
callbackRef.current = callback;
|
|
35
|
+
},
|
|
36
|
+
[callback]
|
|
37
|
+
);
|
|
35
38
|
return (0, import_react.useCallback)((...args) => {
|
|
36
39
|
return callbackRef.current(...args);
|
|
37
40
|
}, []);
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @description
|
|
3
3
|
* `usePrevious` is a React hook that returns the previous value of the input state.
|
|
4
|
-
* It preserves the previous value unchanged when re-
|
|
4
|
+
* It preserves the previous value unchanged when re-renders occur without state changes.
|
|
5
5
|
* If the state is an object or requires custom change detection, a `compare` function can be provided.
|
|
6
6
|
* By default, state changes are detected using `prev === next`.
|
|
7
7
|
*
|
|
8
8
|
* @template T - The type of the state.
|
|
9
9
|
* @param {T} state - The state whose previous value is to be tracked.
|
|
10
|
-
* @param {(prev: T
|
|
10
|
+
* @param {(prev: T, next: T) => boolean} [compare] - An optional comparison function to determine if the state has changed.
|
|
11
11
|
*
|
|
12
|
-
* @returns {T
|
|
12
|
+
* @returns {T} The previous value of the state.
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* const [count, setCount] = useState(0);
|
|
@@ -32,9 +32,12 @@ var import_react2 = require("react");
|
|
|
32
32
|
var import_react = require("react");
|
|
33
33
|
function usePreservedCallback(callback) {
|
|
34
34
|
const callbackRef = (0, import_react.useRef)(callback);
|
|
35
|
-
(0, import_react.useEffect)(
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
(0, import_react.useEffect)(
|
|
36
|
+
function syncCallbackRef() {
|
|
37
|
+
callbackRef.current = callback;
|
|
38
|
+
},
|
|
39
|
+
[callback]
|
|
40
|
+
);
|
|
38
41
|
return (0, import_react.useCallback)((...args) => {
|
|
39
42
|
return callbackRef.current(...args);
|
|
40
43
|
}, []);
|
|
@@ -6,10 +6,10 @@ type CleanupCallback = () => void;
|
|
|
6
6
|
* `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
|
|
7
7
|
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
|
|
8
8
|
*
|
|
9
|
-
* @param {(element:
|
|
9
|
+
* @param {(element: RefElement) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
|
|
10
10
|
* @param {DependencyList} deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
|
|
11
11
|
*
|
|
12
|
-
* @returns {(element:
|
|
12
|
+
* @returns {(element: RefElement | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* import { useRefEffect } from 'react-simplikit';
|
|
@@ -26,6 +26,6 @@ type CleanupCallback = () => void;
|
|
|
26
26
|
* return <div ref={ref}>Basic Example</div>;
|
|
27
27
|
* }
|
|
28
28
|
*/
|
|
29
|
-
declare function useRefEffect<
|
|
29
|
+
declare function useRefEffect<RefElement extends HTMLElement = HTMLElement>(callback: (element: RefElement) => CleanupCallback | void, deps: DependencyList): (element: RefElement | null) => void;
|
|
30
30
|
|
|
31
31
|
export { useRefEffect };
|