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,210 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useGeolocation/useGeolocation.ts
|
|
4
|
+
var CustomGeoLocationError = class extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor({ code, message }) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "CustomGeoLocationError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const GeolocationMountBehavior = {
|
|
13
|
+
GET: "get",
|
|
14
|
+
WATCH: "watch"
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (navigator.geolocation).
|
|
18
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
19
|
+
* This hook will be removed in a future major version.
|
|
20
|
+
*
|
|
21
|
+
* @description
|
|
22
|
+
* `useGeolocation` is a React hook that retrieves and tracks the user's geographical location.
|
|
23
|
+
* It uses the browser's `Geolocation API` to support both one-time position retrieval and continuous location tracking.
|
|
24
|
+
*
|
|
25
|
+
* @param {GeolocationOptions} [options] - Geolocation options configuration
|
|
26
|
+
* @param {GeolocationMountBehaviorType} [options.mountBehavior] - How the hook behaves on mount:
|
|
27
|
+
* -- If not provided, no automatic location fetching occurs
|
|
28
|
+
* -- `get`: automatically fetches location once when component mounts
|
|
29
|
+
* -- `watch`: automatically starts tracking location changes when component mounts
|
|
30
|
+
* @param {boolean} [options.enableHighAccuracy=false] - If true, provides more accurate position information (increases battery consumption)
|
|
31
|
+
* @param {number} [options.maximumAge=0] - Maximum age in milliseconds of a cached position that is acceptable to return
|
|
32
|
+
* @param {number} [options.timeout=Infinity] - Maximum time (in milliseconds) allowed for the location request
|
|
33
|
+
*
|
|
34
|
+
* @returns {Object} Object containing location data and related functions
|
|
35
|
+
* - loading `boolean` - Whether location data is currently being fetched;
|
|
36
|
+
* - error `CustomGeoLocationError|null` - Error object if an error occurred, or null
|
|
37
|
+
* The hook uses standard Geolocation API error codes (`1-3`) and adds a custom code (`0`)
|
|
38
|
+
* : `0` - Geolocation is not supported by the environment
|
|
39
|
+
* : `1` - User denied permission to access geolocation
|
|
40
|
+
* : `2` - Position unavailable
|
|
41
|
+
* : `3` - Timeout - geolocation request took too long;
|
|
42
|
+
* - data `GeolocationData|null` - Location data object or null
|
|
43
|
+
* : latitude `number` - The latitude in decimal degrees
|
|
44
|
+
* : longitude `number` - The longitude in decimal degrees
|
|
45
|
+
* : accuracy `number` - The accuracy of position in meters
|
|
46
|
+
* : altitude `number|null` - The altitude in meters above the WGS84 ellipsoid
|
|
47
|
+
* : altitudeAccuracy `number|null` - The altitude accuracy in meters
|
|
48
|
+
* : heading `number|null` - The heading in degrees clockwise from true north
|
|
49
|
+
* : speed `number|null` - The speed in meters per second
|
|
50
|
+
* : timestamp `number` - The time when the position was retrieved;
|
|
51
|
+
* - getCurrentPosition `Function` - Function to get the current position once;
|
|
52
|
+
* - startTracking `Function` - Function to start tracking location changes;
|
|
53
|
+
* - stopTracking `Function` - Function to stop tracking location;
|
|
54
|
+
* - isTracking `boolean` - Whether location tracking is currently active;
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Basic usage
|
|
58
|
+
* const {
|
|
59
|
+
* loading,
|
|
60
|
+
* error,
|
|
61
|
+
* data,
|
|
62
|
+
* getCurrentPosition
|
|
63
|
+
* } = useGeolocation();
|
|
64
|
+
*
|
|
65
|
+
* // Automatically fetch location when component mounts
|
|
66
|
+
* const {
|
|
67
|
+
* loading,
|
|
68
|
+
* error,
|
|
69
|
+
* data
|
|
70
|
+
* } = useGeolocation({ mountBehavior: 'get' });
|
|
71
|
+
*
|
|
72
|
+
* // Location tracking
|
|
73
|
+
* const {
|
|
74
|
+
* loading,
|
|
75
|
+
* error,
|
|
76
|
+
* data,
|
|
77
|
+
* startTracking,
|
|
78
|
+
* stopTracking,
|
|
79
|
+
* isTracking
|
|
80
|
+
* } = useGeolocation();
|
|
81
|
+
*
|
|
82
|
+
* const handleStartTracking = () => {
|
|
83
|
+
* startTracking();
|
|
84
|
+
* };
|
|
85
|
+
*
|
|
86
|
+
* const handleStopTracking = () => {
|
|
87
|
+
* stopTracking();
|
|
88
|
+
* };
|
|
89
|
+
*/
|
|
90
|
+
function useGeolocation(options) {
|
|
91
|
+
const [state, setState] = (0, react.useState)({
|
|
92
|
+
loading: !!options?.mountBehavior,
|
|
93
|
+
error: null,
|
|
94
|
+
data: null
|
|
95
|
+
});
|
|
96
|
+
const [isTracking, setIsTracking] = (0, react.useState)(false);
|
|
97
|
+
const watchIdRef = (0, react.useRef)(null);
|
|
98
|
+
const checkGeolocationSupport = (0, react.useCallback)(() => {
|
|
99
|
+
if (typeof window === "undefined" || navigator.geolocation === void 0) {
|
|
100
|
+
setState((prev) => ({
|
|
101
|
+
...prev,
|
|
102
|
+
loading: false,
|
|
103
|
+
error: new CustomGeoLocationError({
|
|
104
|
+
code: 0,
|
|
105
|
+
message: "Geolocation is not supported by this environment."
|
|
106
|
+
})
|
|
107
|
+
}));
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}, []);
|
|
112
|
+
const handleSuccess = (0, react.useCallback)((position) => {
|
|
113
|
+
const { coords } = position;
|
|
114
|
+
setState((prev) => ({
|
|
115
|
+
...prev,
|
|
116
|
+
loading: false,
|
|
117
|
+
error: null,
|
|
118
|
+
data: {
|
|
119
|
+
latitude: coords.latitude,
|
|
120
|
+
longitude: coords.longitude,
|
|
121
|
+
accuracy: coords.accuracy,
|
|
122
|
+
altitude: coords.altitude,
|
|
123
|
+
altitudeAccuracy: coords.altitudeAccuracy,
|
|
124
|
+
heading: coords.heading,
|
|
125
|
+
speed: coords.speed,
|
|
126
|
+
timestamp: position.timestamp
|
|
127
|
+
}
|
|
128
|
+
}));
|
|
129
|
+
}, []);
|
|
130
|
+
const handleError = (0, react.useCallback)((error) => {
|
|
131
|
+
const { code, message } = error;
|
|
132
|
+
setState((prev) => ({
|
|
133
|
+
...prev,
|
|
134
|
+
loading: false,
|
|
135
|
+
error: new CustomGeoLocationError({
|
|
136
|
+
code,
|
|
137
|
+
message
|
|
138
|
+
})
|
|
139
|
+
}));
|
|
140
|
+
}, []);
|
|
141
|
+
const getGeolocationOptions = (0, react.useCallback)(() => ({
|
|
142
|
+
enableHighAccuracy: options?.enableHighAccuracy,
|
|
143
|
+
maximumAge: options?.maximumAge,
|
|
144
|
+
timeout: options?.timeout
|
|
145
|
+
}), [
|
|
146
|
+
options?.enableHighAccuracy,
|
|
147
|
+
options?.maximumAge,
|
|
148
|
+
options?.timeout
|
|
149
|
+
]);
|
|
150
|
+
const getCurrentPosition = (0, react.useCallback)(() => {
|
|
151
|
+
if (!checkGeolocationSupport()) return;
|
|
152
|
+
setState((prev) => ({
|
|
153
|
+
...prev,
|
|
154
|
+
loading: true
|
|
155
|
+
}));
|
|
156
|
+
navigator.geolocation.getCurrentPosition(handleSuccess, handleError, getGeolocationOptions());
|
|
157
|
+
}, [
|
|
158
|
+
handleSuccess,
|
|
159
|
+
handleError,
|
|
160
|
+
getGeolocationOptions,
|
|
161
|
+
checkGeolocationSupport
|
|
162
|
+
]);
|
|
163
|
+
const startTracking = (0, react.useCallback)(() => {
|
|
164
|
+
if (!checkGeolocationSupport()) return;
|
|
165
|
+
if (watchIdRef.current !== null) navigator.geolocation.clearWatch(watchIdRef.current);
|
|
166
|
+
setState((prev) => ({
|
|
167
|
+
...prev,
|
|
168
|
+
loading: true
|
|
169
|
+
}));
|
|
170
|
+
watchIdRef.current = navigator.geolocation.watchPosition((position) => {
|
|
171
|
+
setIsTracking(true);
|
|
172
|
+
handleSuccess(position);
|
|
173
|
+
}, handleError, getGeolocationOptions());
|
|
174
|
+
}, [
|
|
175
|
+
handleSuccess,
|
|
176
|
+
handleError,
|
|
177
|
+
getGeolocationOptions,
|
|
178
|
+
checkGeolocationSupport
|
|
179
|
+
]);
|
|
180
|
+
const stopTracking = (0, react.useCallback)(() => {
|
|
181
|
+
if (watchIdRef.current === null) return;
|
|
182
|
+
navigator.geolocation.clearWatch(watchIdRef.current);
|
|
183
|
+
watchIdRef.current = null;
|
|
184
|
+
setIsTracking(false);
|
|
185
|
+
}, []);
|
|
186
|
+
(0, react.useEffect)(() => {
|
|
187
|
+
if (options?.mountBehavior === GeolocationMountBehavior.WATCH) startTracking();
|
|
188
|
+
else if (options?.mountBehavior === GeolocationMountBehavior.GET) getCurrentPosition();
|
|
189
|
+
return () => {
|
|
190
|
+
if (watchIdRef.current !== null) {
|
|
191
|
+
navigator.geolocation.clearWatch(watchIdRef.current);
|
|
192
|
+
watchIdRef.current = null;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}, [
|
|
196
|
+
options?.mountBehavior,
|
|
197
|
+
getCurrentPosition,
|
|
198
|
+
startTracking
|
|
199
|
+
]);
|
|
200
|
+
return {
|
|
201
|
+
...state,
|
|
202
|
+
getCurrentPosition,
|
|
203
|
+
startTracking,
|
|
204
|
+
stopTracking,
|
|
205
|
+
isTracking
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
exports.CustomGeoLocationError = CustomGeoLocationError;
|
|
210
|
+
exports.useGeolocation = useGeolocation;
|
package/{esm/hooks/useGeolocation/index.d.ts → dist/hooks/useGeolocation/useGeolocation.d.cts}
RENAMED
|
@@ -1,27 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useGeolocation/useGeolocation.d.ts
|
|
1
3
|
declare class CustomGeoLocationError extends Error {
|
|
4
|
+
code: number;
|
|
5
|
+
constructor({ code, message }: {
|
|
6
|
+
message: string;
|
|
2
7
|
code: number;
|
|
3
|
-
|
|
4
|
-
message: string;
|
|
5
|
-
code: number;
|
|
6
|
-
});
|
|
8
|
+
});
|
|
7
9
|
}
|
|
8
10
|
type GeolocationData = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
latitude: number;
|
|
12
|
+
longitude: number;
|
|
13
|
+
accuracy: number;
|
|
14
|
+
altitude: number | null;
|
|
15
|
+
altitudeAccuracy: number | null;
|
|
16
|
+
heading: number | null;
|
|
17
|
+
speed: number | null;
|
|
18
|
+
timestamp: number;
|
|
17
19
|
};
|
|
18
20
|
declare const GeolocationMountBehavior: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
readonly GET: "get";
|
|
22
|
+
readonly WATCH: "watch";
|
|
21
23
|
};
|
|
22
24
|
type GeolocationMountBehaviorType = (typeof GeolocationMountBehavior)[keyof typeof GeolocationMountBehavior];
|
|
23
25
|
type GeolocationOptions = {
|
|
24
|
-
|
|
26
|
+
mountBehavior?: GeolocationMountBehaviorType;
|
|
25
27
|
} & PositionOptions;
|
|
26
28
|
/**
|
|
27
29
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (navigator.geolocation).
|
|
@@ -98,13 +100,13 @@ type GeolocationOptions = {
|
|
|
98
100
|
* };
|
|
99
101
|
*/
|
|
100
102
|
declare function useGeolocation(options?: GeolocationOptions): {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
getCurrentPosition: () => void;
|
|
104
|
+
startTracking: () => void;
|
|
105
|
+
stopTracking: () => void;
|
|
106
|
+
isTracking: boolean;
|
|
107
|
+
loading: boolean;
|
|
108
|
+
error: CustomGeoLocationError | null;
|
|
109
|
+
data: GeolocationData | null;
|
|
108
110
|
};
|
|
109
|
-
|
|
110
|
-
export { useGeolocation };
|
|
111
|
+
//#endregion
|
|
112
|
+
export { CustomGeoLocationError, useGeolocation };
|
|
@@ -1,27 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useGeolocation/useGeolocation.d.ts
|
|
1
3
|
declare class CustomGeoLocationError extends Error {
|
|
4
|
+
code: number;
|
|
5
|
+
constructor({ code, message }: {
|
|
6
|
+
message: string;
|
|
2
7
|
code: number;
|
|
3
|
-
|
|
4
|
-
message: string;
|
|
5
|
-
code: number;
|
|
6
|
-
});
|
|
8
|
+
});
|
|
7
9
|
}
|
|
8
10
|
type GeolocationData = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
latitude: number;
|
|
12
|
+
longitude: number;
|
|
13
|
+
accuracy: number;
|
|
14
|
+
altitude: number | null;
|
|
15
|
+
altitudeAccuracy: number | null;
|
|
16
|
+
heading: number | null;
|
|
17
|
+
speed: number | null;
|
|
18
|
+
timestamp: number;
|
|
17
19
|
};
|
|
18
20
|
declare const GeolocationMountBehavior: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
readonly GET: "get";
|
|
22
|
+
readonly WATCH: "watch";
|
|
21
23
|
};
|
|
22
24
|
type GeolocationMountBehaviorType = (typeof GeolocationMountBehavior)[keyof typeof GeolocationMountBehavior];
|
|
23
25
|
type GeolocationOptions = {
|
|
24
|
-
|
|
26
|
+
mountBehavior?: GeolocationMountBehaviorType;
|
|
25
27
|
} & PositionOptions;
|
|
26
28
|
/**
|
|
27
29
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (navigator.geolocation).
|
|
@@ -98,13 +100,13 @@ type GeolocationOptions = {
|
|
|
98
100
|
* };
|
|
99
101
|
*/
|
|
100
102
|
declare function useGeolocation(options?: GeolocationOptions): {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
getCurrentPosition: () => void;
|
|
104
|
+
startTracking: () => void;
|
|
105
|
+
stopTracking: () => void;
|
|
106
|
+
isTracking: boolean;
|
|
107
|
+
loading: boolean;
|
|
108
|
+
error: CustomGeoLocationError | null;
|
|
109
|
+
data: GeolocationData | null;
|
|
108
110
|
};
|
|
109
|
-
|
|
110
|
-
export { useGeolocation };
|
|
111
|
+
//#endregion
|
|
112
|
+
export { CustomGeoLocationError, useGeolocation };
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
//#region src/hooks/useGeolocation/useGeolocation.ts
|
|
4
|
+
var CustomGeoLocationError = class extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor({ code, message }) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "CustomGeoLocationError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const GeolocationMountBehavior = {
|
|
13
|
+
GET: "get",
|
|
14
|
+
WATCH: "watch"
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (navigator.geolocation).
|
|
18
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
19
|
+
* This hook will be removed in a future major version.
|
|
20
|
+
*
|
|
21
|
+
* @description
|
|
22
|
+
* `useGeolocation` is a React hook that retrieves and tracks the user's geographical location.
|
|
23
|
+
* It uses the browser's `Geolocation API` to support both one-time position retrieval and continuous location tracking.
|
|
24
|
+
*
|
|
25
|
+
* @param {GeolocationOptions} [options] - Geolocation options configuration
|
|
26
|
+
* @param {GeolocationMountBehaviorType} [options.mountBehavior] - How the hook behaves on mount:
|
|
27
|
+
* -- If not provided, no automatic location fetching occurs
|
|
28
|
+
* -- `get`: automatically fetches location once when component mounts
|
|
29
|
+
* -- `watch`: automatically starts tracking location changes when component mounts
|
|
30
|
+
* @param {boolean} [options.enableHighAccuracy=false] - If true, provides more accurate position information (increases battery consumption)
|
|
31
|
+
* @param {number} [options.maximumAge=0] - Maximum age in milliseconds of a cached position that is acceptable to return
|
|
32
|
+
* @param {number} [options.timeout=Infinity] - Maximum time (in milliseconds) allowed for the location request
|
|
33
|
+
*
|
|
34
|
+
* @returns {Object} Object containing location data and related functions
|
|
35
|
+
* - loading `boolean` - Whether location data is currently being fetched;
|
|
36
|
+
* - error `CustomGeoLocationError|null` - Error object if an error occurred, or null
|
|
37
|
+
* The hook uses standard Geolocation API error codes (`1-3`) and adds a custom code (`0`)
|
|
38
|
+
* : `0` - Geolocation is not supported by the environment
|
|
39
|
+
* : `1` - User denied permission to access geolocation
|
|
40
|
+
* : `2` - Position unavailable
|
|
41
|
+
* : `3` - Timeout - geolocation request took too long;
|
|
42
|
+
* - data `GeolocationData|null` - Location data object or null
|
|
43
|
+
* : latitude `number` - The latitude in decimal degrees
|
|
44
|
+
* : longitude `number` - The longitude in decimal degrees
|
|
45
|
+
* : accuracy `number` - The accuracy of position in meters
|
|
46
|
+
* : altitude `number|null` - The altitude in meters above the WGS84 ellipsoid
|
|
47
|
+
* : altitudeAccuracy `number|null` - The altitude accuracy in meters
|
|
48
|
+
* : heading `number|null` - The heading in degrees clockwise from true north
|
|
49
|
+
* : speed `number|null` - The speed in meters per second
|
|
50
|
+
* : timestamp `number` - The time when the position was retrieved;
|
|
51
|
+
* - getCurrentPosition `Function` - Function to get the current position once;
|
|
52
|
+
* - startTracking `Function` - Function to start tracking location changes;
|
|
53
|
+
* - stopTracking `Function` - Function to stop tracking location;
|
|
54
|
+
* - isTracking `boolean` - Whether location tracking is currently active;
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Basic usage
|
|
58
|
+
* const {
|
|
59
|
+
* loading,
|
|
60
|
+
* error,
|
|
61
|
+
* data,
|
|
62
|
+
* getCurrentPosition
|
|
63
|
+
* } = useGeolocation();
|
|
64
|
+
*
|
|
65
|
+
* // Automatically fetch location when component mounts
|
|
66
|
+
* const {
|
|
67
|
+
* loading,
|
|
68
|
+
* error,
|
|
69
|
+
* data
|
|
70
|
+
* } = useGeolocation({ mountBehavior: 'get' });
|
|
71
|
+
*
|
|
72
|
+
* // Location tracking
|
|
73
|
+
* const {
|
|
74
|
+
* loading,
|
|
75
|
+
* error,
|
|
76
|
+
* data,
|
|
77
|
+
* startTracking,
|
|
78
|
+
* stopTracking,
|
|
79
|
+
* isTracking
|
|
80
|
+
* } = useGeolocation();
|
|
81
|
+
*
|
|
82
|
+
* const handleStartTracking = () => {
|
|
83
|
+
* startTracking();
|
|
84
|
+
* };
|
|
85
|
+
*
|
|
86
|
+
* const handleStopTracking = () => {
|
|
87
|
+
* stopTracking();
|
|
88
|
+
* };
|
|
89
|
+
*/
|
|
90
|
+
function useGeolocation(options) {
|
|
91
|
+
const [state, setState] = useState({
|
|
92
|
+
loading: !!options?.mountBehavior,
|
|
93
|
+
error: null,
|
|
94
|
+
data: null
|
|
95
|
+
});
|
|
96
|
+
const [isTracking, setIsTracking] = useState(false);
|
|
97
|
+
const watchIdRef = useRef(null);
|
|
98
|
+
const checkGeolocationSupport = useCallback(() => {
|
|
99
|
+
if (typeof window === "undefined" || navigator.geolocation === void 0) {
|
|
100
|
+
setState((prev) => ({
|
|
101
|
+
...prev,
|
|
102
|
+
loading: false,
|
|
103
|
+
error: new CustomGeoLocationError({
|
|
104
|
+
code: 0,
|
|
105
|
+
message: "Geolocation is not supported by this environment."
|
|
106
|
+
})
|
|
107
|
+
}));
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}, []);
|
|
112
|
+
const handleSuccess = useCallback((position) => {
|
|
113
|
+
const { coords } = position;
|
|
114
|
+
setState((prev) => ({
|
|
115
|
+
...prev,
|
|
116
|
+
loading: false,
|
|
117
|
+
error: null,
|
|
118
|
+
data: {
|
|
119
|
+
latitude: coords.latitude,
|
|
120
|
+
longitude: coords.longitude,
|
|
121
|
+
accuracy: coords.accuracy,
|
|
122
|
+
altitude: coords.altitude,
|
|
123
|
+
altitudeAccuracy: coords.altitudeAccuracy,
|
|
124
|
+
heading: coords.heading,
|
|
125
|
+
speed: coords.speed,
|
|
126
|
+
timestamp: position.timestamp
|
|
127
|
+
}
|
|
128
|
+
}));
|
|
129
|
+
}, []);
|
|
130
|
+
const handleError = useCallback((error) => {
|
|
131
|
+
const { code, message } = error;
|
|
132
|
+
setState((prev) => ({
|
|
133
|
+
...prev,
|
|
134
|
+
loading: false,
|
|
135
|
+
error: new CustomGeoLocationError({
|
|
136
|
+
code,
|
|
137
|
+
message
|
|
138
|
+
})
|
|
139
|
+
}));
|
|
140
|
+
}, []);
|
|
141
|
+
const getGeolocationOptions = useCallback(() => ({
|
|
142
|
+
enableHighAccuracy: options?.enableHighAccuracy,
|
|
143
|
+
maximumAge: options?.maximumAge,
|
|
144
|
+
timeout: options?.timeout
|
|
145
|
+
}), [
|
|
146
|
+
options?.enableHighAccuracy,
|
|
147
|
+
options?.maximumAge,
|
|
148
|
+
options?.timeout
|
|
149
|
+
]);
|
|
150
|
+
const getCurrentPosition = useCallback(() => {
|
|
151
|
+
if (!checkGeolocationSupport()) return;
|
|
152
|
+
setState((prev) => ({
|
|
153
|
+
...prev,
|
|
154
|
+
loading: true
|
|
155
|
+
}));
|
|
156
|
+
navigator.geolocation.getCurrentPosition(handleSuccess, handleError, getGeolocationOptions());
|
|
157
|
+
}, [
|
|
158
|
+
handleSuccess,
|
|
159
|
+
handleError,
|
|
160
|
+
getGeolocationOptions,
|
|
161
|
+
checkGeolocationSupport
|
|
162
|
+
]);
|
|
163
|
+
const startTracking = useCallback(() => {
|
|
164
|
+
if (!checkGeolocationSupport()) return;
|
|
165
|
+
if (watchIdRef.current !== null) navigator.geolocation.clearWatch(watchIdRef.current);
|
|
166
|
+
setState((prev) => ({
|
|
167
|
+
...prev,
|
|
168
|
+
loading: true
|
|
169
|
+
}));
|
|
170
|
+
watchIdRef.current = navigator.geolocation.watchPosition((position) => {
|
|
171
|
+
setIsTracking(true);
|
|
172
|
+
handleSuccess(position);
|
|
173
|
+
}, handleError, getGeolocationOptions());
|
|
174
|
+
}, [
|
|
175
|
+
handleSuccess,
|
|
176
|
+
handleError,
|
|
177
|
+
getGeolocationOptions,
|
|
178
|
+
checkGeolocationSupport
|
|
179
|
+
]);
|
|
180
|
+
const stopTracking = useCallback(() => {
|
|
181
|
+
if (watchIdRef.current === null) return;
|
|
182
|
+
navigator.geolocation.clearWatch(watchIdRef.current);
|
|
183
|
+
watchIdRef.current = null;
|
|
184
|
+
setIsTracking(false);
|
|
185
|
+
}, []);
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
if (options?.mountBehavior === GeolocationMountBehavior.WATCH) startTracking();
|
|
188
|
+
else if (options?.mountBehavior === GeolocationMountBehavior.GET) getCurrentPosition();
|
|
189
|
+
return () => {
|
|
190
|
+
if (watchIdRef.current !== null) {
|
|
191
|
+
navigator.geolocation.clearWatch(watchIdRef.current);
|
|
192
|
+
watchIdRef.current = null;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}, [
|
|
196
|
+
options?.mountBehavior,
|
|
197
|
+
getCurrentPosition,
|
|
198
|
+
startTracking
|
|
199
|
+
]);
|
|
200
|
+
return {
|
|
201
|
+
...state,
|
|
202
|
+
getCurrentPosition,
|
|
203
|
+
startTracking,
|
|
204
|
+
stopTracking,
|
|
205
|
+
isTracking
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
export { CustomGeoLocationError, useGeolocation };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
const require_useDebouncedCallback = require("../useDebouncedCallback/useDebouncedCallback.cjs");
|
|
4
|
+
const require_useIntersectionObserver = require("../useIntersectionObserver/useIntersectionObserver.cjs");
|
|
5
|
+
const require_useVisibilityEvent = require("../useVisibilityEvent/useVisibilityEvent.cjs");
|
|
6
|
+
let react = require("react");
|
|
7
|
+
//#region src/hooks/useImpressionRef/useImpressionRef.ts
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver, Visibility API).
|
|
10
|
+
* react-simplikit is now focused on platform-independent, pure state/logic hooks.
|
|
11
|
+
* This hook will be removed in a future major version.
|
|
12
|
+
*
|
|
13
|
+
* @description
|
|
14
|
+
* `useImpressionRef` is a React hook that measures the time a specific DOM element is visible on the screen and executes callbacks when the element enters or exits the viewport.
|
|
15
|
+
* It uses `IntersectionObserver` and the `Visibility API` to track the element's visibility.
|
|
16
|
+
*
|
|
17
|
+
* @param {UseImpressionRefOptions} options - Options for tracking the element's visibility.
|
|
18
|
+
* @param {() => void} [options.onImpressionStart] - Callback function executed when the element enters the view
|
|
19
|
+
* @param {() => void} [options.onImpressionEnd] - Callback function executed when the element exits the view
|
|
20
|
+
* @param {number} [options.timeThreshold=0] - Minimum time the element must be visible (in milliseconds)
|
|
21
|
+
* @param {number} [options.areaThreshold=0] - Minimum ratio of the element that must be visible (0 to 1)
|
|
22
|
+
* @param {string} options.rootMargin - Margin to adjust the detection area
|
|
23
|
+
*
|
|
24
|
+
* @returns {(element: Element | null) => void} A function to set the element. Attach this function to the `ref` attribute, and the callbacks will be executed whenever the element's visibility changes.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* import { useImpressionRef } from 'react-simplikit';
|
|
28
|
+
*
|
|
29
|
+
* function Component() {
|
|
30
|
+
* const ref = useImpressionRef<HTMLDivElement>({
|
|
31
|
+
* onImpressionStart: () => console.log('Element entered view'),
|
|
32
|
+
* onImpressionEnd: () => console.log('Element exited view'),
|
|
33
|
+
* timeThreshold: 1000,
|
|
34
|
+
* areaThreshold: 0.5,
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* return <div ref={ref}>Track my visibility!</div>;
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
function useImpressionRef({ onImpressionStart = () => {}, onImpressionEnd = () => {}, rootMargin, areaThreshold = 0, timeThreshold = 0 }) {
|
|
41
|
+
const impressionStartHandler = require_usePreservedCallback.usePreservedCallback(onImpressionStart);
|
|
42
|
+
const impressionEndHandler = require_usePreservedCallback.usePreservedCallback(onImpressionEnd);
|
|
43
|
+
const isIntersectingRef = (0, react.useRef)(false);
|
|
44
|
+
const impressionEventHandler = require_useDebouncedCallback.useDebouncedCallback({
|
|
45
|
+
timeThreshold,
|
|
46
|
+
onChange: (impressed) => {
|
|
47
|
+
(impressed ? impressionStartHandler : impressionEndHandler)();
|
|
48
|
+
},
|
|
49
|
+
leading: true
|
|
50
|
+
});
|
|
51
|
+
require_useVisibilityEvent.useVisibilityEvent((documentVisible) => {
|
|
52
|
+
if (!isIntersectingRef.current) return;
|
|
53
|
+
impressionEventHandler(documentVisible === "visible");
|
|
54
|
+
});
|
|
55
|
+
return require_useIntersectionObserver.useIntersectionObserver((entry) => {
|
|
56
|
+
if (document.visibilityState === "hidden") return;
|
|
57
|
+
const currentRatio = entry.intersectionRatio;
|
|
58
|
+
const isIntersecting = areaThreshold === 0 ? entry.isIntersecting : currentRatio >= areaThreshold;
|
|
59
|
+
isIntersectingRef.current = isIntersecting;
|
|
60
|
+
impressionEventHandler(isIntersecting);
|
|
61
|
+
}, {
|
|
62
|
+
rootMargin,
|
|
63
|
+
threshold: areaThreshold
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
exports.useImpressionRef = useImpressionRef;
|
package/{esm/hooks/useImpressionRef/index.d.ts → dist/hooks/useImpressionRef/useImpressionRef.d.cts}
RENAMED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useImpressionRef/useImpressionRef.d.ts
|
|
1
3
|
type UseImpressionRefOptions = Partial<{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
onImpressionStart: () => void;
|
|
5
|
+
onImpressionEnd: () => void;
|
|
6
|
+
rootMargin: string;
|
|
7
|
+
areaThreshold: number;
|
|
8
|
+
timeThreshold: number;
|
|
7
9
|
}>;
|
|
8
10
|
/**
|
|
9
11
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver, Visibility API).
|
|
@@ -37,6 +39,6 @@ type UseImpressionRefOptions = Partial<{
|
|
|
37
39
|
* return <div ref={ref}>Track my visibility!</div>;
|
|
38
40
|
* }
|
|
39
41
|
*/
|
|
40
|
-
declare function useImpressionRef<Element extends HTMLElement>({ onImpressionStart, onImpressionEnd, rootMargin, areaThreshold, timeThreshold
|
|
41
|
-
|
|
42
|
-
export {
|
|
42
|
+
declare function useImpressionRef<Element extends HTMLElement>({ onImpressionStart, onImpressionEnd, rootMargin, areaThreshold, timeThreshold }: UseImpressionRefOptions): (element: Element | null) => void;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { UseImpressionRefOptions, useImpressionRef };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useImpressionRef/useImpressionRef.d.ts
|
|
1
3
|
type UseImpressionRefOptions = Partial<{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
onImpressionStart: () => void;
|
|
5
|
+
onImpressionEnd: () => void;
|
|
6
|
+
rootMargin: string;
|
|
7
|
+
areaThreshold: number;
|
|
8
|
+
timeThreshold: number;
|
|
7
9
|
}>;
|
|
8
10
|
/**
|
|
9
11
|
* @deprecated This hook is deprecated as it depends on browser-specific APIs (IntersectionObserver, Visibility API).
|
|
@@ -37,6 +39,6 @@ type UseImpressionRefOptions = Partial<{
|
|
|
37
39
|
* return <div ref={ref}>Track my visibility!</div>;
|
|
38
40
|
* }
|
|
39
41
|
*/
|
|
40
|
-
declare function useImpressionRef<Element extends HTMLElement>({ onImpressionStart, onImpressionEnd, rootMargin, areaThreshold, timeThreshold
|
|
41
|
-
|
|
42
|
-
export {
|
|
42
|
+
declare function useImpressionRef<Element extends HTMLElement>({ onImpressionStart, onImpressionEnd, rootMargin, areaThreshold, timeThreshold }: UseImpressionRefOptions): (element: Element | null) => void;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { UseImpressionRefOptions, useImpressionRef };
|