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,67 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useDebouncedCallback } from "../useDebouncedCallback/useDebouncedCallback.mjs";
|
|
4
|
+
import { useIntersectionObserver } from "../useIntersectionObserver/useIntersectionObserver.mjs";
|
|
5
|
+
import { useVisibilityEvent } from "../useVisibilityEvent/useVisibilityEvent.mjs";
|
|
6
|
+
import { useRef } from "react";
|
|
7
|
+
//#region src/hooks/useImpressionRef/useImpressionRef.ts
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver, Visibility API).
|
|
10
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
11
|
+
* This hook will be removed in a future major version.
|
|
12
|
+
*
|
|
13
|
+
* @description
|
|
14
|
+
* `useImpressionRef` is a React hook that measures the time a specific DOM element is visible on the screen and executes callbacks when the element enters or exits the viewport.
|
|
15
|
+
* It uses `IntersectionObserver` and the `Visibility API` to track the element's visibility.
|
|
16
|
+
*
|
|
17
|
+
* @param {UseImpressionRefOptions} options - Options for tracking the element's visibility.
|
|
18
|
+
* @param {() => void} [options.onImpressionStart] - Callback function executed when the element enters the view
|
|
19
|
+
* @param {() => void} [options.onImpressionEnd] - Callback function executed when the element exits the view
|
|
20
|
+
* @param {number} [options.timeThreshold=0] - Minimum time the element must be visible (in milliseconds)
|
|
21
|
+
* @param {number} [options.areaThreshold=0] - Minimum ratio of the element that must be visible (0 to 1)
|
|
22
|
+
* @param {string} options.rootMargin - Margin to adjust the detection area
|
|
23
|
+
*
|
|
24
|
+
* @returns {(element: Element | null) => void} A function to set the element. Attach this function to the `ref` attribute, and the callbacks will be executed whenever the element's visibility changes.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* import { useImpressionRef } from 'react-simplikit';
|
|
28
|
+
*
|
|
29
|
+
* function Component() {
|
|
30
|
+
* const ref = useImpressionRef<HTMLDivElement>({
|
|
31
|
+
* onImpressionStart: () => console.log('Element entered view'),
|
|
32
|
+
* onImpressionEnd: () => console.log('Element exited view'),
|
|
33
|
+
* timeThreshold: 1000,
|
|
34
|
+
* areaThreshold: 0.5,
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* return <div ref={ref}>Track my visibility!</div>;
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
function useImpressionRef({ onImpressionStart = () => {}, onImpressionEnd = () => {}, rootMargin, areaThreshold = 0, timeThreshold = 0 }) {
|
|
41
|
+
const impressionStartHandler = usePreservedCallback(onImpressionStart);
|
|
42
|
+
const impressionEndHandler = usePreservedCallback(onImpressionEnd);
|
|
43
|
+
const isIntersectingRef = useRef(false);
|
|
44
|
+
const impressionEventHandler = useDebouncedCallback({
|
|
45
|
+
timeThreshold,
|
|
46
|
+
onChange: (impressed) => {
|
|
47
|
+
(impressed ? impressionStartHandler : impressionEndHandler)();
|
|
48
|
+
},
|
|
49
|
+
leading: true
|
|
50
|
+
});
|
|
51
|
+
useVisibilityEvent((documentVisible) => {
|
|
52
|
+
if (!isIntersectingRef.current) return;
|
|
53
|
+
impressionEventHandler(documentVisible === "visible");
|
|
54
|
+
});
|
|
55
|
+
return useIntersectionObserver((entry) => {
|
|
56
|
+
if (document.visibilityState === "hidden") return;
|
|
57
|
+
const currentRatio = entry.intersectionRatio;
|
|
58
|
+
const isIntersecting = areaThreshold === 0 ? entry.isIntersecting : currentRatio >= areaThreshold;
|
|
59
|
+
isIntersectingRef.current = isIntersecting;
|
|
60
|
+
impressionEventHandler(isIntersecting);
|
|
61
|
+
}, {
|
|
62
|
+
rootMargin,
|
|
63
|
+
threshold: areaThreshold
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { useImpressionRef };
|
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
import { ChangeEventHandler } from 'react';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* `useInputState` is a React hook that manages an input state with optional value transformation.
|
|
6
|
-
*
|
|
7
|
-
* @param {string} [initialValue=""] - The initial value of the input. Defaults to an empty string (`""`).
|
|
8
|
-
* @param {(value: string) => string} [transformValue=(v: string) => v] - A function to transform the input value.
|
|
9
|
-
* Defaults to an identity function that returns the input unchanged.
|
|
10
|
-
*
|
|
11
|
-
* @returns {[value: string, onChange: ChangeEventHandler<HTMLInputElement>]} A tuple containing:
|
|
12
|
-
* - value `string` - The current state value;
|
|
13
|
-
* - onChange `ChangeEventHandler<HTMLInputElement>` - A function to update the state;
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* function Example() {
|
|
17
|
-
* const [value, onChange] = useInputState('');
|
|
18
|
-
* return <input type="text" value={value} onChange={onChange} />;
|
|
19
|
-
* }
|
|
20
|
-
*/
|
|
21
|
-
declare function useInputState(initialValue?: string, transformValue?: (value: string) => string): readonly [string, ChangeEventHandler<HTMLInputElement>];
|
|
22
|
-
|
|
23
|
-
export { useInputState };
|
|
2
|
+
import { useInputState } from "./useInputState.cjs";
|
|
3
|
+
export { useInputState };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useInputState/useInputState.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useInputState` is a React hook that manages an input state with optional value transformation.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} [initialValue=""] - The initial value of the input. Defaults to an empty string (`""`).
|
|
9
|
+
* @param {(value: string) => string} [transformValue=(v: string) => v] - A function to transform the input value.
|
|
10
|
+
* Defaults to an identity function that returns the input unchanged.
|
|
11
|
+
*
|
|
12
|
+
* @returns {[value: string, onChange: ChangeEventHandler<HTMLInputElement>]} A tuple containing:
|
|
13
|
+
* - value `string` - The current state value;
|
|
14
|
+
* - onChange `ChangeEventHandler<HTMLInputElement>` - A function to update the state;
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* function Example() {
|
|
18
|
+
* const [value, onChange] = useInputState('');
|
|
19
|
+
* return <input type="text" value={value} onChange={onChange} />;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
function useInputState(initialValue = "", transformValue = echo) {
|
|
23
|
+
const [value, setValue] = (0, react.useState)(initialValue);
|
|
24
|
+
return [value, (0, react.useCallback)(({ target: { value } }) => {
|
|
25
|
+
setValue(transformValue(value));
|
|
26
|
+
}, [transformValue])];
|
|
27
|
+
}
|
|
28
|
+
function echo(v) {
|
|
29
|
+
return v;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
exports.useInputState = useInputState;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ChangeEventHandler } from 'react';
|
|
2
1
|
|
|
2
|
+
import { ChangeEventHandler } from "react";
|
|
3
|
+
//#region src/hooks/useInputState/useInputState.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* @description
|
|
5
6
|
* `useInputState` is a React hook that manages an input state with optional value transformation.
|
|
@@ -19,5 +20,5 @@ import { ChangeEventHandler } from 'react';
|
|
|
19
20
|
* }
|
|
20
21
|
*/
|
|
21
22
|
declare function useInputState(initialValue?: string, transformValue?: (value: string) => string): readonly [string, ChangeEventHandler<HTMLInputElement>];
|
|
22
|
-
|
|
23
|
-
export { useInputState };
|
|
23
|
+
//#endregion
|
|
24
|
+
export { useInputState };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
import { ChangeEventHandler } from "react";
|
|
3
|
+
//#region src/hooks/useInputState/useInputState.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useInputState` is a React hook that manages an input state with optional value transformation.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} [initialValue=""] - The initial value of the input. Defaults to an empty string (`""`).
|
|
9
|
+
* @param {(value: string) => string} [transformValue=(v: string) => v] - A function to transform the input value.
|
|
10
|
+
* Defaults to an identity function that returns the input unchanged.
|
|
11
|
+
*
|
|
12
|
+
* @returns {[value: string, onChange: ChangeEventHandler<HTMLInputElement>]} A tuple containing:
|
|
13
|
+
* - value `string` - The current state value;
|
|
14
|
+
* - onChange `ChangeEventHandler<HTMLInputElement>` - A function to update the state;
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* function Example() {
|
|
18
|
+
* const [value, onChange] = useInputState('');
|
|
19
|
+
* return <input type="text" value={value} onChange={onChange} />;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
declare function useInputState(initialValue?: string, transformValue?: (value: string) => string): readonly [string, ChangeEventHandler<HTMLInputElement>];
|
|
23
|
+
//#endregion
|
|
24
|
+
export { useInputState };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useState } from "react";
|
|
3
|
+
//#region src/hooks/useInputState/useInputState.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useInputState` is a React hook that manages an input state with optional value transformation.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} [initialValue=""] - The initial value of the input. Defaults to an empty string (`""`).
|
|
9
|
+
* @param {(value: string) => string} [transformValue=(v: string) => v] - A function to transform the input value.
|
|
10
|
+
* Defaults to an identity function that returns the input unchanged.
|
|
11
|
+
*
|
|
12
|
+
* @returns {[value: string, onChange: ChangeEventHandler<HTMLInputElement>]} A tuple containing:
|
|
13
|
+
* - value `string` - The current state value;
|
|
14
|
+
* - onChange `ChangeEventHandler<HTMLInputElement>` - A function to update the state;
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* function Example() {
|
|
18
|
+
* const [value, onChange] = useInputState('');
|
|
19
|
+
* return <input type="text" value={value} onChange={onChange} />;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
function useInputState(initialValue = "", transformValue = echo) {
|
|
23
|
+
const [value, setValue] = useState(initialValue);
|
|
24
|
+
return [value, useCallback(({ target: { value } }) => {
|
|
25
|
+
setValue(transformValue(value));
|
|
26
|
+
}, [transformValue])];
|
|
27
|
+
}
|
|
28
|
+
function echo(v) {
|
|
29
|
+
return v;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useInputState };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
const require_useRefEffect = require("../useRefEffect/useRefEffect.cjs");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
//#region src/hooks/useIntersectionObserver/useIntersectionObserver.ts
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver).
|
|
8
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
9
|
+
* This hook will be removed in a future major version.
|
|
10
|
+
*
|
|
11
|
+
* @description
|
|
12
|
+
* `useIntersectionObserver` is a React hook that detects whether a specific DOM element is visible on the screen.
|
|
13
|
+
* It uses the `IntersectionObserver` API to execute a callback when the element enters or exits the viewport.
|
|
14
|
+
*
|
|
15
|
+
* @param {(entry: IntersectionObserverEntry) => void} callback - A callback function that is executed when the visibility of the element changes.
|
|
16
|
+
* You can check `entry.isIntersecting` to determine if the element is in view.
|
|
17
|
+
* @param {IntersectionObserverInit} options - Options for the `IntersectionObserver`.
|
|
18
|
+
* @param {boolean} [options.root] - The element that is used as the viewport for checking visibility of the target.
|
|
19
|
+
* @param {string} [options.rootMargin] - Margin around the root.
|
|
20
|
+
* @param {number | number[]} [options.threshold] - Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed.
|
|
21
|
+
*
|
|
22
|
+
* @returns {(element: Element | null) => void} A function to set the element. Attach this function to the `ref` attribute, and the `callback` will be executed whenever the element's visibility changes.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import { useIntersectionObserver } from 'react-simplikit';
|
|
26
|
+
*
|
|
27
|
+
* function Component() {
|
|
28
|
+
* const ref = useIntersectionObserver<HTMLDivElement>(
|
|
29
|
+
* entry => {
|
|
30
|
+
* if (entry.isIntersecting) {
|
|
31
|
+
* console.log('Element is in view:', entry.target);
|
|
32
|
+
* } else {
|
|
33
|
+
* console.log('Element is out of view:', entry.target);
|
|
34
|
+
* }
|
|
35
|
+
* },
|
|
36
|
+
* { threshold: 0.5 }
|
|
37
|
+
* );
|
|
38
|
+
*
|
|
39
|
+
* return <div ref={ref}>Observe me!</div>;
|
|
40
|
+
* }
|
|
41
|
+
*/
|
|
42
|
+
function useIntersectionObserver(callback, options) {
|
|
43
|
+
const preservedCallback = require_usePreservedCallback.usePreservedCallback(callback);
|
|
44
|
+
const observer = (0, react.useMemo)(() => {
|
|
45
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
46
|
+
return new IntersectionObserver(([entry]) => {
|
|
47
|
+
preservedCallback(entry);
|
|
48
|
+
}, options);
|
|
49
|
+
}, [preservedCallback, options]);
|
|
50
|
+
return require_useRefEffect.useRefEffect((element) => {
|
|
51
|
+
observer?.observe(element);
|
|
52
|
+
return () => {
|
|
53
|
+
observer?.unobserve(element);
|
|
54
|
+
};
|
|
55
|
+
}, [preservedCallback, options]);
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
exports.useIntersectionObserver = useIntersectionObserver;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useIntersectionObserver/useIntersectionObserver.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver).
|
|
3
5
|
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
@@ -35,5 +37,5 @@
|
|
|
35
37
|
* }
|
|
36
38
|
*/
|
|
37
39
|
declare function useIntersectionObserver<Element extends HTMLElement>(callback: (entry: IntersectionObserverEntry) => void, options: IntersectionObserverInit): (element: Element | null) => void;
|
|
38
|
-
|
|
39
|
-
export { useIntersectionObserver };
|
|
40
|
+
//#endregion
|
|
41
|
+
export { useIntersectionObserver };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useIntersectionObserver/useIntersectionObserver.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver).
|
|
3
5
|
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
@@ -35,5 +37,5 @@
|
|
|
35
37
|
* }
|
|
36
38
|
*/
|
|
37
39
|
declare function useIntersectionObserver<Element extends HTMLElement>(callback: (entry: IntersectionObserverEntry) => void, options: IntersectionObserverInit): (element: Element | null) => void;
|
|
38
|
-
|
|
39
|
-
export { useIntersectionObserver };
|
|
40
|
+
//#endregion
|
|
41
|
+
export { useIntersectionObserver };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useRefEffect } from "../useRefEffect/useRefEffect.mjs";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
//#region src/hooks/useIntersectionObserver/useIntersectionObserver.ts
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver).
|
|
8
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
9
|
+
* This hook will be removed in a future major version.
|
|
10
|
+
*
|
|
11
|
+
* @description
|
|
12
|
+
* `useIntersectionObserver` is a React hook that detects whether a specific DOM element is visible on the screen.
|
|
13
|
+
* It uses the `IntersectionObserver` API to execute a callback when the element enters or exits the viewport.
|
|
14
|
+
*
|
|
15
|
+
* @param {(entry: IntersectionObserverEntry) => void} callback - A callback function that is executed when the visibility of the element changes.
|
|
16
|
+
* You can check `entry.isIntersecting` to determine if the element is in view.
|
|
17
|
+
* @param {IntersectionObserverInit} options - Options for the `IntersectionObserver`.
|
|
18
|
+
* @param {boolean} [options.root] - The element that is used as the viewport for checking visibility of the target.
|
|
19
|
+
* @param {string} [options.rootMargin] - Margin around the root.
|
|
20
|
+
* @param {number | number[]} [options.threshold] - Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed.
|
|
21
|
+
*
|
|
22
|
+
* @returns {(element: Element | null) => void} A function to set the element. Attach this function to the `ref` attribute, and the `callback` will be executed whenever the element's visibility changes.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import { useIntersectionObserver } from 'react-simplikit';
|
|
26
|
+
*
|
|
27
|
+
* function Component() {
|
|
28
|
+
* const ref = useIntersectionObserver<HTMLDivElement>(
|
|
29
|
+
* entry => {
|
|
30
|
+
* if (entry.isIntersecting) {
|
|
31
|
+
* console.log('Element is in view:', entry.target);
|
|
32
|
+
* } else {
|
|
33
|
+
* console.log('Element is out of view:', entry.target);
|
|
34
|
+
* }
|
|
35
|
+
* },
|
|
36
|
+
* { threshold: 0.5 }
|
|
37
|
+
* );
|
|
38
|
+
*
|
|
39
|
+
* return <div ref={ref}>Observe me!</div>;
|
|
40
|
+
* }
|
|
41
|
+
*/
|
|
42
|
+
function useIntersectionObserver(callback, options) {
|
|
43
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
44
|
+
const observer = useMemo(() => {
|
|
45
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
46
|
+
return new IntersectionObserver(([entry]) => {
|
|
47
|
+
preservedCallback(entry);
|
|
48
|
+
}, options);
|
|
49
|
+
}, [preservedCallback, options]);
|
|
50
|
+
return useRefEffect((element) => {
|
|
51
|
+
observer?.observe(element);
|
|
52
|
+
return () => {
|
|
53
|
+
observer?.unobserve(element);
|
|
54
|
+
};
|
|
55
|
+
}, [preservedCallback, options]);
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { useIntersectionObserver };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/hooks/useInterval/useInterval.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useInterval` is a React hook that executes a function at a specified interval.
|
|
8
|
+
* It is useful for timers, polling data, and other recurring tasks.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} callback - The function to be executed periodically.
|
|
11
|
+
* @param {number | { delay: number; enabled?: boolean; immediate?: boolean }} options - Configures the interval behavior.
|
|
12
|
+
* @param {number} options.delay - The interval duration in milliseconds. If `null`, the interval will not run.
|
|
13
|
+
* @param {boolean} [options.immediate=false] - If `true`, executes immediately before starting the interval.
|
|
14
|
+
* @param {boolean} [options.enabled=true] - If `false`, the interval will not run.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* import { useInterval } from 'react-simplikit';
|
|
18
|
+
* import { useState } from 'react';
|
|
19
|
+
*
|
|
20
|
+
* function Timer() {
|
|
21
|
+
* const [time, setTime] = useState(0);
|
|
22
|
+
*
|
|
23
|
+
* useInterval(() => {
|
|
24
|
+
* setTime(prev => prev + 1);
|
|
25
|
+
* }, 1000);
|
|
26
|
+
*
|
|
27
|
+
* return (
|
|
28
|
+
* <div>
|
|
29
|
+
* <p>{time} seconds</p>
|
|
30
|
+
* </div>
|
|
31
|
+
* );
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
function useInterval(callback, options) {
|
|
35
|
+
const delay = typeof options === "number" ? options : options.delay;
|
|
36
|
+
const immediate = typeof options === "number" ? false : options.immediate;
|
|
37
|
+
const enabled = typeof options === "number" ? true : options.enabled ?? true;
|
|
38
|
+
const preservedCallback = require_usePreservedCallback.usePreservedCallback(callback);
|
|
39
|
+
const immediateCalledRef = (0, react.useRef)(false);
|
|
40
|
+
(0, react.useEffect)(function runImmediateCallback() {
|
|
41
|
+
if (immediate !== true) {
|
|
42
|
+
immediateCalledRef.current = false;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!enabled) return;
|
|
46
|
+
if (immediateCalledRef.current) return;
|
|
47
|
+
immediateCalledRef.current = true;
|
|
48
|
+
preservedCallback();
|
|
49
|
+
}, [
|
|
50
|
+
immediate,
|
|
51
|
+
preservedCallback,
|
|
52
|
+
enabled
|
|
53
|
+
]);
|
|
54
|
+
(0, react.useEffect)(function startInterval() {
|
|
55
|
+
if (!enabled) return;
|
|
56
|
+
const id = setInterval(preservedCallback, delay);
|
|
57
|
+
return () => clearInterval(id);
|
|
58
|
+
}, [
|
|
59
|
+
delay,
|
|
60
|
+
preservedCallback,
|
|
61
|
+
enabled
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
exports.useInterval = useInterval;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useInterval/useInterval.d.ts
|
|
1
3
|
type IntervalOptions = number | {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
delay: number;
|
|
5
|
+
immediate?: boolean;
|
|
6
|
+
enabled?: boolean;
|
|
5
7
|
};
|
|
6
8
|
/**
|
|
7
9
|
* @description
|
|
@@ -33,5 +35,5 @@ type IntervalOptions = number | {
|
|
|
33
35
|
* }
|
|
34
36
|
*/
|
|
35
37
|
declare function useInterval(callback: () => void, options: IntervalOptions): void;
|
|
36
|
-
|
|
37
|
-
export { useInterval };
|
|
38
|
+
//#endregion
|
|
39
|
+
export { useInterval };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useInterval/useInterval.d.ts
|
|
1
3
|
type IntervalOptions = number | {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
delay: number;
|
|
5
|
+
immediate?: boolean;
|
|
6
|
+
enabled?: boolean;
|
|
5
7
|
};
|
|
6
8
|
/**
|
|
7
9
|
* @description
|
|
@@ -33,5 +35,5 @@ type IntervalOptions = number | {
|
|
|
33
35
|
* }
|
|
34
36
|
*/
|
|
35
37
|
declare function useInterval(callback: () => void, options: IntervalOptions): void;
|
|
36
|
-
|
|
37
|
-
export { useInterval };
|
|
38
|
+
//#endregion
|
|
39
|
+
export { useInterval };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
//#region src/hooks/useInterval/useInterval.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useInterval` is a React hook that executes a function at a specified interval.
|
|
8
|
+
* It is useful for timers, polling data, and other recurring tasks.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} callback - The function to be executed periodically.
|
|
11
|
+
* @param {number | { delay: number; enabled?: boolean; immediate?: boolean }} options - Configures the interval behavior.
|
|
12
|
+
* @param {number} options.delay - The interval duration in milliseconds. If `null`, the interval will not run.
|
|
13
|
+
* @param {boolean} [options.immediate=false] - If `true`, executes immediately before starting the interval.
|
|
14
|
+
* @param {boolean} [options.enabled=true] - If `false`, the interval will not run.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* import { useInterval } from 'react-simplikit';
|
|
18
|
+
* import { useState } from 'react';
|
|
19
|
+
*
|
|
20
|
+
* function Timer() {
|
|
21
|
+
* const [time, setTime] = useState(0);
|
|
22
|
+
*
|
|
23
|
+
* useInterval(() => {
|
|
24
|
+
* setTime(prev => prev + 1);
|
|
25
|
+
* }, 1000);
|
|
26
|
+
*
|
|
27
|
+
* return (
|
|
28
|
+
* <div>
|
|
29
|
+
* <p>{time} seconds</p>
|
|
30
|
+
* </div>
|
|
31
|
+
* );
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
function useInterval(callback, options) {
|
|
35
|
+
const delay = typeof options === "number" ? options : options.delay;
|
|
36
|
+
const immediate = typeof options === "number" ? false : options.immediate;
|
|
37
|
+
const enabled = typeof options === "number" ? true : options.enabled ?? true;
|
|
38
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
39
|
+
const immediateCalledRef = useRef(false);
|
|
40
|
+
useEffect(function runImmediateCallback() {
|
|
41
|
+
if (immediate !== true) {
|
|
42
|
+
immediateCalledRef.current = false;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!enabled) return;
|
|
46
|
+
if (immediateCalledRef.current) return;
|
|
47
|
+
immediateCalledRef.current = true;
|
|
48
|
+
preservedCallback();
|
|
49
|
+
}, [
|
|
50
|
+
immediate,
|
|
51
|
+
preservedCallback,
|
|
52
|
+
enabled
|
|
53
|
+
]);
|
|
54
|
+
useEffect(function startInterval() {
|
|
55
|
+
if (!enabled) return;
|
|
56
|
+
const id = setInterval(preservedCallback, delay);
|
|
57
|
+
return () => clearInterval(id);
|
|
58
|
+
}, [
|
|
59
|
+
delay,
|
|
60
|
+
preservedCallback,
|
|
61
|
+
enabled
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { useInterval };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useIsClient/useIsClient.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useIsClient` is a React hook that returns `true` only in the client-side environment.
|
|
7
|
+
* It is primarily used to differentiate between client-side and server-side rendering (SSR).
|
|
8
|
+
* The state is set to `true` only after the component is mounted in the client-side environment.
|
|
9
|
+
*
|
|
10
|
+
* @returns {boolean} Returns `true` in a client-side environment, and `false` otherwise.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* function ClientSideContent() {
|
|
14
|
+
* const isClient = useIsClient();
|
|
15
|
+
*
|
|
16
|
+
* if (!isClient) {
|
|
17
|
+
* return <div>Loading...</div>; // Rendered on the server side
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* return <div>Client-side rendered content</div>; // Rendered on the client side
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* function ClientOnlyMap() {
|
|
25
|
+
* const isClient = useIsClient();
|
|
26
|
+
*
|
|
27
|
+
* if (!isClient) return null;
|
|
28
|
+
*
|
|
29
|
+
* return <div id="map" />;
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* function ClientTheme() {
|
|
34
|
+
* const isClient = useIsClient();
|
|
35
|
+
*
|
|
36
|
+
* const theme = isClient ? localStorage.getItem('theme') : 'light';
|
|
37
|
+
*
|
|
38
|
+
* return <div>Current theme: {theme}</div>;
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
function useIsClient() {
|
|
42
|
+
const [isClient, setIsClient] = (0, react.useState)(false);
|
|
43
|
+
(0, react.useEffect)(function syncClientState() {
|
|
44
|
+
setIsClient(true);
|
|
45
|
+
}, []);
|
|
46
|
+
return isClient;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.useIsClient = useIsClient;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useIsClient/useIsClient.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `useIsClient` is a React hook that returns `true` only in the client-side environment.
|
|
@@ -36,5 +38,5 @@
|
|
|
36
38
|
* }
|
|
37
39
|
*/
|
|
38
40
|
declare function useIsClient(): boolean;
|
|
39
|
-
|
|
40
|
-
export { useIsClient };
|
|
41
|
+
//#endregion
|
|
42
|
+
export { useIsClient };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useIsClient/useIsClient.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `useIsClient` is a React hook that returns `true` only in the client-side environment.
|
|
@@ -36,5 +38,5 @@
|
|
|
36
38
|
* }
|
|
37
39
|
*/
|
|
38
40
|
declare function useIsClient(): boolean;
|
|
39
|
-
|
|
40
|
-
export { useIsClient };
|
|
41
|
+
//#endregion
|
|
42
|
+
export { useIsClient };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
//#region src/hooks/useIsClient/useIsClient.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useIsClient` is a React hook that returns `true` only in the client-side environment.
|
|
7
|
+
* It is primarily used to differentiate between client-side and server-side rendering (SSR).
|
|
8
|
+
* The state is set to `true` only after the component is mounted in the client-side environment.
|
|
9
|
+
*
|
|
10
|
+
* @returns {boolean} Returns `true` in a client-side environment, and `false` otherwise.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* function ClientSideContent() {
|
|
14
|
+
* const isClient = useIsClient();
|
|
15
|
+
*
|
|
16
|
+
* if (!isClient) {
|
|
17
|
+
* return <div>Loading...</div>; // Rendered on the server side
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* return <div>Client-side rendered content</div>; // Rendered on the client side
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* function ClientOnlyMap() {
|
|
25
|
+
* const isClient = useIsClient();
|
|
26
|
+
*
|
|
27
|
+
* if (!isClient) return null;
|
|
28
|
+
*
|
|
29
|
+
* return <div id="map" />;
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* function ClientTheme() {
|
|
34
|
+
* const isClient = useIsClient();
|
|
35
|
+
*
|
|
36
|
+
* const theme = isClient ? localStorage.getItem('theme') : 'light';
|
|
37
|
+
*
|
|
38
|
+
* return <div>Current theme: {theme}</div>;
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
function useIsClient() {
|
|
42
|
+
const [isClient, setIsClient] = useState(false);
|
|
43
|
+
useEffect(function syncClientState() {
|
|
44
|
+
setIsClient(true);
|
|
45
|
+
}, []);
|
|
46
|
+
return isClient;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
export { useIsClient };
|