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
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/hooks/useSet/useSet.ts
|
|
4
|
+
import { useMemo as useMemo2, useState } from "react";
|
|
5
|
+
|
|
6
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
7
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
8
|
+
function usePreservedCallback(callback) {
|
|
9
|
+
const callbackRef = useRef(callback);
|
|
10
|
+
useEffect(
|
|
11
|
+
function syncCallbackRef() {
|
|
12
|
+
callbackRef.current = callback;
|
|
13
|
+
},
|
|
14
|
+
[callback]
|
|
15
|
+
);
|
|
16
|
+
return useCallback((...args) => {
|
|
17
|
+
return callbackRef.current(...args);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/hooks/usePreservedReference/usePreservedReference.ts
|
|
22
|
+
import { useMemo, useRef as useRef2 } from "react";
|
|
23
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
24
|
+
const ref = useRef2(value);
|
|
25
|
+
return useMemo(() => {
|
|
26
|
+
if (!areValuesEqual(ref.current, value)) {
|
|
27
|
+
ref.current = value;
|
|
28
|
+
}
|
|
29
|
+
return ref.current;
|
|
30
|
+
}, [areValuesEqual, value]);
|
|
31
|
+
}
|
|
32
|
+
function areDeeplyEqual(x, y) {
|
|
33
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/hooks/useSet/useSet.ts
|
|
37
|
+
function useSet(initialState = /* @__PURE__ */ new Set()) {
|
|
38
|
+
const [set, setSet] = useState(() => new Set(initialState));
|
|
39
|
+
const preservedInitialState = usePreservedReference(initialState);
|
|
40
|
+
const add = usePreservedCallback((value) => {
|
|
41
|
+
setSet((prev) => {
|
|
42
|
+
const nextSet = new Set(prev);
|
|
43
|
+
nextSet.add(value);
|
|
44
|
+
return nextSet;
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
const remove = usePreservedCallback((value) => {
|
|
48
|
+
setSet((prev) => {
|
|
49
|
+
const nextSet = new Set(prev);
|
|
50
|
+
nextSet.delete(value);
|
|
51
|
+
return nextSet;
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
const toggle = usePreservedCallback((value) => {
|
|
55
|
+
setSet((prev) => {
|
|
56
|
+
const nextSet = new Set(prev);
|
|
57
|
+
if (nextSet.has(value)) {
|
|
58
|
+
nextSet.delete(value);
|
|
59
|
+
} else {
|
|
60
|
+
nextSet.add(value);
|
|
61
|
+
}
|
|
62
|
+
return nextSet;
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
const setAll = usePreservedCallback((values) => {
|
|
66
|
+
setSet(() => new Set(values));
|
|
67
|
+
});
|
|
68
|
+
const reset = usePreservedCallback(() => {
|
|
69
|
+
setSet(() => new Set(preservedInitialState));
|
|
70
|
+
});
|
|
71
|
+
const actions = useMemo2(
|
|
72
|
+
() => ({ add, remove, toggle, setAll, reset }),
|
|
73
|
+
[add, remove, toggle, setAll, reset]
|
|
74
|
+
);
|
|
75
|
+
return [set, actions];
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
useSet
|
|
79
|
+
};
|
|
@@ -7,9 +7,12 @@ import { useEffect as useEffect2, useMemo as useMemo2 } from "react";
|
|
|
7
7
|
import { useCallback, useEffect, useRef } from "react";
|
|
8
8
|
function usePreservedCallback(callback) {
|
|
9
9
|
const callbackRef = useRef(callback);
|
|
10
|
-
useEffect(
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
useEffect(
|
|
11
|
+
function syncCallbackRef() {
|
|
12
|
+
callbackRef.current = callback;
|
|
13
|
+
},
|
|
14
|
+
[callback]
|
|
15
|
+
);
|
|
13
16
|
return useCallback((...args) => {
|
|
14
17
|
return callbackRef.current(...args);
|
|
15
18
|
}, []);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type ThrottleOptions = {
|
|
2
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
|
|
7
|
+
* The throttled callback will only be invoked at most once per specified interval.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} options - The options object.
|
|
10
|
+
* @param {Function} options.onChange - The callback function to throttle.
|
|
11
|
+
* @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
|
|
12
|
+
* @param {Array<'leading' | 'trailing'>} [options.edges=['leading', 'trailing']] - An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Function} A throttled function that limits invoking the callback.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* function ScrollTracker() {
|
|
18
|
+
* const throttledScroll = useThrottledCallback({
|
|
19
|
+
* onChange: (scrollY: number) => console.log(scrollY),
|
|
20
|
+
* timeThreshold: 200,
|
|
21
|
+
* });
|
|
22
|
+
* return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
declare function useThrottledCallback({ onChange, timeThreshold, edges, }: ThrottleOptions & {
|
|
26
|
+
onChange: (newValue: boolean) => void;
|
|
27
|
+
timeThreshold: number;
|
|
28
|
+
}): (nextValue: boolean) => void;
|
|
29
|
+
|
|
30
|
+
export { useThrottledCallback };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/hooks/useThrottledCallback/useThrottledCallback.ts
|
|
4
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useRef as useRef3 } from "react";
|
|
5
|
+
|
|
6
|
+
// src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
7
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
8
|
+
function usePreservedCallback(callback) {
|
|
9
|
+
const callbackRef = useRef(callback);
|
|
10
|
+
useEffect(
|
|
11
|
+
function syncCallbackRef() {
|
|
12
|
+
callbackRef.current = callback;
|
|
13
|
+
},
|
|
14
|
+
[callback]
|
|
15
|
+
);
|
|
16
|
+
return useCallback((...args) => {
|
|
17
|
+
return callbackRef.current(...args);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/hooks/usePreservedReference/usePreservedReference.ts
|
|
22
|
+
import { useMemo, useRef as useRef2 } from "react";
|
|
23
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
24
|
+
const ref = useRef2(value);
|
|
25
|
+
return useMemo(() => {
|
|
26
|
+
if (!areValuesEqual(ref.current, value)) {
|
|
27
|
+
ref.current = value;
|
|
28
|
+
}
|
|
29
|
+
return ref.current;
|
|
30
|
+
}, [areValuesEqual, value]);
|
|
31
|
+
}
|
|
32
|
+
function areDeeplyEqual(x, y) {
|
|
33
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/hooks/useDebounce/debounce.ts
|
|
37
|
+
function debounce(func, debounceMs, { edges = ["leading", "trailing"] } = {}) {
|
|
38
|
+
let pendingThis = void 0;
|
|
39
|
+
let pendingArgs = null;
|
|
40
|
+
const leading = edges != null && edges.includes("leading");
|
|
41
|
+
const trailing = edges == null || edges.includes("trailing");
|
|
42
|
+
const invoke = () => {
|
|
43
|
+
if (pendingArgs !== null) {
|
|
44
|
+
func.apply(pendingThis, pendingArgs);
|
|
45
|
+
pendingThis = void 0;
|
|
46
|
+
pendingArgs = null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const onTimerEnd = () => {
|
|
50
|
+
if (trailing) {
|
|
51
|
+
invoke();
|
|
52
|
+
}
|
|
53
|
+
cancel();
|
|
54
|
+
};
|
|
55
|
+
let timeoutId = null;
|
|
56
|
+
const schedule = () => {
|
|
57
|
+
if (timeoutId != null) {
|
|
58
|
+
clearTimeout(timeoutId);
|
|
59
|
+
}
|
|
60
|
+
timeoutId = setTimeout(() => {
|
|
61
|
+
timeoutId = null;
|
|
62
|
+
onTimerEnd();
|
|
63
|
+
}, debounceMs);
|
|
64
|
+
};
|
|
65
|
+
const cancelTimer = () => {
|
|
66
|
+
if (timeoutId !== null) {
|
|
67
|
+
clearTimeout(timeoutId);
|
|
68
|
+
timeoutId = null;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const cancel = () => {
|
|
72
|
+
cancelTimer();
|
|
73
|
+
pendingThis = void 0;
|
|
74
|
+
pendingArgs = null;
|
|
75
|
+
};
|
|
76
|
+
const debounced = function(...args) {
|
|
77
|
+
pendingThis = this;
|
|
78
|
+
pendingArgs = args;
|
|
79
|
+
const isFirstCall = timeoutId == null;
|
|
80
|
+
schedule();
|
|
81
|
+
if (leading && isFirstCall) {
|
|
82
|
+
invoke();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
debounced.cancel = cancel;
|
|
86
|
+
return debounced;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/hooks/useThrottle/throttle.ts
|
|
90
|
+
function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
|
|
91
|
+
let pendingAt = null;
|
|
92
|
+
const debounced = debounce(func, throttleMs, { edges });
|
|
93
|
+
const throttled = function(...args) {
|
|
94
|
+
if (pendingAt == null) {
|
|
95
|
+
pendingAt = Date.now();
|
|
96
|
+
} else {
|
|
97
|
+
if (Date.now() - pendingAt >= throttleMs) {
|
|
98
|
+
pendingAt = Date.now();
|
|
99
|
+
debounced.cancel();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
debounced(...args);
|
|
103
|
+
};
|
|
104
|
+
throttled.cancel = debounced.cancel;
|
|
105
|
+
return throttled;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/hooks/useThrottledCallback/useThrottledCallback.ts
|
|
109
|
+
function useThrottledCallback({
|
|
110
|
+
onChange,
|
|
111
|
+
timeThreshold,
|
|
112
|
+
edges = ["leading", "trailing"]
|
|
113
|
+
}) {
|
|
114
|
+
const handleChange = usePreservedCallback(onChange);
|
|
115
|
+
const ref = useRef3({ value: false, clearPreviousThrottle: () => {
|
|
116
|
+
} });
|
|
117
|
+
useEffect2(function cleanupThrottleOnUnmount() {
|
|
118
|
+
const current = ref.current;
|
|
119
|
+
return () => {
|
|
120
|
+
current.clearPreviousThrottle();
|
|
121
|
+
};
|
|
122
|
+
}, []);
|
|
123
|
+
const preservedEdges = usePreservedReference(edges);
|
|
124
|
+
return useCallback2(
|
|
125
|
+
(nextValue) => {
|
|
126
|
+
if (nextValue === ref.current.value) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const throttled = throttle(
|
|
130
|
+
() => {
|
|
131
|
+
handleChange(nextValue);
|
|
132
|
+
ref.current.value = nextValue;
|
|
133
|
+
},
|
|
134
|
+
timeThreshold,
|
|
135
|
+
{ edges: preservedEdges }
|
|
136
|
+
);
|
|
137
|
+
ref.current.clearPreviousThrottle();
|
|
138
|
+
throttled();
|
|
139
|
+
ref.current.clearPreviousThrottle = throttled.cancel;
|
|
140
|
+
},
|
|
141
|
+
[handleChange, timeThreshold, preservedEdges]
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
export {
|
|
145
|
+
useThrottledCallback
|
|
146
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @description
|
|
3
3
|
* `useTimeout` is a React hook that executes a callback function after a specified delay.
|
|
4
|
-
* It manages `
|
|
4
|
+
* It manages `setTimeout` in accordance with the React lifecycle, ensuring cleanup on unmount or when dependencies change.
|
|
5
5
|
*
|
|
6
6
|
* @param {() => void} callback - The function to be executed after the delay.
|
|
7
7
|
* @param {number} [delay=0] - The time in milliseconds to wait before executing the callback.
|
|
@@ -7,9 +7,12 @@ import { useEffect as useEffect2 } from "react";
|
|
|
7
7
|
import { useCallback, useEffect, useRef } from "react";
|
|
8
8
|
function usePreservedCallback(callback) {
|
|
9
9
|
const callbackRef = useRef(callback);
|
|
10
|
-
useEffect(
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
useEffect(
|
|
11
|
+
function syncCallbackRef() {
|
|
12
|
+
callbackRef.current = callback;
|
|
13
|
+
},
|
|
14
|
+
[callback]
|
|
15
|
+
);
|
|
13
16
|
return useCallback((...args) => {
|
|
14
17
|
return callbackRef.current(...args);
|
|
15
18
|
}, []);
|
|
@@ -19,8 +22,8 @@ function usePreservedCallback(callback) {
|
|
|
19
22
|
function useTimeout(callback, delay = 0) {
|
|
20
23
|
const preservedCallback = usePreservedCallback(callback);
|
|
21
24
|
useEffect2(() => {
|
|
22
|
-
const timeoutId =
|
|
23
|
-
return () =>
|
|
25
|
+
const timeoutId = setTimeout(preservedCallback, delay);
|
|
26
|
+
return () => clearTimeout(timeoutId);
|
|
24
27
|
}, [delay, preservedCallback]);
|
|
25
28
|
}
|
|
26
29
|
export {
|
package/esm/index.d.ts
CHANGED
|
@@ -15,7 +15,9 @@ export { useImpressionRef } from './hooks/useImpressionRef/index.js';
|
|
|
15
15
|
export { useInputState } from './hooks/useInputState/index.js';
|
|
16
16
|
export { useIntersectionObserver } from './hooks/useIntersectionObserver/index.js';
|
|
17
17
|
export { useInterval } from './hooks/useInterval/index.js';
|
|
18
|
+
export { useIsClient } from './hooks/useIsClient/index.js';
|
|
18
19
|
export { useIsomorphicLayoutEffect } from './hooks/useIsomorphicLayoutEffect/index.js';
|
|
20
|
+
export { useList } from './hooks/useList/index.js';
|
|
19
21
|
export { useLoading } from './hooks/useLoading/index.js';
|
|
20
22
|
export { useLongPress } from './hooks/useLongPress/index.js';
|
|
21
23
|
export { useMap } from './hooks/useMap/index.js';
|
|
@@ -24,8 +26,10 @@ export { usePreservedCallback } from './hooks/usePreservedCallback/index.js';
|
|
|
24
26
|
export { usePreservedReference } from './hooks/usePreservedReference/index.js';
|
|
25
27
|
export { usePrevious } from './hooks/usePrevious/index.js';
|
|
26
28
|
export { useRefEffect } from './hooks/useRefEffect/index.js';
|
|
29
|
+
export { useSet } from './hooks/useSet/index.js';
|
|
27
30
|
export { useStorageState } from './hooks/useStorageState/index.js';
|
|
28
31
|
export { useThrottle } from './hooks/useThrottle/index.js';
|
|
32
|
+
export { useThrottledCallback } from './hooks/useThrottledCallback/index.js';
|
|
29
33
|
export { useTimeout } from './hooks/useTimeout/index.js';
|
|
30
34
|
export { useToggle } from './hooks/useToggle/index.js';
|
|
31
35
|
export { useVisibilityEvent } from './hooks/useVisibilityEvent/index.js';
|