react-simplikit 0.0.50 → 0.0.52
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/ImpressionArea.cjs +57 -0
- package/{esm/components/ImpressionArea/index.d.ts → dist/components/ImpressionArea/ImpressionArea.d.cts} +9 -8
- package/dist/components/ImpressionArea/ImpressionArea.d.mts +48 -0
- package/dist/components/ImpressionArea/ImpressionArea.mjs +57 -0
- package/dist/components/ImpressionArea/index.d.cts +2 -46
- package/dist/components/ImpressionArea/index.d.mts +3 -0
- package/dist/components/Separated/Separated.cjs +38 -0
- package/{esm/components/Separated/index.d.ts → dist/components/Separated/Separated.d.cts} +7 -7
- package/dist/components/Separated/Separated.d.mts +38 -0
- package/dist/components/Separated/Separated.mjs +38 -0
- package/dist/components/Separated/index.d.cts +2 -37
- package/dist/components/Separated/index.d.mts +3 -0
- package/dist/components/SwitchCase/SwitchCase.cjs +40 -0
- package/{esm/components/SwitchCase/index.d.ts → dist/components/SwitchCase/SwitchCase.d.cts} +7 -8
- package/dist/components/SwitchCase/SwitchCase.d.mts +45 -0
- package/dist/components/SwitchCase/SwitchCase.mjs +40 -0
- package/dist/components/SwitchCase/index.d.cts +2 -45
- package/dist/components/SwitchCase/index.d.mts +3 -0
- package/dist/hooks/useAsyncEffect/index.d.cts +2 -24
- package/dist/hooks/useAsyncEffect/index.d.mts +3 -0
- package/dist/hooks/useAsyncEffect/useAsyncEffect.cjs +39 -0
- package/{esm/hooks/useAsyncEffect/index.d.ts → dist/hooks/useAsyncEffect/useAsyncEffect.d.cts} +4 -3
- package/dist/hooks/useAsyncEffect/useAsyncEffect.d.mts +26 -0
- package/dist/hooks/useAsyncEffect/useAsyncEffect.mjs +39 -0
- package/dist/hooks/useBooleanState/useBooleanState.cjs +36 -0
- package/dist/hooks/useBooleanState/{index.d.cts → useBooleanState.d.cts} +4 -2
- package/{esm/hooks/useBooleanState/index.d.ts → dist/hooks/useBooleanState/useBooleanState.d.mts} +4 -2
- package/dist/hooks/useBooleanState/useBooleanState.mjs +36 -0
- package/dist/hooks/useCallbackOncePerRender/index.d.cts +2 -39
- package/dist/hooks/useCallbackOncePerRender/index.d.mts +3 -0
- package/dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.cjs +52 -0
- package/{esm/hooks/useCallbackOncePerRender/index.d.ts → dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.d.cts} +4 -3
- package/dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.d.mts +41 -0
- package/dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.mjs +52 -0
- package/dist/hooks/useConditionalEffect/index.d.cts +2 -42
- package/dist/hooks/useConditionalEffect/index.d.mts +3 -0
- package/dist/hooks/useConditionalEffect/useConditionalEffect.cjs +57 -0
- package/{esm/hooks/useConditionalEffect/index.d.ts → dist/hooks/useConditionalEffect/useConditionalEffect.d.cts} +4 -3
- package/dist/hooks/useConditionalEffect/useConditionalEffect.d.mts +44 -0
- package/dist/hooks/useConditionalEffect/useConditionalEffect.mjs +57 -0
- package/dist/hooks/useControlledState/index.d.cts +2 -50
- package/dist/hooks/useControlledState/index.d.mts +3 -0
- package/dist/hooks/useControlledState/useControlledState.cjs +60 -0
- package/{esm/hooks/useControlledState/index.d.ts → dist/hooks/useControlledState/useControlledState.d.cts} +11 -10
- package/dist/hooks/useControlledState/useControlledState.d.mts +52 -0
- package/dist/hooks/useControlledState/useControlledState.mjs +60 -0
- package/dist/hooks/useCounter/useCounter.cjs +75 -0
- package/{esm/hooks/useCounter/index.d.ts → dist/hooks/useCounter/useCounter.d.cts} +12 -10
- package/dist/hooks/useCounter/{index.d.cts → useCounter.d.mts} +12 -10
- package/dist/hooks/useCounter/useCounter.mjs +75 -0
- package/dist/hooks/useDebounce/debounce.cjs +49 -0
- package/dist/hooks/useDebounce/debounce.d.cts +8 -0
- package/dist/hooks/useDebounce/debounce.d.mts +8 -0
- package/dist/hooks/useDebounce/debounce.mjs +49 -0
- package/dist/hooks/useDebounce/useDebounce.cjs +66 -0
- package/{esm/hooks/useDebounce/index.d.ts → dist/hooks/useDebounce/useDebounce.d.cts} +6 -8
- package/dist/hooks/useDebounce/{index.d.cts → useDebounce.d.mts} +6 -8
- package/dist/hooks/useDebounce/useDebounce.mjs +66 -0
- package/dist/hooks/useDebouncedCallback/useDebouncedCallback.cjs +62 -0
- package/{esm/hooks/useDebouncedCallback/index.d.ts → dist/hooks/useDebouncedCallback/useDebouncedCallback.d.cts} +9 -7
- package/dist/hooks/useDebouncedCallback/{index.d.cts → useDebouncedCallback.d.mts} +9 -7
- package/dist/hooks/useDebouncedCallback/useDebouncedCallback.mjs +62 -0
- package/dist/hooks/useDoubleClick/index.d.cts +2 -45
- package/dist/hooks/useDoubleClick/index.d.mts +3 -0
- package/dist/hooks/useDoubleClick/useDoubleClick.cjs +64 -0
- package/{esm/hooks/useDoubleClick/index.d.ts → dist/hooks/useDoubleClick/useDoubleClick.d.cts} +8 -7
- package/dist/hooks/useDoubleClick/useDoubleClick.d.mts +47 -0
- package/dist/hooks/useDoubleClick/useDoubleClick.mjs +64 -0
- package/dist/hooks/useGeolocation/useGeolocation.cjs +210 -0
- package/{esm/hooks/useGeolocation/index.d.ts → dist/hooks/useGeolocation/useGeolocation.d.cts} +26 -24
- package/dist/hooks/useGeolocation/{index.d.cts → useGeolocation.d.mts} +26 -24
- package/dist/hooks/useGeolocation/useGeolocation.mjs +209 -0
- package/dist/hooks/useImpressionRef/useImpressionRef.cjs +67 -0
- package/{esm/hooks/useImpressionRef/index.d.ts → dist/hooks/useImpressionRef/useImpressionRef.d.cts} +10 -8
- package/dist/hooks/useImpressionRef/{index.d.cts → useImpressionRef.d.mts} +10 -8
- package/dist/hooks/useImpressionRef/useImpressionRef.mjs +67 -0
- package/dist/hooks/useInputState/index.d.cts +2 -22
- package/dist/hooks/useInputState/index.d.mts +3 -0
- package/dist/hooks/useInputState/useInputState.cjs +32 -0
- package/{esm/hooks/useInputState/index.d.ts → dist/hooks/useInputState/useInputState.d.cts} +4 -3
- package/dist/hooks/useInputState/useInputState.d.mts +24 -0
- package/dist/hooks/useInputState/useInputState.mjs +32 -0
- package/dist/hooks/useIntersectionObserver/useIntersectionObserver.cjs +58 -0
- package/dist/hooks/useIntersectionObserver/{index.d.cts → useIntersectionObserver.d.cts} +4 -2
- package/{esm/hooks/useIntersectionObserver/index.d.ts → dist/hooks/useIntersectionObserver/useIntersectionObserver.d.mts} +4 -2
- package/dist/hooks/useIntersectionObserver/useIntersectionObserver.mjs +58 -0
- package/dist/hooks/useInterval/useInterval.cjs +65 -0
- package/dist/hooks/useInterval/{index.d.cts → useInterval.d.cts} +7 -5
- package/{esm/hooks/useInterval/index.d.ts → dist/hooks/useInterval/useInterval.d.mts} +7 -5
- package/dist/hooks/useInterval/useInterval.mjs +65 -0
- package/dist/hooks/useIsClient/useIsClient.cjs +49 -0
- package/dist/hooks/useIsClient/{index.d.cts → useIsClient.d.cts} +4 -2
- package/{esm/hooks/useIsClient/index.d.ts → dist/hooks/useIsClient/useIsClient.d.mts} +4 -2
- package/dist/hooks/useIsClient/useIsClient.mjs +49 -0
- package/dist/hooks/useIsomorphicLayoutEffect/index.d.cts +2 -24
- package/dist/hooks/useIsomorphicLayoutEffect/index.d.mts +3 -0
- package/dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.cjs +25 -0
- package/{esm/hooks/useIsomorphicLayoutEffect/index.d.ts → dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.d.cts} +4 -3
- package/dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.d.mts +26 -0
- package/dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.mjs +25 -0
- package/dist/hooks/useList/useList.cjs +94 -0
- package/{esm/hooks/useList/index.d.ts → dist/hooks/useList/useList.d.cts} +10 -8
- package/dist/hooks/useList/{index.d.cts → useList.d.mts} +10 -8
- package/dist/hooks/useList/useList.mjs +94 -0
- package/dist/hooks/useLoading/useLoading.cjs +61 -0
- package/dist/hooks/useLoading/{index.d.cts → useLoading.d.cts} +4 -2
- package/{esm/hooks/useLoading/index.d.ts → dist/hooks/useLoading/useLoading.d.mts} +4 -2
- package/dist/hooks/useLoading/useLoading.mjs +61 -0
- package/dist/hooks/useLongPress/index.d.cts +2 -73
- package/dist/hooks/useLongPress/index.d.mts +3 -0
- package/dist/hooks/useLongPress/useLongPress.cjs +133 -0
- package/{esm/hooks/useLongPress/index.d.ts → dist/hooks/useLongPress/useLongPress.d.cts} +18 -17
- package/dist/hooks/useLongPress/useLongPress.d.mts +75 -0
- package/dist/hooks/useLongPress/useLongPress.mjs +133 -0
- package/dist/hooks/useMap/useMap.cjs +64 -0
- package/dist/hooks/useMap/{index.d.cts → useMap.d.cts} +12 -10
- package/{esm/hooks/useMap/index.d.ts → dist/hooks/useMap/useMap.d.mts} +12 -10
- package/dist/hooks/useMap/useMap.mjs +64 -0
- package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.cjs +51 -0
- package/{esm/hooks/useOutsideClickEffect/index.d.ts → dist/hooks/useOutsideClickEffect/useOutsideClickEffect.d.cts} +4 -2
- package/dist/hooks/useOutsideClickEffect/{index.d.cts → useOutsideClickEffect.d.mts} +4 -2
- package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.mjs +51 -0
- package/dist/hooks/usePreservedCallback/usePreservedCallback.cjs +41 -0
- package/dist/hooks/usePreservedCallback/{index.d.cts → usePreservedCallback.d.cts} +4 -2
- package/{esm/hooks/usePreservedCallback/index.d.ts → dist/hooks/usePreservedCallback/usePreservedCallback.d.mts} +4 -2
- package/dist/hooks/usePreservedCallback/usePreservedCallback.mjs +41 -0
- package/dist/hooks/usePreservedReference/usePreservedReference.cjs +55 -0
- package/dist/hooks/usePreservedReference/{index.d.cts → usePreservedReference.d.cts} +4 -2
- package/{esm/hooks/usePreservedReference/index.d.ts → dist/hooks/usePreservedReference/usePreservedReference.d.mts} +4 -2
- package/dist/hooks/usePreservedReference/usePreservedReference.mjs +55 -0
- package/dist/hooks/usePrevious/usePrevious.cjs +38 -0
- package/{esm/hooks/usePrevious/index.d.ts → dist/hooks/usePrevious/usePrevious.d.cts} +4 -2
- package/dist/hooks/usePrevious/{index.d.cts → usePrevious.d.mts} +4 -2
- package/dist/hooks/usePrevious/usePrevious.mjs +38 -0
- package/dist/hooks/useRefEffect/index.d.cts +2 -30
- package/dist/hooks/useRefEffect/index.d.mts +3 -0
- package/dist/hooks/useRefEffect/useRefEffect.cjs +42 -0
- package/{esm/hooks/useRefEffect/index.d.ts → dist/hooks/useRefEffect/useRefEffect.d.cts} +4 -3
- package/dist/hooks/useRefEffect/useRefEffect.d.mts +32 -0
- package/dist/hooks/useRefEffect/useRefEffect.mjs +42 -0
- package/dist/hooks/useSet/useSet.cjs +79 -0
- package/dist/hooks/useSet/{index.d.cts → useSet.d.cts} +9 -7
- package/{esm/hooks/useSet/index.d.ts → dist/hooks/useSet/useSet.d.mts} +9 -7
- package/dist/hooks/useSet/useSet.mjs +79 -0
- package/dist/hooks/useStorageState/index.d.cts +2 -60
- package/dist/hooks/useStorageState/index.d.mts +3 -0
- package/dist/hooks/useStorageState/storage.cjs +85 -0
- package/dist/hooks/useStorageState/storage.d.cts +10 -0
- package/dist/hooks/useStorageState/storage.d.mts +10 -0
- package/dist/hooks/useStorageState/storage.mjs +80 -0
- package/dist/hooks/useStorageState/useStorageState.cjs +81 -0
- package/{esm/hooks/useStorageState/index.d.ts → dist/hooks/useStorageState/useStorageState.d.cts} +10 -15
- package/dist/hooks/useStorageState/useStorageState.d.mts +56 -0
- package/dist/hooks/useStorageState/useStorageState.mjs +81 -0
- package/dist/hooks/useThrottle/throttle.cjs +19 -0
- package/dist/hooks/useThrottle/throttle.d.cts +19 -0
- package/dist/hooks/useThrottle/throttle.d.mts +19 -0
- package/dist/hooks/useThrottle/throttle.mjs +19 -0
- package/dist/hooks/useThrottle/useThrottle.cjs +49 -0
- package/dist/hooks/useThrottle/{index.d.cts → useThrottle.d.cts} +6 -19
- package/{esm/hooks/useThrottle/index.d.ts → dist/hooks/useThrottle/useThrottle.d.mts} +6 -19
- package/dist/hooks/useThrottle/useThrottle.mjs +49 -0
- package/dist/hooks/useThrottledCallback/useThrottledCallback.cjs +57 -0
- package/{esm/hooks/useThrottledCallback/index.d.ts → dist/hooks/useThrottledCallback/useThrottledCallback.d.cts} +8 -6
- package/dist/hooks/useThrottledCallback/{index.d.cts → useThrottledCallback.d.mts} +8 -6
- package/dist/hooks/useThrottledCallback/useThrottledCallback.mjs +57 -0
- package/dist/hooks/useTimeout/useTimeout.cjs +40 -0
- package/dist/hooks/useTimeout/{index.d.cts → useTimeout.d.cts} +4 -2
- package/{esm/hooks/useTimeout/index.d.ts → dist/hooks/useTimeout/useTimeout.d.mts} +4 -2
- package/dist/hooks/useTimeout/useTimeout.mjs +40 -0
- package/dist/hooks/useToggle/useToggle.cjs +34 -0
- package/dist/hooks/useToggle/{index.d.cts → useToggle.d.cts} +4 -4
- package/{esm/hooks/useToggle/index.d.ts → dist/hooks/useToggle/useToggle.d.mts} +4 -4
- package/dist/hooks/useToggle/useToggle.mjs +34 -0
- package/dist/hooks/useVisibilityEvent/useVisibilityEvent.cjs +42 -0
- package/{esm/hooks/useVisibilityEvent/index.d.ts → dist/hooks/useVisibilityEvent/useVisibilityEvent.d.cts} +5 -3
- package/dist/hooks/useVisibilityEvent/{index.d.cts → useVisibilityEvent.d.mts} +5 -3
- package/dist/hooks/useVisibilityEvent/useVisibilityEvent.mjs +42 -0
- package/dist/index.cjs +77 -1382
- package/dist/index.d.cts +56 -40
- package/dist/index.d.mts +56 -0
- package/dist/index.mjs +40 -0
- package/dist/utils/buildContext/buildContext.cjs +50 -0
- package/{esm/utils/buildContext/index.d.ts → dist/utils/buildContext/buildContext.d.cts} +7 -7
- package/dist/utils/buildContext/buildContext.d.mts +38 -0
- package/dist/utils/buildContext/buildContext.mjs +50 -0
- package/dist/utils/buildContext/index.d.cts +2 -37
- package/dist/utils/buildContext/index.d.mts +3 -0
- package/dist/utils/mergeProps/index.d.cts +2 -25
- package/dist/utils/mergeProps/index.d.mts +3 -0
- package/dist/utils/mergeProps/mergeProps.cjs +53 -0
- package/{esm/utils/mergeProps/index.d.ts → dist/utils/mergeProps/mergeProps.d.cts} +7 -8
- package/dist/utils/mergeProps/mergeProps.d.mts +25 -0
- package/dist/utils/mergeProps/mergeProps.mjs +53 -0
- package/dist/utils/mergeRefs/index.d.cts +2 -38
- package/dist/utils/mergeRefs/index.d.mts +3 -0
- package/dist/utils/mergeRefs/mergeRefs.cjs +50 -0
- package/{esm/utils/mergeRefs/index.d.ts → dist/utils/mergeRefs/mergeRefs.d.cts} +4 -3
- package/dist/utils/mergeRefs/mergeRefs.d.mts +40 -0
- package/dist/utils/mergeRefs/mergeRefs.mjs +50 -0
- package/package.json +9 -11
- package/dist/components/ImpressionArea/index.cjs +0 -295
- package/dist/components/Separated/index.cjs +0 -41
- package/dist/components/SwitchCase/index.cjs +0 -36
- package/dist/hooks/useAsyncEffect/index.cjs +0 -44
- package/dist/hooks/useBooleanState/index.cjs +0 -46
- package/dist/hooks/useCallbackOncePerRender/index.cjs +0 -63
- package/dist/hooks/useConditionalEffect/index.cjs +0 -51
- package/dist/hooks/useControlledState/index.cjs +0 -57
- package/dist/hooks/useCounter/index.cjs +0 -70
- package/dist/hooks/useDebounce/index.cjs +0 -130
- package/dist/hooks/useDebouncedCallback/index.cjs +0 -148
- package/dist/hooks/useDoubleClick/index.cjs +0 -79
- package/dist/hooks/useGeolocation/index.cjs +0 -154
- package/dist/hooks/useImpressionRef/index.cjs +0 -258
- package/dist/hooks/useInputState/index.cjs +0 -46
- package/dist/hooks/useIntersectionObserver/index.cjs +0 -95
- package/dist/hooks/useInterval/index.cjs +0 -74
- package/dist/hooks/useIsClient/index.cjs +0 -40
- package/dist/hooks/useIsomorphicLayoutEffect/index.cjs +0 -35
- package/dist/hooks/useList/index.cjs +0 -104
- package/dist/hooks/useLoading/index.cjs +0 -65
- package/dist/hooks/useLongPress/index.cjs +0 -127
- package/dist/hooks/useMap/index.cjs +0 -78
- package/dist/hooks/useOutsideClickEffect/index.cjs +0 -74
- package/dist/hooks/usePreservedCallback/index.cjs +0 -45
- package/dist/hooks/usePreservedReference/index.cjs +0 -45
- package/dist/hooks/usePrevious/index.cjs +0 -48
- package/dist/hooks/useRefEffect/index.cjs +0 -72
- package/dist/hooks/useSet/index.cjs +0 -105
- package/dist/hooks/useStorageState/index.cjs +0 -196
- package/dist/hooks/useThrottle/index.cjs +0 -151
- package/dist/hooks/useThrottledCallback/index.cjs +0 -172
- package/dist/hooks/useTimeout/index.cjs +0 -57
- package/dist/hooks/useToggle/index.cjs +0 -37
- package/dist/hooks/useVisibilityEvent/index.cjs +0 -47
- package/dist/utils/buildContext/index.cjs +0 -56
- package/dist/utils/mergeProps/index.cjs +0 -71
- package/dist/utils/mergeRefs/index.cjs +0 -46
- package/esm/components/ImpressionArea/index.js +0 -269
- package/esm/components/Separated/index.js +0 -15
- package/esm/components/SwitchCase/index.js +0 -10
- package/esm/hooks/useAsyncEffect/index.js +0 -18
- package/esm/hooks/useBooleanState/index.js +0 -20
- package/esm/hooks/useCallbackOncePerRender/index.js +0 -37
- package/esm/hooks/useConditionalEffect/index.js +0 -25
- package/esm/hooks/useControlledState/index.js +0 -31
- package/esm/hooks/useCounter/index.js +0 -44
- package/esm/hooks/useDebounce/index.js +0 -104
- package/esm/hooks/useDebouncedCallback/index.js +0 -122
- package/esm/hooks/useDoubleClick/index.js +0 -53
- package/esm/hooks/useGeolocation/index.js +0 -128
- package/esm/hooks/useImpressionRef/index.js +0 -232
- package/esm/hooks/useInputState/index.js +0 -20
- package/esm/hooks/useIntersectionObserver/index.js +0 -69
- package/esm/hooks/useInterval/index.js +0 -48
- package/esm/hooks/useIsClient/index.js +0 -14
- package/esm/hooks/useIsomorphicLayoutEffect/index.js +0 -9
- package/esm/hooks/useList/index.js +0 -78
- package/esm/hooks/useLoading/index.js +0 -39
- package/esm/hooks/useLongPress/index.js +0 -101
- package/esm/hooks/useMap/index.js +0 -52
- package/esm/hooks/useOutsideClickEffect/index.js +0 -48
- package/esm/hooks/usePreservedCallback/index.js +0 -19
- package/esm/hooks/usePreservedReference/index.js +0 -19
- package/esm/hooks/usePrevious/index.js +0 -22
- package/esm/hooks/useRefEffect/index.js +0 -46
- package/esm/hooks/useSet/index.js +0 -79
- package/esm/hooks/useStorageState/index.js +0 -170
- package/esm/hooks/useThrottle/index.js +0 -125
- package/esm/hooks/useThrottledCallback/index.js +0 -146
- package/esm/hooks/useTimeout/index.js +0 -31
- package/esm/hooks/useToggle/index.js +0 -11
- package/esm/hooks/useVisibilityEvent/index.js +0 -21
- package/esm/index.d.ts +0 -40
- package/esm/index.js +0 -1320
- package/esm/utils/buildContext/index.js +0 -30
- package/esm/utils/mergeProps/index.js +0 -45
- package/esm/utils/mergeRefs/index.js +0 -20
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useThrottle/throttle.d.ts
|
|
3
|
+
type ThrottleOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
6
|
+
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
7
|
+
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
8
|
+
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
9
|
+
* @default ["leading", "trailing"]
|
|
10
|
+
*/
|
|
11
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
12
|
+
};
|
|
13
|
+
type ThrottledFunction<F extends (...args: any[]) => void> = {
|
|
14
|
+
(...args: Parameters<F>): void;
|
|
15
|
+
cancel: () => void;
|
|
16
|
+
};
|
|
17
|
+
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { edges }?: ThrottleOptions): ThrottledFunction<F>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { throttle };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useThrottle/throttle.d.ts
|
|
3
|
+
type ThrottleOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
6
|
+
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
7
|
+
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
8
|
+
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
9
|
+
* @default ["leading", "trailing"]
|
|
10
|
+
*/
|
|
11
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
12
|
+
};
|
|
13
|
+
type ThrottledFunction<F extends (...args: any[]) => void> = {
|
|
14
|
+
(...args: Parameters<F>): void;
|
|
15
|
+
cancel: () => void;
|
|
16
|
+
};
|
|
17
|
+
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { edges }?: ThrottleOptions): ThrottledFunction<F>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { throttle };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { debounce } from "../useDebounce/debounce.mjs";
|
|
3
|
+
//#region src/hooks/useThrottle/throttle.ts
|
|
4
|
+
function throttle(func, throttleMs, { edges = ["leading", "trailing"] } = {}) {
|
|
5
|
+
let pendingAt = null;
|
|
6
|
+
const debounced = debounce(func, throttleMs, { edges });
|
|
7
|
+
const throttled = function(...args) {
|
|
8
|
+
if (pendingAt == null) pendingAt = Date.now();
|
|
9
|
+
else if (Date.now() - pendingAt >= throttleMs) {
|
|
10
|
+
pendingAt = Date.now();
|
|
11
|
+
debounced.cancel();
|
|
12
|
+
}
|
|
13
|
+
debounced(...args);
|
|
14
|
+
};
|
|
15
|
+
throttled.cancel = debounced.cancel;
|
|
16
|
+
return throttled;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { throttle };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
const require_usePreservedReference = require("../usePreservedReference/usePreservedReference.cjs");
|
|
4
|
+
const require_throttle = require("./throttle.cjs");
|
|
5
|
+
let react = require("react");
|
|
6
|
+
//#region src/hooks/useThrottle/useThrottle.ts
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* `useThrottle` is a React hook that creates a throttled version of a callback function.
|
|
10
|
+
* This is useful for limiting the rate at which a function can be called,
|
|
11
|
+
* such as when handling scroll or resize events.
|
|
12
|
+
*
|
|
13
|
+
* @template {(...args: any[]) => any} F - The type of the callback function.
|
|
14
|
+
* @param {F} callback - The function to be throttled.
|
|
15
|
+
* @param {number} wait - The number of milliseconds to throttle invocations to.
|
|
16
|
+
* @param {{ edges?: Array<'leading' | 'trailing'> }} [options] - Options to control the behavior of the throttle.
|
|
17
|
+
* @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.
|
|
18
|
+
* @returns {F & { cancel: () => void }} - Returns the throttled function with a `cancel` method to cancel pending executions.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const throttledScroll = useThrottle(() => {
|
|
22
|
+
* console.log('Scroll event');
|
|
23
|
+
* }, 200, { edges: ['leading', 'trailing'] });
|
|
24
|
+
*
|
|
25
|
+
* useEffect(() => {
|
|
26
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
27
|
+
* return () => {
|
|
28
|
+
* window.removeEventListener('scroll', throttledScroll);
|
|
29
|
+
* throttledScroll.cancel();
|
|
30
|
+
* };
|
|
31
|
+
* }, [throttledScroll]);
|
|
32
|
+
*/
|
|
33
|
+
function useThrottle(callback, wait, options) {
|
|
34
|
+
const preservedCallback = require_usePreservedCallback.usePreservedCallback(callback);
|
|
35
|
+
const preservedOptions = require_usePreservedReference.usePreservedReference(options ?? {});
|
|
36
|
+
const throttledCallback = (0, react.useMemo)(() => require_throttle.throttle(preservedCallback, wait, preservedOptions), [
|
|
37
|
+
preservedOptions,
|
|
38
|
+
preservedCallback,
|
|
39
|
+
wait
|
|
40
|
+
]);
|
|
41
|
+
(0, react.useEffect)(() => {
|
|
42
|
+
return () => {
|
|
43
|
+
throttledCallback.cancel();
|
|
44
|
+
};
|
|
45
|
+
}, [throttledCallback]);
|
|
46
|
+
return throttledCallback;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.useThrottle = useThrottle;
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
type ThrottleOptions = {
|
|
2
|
-
/**
|
|
3
|
-
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
4
|
-
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
5
|
-
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
6
|
-
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
7
|
-
* @default ["leading", "trailing"]
|
|
8
|
-
*/
|
|
9
|
-
edges?: Array<'leading' | 'trailing'>;
|
|
10
|
-
};
|
|
11
|
-
type ThrottledFunction<F extends (...args: any[]) => void> = {
|
|
12
|
-
(...args: Parameters<F>): void;
|
|
13
|
-
cancel: () => void;
|
|
14
|
-
};
|
|
15
|
-
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { edges }?: ThrottleOptions): ThrottledFunction<F>;
|
|
16
1
|
|
|
2
|
+
import { throttle } from "./throttle.cjs";
|
|
3
|
+
//#region src/hooks/useThrottle/useThrottle.d.ts
|
|
17
4
|
/**
|
|
18
5
|
* @description
|
|
19
6
|
* `useThrottle` is a React hook that creates a throttled version of a callback function.
|
|
@@ -41,8 +28,8 @@ declare function throttle<F extends (...args: any[]) => void>(func: F, throttleM
|
|
|
41
28
|
* }, [throttledScroll]);
|
|
42
29
|
*/
|
|
43
30
|
declare function useThrottle<F extends (...args: any[]) => any>(callback: F, wait: number, options?: Parameters<typeof throttle>[2]): {
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
(...args: any[]): void;
|
|
32
|
+
cancel: () => void;
|
|
46
33
|
};
|
|
47
|
-
|
|
48
|
-
export { useThrottle };
|
|
34
|
+
//#endregion
|
|
35
|
+
export { useThrottle };
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
type ThrottleOptions = {
|
|
2
|
-
/**
|
|
3
|
-
* An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
|
|
4
|
-
* If `edges` includes "leading", the function will be invoked at the start of the delay period.
|
|
5
|
-
* If `edges` includes "trailing", the function will be invoked at the end of the delay period.
|
|
6
|
-
* If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
|
|
7
|
-
* @default ["leading", "trailing"]
|
|
8
|
-
*/
|
|
9
|
-
edges?: Array<'leading' | 'trailing'>;
|
|
10
|
-
};
|
|
11
|
-
type ThrottledFunction<F extends (...args: any[]) => void> = {
|
|
12
|
-
(...args: Parameters<F>): void;
|
|
13
|
-
cancel: () => void;
|
|
14
|
-
};
|
|
15
|
-
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { edges }?: ThrottleOptions): ThrottledFunction<F>;
|
|
16
1
|
|
|
2
|
+
import { throttle } from "./throttle.mjs";
|
|
3
|
+
//#region src/hooks/useThrottle/useThrottle.d.ts
|
|
17
4
|
/**
|
|
18
5
|
* @description
|
|
19
6
|
* `useThrottle` is a React hook that creates a throttled version of a callback function.
|
|
@@ -41,8 +28,8 @@ declare function throttle<F extends (...args: any[]) => void>(func: F, throttleM
|
|
|
41
28
|
* }, [throttledScroll]);
|
|
42
29
|
*/
|
|
43
30
|
declare function useThrottle<F extends (...args: any[]) => any>(callback: F, wait: number, options?: Parameters<typeof throttle>[2]): {
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
(...args: any[]): void;
|
|
32
|
+
cancel: () => void;
|
|
46
33
|
};
|
|
47
|
-
|
|
48
|
-
export { useThrottle };
|
|
34
|
+
//#endregion
|
|
35
|
+
export { useThrottle };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { usePreservedReference } from "../usePreservedReference/usePreservedReference.mjs";
|
|
4
|
+
import { throttle } from "./throttle.mjs";
|
|
5
|
+
import { useEffect, useMemo } from "react";
|
|
6
|
+
//#region src/hooks/useThrottle/useThrottle.ts
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* `useThrottle` is a React hook that creates a throttled version of a callback function.
|
|
10
|
+
* This is useful for limiting the rate at which a function can be called,
|
|
11
|
+
* such as when handling scroll or resize events.
|
|
12
|
+
*
|
|
13
|
+
* @template {(...args: any[]) => any} F - The type of the callback function.
|
|
14
|
+
* @param {F} callback - The function to be throttled.
|
|
15
|
+
* @param {number} wait - The number of milliseconds to throttle invocations to.
|
|
16
|
+
* @param {{ edges?: Array<'leading' | 'trailing'> }} [options] - Options to control the behavior of the throttle.
|
|
17
|
+
* @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.
|
|
18
|
+
* @returns {F & { cancel: () => void }} - Returns the throttled function with a `cancel` method to cancel pending executions.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const throttledScroll = useThrottle(() => {
|
|
22
|
+
* console.log('Scroll event');
|
|
23
|
+
* }, 200, { edges: ['leading', 'trailing'] });
|
|
24
|
+
*
|
|
25
|
+
* useEffect(() => {
|
|
26
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
27
|
+
* return () => {
|
|
28
|
+
* window.removeEventListener('scroll', throttledScroll);
|
|
29
|
+
* throttledScroll.cancel();
|
|
30
|
+
* };
|
|
31
|
+
* }, [throttledScroll]);
|
|
32
|
+
*/
|
|
33
|
+
function useThrottle(callback, wait, options) {
|
|
34
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
35
|
+
const preservedOptions = usePreservedReference(options ?? {});
|
|
36
|
+
const throttledCallback = useMemo(() => throttle(preservedCallback, wait, preservedOptions), [
|
|
37
|
+
preservedOptions,
|
|
38
|
+
preservedCallback,
|
|
39
|
+
wait
|
|
40
|
+
]);
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
return () => {
|
|
43
|
+
throttledCallback.cancel();
|
|
44
|
+
};
|
|
45
|
+
}, [throttledCallback]);
|
|
46
|
+
return throttledCallback;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
export { useThrottle };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
const require_usePreservedReference = require("../usePreservedReference/usePreservedReference.cjs");
|
|
4
|
+
const require_throttle = require("../useThrottle/throttle.cjs");
|
|
5
|
+
let react = require("react");
|
|
6
|
+
//#region src/hooks/useThrottledCallback/useThrottledCallback.ts
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
|
|
10
|
+
* The throttled callback will only be invoked at most once per specified interval.
|
|
11
|
+
*
|
|
12
|
+
* @param {Object} options - The options object.
|
|
13
|
+
* @param {Function} options.onChange - The callback function to throttle.
|
|
14
|
+
* @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
|
|
15
|
+
* @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.
|
|
16
|
+
*
|
|
17
|
+
* @returns {Function} A throttled function that limits invoking the callback.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* function ScrollTracker() {
|
|
21
|
+
* const throttledScroll = useThrottledCallback({
|
|
22
|
+
* onChange: (scrollY: number) => console.log(scrollY),
|
|
23
|
+
* timeThreshold: 200,
|
|
24
|
+
* });
|
|
25
|
+
* return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
function useThrottledCallback({ onChange, timeThreshold, edges = ["leading", "trailing"] }) {
|
|
29
|
+
const handleChange = require_usePreservedCallback.usePreservedCallback(onChange);
|
|
30
|
+
const ref = (0, react.useRef)({
|
|
31
|
+
value: false,
|
|
32
|
+
clearPreviousThrottle: () => {}
|
|
33
|
+
});
|
|
34
|
+
(0, react.useEffect)(function cleanupThrottleOnUnmount() {
|
|
35
|
+
const current = ref.current;
|
|
36
|
+
return () => {
|
|
37
|
+
current.clearPreviousThrottle();
|
|
38
|
+
};
|
|
39
|
+
}, []);
|
|
40
|
+
const preservedEdges = require_usePreservedReference.usePreservedReference(edges);
|
|
41
|
+
return (0, react.useCallback)((nextValue) => {
|
|
42
|
+
if (nextValue === ref.current.value) return;
|
|
43
|
+
const throttled = require_throttle.throttle(() => {
|
|
44
|
+
handleChange(nextValue);
|
|
45
|
+
ref.current.value = nextValue;
|
|
46
|
+
}, timeThreshold, { edges: preservedEdges });
|
|
47
|
+
ref.current.clearPreviousThrottle();
|
|
48
|
+
throttled();
|
|
49
|
+
ref.current.clearPreviousThrottle = throttled.cancel;
|
|
50
|
+
}, [
|
|
51
|
+
handleChange,
|
|
52
|
+
timeThreshold,
|
|
53
|
+
preservedEdges
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
exports.useThrottledCallback = useThrottledCallback;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useThrottledCallback/useThrottledCallback.d.ts
|
|
1
3
|
type ThrottleOptions = {
|
|
2
|
-
|
|
4
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
3
5
|
};
|
|
4
6
|
/**
|
|
5
7
|
* @description
|
|
@@ -22,9 +24,9 @@ type ThrottleOptions = {
|
|
|
22
24
|
* return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
|
|
23
25
|
* }
|
|
24
26
|
*/
|
|
25
|
-
declare function useThrottledCallback({ onChange, timeThreshold, edges
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
declare function useThrottledCallback({ onChange, timeThreshold, edges }: ThrottleOptions & {
|
|
28
|
+
onChange: (newValue: boolean) => void;
|
|
29
|
+
timeThreshold: number;
|
|
28
30
|
}): (nextValue: boolean) => void;
|
|
29
|
-
|
|
30
|
-
export { useThrottledCallback };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useThrottledCallback };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useThrottledCallback/useThrottledCallback.d.ts
|
|
1
3
|
type ThrottleOptions = {
|
|
2
|
-
|
|
4
|
+
edges?: Array<'leading' | 'trailing'>;
|
|
3
5
|
};
|
|
4
6
|
/**
|
|
5
7
|
* @description
|
|
@@ -22,9 +24,9 @@ type ThrottleOptions = {
|
|
|
22
24
|
* return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
|
|
23
25
|
* }
|
|
24
26
|
*/
|
|
25
|
-
declare function useThrottledCallback({ onChange, timeThreshold, edges
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
declare function useThrottledCallback({ onChange, timeThreshold, edges }: ThrottleOptions & {
|
|
28
|
+
onChange: (newValue: boolean) => void;
|
|
29
|
+
timeThreshold: number;
|
|
28
30
|
}): (nextValue: boolean) => void;
|
|
29
|
-
|
|
30
|
-
export { useThrottledCallback };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useThrottledCallback };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { usePreservedReference } from "../usePreservedReference/usePreservedReference.mjs";
|
|
4
|
+
import { throttle } from "../useThrottle/throttle.mjs";
|
|
5
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
6
|
+
//#region src/hooks/useThrottledCallback/useThrottledCallback.ts
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
|
|
10
|
+
* The throttled callback will only be invoked at most once per specified interval.
|
|
11
|
+
*
|
|
12
|
+
* @param {Object} options - The options object.
|
|
13
|
+
* @param {Function} options.onChange - The callback function to throttle.
|
|
14
|
+
* @param {number} options.timeThreshold - The number of milliseconds to throttle invocations to.
|
|
15
|
+
* @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.
|
|
16
|
+
*
|
|
17
|
+
* @returns {Function} A throttled function that limits invoking the callback.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* function ScrollTracker() {
|
|
21
|
+
* const throttledScroll = useThrottledCallback({
|
|
22
|
+
* onChange: (scrollY: number) => console.log(scrollY),
|
|
23
|
+
* timeThreshold: 200,
|
|
24
|
+
* });
|
|
25
|
+
* return <div onScroll={(e) => throttledScroll(e.currentTarget.scrollTop)} />;
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
function useThrottledCallback({ onChange, timeThreshold, edges = ["leading", "trailing"] }) {
|
|
29
|
+
const handleChange = usePreservedCallback(onChange);
|
|
30
|
+
const ref = useRef({
|
|
31
|
+
value: false,
|
|
32
|
+
clearPreviousThrottle: () => {}
|
|
33
|
+
});
|
|
34
|
+
useEffect(function cleanupThrottleOnUnmount() {
|
|
35
|
+
const current = ref.current;
|
|
36
|
+
return () => {
|
|
37
|
+
current.clearPreviousThrottle();
|
|
38
|
+
};
|
|
39
|
+
}, []);
|
|
40
|
+
const preservedEdges = usePreservedReference(edges);
|
|
41
|
+
return useCallback((nextValue) => {
|
|
42
|
+
if (nextValue === ref.current.value) return;
|
|
43
|
+
const throttled = throttle(() => {
|
|
44
|
+
handleChange(nextValue);
|
|
45
|
+
ref.current.value = nextValue;
|
|
46
|
+
}, timeThreshold, { edges: preservedEdges });
|
|
47
|
+
ref.current.clearPreviousThrottle();
|
|
48
|
+
throttled();
|
|
49
|
+
ref.current.clearPreviousThrottle = throttled.cancel;
|
|
50
|
+
}, [
|
|
51
|
+
handleChange,
|
|
52
|
+
timeThreshold,
|
|
53
|
+
preservedEdges
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
export { useThrottledCallback };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/hooks/useTimeout/useTimeout.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useTimeout` is a React hook that executes a callback function after a specified delay.
|
|
8
|
+
* It manages `setTimeout` in accordance with the React lifecycle, ensuring cleanup on unmount or when dependencies change.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} callback - The function to be executed after the delay.
|
|
11
|
+
* @param {number} [delay=0] - The time in milliseconds to wait before executing the callback.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Updating a title after a delay
|
|
15
|
+
* import { useTimeout } from 'react-simplikit';
|
|
16
|
+
* import { useState } from 'react';
|
|
17
|
+
*
|
|
18
|
+
* function Example() {
|
|
19
|
+
* const [title, setTitle] = useState('');
|
|
20
|
+
*
|
|
21
|
+
* useTimeout(() => {
|
|
22
|
+
* setTitle('Searching for products...');
|
|
23
|
+
* }, 2000);
|
|
24
|
+
*
|
|
25
|
+
* useTimeout(() => {
|
|
26
|
+
* setTitle('Almost done...');
|
|
27
|
+
* }, 4000);
|
|
28
|
+
*
|
|
29
|
+
* return <div>{title}</div>;
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
function useTimeout(callback, delay = 0) {
|
|
33
|
+
const preservedCallback = require_usePreservedCallback.usePreservedCallback(callback);
|
|
34
|
+
(0, react.useEffect)(() => {
|
|
35
|
+
const timeoutId = setTimeout(preservedCallback, delay);
|
|
36
|
+
return () => clearTimeout(timeoutId);
|
|
37
|
+
}, [delay, preservedCallback]);
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
exports.useTimeout = useTimeout;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useTimeout/useTimeout.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `useTimeout` is a React hook that executes a callback function after a specified delay.
|
|
@@ -26,5 +28,5 @@
|
|
|
26
28
|
* }
|
|
27
29
|
*/
|
|
28
30
|
declare function useTimeout(callback: () => void, delay?: number): void;
|
|
29
|
-
|
|
30
|
-
export { useTimeout };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useTimeout };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useTimeout/useTimeout.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `useTimeout` is a React hook that executes a callback function after a specified delay.
|
|
@@ -26,5 +28,5 @@
|
|
|
26
28
|
* }
|
|
27
29
|
*/
|
|
28
30
|
declare function useTimeout(callback: () => void, delay?: number): void;
|
|
29
|
-
|
|
30
|
-
export { useTimeout };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useTimeout };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
//#region src/hooks/useTimeout/useTimeout.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useTimeout` is a React hook that executes a callback function after a specified delay.
|
|
8
|
+
* It manages `setTimeout` in accordance with the React lifecycle, ensuring cleanup on unmount or when dependencies change.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} callback - The function to be executed after the delay.
|
|
11
|
+
* @param {number} [delay=0] - The time in milliseconds to wait before executing the callback.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Updating a title after a delay
|
|
15
|
+
* import { useTimeout } from 'react-simplikit';
|
|
16
|
+
* import { useState } from 'react';
|
|
17
|
+
*
|
|
18
|
+
* function Example() {
|
|
19
|
+
* const [title, setTitle] = useState('');
|
|
20
|
+
*
|
|
21
|
+
* useTimeout(() => {
|
|
22
|
+
* setTitle('Searching for products...');
|
|
23
|
+
* }, 2000);
|
|
24
|
+
*
|
|
25
|
+
* useTimeout(() => {
|
|
26
|
+
* setTitle('Almost done...');
|
|
27
|
+
* }, 4000);
|
|
28
|
+
*
|
|
29
|
+
* return <div>{title}</div>;
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
function useTimeout(callback, delay = 0) {
|
|
33
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const timeoutId = setTimeout(preservedCallback, delay);
|
|
36
|
+
return () => clearTimeout(timeoutId);
|
|
37
|
+
}, [delay, preservedCallback]);
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { useTimeout };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useToggle/useToggle.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useToggle` is a React hook that simplifies managing a boolean state.
|
|
7
|
+
* It provides a function to toggle the state between `true` and `false`.
|
|
8
|
+
*
|
|
9
|
+
* @param {boolean} [initialValue=false] - The initial state value. Defaults to `false`.
|
|
10
|
+
*
|
|
11
|
+
* @returns {[state: boolean, toggle: () => void]} A tuple:
|
|
12
|
+
* - state `boolean` - The current state value;
|
|
13
|
+
* - toggle `() => void` - A function to toggle the state;
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useToggle } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const [open, toggle] = useToggle(false);
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div>
|
|
23
|
+
* <p>Bottom Sheet state: {open ? 'opened' : 'closed'}</p>
|
|
24
|
+
* <button onClick={toggle}>Toggle</button>
|
|
25
|
+
* </div>
|
|
26
|
+
* );
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
function useToggle(initialValue = false) {
|
|
30
|
+
return (0, react.useReducer)(toggle, initialValue);
|
|
31
|
+
}
|
|
32
|
+
const toggle = (state) => !state;
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.useToggle = useToggle;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
1
|
|
|
2
|
+
//#region src/hooks/useToggle/useToggle.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* @description
|
|
5
5
|
* `useToggle` is a React hook that simplifies managing a boolean state.
|
|
@@ -25,6 +25,6 @@ import * as react from 'react';
|
|
|
25
25
|
* );
|
|
26
26
|
* }
|
|
27
27
|
*/
|
|
28
|
-
declare function useToggle(initialValue?: boolean): [boolean, react.ActionDispatch<[]>];
|
|
29
|
-
|
|
30
|
-
export { useToggle };
|
|
28
|
+
declare function useToggle(initialValue?: boolean): [boolean, import("react").ActionDispatch<[]>];
|
|
29
|
+
//#endregion
|
|
30
|
+
export { useToggle };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
1
|
|
|
2
|
+
//#region src/hooks/useToggle/useToggle.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* @description
|
|
5
5
|
* `useToggle` is a React hook that simplifies managing a boolean state.
|
|
@@ -25,6 +25,6 @@ import * as react from 'react';
|
|
|
25
25
|
* );
|
|
26
26
|
* }
|
|
27
27
|
*/
|
|
28
|
-
declare function useToggle(initialValue?: boolean): [boolean, react.ActionDispatch<[]>];
|
|
29
|
-
|
|
30
|
-
export { useToggle };
|
|
28
|
+
declare function useToggle(initialValue?: boolean): [boolean, import("react").ActionDispatch<[]>];
|
|
29
|
+
//#endregion
|
|
30
|
+
export { useToggle };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useReducer } from "react";
|
|
3
|
+
//#region src/hooks/useToggle/useToggle.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useToggle` is a React hook that simplifies managing a boolean state.
|
|
7
|
+
* It provides a function to toggle the state between `true` and `false`.
|
|
8
|
+
*
|
|
9
|
+
* @param {boolean} [initialValue=false] - The initial state value. Defaults to `false`.
|
|
10
|
+
*
|
|
11
|
+
* @returns {[state: boolean, toggle: () => void]} A tuple:
|
|
12
|
+
* - state `boolean` - The current state value;
|
|
13
|
+
* - toggle `() => void` - A function to toggle the state;
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useToggle } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const [open, toggle] = useToggle(false);
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div>
|
|
23
|
+
* <p>Bottom Sheet state: {open ? 'opened' : 'closed'}</p>
|
|
24
|
+
* <button onClick={toggle}>Toggle</button>
|
|
25
|
+
* </div>
|
|
26
|
+
* );
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
function useToggle(initialValue = false) {
|
|
30
|
+
return useReducer(toggle, initialValue);
|
|
31
|
+
}
|
|
32
|
+
const toggle = (state) => !state;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { useToggle };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useVisibilityEvent/useVisibilityEvent.ts
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (document.visibilityState).
|
|
6
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
7
|
+
* This hook will be removed in a future major version.
|
|
8
|
+
*
|
|
9
|
+
* @description
|
|
10
|
+
* `useVisibilityEvent` is a React hook that listens to changes in the document's visibility state and triggers a callback.
|
|
11
|
+
*
|
|
12
|
+
* @param {(visibilityState: 'visible' | 'hidden') => void} callback - A function to be called
|
|
13
|
+
* when the visibility state changes. It receives the current visibility state ('visible' or 'hidden') as an argument.
|
|
14
|
+
* @param {object} [options] - Optional configuration for the hook.
|
|
15
|
+
* @param {boolean} [options.immediate=false] - If true, the callback is invoked immediately upon mounting
|
|
16
|
+
* with the current visibility state.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import { useVisibilityEvent } from 'react-simplikit';
|
|
20
|
+
*
|
|
21
|
+
* function Component() {
|
|
22
|
+
* useVisibilityEvent(visibilityState => {
|
|
23
|
+
* console.log(`Document is now ${visibilityState}`);
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* return <p>Check the console for visibility changes.</p>;
|
|
27
|
+
* }
|
|
28
|
+
*/
|
|
29
|
+
function useVisibilityEvent(callback, options = {}) {
|
|
30
|
+
const handleVisibilityChange = (0, react.useCallback)(() => {
|
|
31
|
+
callback(document.visibilityState);
|
|
32
|
+
}, [callback]);
|
|
33
|
+
(0, react.useEffect)(() => {
|
|
34
|
+
if (options?.immediate ?? false) handleVisibilityChange();
|
|
35
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
36
|
+
return () => {
|
|
37
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
38
|
+
};
|
|
39
|
+
}, [handleVisibilityChange, options?.immediate]);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.useVisibilityEvent = useVisibilityEvent;
|