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,133 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/hooks/useLongPress/useLongPress.ts
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, window.setTimeout).
|
|
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
|
+
* `useLongPress` is a React hook that detects when an element is pressed and held for a specified duration.
|
|
12
|
+
* It handles both mouse and touch events, making it work consistently across desktop and mobile devices.
|
|
13
|
+
*
|
|
14
|
+
* @template {HTMLElement} E - The HTML element type that will use the long press handlers.
|
|
15
|
+
* @param {(event: React.MouseEvent<E> | React.TouchEvent<E>) => void} onLongPress - The callback function to be executed when a long press is detected.
|
|
16
|
+
* @param {UseLongPressOptions} [options] - Configuration options for the long press behavior.
|
|
17
|
+
* @param {number} [options.delay=500] - The time in milliseconds before triggering the long press. Defaults to 500ms.
|
|
18
|
+
* @param {Object} [options.moveThreshold] - Maximum movement allowed before canceling a long press.
|
|
19
|
+
* @param {number} [options.moveThreshold.x] - Maximum horizontal movement in pixels.
|
|
20
|
+
* @param {number} [options.moveThreshold.y] - Maximum vertical movement in pixels.
|
|
21
|
+
* @param {(event) => void} [options.onClick] - Optional function to execute on a normal click (press and release before delay).
|
|
22
|
+
* @param {(event) => void} [options.onLongPressEnd] - Optional function to execute when a long press ends.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Object} Event handlers to attach to an element.
|
|
25
|
+
* - onMouseDown `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse down events.
|
|
26
|
+
* - onMouseUp `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse up events.
|
|
27
|
+
* - onMouseLeave `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse leave events.
|
|
28
|
+
* - onTouchStart `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch start events.
|
|
29
|
+
* - onTouchEnd `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch end events.
|
|
30
|
+
* - onMouseMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse move events. Included if `moveThreshold` is provided.
|
|
31
|
+
* - onTouchMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch move events. Included if `moveThreshold` is provided.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* import { useLongPress } from 'react-simplikit';
|
|
35
|
+
*
|
|
36
|
+
* function ContextMenu() {
|
|
37
|
+
* const [menuVisible, setMenuVisible] = useState(false);
|
|
38
|
+
*
|
|
39
|
+
* const longPressHandlers = useLongPress(
|
|
40
|
+
* () => setMenuVisible(true),
|
|
41
|
+
* {
|
|
42
|
+
* delay: 400,
|
|
43
|
+
* onClick: () => console.log('Normal click'),
|
|
44
|
+
* onLongPressEnd: () => console.log('Long press completed')
|
|
45
|
+
* }
|
|
46
|
+
* );
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div>
|
|
50
|
+
* <button {...longPressHandlers}>Press and hold</button>
|
|
51
|
+
* {menuVisible && <div className="context-menu">Context Menu</div>}
|
|
52
|
+
* </div>
|
|
53
|
+
* );
|
|
54
|
+
* }
|
|
55
|
+
*/
|
|
56
|
+
function useLongPress(onLongPress, { delay = 500, moveThreshold, onClick, onLongPressEnd } = {}) {
|
|
57
|
+
const timeoutRef = (0, react.useRef)(null);
|
|
58
|
+
const isLongPressActiveRef = (0, react.useRef)(false);
|
|
59
|
+
const initialPositionRef = (0, react.useRef)({
|
|
60
|
+
x: 0,
|
|
61
|
+
y: 0
|
|
62
|
+
});
|
|
63
|
+
const preservedOnLongPress = require_usePreservedCallback.usePreservedCallback(onLongPress);
|
|
64
|
+
const preservedOnClick = require_usePreservedCallback.usePreservedCallback(onClick || (() => {}));
|
|
65
|
+
const preservedOnLongPressEnd = require_usePreservedCallback.usePreservedCallback(onLongPressEnd || (() => {}));
|
|
66
|
+
const hasThreshold = moveThreshold?.x !== void 0 || moveThreshold?.y !== void 0;
|
|
67
|
+
const getClientPosition = (0, react.useCallback)((event) => {
|
|
68
|
+
if ("touches" in event.nativeEvent) {
|
|
69
|
+
const touch = event.nativeEvent.touches[0];
|
|
70
|
+
return {
|
|
71
|
+
x: touch.clientX,
|
|
72
|
+
y: touch.clientY
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
x: event.nativeEvent.clientX,
|
|
77
|
+
y: event.nativeEvent.clientY
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
const isMovedBeyondThreshold = (0, react.useCallback)((event) => {
|
|
81
|
+
const { x, y } = getClientPosition(event);
|
|
82
|
+
const deltaX = Math.abs(x - initialPositionRef.current.x);
|
|
83
|
+
const deltaY = Math.abs(y - initialPositionRef.current.y);
|
|
84
|
+
return moveThreshold?.x !== void 0 && deltaX > moveThreshold.x || moveThreshold?.y !== void 0 && deltaY > moveThreshold.y;
|
|
85
|
+
}, [getClientPosition, moveThreshold]);
|
|
86
|
+
const cancelLongPress = (0, react.useCallback)(() => {
|
|
87
|
+
if (timeoutRef.current !== null) {
|
|
88
|
+
window.clearTimeout(timeoutRef.current);
|
|
89
|
+
timeoutRef.current = null;
|
|
90
|
+
}
|
|
91
|
+
}, []);
|
|
92
|
+
const handlePressStart = (0, react.useCallback)((event) => {
|
|
93
|
+
cancelLongPress();
|
|
94
|
+
const position = getClientPosition(event);
|
|
95
|
+
initialPositionRef.current = position;
|
|
96
|
+
isLongPressActiveRef.current = false;
|
|
97
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
98
|
+
isLongPressActiveRef.current = true;
|
|
99
|
+
preservedOnLongPress(event);
|
|
100
|
+
}, delay);
|
|
101
|
+
}, [
|
|
102
|
+
cancelLongPress,
|
|
103
|
+
delay,
|
|
104
|
+
getClientPosition,
|
|
105
|
+
preservedOnLongPress
|
|
106
|
+
]);
|
|
107
|
+
const handlePressEnd = (0, react.useCallback)((event) => {
|
|
108
|
+
if (isLongPressActiveRef.current) preservedOnLongPressEnd(event);
|
|
109
|
+
else if (timeoutRef.current !== null) preservedOnClick(event);
|
|
110
|
+
cancelLongPress();
|
|
111
|
+
isLongPressActiveRef.current = false;
|
|
112
|
+
}, [
|
|
113
|
+
cancelLongPress,
|
|
114
|
+
preservedOnClick,
|
|
115
|
+
preservedOnLongPressEnd
|
|
116
|
+
]);
|
|
117
|
+
const handlePressMove = (0, react.useCallback)((event) => {
|
|
118
|
+
if (timeoutRef.current !== null && isMovedBeyondThreshold(event)) cancelLongPress();
|
|
119
|
+
}, [cancelLongPress, isMovedBeyondThreshold]);
|
|
120
|
+
return {
|
|
121
|
+
onMouseDown: handlePressStart,
|
|
122
|
+
onMouseUp: handlePressEnd,
|
|
123
|
+
onMouseLeave: cancelLongPress,
|
|
124
|
+
onTouchStart: handlePressStart,
|
|
125
|
+
onTouchEnd: handlePressEnd,
|
|
126
|
+
...hasThreshold ? {
|
|
127
|
+
onTouchMove: handlePressMove,
|
|
128
|
+
onMouseMove: handlePressMove
|
|
129
|
+
} : {}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
exports.useLongPress = useLongPress;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { MouseEvent, TouchEvent } from 'react';
|
|
2
1
|
|
|
2
|
+
import { MouseEvent, TouchEvent } from "react";
|
|
3
|
+
//#region src/hooks/useLongPress/useLongPress.d.ts
|
|
3
4
|
type Handler<E extends HTMLElement> = (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
4
5
|
type UseLongPressOptions<E extends HTMLElement> = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
delay?: number;
|
|
7
|
+
moveThreshold?: {
|
|
8
|
+
x?: number;
|
|
9
|
+
y?: number;
|
|
10
|
+
};
|
|
11
|
+
onClick?: Handler<E>;
|
|
12
|
+
onLongPressEnd?: Handler<E>;
|
|
12
13
|
};
|
|
13
14
|
/**
|
|
14
15
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, window.setTimeout).
|
|
@@ -62,13 +63,13 @@ type UseLongPressOptions<E extends HTMLElement> = {
|
|
|
62
63
|
* }
|
|
63
64
|
*/
|
|
64
65
|
declare function useLongPress<E extends HTMLElement = HTMLElement>(onLongPress: Handler<E>, { delay, moveThreshold, onClick, onLongPressEnd }?: UseLongPressOptions<E>): {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
onTouchMove?: ((event: MouseEvent<E> | TouchEvent<E>) => void) | undefined;
|
|
67
|
+
onMouseMove?: ((event: MouseEvent<E> | TouchEvent<E>) => void) | undefined;
|
|
68
|
+
onMouseDown: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
69
|
+
onMouseUp: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
70
|
+
onMouseLeave: () => void;
|
|
71
|
+
onTouchStart: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
72
|
+
onTouchEnd: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
72
73
|
};
|
|
73
|
-
|
|
74
|
-
export { useLongPress };
|
|
74
|
+
//#endregion
|
|
75
|
+
export { UseLongPressOptions, useLongPress };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
|
|
2
|
+
import { MouseEvent, TouchEvent } from "react";
|
|
3
|
+
//#region src/hooks/useLongPress/useLongPress.d.ts
|
|
4
|
+
type Handler<E extends HTMLElement> = (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
5
|
+
type UseLongPressOptions<E extends HTMLElement> = {
|
|
6
|
+
delay?: number;
|
|
7
|
+
moveThreshold?: {
|
|
8
|
+
x?: number;
|
|
9
|
+
y?: number;
|
|
10
|
+
};
|
|
11
|
+
onClick?: Handler<E>;
|
|
12
|
+
onLongPressEnd?: Handler<E>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, window.setTimeout).
|
|
16
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
17
|
+
* This hook will be removed in a future major version.
|
|
18
|
+
*
|
|
19
|
+
* @description
|
|
20
|
+
* `useLongPress` is a React hook that detects when an element is pressed and held for a specified duration.
|
|
21
|
+
* It handles both mouse and touch events, making it work consistently across desktop and mobile devices.
|
|
22
|
+
*
|
|
23
|
+
* @template {HTMLElement} E - The HTML element type that will use the long press handlers.
|
|
24
|
+
* @param {(event: React.MouseEvent<E> | React.TouchEvent<E>) => void} onLongPress - The callback function to be executed when a long press is detected.
|
|
25
|
+
* @param {UseLongPressOptions} [options] - Configuration options for the long press behavior.
|
|
26
|
+
* @param {number} [options.delay=500] - The time in milliseconds before triggering the long press. Defaults to 500ms.
|
|
27
|
+
* @param {Object} [options.moveThreshold] - Maximum movement allowed before canceling a long press.
|
|
28
|
+
* @param {number} [options.moveThreshold.x] - Maximum horizontal movement in pixels.
|
|
29
|
+
* @param {number} [options.moveThreshold.y] - Maximum vertical movement in pixels.
|
|
30
|
+
* @param {(event) => void} [options.onClick] - Optional function to execute on a normal click (press and release before delay).
|
|
31
|
+
* @param {(event) => void} [options.onLongPressEnd] - Optional function to execute when a long press ends.
|
|
32
|
+
*
|
|
33
|
+
* @returns {Object} Event handlers to attach to an element.
|
|
34
|
+
* - onMouseDown `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse down events.
|
|
35
|
+
* - onMouseUp `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse up events.
|
|
36
|
+
* - onMouseLeave `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse leave events.
|
|
37
|
+
* - onTouchStart `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch start events.
|
|
38
|
+
* - onTouchEnd `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch end events.
|
|
39
|
+
* - onMouseMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse move events. Included if `moveThreshold` is provided.
|
|
40
|
+
* - onTouchMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch move events. Included if `moveThreshold` is provided.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* import { useLongPress } from 'react-simplikit';
|
|
44
|
+
*
|
|
45
|
+
* function ContextMenu() {
|
|
46
|
+
* const [menuVisible, setMenuVisible] = useState(false);
|
|
47
|
+
*
|
|
48
|
+
* const longPressHandlers = useLongPress(
|
|
49
|
+
* () => setMenuVisible(true),
|
|
50
|
+
* {
|
|
51
|
+
* delay: 400,
|
|
52
|
+
* onClick: () => console.log('Normal click'),
|
|
53
|
+
* onLongPressEnd: () => console.log('Long press completed')
|
|
54
|
+
* }
|
|
55
|
+
* );
|
|
56
|
+
*
|
|
57
|
+
* return (
|
|
58
|
+
* <div>
|
|
59
|
+
* <button {...longPressHandlers}>Press and hold</button>
|
|
60
|
+
* {menuVisible && <div className="context-menu">Context Menu</div>}
|
|
61
|
+
* </div>
|
|
62
|
+
* );
|
|
63
|
+
* }
|
|
64
|
+
*/
|
|
65
|
+
declare function useLongPress<E extends HTMLElement = HTMLElement>(onLongPress: Handler<E>, { delay, moveThreshold, onClick, onLongPressEnd }?: UseLongPressOptions<E>): {
|
|
66
|
+
onTouchMove?: ((event: MouseEvent<E> | TouchEvent<E>) => void) | undefined;
|
|
67
|
+
onMouseMove?: ((event: MouseEvent<E> | TouchEvent<E>) => void) | undefined;
|
|
68
|
+
onMouseDown: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
69
|
+
onMouseUp: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
70
|
+
onMouseLeave: () => void;
|
|
71
|
+
onTouchStart: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
72
|
+
onTouchEnd: (event: MouseEvent<E> | TouchEvent<E>) => void;
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
export { UseLongPressOptions, useLongPress };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useCallback, useRef } from "react";
|
|
4
|
+
//#region src/hooks/useLongPress/useLongPress.ts
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, window.setTimeout).
|
|
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
|
+
* `useLongPress` is a React hook that detects when an element is pressed and held for a specified duration.
|
|
12
|
+
* It handles both mouse and touch events, making it work consistently across desktop and mobile devices.
|
|
13
|
+
*
|
|
14
|
+
* @template {HTMLElement} E - The HTML element type that will use the long press handlers.
|
|
15
|
+
* @param {(event: React.MouseEvent<E> | React.TouchEvent<E>) => void} onLongPress - The callback function to be executed when a long press is detected.
|
|
16
|
+
* @param {UseLongPressOptions} [options] - Configuration options for the long press behavior.
|
|
17
|
+
* @param {number} [options.delay=500] - The time in milliseconds before triggering the long press. Defaults to 500ms.
|
|
18
|
+
* @param {Object} [options.moveThreshold] - Maximum movement allowed before canceling a long press.
|
|
19
|
+
* @param {number} [options.moveThreshold.x] - Maximum horizontal movement in pixels.
|
|
20
|
+
* @param {number} [options.moveThreshold.y] - Maximum vertical movement in pixels.
|
|
21
|
+
* @param {(event) => void} [options.onClick] - Optional function to execute on a normal click (press and release before delay).
|
|
22
|
+
* @param {(event) => void} [options.onLongPressEnd] - Optional function to execute when a long press ends.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Object} Event handlers to attach to an element.
|
|
25
|
+
* - onMouseDown `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse down events.
|
|
26
|
+
* - onMouseUp `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse up events.
|
|
27
|
+
* - onMouseLeave `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse leave events.
|
|
28
|
+
* - onTouchStart `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch start events.
|
|
29
|
+
* - onTouchEnd `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch end events.
|
|
30
|
+
* - onMouseMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for mouse move events. Included if `moveThreshold` is provided.
|
|
31
|
+
* - onTouchMove `(event: MouseEvent<E> | TouchEvent<E>) => void` - Event handler for touch move events. Included if `moveThreshold` is provided.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* import { useLongPress } from 'react-simplikit';
|
|
35
|
+
*
|
|
36
|
+
* function ContextMenu() {
|
|
37
|
+
* const [menuVisible, setMenuVisible] = useState(false);
|
|
38
|
+
*
|
|
39
|
+
* const longPressHandlers = useLongPress(
|
|
40
|
+
* () => setMenuVisible(true),
|
|
41
|
+
* {
|
|
42
|
+
* delay: 400,
|
|
43
|
+
* onClick: () => console.log('Normal click'),
|
|
44
|
+
* onLongPressEnd: () => console.log('Long press completed')
|
|
45
|
+
* }
|
|
46
|
+
* );
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div>
|
|
50
|
+
* <button {...longPressHandlers}>Press and hold</button>
|
|
51
|
+
* {menuVisible && <div className="context-menu">Context Menu</div>}
|
|
52
|
+
* </div>
|
|
53
|
+
* );
|
|
54
|
+
* }
|
|
55
|
+
*/
|
|
56
|
+
function useLongPress(onLongPress, { delay = 500, moveThreshold, onClick, onLongPressEnd } = {}) {
|
|
57
|
+
const timeoutRef = useRef(null);
|
|
58
|
+
const isLongPressActiveRef = useRef(false);
|
|
59
|
+
const initialPositionRef = useRef({
|
|
60
|
+
x: 0,
|
|
61
|
+
y: 0
|
|
62
|
+
});
|
|
63
|
+
const preservedOnLongPress = usePreservedCallback(onLongPress);
|
|
64
|
+
const preservedOnClick = usePreservedCallback(onClick || (() => {}));
|
|
65
|
+
const preservedOnLongPressEnd = usePreservedCallback(onLongPressEnd || (() => {}));
|
|
66
|
+
const hasThreshold = moveThreshold?.x !== void 0 || moveThreshold?.y !== void 0;
|
|
67
|
+
const getClientPosition = useCallback((event) => {
|
|
68
|
+
if ("touches" in event.nativeEvent) {
|
|
69
|
+
const touch = event.nativeEvent.touches[0];
|
|
70
|
+
return {
|
|
71
|
+
x: touch.clientX,
|
|
72
|
+
y: touch.clientY
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
x: event.nativeEvent.clientX,
|
|
77
|
+
y: event.nativeEvent.clientY
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
const isMovedBeyondThreshold = useCallback((event) => {
|
|
81
|
+
const { x, y } = getClientPosition(event);
|
|
82
|
+
const deltaX = Math.abs(x - initialPositionRef.current.x);
|
|
83
|
+
const deltaY = Math.abs(y - initialPositionRef.current.y);
|
|
84
|
+
return moveThreshold?.x !== void 0 && deltaX > moveThreshold.x || moveThreshold?.y !== void 0 && deltaY > moveThreshold.y;
|
|
85
|
+
}, [getClientPosition, moveThreshold]);
|
|
86
|
+
const cancelLongPress = useCallback(() => {
|
|
87
|
+
if (timeoutRef.current !== null) {
|
|
88
|
+
window.clearTimeout(timeoutRef.current);
|
|
89
|
+
timeoutRef.current = null;
|
|
90
|
+
}
|
|
91
|
+
}, []);
|
|
92
|
+
const handlePressStart = useCallback((event) => {
|
|
93
|
+
cancelLongPress();
|
|
94
|
+
const position = getClientPosition(event);
|
|
95
|
+
initialPositionRef.current = position;
|
|
96
|
+
isLongPressActiveRef.current = false;
|
|
97
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
98
|
+
isLongPressActiveRef.current = true;
|
|
99
|
+
preservedOnLongPress(event);
|
|
100
|
+
}, delay);
|
|
101
|
+
}, [
|
|
102
|
+
cancelLongPress,
|
|
103
|
+
delay,
|
|
104
|
+
getClientPosition,
|
|
105
|
+
preservedOnLongPress
|
|
106
|
+
]);
|
|
107
|
+
const handlePressEnd = useCallback((event) => {
|
|
108
|
+
if (isLongPressActiveRef.current) preservedOnLongPressEnd(event);
|
|
109
|
+
else if (timeoutRef.current !== null) preservedOnClick(event);
|
|
110
|
+
cancelLongPress();
|
|
111
|
+
isLongPressActiveRef.current = false;
|
|
112
|
+
}, [
|
|
113
|
+
cancelLongPress,
|
|
114
|
+
preservedOnClick,
|
|
115
|
+
preservedOnLongPressEnd
|
|
116
|
+
]);
|
|
117
|
+
const handlePressMove = useCallback((event) => {
|
|
118
|
+
if (timeoutRef.current !== null && isMovedBeyondThreshold(event)) cancelLongPress();
|
|
119
|
+
}, [cancelLongPress, isMovedBeyondThreshold]);
|
|
120
|
+
return {
|
|
121
|
+
onMouseDown: handlePressStart,
|
|
122
|
+
onMouseUp: handlePressEnd,
|
|
123
|
+
onMouseLeave: cancelLongPress,
|
|
124
|
+
onTouchStart: handlePressStart,
|
|
125
|
+
onTouchEnd: handlePressEnd,
|
|
126
|
+
...hasThreshold ? {
|
|
127
|
+
onTouchMove: handlePressMove,
|
|
128
|
+
onMouseMove: handlePressMove
|
|
129
|
+
} : {}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { useLongPress };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedReference = require("../usePreservedReference/usePreservedReference.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/hooks/useMap/useMap.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* A React hook that manages a key-value Map as state.
|
|
8
|
+
* Provides efficient state management and stable action functions.
|
|
9
|
+
*
|
|
10
|
+
* @param {MapOrEntries<K, V>} initialState - Initial Map state (Map object or array of key-value pairs)
|
|
11
|
+
* @returns {UseMapReturn<K, V>} A tuple containing the Map state and actions to manipulate it
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const [userMap, actions] = useMap<string, User>([
|
|
16
|
+
* ['user1', { name: 'John', age: 30 }]
|
|
17
|
+
* ]);
|
|
18
|
+
*
|
|
19
|
+
* // Using values from the Map
|
|
20
|
+
* const user1 = userMap.get('user1');
|
|
21
|
+
*
|
|
22
|
+
* // Updating the Map
|
|
23
|
+
* actions.set('user2', { name: 'Jane', age: 25 });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function useMap(initialState = /* @__PURE__ */ new Map()) {
|
|
27
|
+
const [map, setMap] = (0, react.useState)(() => new Map(initialState));
|
|
28
|
+
const preservedInitialState = require_usePreservedReference.usePreservedReference(initialState);
|
|
29
|
+
const set = (0, react.useCallback)((key, value) => {
|
|
30
|
+
setMap((prev) => {
|
|
31
|
+
const nextMap = new Map(prev);
|
|
32
|
+
nextMap.set(key, value);
|
|
33
|
+
return nextMap;
|
|
34
|
+
});
|
|
35
|
+
}, []);
|
|
36
|
+
const setAll = (0, react.useCallback)((entries) => {
|
|
37
|
+
setMap(() => new Map(entries));
|
|
38
|
+
}, []);
|
|
39
|
+
const remove = (0, react.useCallback)((key) => {
|
|
40
|
+
setMap((prev) => {
|
|
41
|
+
const nextMap = new Map(prev);
|
|
42
|
+
nextMap.delete(key);
|
|
43
|
+
return nextMap;
|
|
44
|
+
});
|
|
45
|
+
}, []);
|
|
46
|
+
const reset = (0, react.useCallback)(() => {
|
|
47
|
+
setMap(() => new Map(preservedInitialState));
|
|
48
|
+
}, [preservedInitialState]);
|
|
49
|
+
return [map, (0, react.useMemo)(() => {
|
|
50
|
+
return {
|
|
51
|
+
set,
|
|
52
|
+
setAll,
|
|
53
|
+
remove,
|
|
54
|
+
reset
|
|
55
|
+
};
|
|
56
|
+
}, [
|
|
57
|
+
set,
|
|
58
|
+
setAll,
|
|
59
|
+
remove,
|
|
60
|
+
reset
|
|
61
|
+
])];
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
exports.useMap = useMap;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useMap/useMap.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* Defines the type for either a Map or an array of key-value pairs.
|
|
3
5
|
*/
|
|
@@ -6,14 +8,14 @@ type MapOrEntries<K, V> = Map<K, V> | [K, V][];
|
|
|
6
8
|
* Actions to manipulate the Map state.
|
|
7
9
|
*/
|
|
8
10
|
type MapActions<K, V> = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
/** Sets a key-value pair in the map. */
|
|
12
|
+
set: (key: K, value: V) => void;
|
|
13
|
+
/** Sets multiple key-value pairs in the map at once. */
|
|
14
|
+
setAll: (entries: MapOrEntries<K, V>) => void;
|
|
15
|
+
/** Removes a key from the map. */
|
|
16
|
+
remove: (key: K) => void;
|
|
17
|
+
/** Resets the map to its initial state. */
|
|
18
|
+
reset: () => void;
|
|
17
19
|
};
|
|
18
20
|
/**
|
|
19
21
|
* Return type of the useMap hook.
|
|
@@ -42,5 +44,5 @@ type UseMapReturn<K, V> = [Omit<Map<K, V>, 'set' | 'clear' | 'delete'>, MapActio
|
|
|
42
44
|
* ```
|
|
43
45
|
*/
|
|
44
46
|
declare function useMap<K, V>(initialState?: MapOrEntries<K, V>): UseMapReturn<K, V>;
|
|
45
|
-
|
|
46
|
-
export { useMap };
|
|
47
|
+
//#endregion
|
|
48
|
+
export { useMap };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useMap/useMap.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* Defines the type for either a Map or an array of key-value pairs.
|
|
3
5
|
*/
|
|
@@ -6,14 +8,14 @@ type MapOrEntries<K, V> = Map<K, V> | [K, V][];
|
|
|
6
8
|
* Actions to manipulate the Map state.
|
|
7
9
|
*/
|
|
8
10
|
type MapActions<K, V> = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
/** Sets a key-value pair in the map. */
|
|
12
|
+
set: (key: K, value: V) => void;
|
|
13
|
+
/** Sets multiple key-value pairs in the map at once. */
|
|
14
|
+
setAll: (entries: MapOrEntries<K, V>) => void;
|
|
15
|
+
/** Removes a key from the map. */
|
|
16
|
+
remove: (key: K) => void;
|
|
17
|
+
/** Resets the map to its initial state. */
|
|
18
|
+
reset: () => void;
|
|
17
19
|
};
|
|
18
20
|
/**
|
|
19
21
|
* Return type of the useMap hook.
|
|
@@ -42,5 +44,5 @@ type UseMapReturn<K, V> = [Omit<Map<K, V>, 'set' | 'clear' | 'delete'>, MapActio
|
|
|
42
44
|
* ```
|
|
43
45
|
*/
|
|
44
46
|
declare function useMap<K, V>(initialState?: MapOrEntries<K, V>): UseMapReturn<K, V>;
|
|
45
|
-
|
|
46
|
-
export { useMap };
|
|
47
|
+
//#endregion
|
|
48
|
+
export { useMap };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedReference } from "../usePreservedReference/usePreservedReference.mjs";
|
|
3
|
+
import { useCallback, useMemo, useState } from "react";
|
|
4
|
+
//#region src/hooks/useMap/useMap.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* A React hook that manages a key-value Map as state.
|
|
8
|
+
* Provides efficient state management and stable action functions.
|
|
9
|
+
*
|
|
10
|
+
* @param {MapOrEntries<K, V>} initialState - Initial Map state (Map object or array of key-value pairs)
|
|
11
|
+
* @returns {UseMapReturn<K, V>} A tuple containing the Map state and actions to manipulate it
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const [userMap, actions] = useMap<string, User>([
|
|
16
|
+
* ['user1', { name: 'John', age: 30 }]
|
|
17
|
+
* ]);
|
|
18
|
+
*
|
|
19
|
+
* // Using values from the Map
|
|
20
|
+
* const user1 = userMap.get('user1');
|
|
21
|
+
*
|
|
22
|
+
* // Updating the Map
|
|
23
|
+
* actions.set('user2', { name: 'Jane', age: 25 });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function useMap(initialState = /* @__PURE__ */ new Map()) {
|
|
27
|
+
const [map, setMap] = useState(() => new Map(initialState));
|
|
28
|
+
const preservedInitialState = usePreservedReference(initialState);
|
|
29
|
+
const set = useCallback((key, value) => {
|
|
30
|
+
setMap((prev) => {
|
|
31
|
+
const nextMap = new Map(prev);
|
|
32
|
+
nextMap.set(key, value);
|
|
33
|
+
return nextMap;
|
|
34
|
+
});
|
|
35
|
+
}, []);
|
|
36
|
+
const setAll = useCallback((entries) => {
|
|
37
|
+
setMap(() => new Map(entries));
|
|
38
|
+
}, []);
|
|
39
|
+
const remove = useCallback((key) => {
|
|
40
|
+
setMap((prev) => {
|
|
41
|
+
const nextMap = new Map(prev);
|
|
42
|
+
nextMap.delete(key);
|
|
43
|
+
return nextMap;
|
|
44
|
+
});
|
|
45
|
+
}, []);
|
|
46
|
+
const reset = useCallback(() => {
|
|
47
|
+
setMap(() => new Map(preservedInitialState));
|
|
48
|
+
}, [preservedInitialState]);
|
|
49
|
+
return [map, useMemo(() => {
|
|
50
|
+
return {
|
|
51
|
+
set,
|
|
52
|
+
setAll,
|
|
53
|
+
remove,
|
|
54
|
+
reset
|
|
55
|
+
};
|
|
56
|
+
}, [
|
|
57
|
+
set,
|
|
58
|
+
setAll,
|
|
59
|
+
remove,
|
|
60
|
+
reset
|
|
61
|
+
])];
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { useMap };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
let react = require("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 = (0, react.useRef)([]);
|
|
34
|
+
const handleDocumentClick = require_usePreservedCallback.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
|
+
(0, react.useEffect)(() => {
|
|
41
|
+
containers.current = [container].flat(1).filter((item) => item != null);
|
|
42
|
+
}, [container]);
|
|
43
|
+
(0, react.useEffect)(() => {
|
|
44
|
+
document.addEventListener("click", handleDocumentClick);
|
|
45
|
+
return () => {
|
|
46
|
+
document.removeEventListener("click", handleDocumentClick);
|
|
47
|
+
};
|
|
48
|
+
}, [handleDocumentClick]);
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.useOutsideClickEffect = useOutsideClickEffect;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useOutsideClickEffect/useOutsideClickEffect.d.ts
|
|
1
3
|
type OneOrMore<T> = T | T[];
|
|
2
4
|
/**
|
|
3
5
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, document).
|
|
@@ -27,5 +29,5 @@ type OneOrMore<T> = T | T[];
|
|
|
27
29
|
* }
|
|
28
30
|
*/
|
|
29
31
|
declare function useOutsideClickEffect(container: OneOrMore<HTMLElement | null>, callback: () => void): void;
|
|
30
|
-
|
|
31
|
-
export { useOutsideClickEffect };
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useOutsideClickEffect };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useOutsideClickEffect/useOutsideClickEffect.d.ts
|
|
1
3
|
type OneOrMore<T> = T | T[];
|
|
2
4
|
/**
|
|
3
5
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (DOM events, document).
|
|
@@ -27,5 +29,5 @@ type OneOrMore<T> = T | T[];
|
|
|
27
29
|
* }
|
|
28
30
|
*/
|
|
29
31
|
declare function useOutsideClickEffect(container: OneOrMore<HTMLElement | null>, callback: () => void): void;
|
|
30
|
-
|
|
31
|
-
export { useOutsideClickEffect };
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useOutsideClickEffect };
|