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,51 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
//#region src/hooks/useOutsideClickEffect/useOutsideClickEffect.ts
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, document).
|
|
7
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
8
|
+
* This hook will be removed in a future major version.
|
|
9
|
+
*
|
|
10
|
+
* @description
|
|
11
|
+
* `useOutsideClickEffect` is a React hook that triggers a callback when a click event occurs outside the specified container(s).
|
|
12
|
+
* It is useful for closing modals, dropdowns, tooltips, and other UI components when clicking outside.
|
|
13
|
+
*
|
|
14
|
+
* @param {HTMLElement | HTMLElement[] | null} container - A single HTML element, an array of HTML elements, or `null`.
|
|
15
|
+
* If `null`, no event listener is attached.
|
|
16
|
+
* @param {() => void} callback - A function that is executed when clicking outside the specified container(s).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import { useOutsideClickEffect } from 'react-simplikit';
|
|
20
|
+
* import { useState } from 'react';
|
|
21
|
+
*
|
|
22
|
+
* function Example() {
|
|
23
|
+
* const [wrapperEl, setWrapperEl] = useState<HTMLDivElement | null>(null);
|
|
24
|
+
*
|
|
25
|
+
* useOutsideClickEffect(wrapperEl, () => {
|
|
26
|
+
* console.log('Outside clicked!');
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* return <div ref={setWrapperEl}>Content</div>;
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
function useOutsideClickEffect(container, callback) {
|
|
33
|
+
const containers = useRef([]);
|
|
34
|
+
const handleDocumentClick = usePreservedCallback(({ target }) => {
|
|
35
|
+
if (target === null) return;
|
|
36
|
+
if (containers.current.length === 0) return;
|
|
37
|
+
if (containers.current.some((x) => x.contains(target))) return;
|
|
38
|
+
callback();
|
|
39
|
+
});
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
containers.current = [container].flat(1).filter((item) => item != null);
|
|
42
|
+
}, [container]);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
document.addEventListener("click", handleDocumentClick);
|
|
45
|
+
return () => {
|
|
46
|
+
document.removeEventListener("click", handleDocumentClick);
|
|
47
|
+
};
|
|
48
|
+
}, [handleDocumentClick]);
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { useOutsideClickEffect };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `usePreservedCallback` is a React hook that maintains a stable reference to a callback function
|
|
7
|
+
* while ensuring it always has access to the latest state or props. This prevents unnecessary re-renders
|
|
8
|
+
* and simplifies dependency management when passing callbacks to child components or handling event listeners.
|
|
9
|
+
*
|
|
10
|
+
* @param {(...args: any[]) => any} callback - The function to preserve.
|
|
11
|
+
* It always references the latest state or props, even when the component re-renders.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(...args: any[]) => any} A function with the same signature as the input callback.
|
|
14
|
+
* The returned function maintains a stable reference while accessing the latest state or props.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* import { usePreservedCallback } from 'react-simplikit';
|
|
18
|
+
* import { useState } from 'react';
|
|
19
|
+
*
|
|
20
|
+
* function Counter() {
|
|
21
|
+
* const [count, setCount] = useState(0);
|
|
22
|
+
*
|
|
23
|
+
* const handleClick = usePreservedCallback(() => {
|
|
24
|
+
* console.log(`Current count: ${count}`);
|
|
25
|
+
* setCount(prev => prev + 1);
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* return <button onClick={handleClick}>Click me</button>;
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
function usePreservedCallback(callback) {
|
|
32
|
+
const callbackRef = (0, react.useRef)(callback);
|
|
33
|
+
(0, react.useEffect)(function syncCallbackRef() {
|
|
34
|
+
callbackRef.current = callback;
|
|
35
|
+
}, [callback]);
|
|
36
|
+
return (0, react.useCallback)((...args) => {
|
|
37
|
+
return callbackRef.current(...args);
|
|
38
|
+
}, []);
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
exports.usePreservedCallback = usePreservedCallback;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/usePreservedCallback/usePreservedCallback.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `usePreservedCallback` is a React hook that maintains a stable reference to a callback function
|
|
@@ -26,5 +28,5 @@
|
|
|
26
28
|
* }
|
|
27
29
|
*/
|
|
28
30
|
declare function usePreservedCallback<Arguments extends any[] = any[], ReturnValue = unknown>(callback: (...args: Arguments) => ReturnValue): (...args: Arguments) => ReturnValue;
|
|
29
|
-
|
|
30
|
-
export { usePreservedCallback };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { usePreservedCallback };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/usePreservedCallback/usePreservedCallback.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `usePreservedCallback` is a React hook that maintains a stable reference to a callback function
|
|
@@ -26,5 +28,5 @@
|
|
|
26
28
|
* }
|
|
27
29
|
*/
|
|
28
30
|
declare function usePreservedCallback<Arguments extends any[] = any[], ReturnValue = unknown>(callback: (...args: Arguments) => ReturnValue): (...args: Arguments) => ReturnValue;
|
|
29
|
-
|
|
30
|
-
export { usePreservedCallback };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { usePreservedCallback };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
3
|
+
//#region src/hooks/usePreservedCallback/usePreservedCallback.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `usePreservedCallback` is a React hook that maintains a stable reference to a callback function
|
|
7
|
+
* while ensuring it always has access to the latest state or props. This prevents unnecessary re-renders
|
|
8
|
+
* and simplifies dependency management when passing callbacks to child components or handling event listeners.
|
|
9
|
+
*
|
|
10
|
+
* @param {(...args: any[]) => any} callback - The function to preserve.
|
|
11
|
+
* It always references the latest state or props, even when the component re-renders.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(...args: any[]) => any} A function with the same signature as the input callback.
|
|
14
|
+
* The returned function maintains a stable reference while accessing the latest state or props.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* import { usePreservedCallback } from 'react-simplikit';
|
|
18
|
+
* import { useState } from 'react';
|
|
19
|
+
*
|
|
20
|
+
* function Counter() {
|
|
21
|
+
* const [count, setCount] = useState(0);
|
|
22
|
+
*
|
|
23
|
+
* const handleClick = usePreservedCallback(() => {
|
|
24
|
+
* console.log(`Current count: ${count}`);
|
|
25
|
+
* setCount(prev => prev + 1);
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* return <button onClick={handleClick}>Click me</button>;
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
function usePreservedCallback(callback) {
|
|
32
|
+
const callbackRef = useRef(callback);
|
|
33
|
+
useEffect(function syncCallbackRef() {
|
|
34
|
+
callbackRef.current = callback;
|
|
35
|
+
}, [callback]);
|
|
36
|
+
return useCallback((...args) => {
|
|
37
|
+
return callbackRef.current(...args);
|
|
38
|
+
}, []);
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { usePreservedCallback };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/usePreservedReference/usePreservedReference.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `usePreservedReference` is a React hook that helps maintain the reference of a value
|
|
7
|
+
* when it hasn't changed, while ensuring you can safely use the latest state.
|
|
8
|
+
* It prevents unnecessary re-renders while always allowing access to the latest data.
|
|
9
|
+
*
|
|
10
|
+
* @template {NotNullishValue} T - The type of target to be referenced.
|
|
11
|
+
*
|
|
12
|
+
* @param {T} value - The value to maintain the reference for. It returns a new reference
|
|
13
|
+
* if the state value changes after comparison.
|
|
14
|
+
* @param {(a: T, b: T) => boolean} [areValuesEqual] - An optional function to determine
|
|
15
|
+
* if two values are equal. By default, it uses `JSON.stringify` for comparison.
|
|
16
|
+
*
|
|
17
|
+
* @returns {T} Returns the same reference if the value is considered equal to the previous one,
|
|
18
|
+
* otherwise returns a new reference.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { usePreservedReference } from 'react-simplikit';
|
|
22
|
+
* import { useState } from 'react';
|
|
23
|
+
*
|
|
24
|
+
* function ExampleComponent() {
|
|
25
|
+
* const [state, setState] = useState({ key: 'value' });
|
|
26
|
+
*
|
|
27
|
+
* const preservedState = usePreservedReference(state);
|
|
28
|
+
*
|
|
29
|
+
* return <div>{preservedState.key}</div>;
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* import { usePreservedReference } from 'react-simplikit';
|
|
34
|
+
* import { useState } from 'react';
|
|
35
|
+
*
|
|
36
|
+
* function ExampleComponent() {
|
|
37
|
+
* const [state, setState] = useState({ key: 'value' });
|
|
38
|
+
*
|
|
39
|
+
* const preservedState = usePreservedReference(state, (a, b) => a.key === b.key);
|
|
40
|
+
*
|
|
41
|
+
* return <div>{preservedState.key}</div>;
|
|
42
|
+
* }
|
|
43
|
+
*/
|
|
44
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
45
|
+
const ref = (0, react.useRef)(value);
|
|
46
|
+
return (0, react.useMemo)(() => {
|
|
47
|
+
if (!areValuesEqual(ref.current, value)) ref.current = value;
|
|
48
|
+
return ref.current;
|
|
49
|
+
}, [areValuesEqual, value]);
|
|
50
|
+
}
|
|
51
|
+
function areDeeplyEqual(x, y) {
|
|
52
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
exports.usePreservedReference = usePreservedReference;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/usePreservedReference/usePreservedReference.d.ts
|
|
1
3
|
type NotNullishValue = {};
|
|
2
4
|
/**
|
|
3
5
|
* @description
|
|
@@ -40,5 +42,5 @@ type NotNullishValue = {};
|
|
|
40
42
|
* }
|
|
41
43
|
*/
|
|
42
44
|
declare function usePreservedReference<T extends NotNullishValue>(value: T, areValuesEqual?: (a: T, b: T) => boolean): T;
|
|
43
|
-
|
|
44
|
-
export { usePreservedReference };
|
|
45
|
+
//#endregion
|
|
46
|
+
export { usePreservedReference };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/usePreservedReference/usePreservedReference.d.ts
|
|
1
3
|
type NotNullishValue = {};
|
|
2
4
|
/**
|
|
3
5
|
* @description
|
|
@@ -40,5 +42,5 @@ type NotNullishValue = {};
|
|
|
40
42
|
* }
|
|
41
43
|
*/
|
|
42
44
|
declare function usePreservedReference<T extends NotNullishValue>(value: T, areValuesEqual?: (a: T, b: T) => boolean): T;
|
|
43
|
-
|
|
44
|
-
export { usePreservedReference };
|
|
45
|
+
//#endregion
|
|
46
|
+
export { usePreservedReference };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useMemo, useRef } from "react";
|
|
3
|
+
//#region src/hooks/usePreservedReference/usePreservedReference.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `usePreservedReference` is a React hook that helps maintain the reference of a value
|
|
7
|
+
* when it hasn't changed, while ensuring you can safely use the latest state.
|
|
8
|
+
* It prevents unnecessary re-renders while always allowing access to the latest data.
|
|
9
|
+
*
|
|
10
|
+
* @template {NotNullishValue} T - The type of target to be referenced.
|
|
11
|
+
*
|
|
12
|
+
* @param {T} value - The value to maintain the reference for. It returns a new reference
|
|
13
|
+
* if the state value changes after comparison.
|
|
14
|
+
* @param {(a: T, b: T) => boolean} [areValuesEqual] - An optional function to determine
|
|
15
|
+
* if two values are equal. By default, it uses `JSON.stringify` for comparison.
|
|
16
|
+
*
|
|
17
|
+
* @returns {T} Returns the same reference if the value is considered equal to the previous one,
|
|
18
|
+
* otherwise returns a new reference.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { usePreservedReference } from 'react-simplikit';
|
|
22
|
+
* import { useState } from 'react';
|
|
23
|
+
*
|
|
24
|
+
* function ExampleComponent() {
|
|
25
|
+
* const [state, setState] = useState({ key: 'value' });
|
|
26
|
+
*
|
|
27
|
+
* const preservedState = usePreservedReference(state);
|
|
28
|
+
*
|
|
29
|
+
* return <div>{preservedState.key}</div>;
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* import { usePreservedReference } from 'react-simplikit';
|
|
34
|
+
* import { useState } from 'react';
|
|
35
|
+
*
|
|
36
|
+
* function ExampleComponent() {
|
|
37
|
+
* const [state, setState] = useState({ key: 'value' });
|
|
38
|
+
*
|
|
39
|
+
* const preservedState = usePreservedReference(state, (a, b) => a.key === b.key);
|
|
40
|
+
*
|
|
41
|
+
* return <div>{preservedState.key}</div>;
|
|
42
|
+
* }
|
|
43
|
+
*/
|
|
44
|
+
function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
|
|
45
|
+
const ref = useRef(value);
|
|
46
|
+
return useMemo(() => {
|
|
47
|
+
if (!areValuesEqual(ref.current, value)) ref.current = value;
|
|
48
|
+
return ref.current;
|
|
49
|
+
}, [areValuesEqual, value]);
|
|
50
|
+
}
|
|
51
|
+
function areDeeplyEqual(x, y) {
|
|
52
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { usePreservedReference };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/usePrevious/usePrevious.ts
|
|
4
|
+
const strictEquals = (prev, next) => prev === next;
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `usePrevious` is a React hook that returns the previous value of the input state.
|
|
8
|
+
* It preserves the previous value unchanged when re-renders occur without state changes.
|
|
9
|
+
* If the state is an object or requires custom change detection, a `compare` function can be provided.
|
|
10
|
+
* By default, state changes are detected using `prev === next`.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of the state.
|
|
13
|
+
* @param {T} state - The state whose previous value is to be tracked.
|
|
14
|
+
* @param {(prev: T, next: T) => boolean} [compare] - An optional comparison function to determine if the state has changed.
|
|
15
|
+
*
|
|
16
|
+
* @returns {T} The previous value of the state.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const [count, setCount] = useState(0);
|
|
20
|
+
* // initial value of previousCount is `0`
|
|
21
|
+
* const previousCount = usePrevious(count);
|
|
22
|
+
*/
|
|
23
|
+
function usePrevious(state, compare = strictEquals) {
|
|
24
|
+
const prevRef = (0, react.useRef)(state);
|
|
25
|
+
const currentRef = (0, react.useRef)(state);
|
|
26
|
+
const isFirstRender = (0, react.useRef)(true);
|
|
27
|
+
if (isFirstRender.current) {
|
|
28
|
+
isFirstRender.current = false;
|
|
29
|
+
return prevRef.current;
|
|
30
|
+
}
|
|
31
|
+
if (!compare(currentRef.current, state)) {
|
|
32
|
+
prevRef.current = currentRef.current;
|
|
33
|
+
currentRef.current = state;
|
|
34
|
+
}
|
|
35
|
+
return prevRef.current;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
exports.usePrevious = usePrevious;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/usePrevious/usePrevious.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `usePrevious` is a React hook that returns the previous value of the input state.
|
|
@@ -17,5 +19,5 @@
|
|
|
17
19
|
* const previousCount = usePrevious(count);
|
|
18
20
|
*/
|
|
19
21
|
declare function usePrevious<T>(state: T, compare?: (prev: T, next: T) => boolean): T;
|
|
20
|
-
|
|
21
|
-
export { usePrevious };
|
|
22
|
+
//#endregion
|
|
23
|
+
export { usePrevious };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/usePrevious/usePrevious.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `usePrevious` is a React hook that returns the previous value of the input state.
|
|
@@ -17,5 +19,5 @@
|
|
|
17
19
|
* const previousCount = usePrevious(count);
|
|
18
20
|
*/
|
|
19
21
|
declare function usePrevious<T>(state: T, compare?: (prev: T, next: T) => boolean): T;
|
|
20
|
-
|
|
21
|
-
export { usePrevious };
|
|
22
|
+
//#endregion
|
|
23
|
+
export { usePrevious };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
//#region src/hooks/usePrevious/usePrevious.ts
|
|
4
|
+
const strictEquals = (prev, next) => prev === next;
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `usePrevious` is a React hook that returns the previous value of the input state.
|
|
8
|
+
* It preserves the previous value unchanged when re-renders occur without state changes.
|
|
9
|
+
* If the state is an object or requires custom change detection, a `compare` function can be provided.
|
|
10
|
+
* By default, state changes are detected using `prev === next`.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of the state.
|
|
13
|
+
* @param {T} state - The state whose previous value is to be tracked.
|
|
14
|
+
* @param {(prev: T, next: T) => boolean} [compare] - An optional comparison function to determine if the state has changed.
|
|
15
|
+
*
|
|
16
|
+
* @returns {T} The previous value of the state.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const [count, setCount] = useState(0);
|
|
20
|
+
* // initial value of previousCount is `0`
|
|
21
|
+
* const previousCount = usePrevious(count);
|
|
22
|
+
*/
|
|
23
|
+
function usePrevious(state, compare = strictEquals) {
|
|
24
|
+
const prevRef = useRef(state);
|
|
25
|
+
const currentRef = useRef(state);
|
|
26
|
+
const isFirstRender = useRef(true);
|
|
27
|
+
if (isFirstRender.current) {
|
|
28
|
+
isFirstRender.current = false;
|
|
29
|
+
return prevRef.current;
|
|
30
|
+
}
|
|
31
|
+
if (!compare(currentRef.current, state)) {
|
|
32
|
+
prevRef.current = currentRef.current;
|
|
33
|
+
currentRef.current = state;
|
|
34
|
+
}
|
|
35
|
+
return prevRef.current;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { usePrevious };
|
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
import { DependencyList } from 'react';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* @description
|
|
6
|
-
* `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
|
|
7
|
-
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
|
|
8
|
-
*
|
|
9
|
-
* @param {(element: RefElement) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
|
|
10
|
-
* @param {DependencyList} deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
|
|
11
|
-
*
|
|
12
|
-
* @returns {(element: RefElement | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* import { useRefEffect } from 'react-simplikit';
|
|
16
|
-
*
|
|
17
|
-
* function Component() {
|
|
18
|
-
* const ref = useRefEffect<HTMLDivElement>(element => {
|
|
19
|
-
* console.log('Element mounted:', element);
|
|
20
|
-
*
|
|
21
|
-
* return () => {
|
|
22
|
-
* console.log('Element unmounted:', element);
|
|
23
|
-
* };
|
|
24
|
-
* }, []);
|
|
25
|
-
*
|
|
26
|
-
* return <div ref={ref}>Basic Example</div>;
|
|
27
|
-
* }
|
|
28
|
-
*/
|
|
29
|
-
declare function useRefEffect<RefElement extends HTMLElement = HTMLElement>(callback: (element: RefElement) => CleanupCallback | void, deps: DependencyList): (element: RefElement | null) => void;
|
|
30
|
-
|
|
31
|
-
export { useRefEffect };
|
|
2
|
+
import { useRefEffect } from "./useRefEffect.cjs";
|
|
3
|
+
export { useRefEffect };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/hooks/useRefEffect/useRefEffect.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
|
|
8
|
+
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
|
|
9
|
+
*
|
|
10
|
+
* @param {(element: RefElement) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
|
|
11
|
+
* @param {DependencyList} deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(element: RefElement | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useRefEffect } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const ref = useRefEffect<HTMLDivElement>(element => {
|
|
20
|
+
* console.log('Element mounted:', element);
|
|
21
|
+
*
|
|
22
|
+
* return () => {
|
|
23
|
+
* console.log('Element unmounted:', element);
|
|
24
|
+
* };
|
|
25
|
+
* }, []);
|
|
26
|
+
*
|
|
27
|
+
* return <div ref={ref}>Basic Example</div>;
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
function useRefEffect(callback, deps) {
|
|
31
|
+
const preservedCallback = require_usePreservedCallback.usePreservedCallback(callback);
|
|
32
|
+
const cleanupCallbackRef = (0, react.useRef)(() => {});
|
|
33
|
+
return (0, react.useCallback)((element) => {
|
|
34
|
+
cleanupCallbackRef.current();
|
|
35
|
+
cleanupCallbackRef.current = () => {};
|
|
36
|
+
if (element == null) return;
|
|
37
|
+
const cleanup = preservedCallback(element);
|
|
38
|
+
if (typeof cleanup === "function") cleanupCallbackRef.current = cleanup;
|
|
39
|
+
}, [preservedCallback, ...deps]);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.useRefEffect = useRefEffect;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DependencyList } from 'react';
|
|
2
1
|
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
|
+
//#region src/hooks/useRefEffect/useRefEffect.d.ts
|
|
3
4
|
type CleanupCallback = () => void;
|
|
4
5
|
/**
|
|
5
6
|
* @description
|
|
@@ -27,5 +28,5 @@ type CleanupCallback = () => void;
|
|
|
27
28
|
* }
|
|
28
29
|
*/
|
|
29
30
|
declare function useRefEffect<RefElement extends HTMLElement = HTMLElement>(callback: (element: RefElement) => CleanupCallback | void, deps: DependencyList): (element: RefElement | null) => void;
|
|
30
|
-
|
|
31
|
-
export { useRefEffect };
|
|
31
|
+
//#endregion
|
|
32
|
+
export { CleanupCallback, useRefEffect };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
|
+
//#region src/hooks/useRefEffect/useRefEffect.d.ts
|
|
4
|
+
type CleanupCallback = () => void;
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
|
|
8
|
+
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
|
|
9
|
+
*
|
|
10
|
+
* @param {(element: RefElement) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
|
|
11
|
+
* @param {DependencyList} deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(element: RefElement | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useRefEffect } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const ref = useRefEffect<HTMLDivElement>(element => {
|
|
20
|
+
* console.log('Element mounted:', element);
|
|
21
|
+
*
|
|
22
|
+
* return () => {
|
|
23
|
+
* console.log('Element unmounted:', element);
|
|
24
|
+
* };
|
|
25
|
+
* }, []);
|
|
26
|
+
*
|
|
27
|
+
* return <div ref={ref}>Basic Example</div>;
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
declare function useRefEffect<RefElement extends HTMLElement = HTMLElement>(callback: (element: RefElement) => CleanupCallback | void, deps: DependencyList): (element: RefElement | null) => void;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { CleanupCallback, useRefEffect };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useCallback, useRef } from "react";
|
|
4
|
+
//#region src/hooks/useRefEffect/useRefEffect.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
|
|
8
|
+
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
|
|
9
|
+
*
|
|
10
|
+
* @param {(element: RefElement) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
|
|
11
|
+
* @param {DependencyList} deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(element: RefElement | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useRefEffect } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const ref = useRefEffect<HTMLDivElement>(element => {
|
|
20
|
+
* console.log('Element mounted:', element);
|
|
21
|
+
*
|
|
22
|
+
* return () => {
|
|
23
|
+
* console.log('Element unmounted:', element);
|
|
24
|
+
* };
|
|
25
|
+
* }, []);
|
|
26
|
+
*
|
|
27
|
+
* return <div ref={ref}>Basic Example</div>;
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
function useRefEffect(callback, deps) {
|
|
31
|
+
const preservedCallback = usePreservedCallback(callback);
|
|
32
|
+
const cleanupCallbackRef = useRef(() => {});
|
|
33
|
+
return useCallback((element) => {
|
|
34
|
+
cleanupCallbackRef.current();
|
|
35
|
+
cleanupCallbackRef.current = () => {};
|
|
36
|
+
if (element == null) return;
|
|
37
|
+
const cleanup = preservedCallback(element);
|
|
38
|
+
if (typeof cleanup === "function") cleanupCallbackRef.current = cleanup;
|
|
39
|
+
}, [preservedCallback, ...deps]);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { useRefEffect };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
const require_usePreservedReference = require("../usePreservedReference/usePreservedReference.cjs");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
//#region src/hooks/useSet/useSet.ts
|
|
6
|
+
/**
|
|
7
|
+
* @description
|
|
8
|
+
* A React hook that manages a Set as state.
|
|
9
|
+
* Provides efficient state management and stable action functions.
|
|
10
|
+
*
|
|
11
|
+
* @param {SetOrValues<T>} initialState - Initial Set state (Set object or array of values)
|
|
12
|
+
* @returns {UseSetReturn<T>} A tuple containing the Set state and actions to manipulate it
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* import { useSet } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function TagSelector() {
|
|
19
|
+
* const [selectedTags, { add, remove, toggle }] = useSet<string>(['react']);
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div>
|
|
23
|
+
* {['react', 'vue', 'svelte'].map(tag => (
|
|
24
|
+
* <button key={tag} onClick={() => toggle(tag)}>
|
|
25
|
+
* {selectedTags.has(tag) ? '✓' : ''} {tag}
|
|
26
|
+
* </button>
|
|
27
|
+
* ))}
|
|
28
|
+
* </div>
|
|
29
|
+
* );
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function useSet(initialState = /* @__PURE__ */ new Set()) {
|
|
34
|
+
const [set, setSet] = (0, react.useState)(() => new Set(initialState));
|
|
35
|
+
const preservedInitialState = require_usePreservedReference.usePreservedReference(initialState);
|
|
36
|
+
const add = require_usePreservedCallback.usePreservedCallback((value) => {
|
|
37
|
+
setSet((prev) => {
|
|
38
|
+
const nextSet = new Set(prev);
|
|
39
|
+
nextSet.add(value);
|
|
40
|
+
return nextSet;
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
const remove = require_usePreservedCallback.usePreservedCallback((value) => {
|
|
44
|
+
setSet((prev) => {
|
|
45
|
+
const nextSet = new Set(prev);
|
|
46
|
+
nextSet.delete(value);
|
|
47
|
+
return nextSet;
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
const toggle = require_usePreservedCallback.usePreservedCallback((value) => {
|
|
51
|
+
setSet((prev) => {
|
|
52
|
+
const nextSet = new Set(prev);
|
|
53
|
+
if (nextSet.has(value)) nextSet.delete(value);
|
|
54
|
+
else nextSet.add(value);
|
|
55
|
+
return nextSet;
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
const setAll = require_usePreservedCallback.usePreservedCallback((values) => {
|
|
59
|
+
setSet(() => new Set(values));
|
|
60
|
+
});
|
|
61
|
+
const reset = require_usePreservedCallback.usePreservedCallback(() => {
|
|
62
|
+
setSet(() => new Set(preservedInitialState));
|
|
63
|
+
});
|
|
64
|
+
return [set, (0, react.useMemo)(() => ({
|
|
65
|
+
add,
|
|
66
|
+
remove,
|
|
67
|
+
toggle,
|
|
68
|
+
setAll,
|
|
69
|
+
reset
|
|
70
|
+
}), [
|
|
71
|
+
add,
|
|
72
|
+
remove,
|
|
73
|
+
toggle,
|
|
74
|
+
setAll,
|
|
75
|
+
reset
|
|
76
|
+
])];
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
exports.useSet = useSet;
|