@siberiacancode/reactuse 0.3.3 → 0.3.4
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/cjs/hooks/useActiveElement/useActiveElement.cjs +1 -1
- package/dist/cjs/hooks/useActiveElement/useActiveElement.cjs.map +1 -1
- package/dist/cjs/hooks/useAutoScroll/useAutoScroll.cjs +1 -1
- package/dist/cjs/hooks/useAutoScroll/useAutoScroll.cjs.map +1 -1
- package/dist/cjs/hooks/useBreakpoints/useBreakpoints.cjs +1 -1
- package/dist/cjs/hooks/useBreakpoints/useBreakpoints.cjs.map +1 -1
- package/dist/cjs/hooks/useClickOutside/useClickOutside.cjs +1 -1
- package/dist/cjs/hooks/useClickOutside/useClickOutside.cjs.map +1 -1
- package/dist/cjs/hooks/useCookie/useCookie.cjs +1 -1
- package/dist/cjs/hooks/useCookie/useCookie.cjs.map +1 -1
- package/dist/cjs/hooks/useCounter/useCounter.cjs +1 -1
- package/dist/cjs/hooks/useCounter/useCounter.cjs.map +1 -1
- package/dist/cjs/hooks/useCssVar/useCssVar.cjs +1 -1
- package/dist/cjs/hooks/useCssVar/useCssVar.cjs.map +1 -1
- package/dist/cjs/hooks/useDeviceMotion/useDeviceMotion.cjs +1 -1
- package/dist/cjs/hooks/useDeviceMotion/useDeviceMotion.cjs.map +1 -1
- package/dist/cjs/hooks/useDisplayMedia/useDisplayMedia.cjs +1 -1
- package/dist/cjs/hooks/useDisplayMedia/useDisplayMedia.cjs.map +1 -1
- package/dist/cjs/hooks/useDoubleClick/useDoubleClick.cjs +1 -1
- package/dist/cjs/hooks/useDoubleClick/useDoubleClick.cjs.map +1 -1
- package/dist/cjs/hooks/useDropZone/useDropZone.cjs +1 -1
- package/dist/cjs/hooks/useDropZone/useDropZone.cjs.map +1 -1
- package/dist/cjs/hooks/useElementSize/useElementSize.cjs +1 -1
- package/dist/cjs/hooks/useElementSize/useElementSize.cjs.map +1 -1
- package/dist/cjs/hooks/useEventListener/useEventListener.cjs +1 -1
- package/dist/cjs/hooks/useEventListener/useEventListener.cjs.map +1 -1
- package/dist/cjs/hooks/useFileDialog/useFileDialog.cjs +1 -1
- package/dist/cjs/hooks/useFileDialog/useFileDialog.cjs.map +1 -1
- package/dist/cjs/hooks/useFocus/useFocus.cjs +1 -1
- package/dist/cjs/hooks/useFocus/useFocus.cjs.map +1 -1
- package/dist/cjs/hooks/useFocusTrap/useFocusTrap.cjs +1 -1
- package/dist/cjs/hooks/useFocusTrap/useFocusTrap.cjs.map +1 -1
- package/dist/cjs/hooks/useFullscreen/useFullscreen.cjs +1 -1
- package/dist/cjs/hooks/useFullscreen/useFullscreen.cjs.map +1 -1
- package/dist/cjs/hooks/useHash/useHash.cjs +1 -1
- package/dist/cjs/hooks/useHash/useHash.cjs.map +1 -1
- package/dist/cjs/hooks/useHotkeys/useHotkeys.cjs +1 -1
- package/dist/cjs/hooks/useHotkeys/useHotkeys.cjs.map +1 -1
- package/dist/cjs/hooks/useHover/useHover.cjs +1 -1
- package/dist/cjs/hooks/useHover/useHover.cjs.map +1 -1
- package/dist/cjs/hooks/useInfiniteScroll/useInfiniteScroll.cjs +1 -1
- package/dist/cjs/hooks/useInfiniteScroll/useInfiniteScroll.cjs.map +1 -1
- package/dist/cjs/hooks/useIntersectionObserver/useIntersectionObserver.cjs +1 -1
- package/dist/cjs/hooks/useIntersectionObserver/useIntersectionObserver.cjs.map +1 -1
- package/dist/cjs/hooks/useInterval/useInterval.cjs +1 -1
- package/dist/cjs/hooks/useInterval/useInterval.cjs.map +1 -1
- package/dist/cjs/hooks/useKeyPress/useKeyPress.cjs +1 -1
- package/dist/cjs/hooks/useKeyPress/useKeyPress.cjs.map +1 -1
- package/dist/cjs/hooks/useKeyPressEvent/useKeyPressEvent.cjs +1 -1
- package/dist/cjs/hooks/useKeyPressEvent/useKeyPressEvent.cjs.map +1 -1
- package/dist/cjs/hooks/useKeyboard/useKeyboard.cjs +1 -1
- package/dist/cjs/hooks/useKeyboard/useKeyboard.cjs.map +1 -1
- package/dist/cjs/hooks/useKeysPressed/useKeysPressed.cjs +1 -1
- package/dist/cjs/hooks/useKeysPressed/useKeysPressed.cjs.map +1 -1
- package/dist/cjs/hooks/useLockScroll/useLockScroll.cjs +1 -1
- package/dist/cjs/hooks/useLockScroll/useLockScroll.cjs.map +1 -1
- package/dist/cjs/hooks/useLongPress/useLongPress.cjs +1 -1
- package/dist/cjs/hooks/useLongPress/useLongPress.cjs.map +1 -1
- package/dist/cjs/hooks/useMeasure/useMeasure.cjs +1 -1
- package/dist/cjs/hooks/useMeasure/useMeasure.cjs.map +1 -1
- package/dist/cjs/hooks/useMediaControls/useMediaControls.cjs +1 -1
- package/dist/cjs/hooks/useMediaControls/useMediaControls.cjs.map +1 -1
- package/dist/cjs/hooks/useMouse/useMouse.cjs +1 -1
- package/dist/cjs/hooks/useMouse/useMouse.cjs.map +1 -1
- package/dist/cjs/hooks/useMutationObserver/useMutationObserver.cjs +1 -1
- package/dist/cjs/hooks/useMutationObserver/useMutationObserver.cjs.map +1 -1
- package/dist/cjs/hooks/useOtpCredential/useOtpCredential.cjs +1 -1
- package/dist/cjs/hooks/useOtpCredential/useOtpCredential.cjs.map +1 -1
- package/dist/cjs/hooks/usePaint/usePaint.cjs +1 -1
- package/dist/cjs/hooks/usePaint/usePaint.cjs.map +1 -1
- package/dist/cjs/hooks/useParallax/useParallax.cjs +1 -1
- package/dist/cjs/hooks/useParallax/useParallax.cjs.map +1 -1
- package/dist/cjs/hooks/usePictureInPicture/usePictureInPicture.cjs +1 -1
- package/dist/cjs/hooks/usePictureInPicture/usePictureInPicture.cjs.map +1 -1
- package/dist/cjs/hooks/useRefState/useRefState.cjs +1 -1
- package/dist/cjs/hooks/useRefState/useRefState.cjs.map +1 -1
- package/dist/cjs/hooks/useResizeObserver/useResizeObserver.cjs +1 -1
- package/dist/cjs/hooks/useResizeObserver/useResizeObserver.cjs.map +1 -1
- package/dist/cjs/hooks/useRightClick/useRightClick.cjs +1 -1
- package/dist/cjs/hooks/useRightClick/useRightClick.cjs.map +1 -1
- package/dist/cjs/hooks/useScroll/useScroll.cjs +1 -1
- package/dist/cjs/hooks/useScroll/useScroll.cjs.map +1 -1
- package/dist/cjs/hooks/useScrollIntoView/useScrollIntoView.cjs +1 -1
- package/dist/cjs/hooks/useScrollIntoView/useScrollIntoView.cjs.map +1 -1
- package/dist/cjs/hooks/useScrollTo/useScrollTo.cjs +1 -1
- package/dist/cjs/hooks/useScrollTo/useScrollTo.cjs.map +1 -1
- package/dist/cjs/hooks/useSticky/useSticky.cjs +1 -1
- package/dist/cjs/hooks/useSticky/useSticky.cjs.map +1 -1
- package/dist/cjs/hooks/useStopwatch/useStopwatch.cjs +1 -1
- package/dist/cjs/hooks/useStopwatch/useStopwatch.cjs.map +1 -1
- package/dist/cjs/hooks/useStorage/useStorage.cjs +1 -1
- package/dist/cjs/hooks/useStorage/useStorage.cjs.map +1 -1
- package/dist/cjs/hooks/useTextDirection/useTextDirection.cjs +1 -1
- package/dist/cjs/hooks/useTextDirection/useTextDirection.cjs.map +1 -1
- package/dist/cjs/hooks/useTextareaAutosize/useTextareaAutosize.cjs +1 -1
- package/dist/cjs/hooks/useTextareaAutosize/useTextareaAutosize.cjs.map +1 -1
- package/dist/cjs/hooks/useTimer/useTimer.cjs +1 -1
- package/dist/cjs/hooks/useTimer/useTimer.cjs.map +1 -1
- package/dist/cjs/hooks/useUrlSearchParam/useUrlSearchParam.cjs +1 -1
- package/dist/cjs/hooks/useUrlSearchParam/useUrlSearchParam.cjs.map +1 -1
- package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs +1 -1
- package/dist/cjs/hooks/useUrlSearchParams/useUrlSearchParams.cjs.map +1 -1
- package/dist/cjs/hooks/useVisibility/useVisibility.cjs +1 -1
- package/dist/cjs/hooks/useVisibility/useVisibility.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/utils/helpers/isTarget.cjs +1 -1
- package/dist/cjs/utils/helpers/isTarget.cjs.map +1 -1
- package/dist/esm/hooks/useActiveElement/useActiveElement.mjs +13 -13
- package/dist/esm/hooks/useActiveElement/useActiveElement.mjs.map +1 -1
- package/dist/esm/hooks/useAutoScroll/useAutoScroll.mjs +24 -24
- package/dist/esm/hooks/useAutoScroll/useAutoScroll.mjs.map +1 -1
- package/dist/esm/hooks/useBreakpoints/useBreakpoints.mjs +30 -35
- package/dist/esm/hooks/useBreakpoints/useBreakpoints.mjs.map +1 -1
- package/dist/esm/hooks/useClickOutside/useClickOutside.mjs +15 -14
- package/dist/esm/hooks/useClickOutside/useClickOutside.mjs.map +1 -1
- package/dist/esm/hooks/useCookie/useCookie.mjs +2 -2
- package/dist/esm/hooks/useCookie/useCookie.mjs.map +1 -1
- package/dist/esm/hooks/useCounter/useCounter.mjs +2 -2
- package/dist/esm/hooks/useCounter/useCounter.mjs.map +1 -1
- package/dist/esm/hooks/useCssVar/useCssVar.mjs +8 -8
- package/dist/esm/hooks/useCssVar/useCssVar.mjs.map +1 -1
- package/dist/esm/hooks/useDeviceMotion/useDeviceMotion.mjs +2 -2
- package/dist/esm/hooks/useDeviceMotion/useDeviceMotion.mjs.map +1 -1
- package/dist/esm/hooks/useDisplayMedia/useDisplayMedia.mjs +17 -17
- package/dist/esm/hooks/useDisplayMedia/useDisplayMedia.mjs.map +1 -1
- package/dist/esm/hooks/useDoubleClick/useDoubleClick.mjs +14 -14
- package/dist/esm/hooks/useDoubleClick/useDoubleClick.mjs.map +1 -1
- package/dist/esm/hooks/useDropZone/useDropZone.mjs +23 -23
- package/dist/esm/hooks/useDropZone/useDropZone.mjs.map +1 -1
- package/dist/esm/hooks/useElementSize/useElementSize.mjs +14 -14
- package/dist/esm/hooks/useElementSize/useElementSize.mjs.map +1 -1
- package/dist/esm/hooks/useEventListener/useEventListener.mjs +14 -14
- package/dist/esm/hooks/useEventListener/useEventListener.mjs.map +1 -1
- package/dist/esm/hooks/useFileDialog/useFileDialog.mjs +2 -2
- package/dist/esm/hooks/useFileDialog/useFileDialog.mjs.map +1 -1
- package/dist/esm/hooks/useFocus/useFocus.mjs +20 -20
- package/dist/esm/hooks/useFocus/useFocus.mjs.map +1 -1
- package/dist/esm/hooks/useFocusTrap/useFocusTrap.mjs +21 -21
- package/dist/esm/hooks/useFocusTrap/useFocusTrap.mjs.map +1 -1
- package/dist/esm/hooks/useFullscreen/useFullscreen.mjs +2 -2
- package/dist/esm/hooks/useFullscreen/useFullscreen.mjs.map +1 -1
- package/dist/esm/hooks/useHash/useHash.mjs +2 -2
- package/dist/esm/hooks/useHash/useHash.mjs.map +1 -1
- package/dist/esm/hooks/useHotkeys/useHotkeys.mjs +24 -16
- package/dist/esm/hooks/useHotkeys/useHotkeys.mjs.map +1 -1
- package/dist/esm/hooks/useHover/useHover.mjs +16 -16
- package/dist/esm/hooks/useHover/useHover.mjs.map +1 -1
- package/dist/esm/hooks/useInfiniteScroll/useInfiniteScroll.mjs +16 -16
- package/dist/esm/hooks/useInfiniteScroll/useInfiniteScroll.mjs.map +1 -1
- package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs +26 -18
- package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs.map +1 -1
- package/dist/esm/hooks/useInterval/useInterval.mjs +2 -2
- package/dist/esm/hooks/useInterval/useInterval.mjs.map +1 -1
- package/dist/esm/hooks/useKeyPress/useKeyPress.mjs +15 -15
- package/dist/esm/hooks/useKeyPress/useKeyPress.mjs.map +1 -1
- package/dist/esm/hooks/useKeyPressEvent/useKeyPressEvent.mjs +22 -15
- package/dist/esm/hooks/useKeyPressEvent/useKeyPressEvent.mjs.map +1 -1
- package/dist/esm/hooks/useKeyboard/useKeyboard.mjs +10 -10
- package/dist/esm/hooks/useKeyboard/useKeyboard.mjs.map +1 -1
- package/dist/esm/hooks/useKeysPressed/useKeysPressed.mjs +12 -12
- package/dist/esm/hooks/useKeysPressed/useKeysPressed.mjs.map +1 -1
- package/dist/esm/hooks/useLockScroll/useLockScroll.mjs +23 -23
- package/dist/esm/hooks/useLockScroll/useLockScroll.mjs.map +1 -1
- package/dist/esm/hooks/useLongPress/useLongPress.mjs +16 -16
- package/dist/esm/hooks/useLongPress/useLongPress.mjs.map +1 -1
- package/dist/esm/hooks/useMeasure/useMeasure.mjs +15 -15
- package/dist/esm/hooks/useMeasure/useMeasure.mjs.map +1 -1
- package/dist/esm/hooks/useMediaControls/useMediaControls.mjs +28 -28
- package/dist/esm/hooks/useMediaControls/useMediaControls.mjs.map +1 -1
- package/dist/esm/hooks/useMouse/useMouse.mjs +17 -17
- package/dist/esm/hooks/useMouse/useMouse.mjs.map +1 -1
- package/dist/esm/hooks/useMutationObserver/useMutationObserver.mjs +17 -16
- package/dist/esm/hooks/useMutationObserver/useMutationObserver.mjs.map +1 -1
- package/dist/esm/hooks/useOtpCredential/useOtpCredential.mjs +2 -2
- package/dist/esm/hooks/useOtpCredential/useOtpCredential.mjs.map +1 -1
- package/dist/esm/hooks/usePaint/usePaint.mjs +31 -31
- package/dist/esm/hooks/usePaint/usePaint.mjs.map +1 -1
- package/dist/esm/hooks/useParallax/useParallax.mjs +24 -23
- package/dist/esm/hooks/useParallax/useParallax.mjs.map +1 -1
- package/dist/esm/hooks/usePictureInPicture/usePictureInPicture.mjs +25 -25
- package/dist/esm/hooks/usePictureInPicture/usePictureInPicture.mjs.map +1 -1
- package/dist/esm/hooks/useRefState/useRefState.mjs +1 -0
- package/dist/esm/hooks/useRefState/useRefState.mjs.map +1 -1
- package/dist/esm/hooks/useResizeObserver/useResizeObserver.mjs +16 -16
- package/dist/esm/hooks/useResizeObserver/useResizeObserver.mjs.map +1 -1
- package/dist/esm/hooks/useRightClick/useRightClick.mjs +14 -14
- package/dist/esm/hooks/useRightClick/useRightClick.mjs.map +1 -1
- package/dist/esm/hooks/useScroll/useScroll.mjs +22 -22
- package/dist/esm/hooks/useScroll/useScroll.mjs.map +1 -1
- package/dist/esm/hooks/useScrollIntoView/useScrollIntoView.mjs +16 -16
- package/dist/esm/hooks/useScrollIntoView/useScrollIntoView.mjs.map +1 -1
- package/dist/esm/hooks/useScrollTo/useScrollTo.mjs +10 -10
- package/dist/esm/hooks/useScrollTo/useScrollTo.mjs.map +1 -1
- package/dist/esm/hooks/useSticky/useSticky.mjs +17 -17
- package/dist/esm/hooks/useSticky/useSticky.mjs.map +1 -1
- package/dist/esm/hooks/useStopwatch/useStopwatch.mjs +4 -4
- package/dist/esm/hooks/useStopwatch/useStopwatch.mjs.map +1 -1
- package/dist/esm/hooks/useStorage/useStorage.mjs +2 -2
- package/dist/esm/hooks/useStorage/useStorage.mjs.map +1 -1
- package/dist/esm/hooks/useTextDirection/useTextDirection.mjs +12 -12
- package/dist/esm/hooks/useTextDirection/useTextDirection.mjs.map +1 -1
- package/dist/esm/hooks/useTextareaAutosize/useTextareaAutosize.mjs +20 -20
- package/dist/esm/hooks/useTextareaAutosize/useTextareaAutosize.mjs.map +1 -1
- package/dist/esm/hooks/useTimer/useTimer.mjs +3 -3
- package/dist/esm/hooks/useTimer/useTimer.mjs.map +1 -1
- package/dist/esm/hooks/useUrlSearchParam/useUrlSearchParam.mjs +2 -2
- package/dist/esm/hooks/useUrlSearchParam/useUrlSearchParam.mjs.map +1 -1
- package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs +2 -2
- package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs.map +1 -1
- package/dist/esm/hooks/useVisibility/useVisibility.mjs +25 -17
- package/dist/esm/hooks/useVisibility/useVisibility.mjs.map +1 -1
- package/dist/esm/index.mjs +98 -96
- package/dist/esm/utils/helpers/isTarget.mjs +10 -8
- package/dist/esm/utils/helpers/isTarget.mjs.map +1 -1
- package/dist/types/hooks/useBreakpoints/useBreakpoints.d.ts +1 -11
- package/dist/types/hooks/useClickOutside/useClickOutside.d.ts +1 -1
- package/dist/types/hooks/useHover/useHover.d.ts +6 -6
- package/dist/types/hooks/useRefState/useRefState.d.ts +1 -0
- package/dist/types/utils/helpers/isTarget.d.ts +9 -1
- package/package.json +89 -89
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
import { useState as m, useEffect as v } from "react";
|
|
2
2
|
import { useRefState as L } from "../useRefState/useRefState.mjs";
|
|
3
|
-
import { isTarget as
|
|
4
|
-
const R = (...o) => {
|
|
5
|
-
const
|
|
3
|
+
import { isTarget as s } from "../../utils/helpers/isTarget.mjs";
|
|
4
|
+
const R = ((...o) => {
|
|
5
|
+
const e = s(o[0]) ? o[0] : void 0, r = e ? o[1] : o[0], c = r?.axis ?? "vertical", i = L(), [u, d] = m(!1);
|
|
6
6
|
return v(() => {
|
|
7
|
-
if (!
|
|
8
|
-
const
|
|
9
|
-
if (!
|
|
10
|
-
const t = r?.root ?
|
|
11
|
-
if (
|
|
12
|
-
const
|
|
13
|
-
d(
|
|
7
|
+
if (!e && !i.state) return;
|
|
8
|
+
const l = e ? s.getElement(e) : i.current;
|
|
9
|
+
if (!l) return;
|
|
10
|
+
const t = r?.root ? s.getElement(r.root) : document, a = l.getBoundingClientRect().top + t.scrollTop - t.getBoundingClientRect().top, g = l.getBoundingClientRect().left + t.scrollLeft - t.getBoundingClientRect().left, n = () => {
|
|
11
|
+
if (c === "vertical") {
|
|
12
|
+
const f = t.scrollTop;
|
|
13
|
+
d(f >= a);
|
|
14
14
|
}
|
|
15
|
-
if (
|
|
16
|
-
const
|
|
17
|
-
d(
|
|
15
|
+
if (c === "horizontal") {
|
|
16
|
+
const f = t.scrollLeft;
|
|
17
|
+
d(f >= g);
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
return t.addEventListener("scroll",
|
|
21
|
-
t.removeEventListener("scroll",
|
|
20
|
+
return t.addEventListener("scroll", n), window.addEventListener("resize", n), window.addEventListener("orientationchange", n), n(), () => {
|
|
21
|
+
t.removeEventListener("scroll", n), window.removeEventListener("resize", n), window.removeEventListener("orientationchange", n);
|
|
22
22
|
};
|
|
23
|
-
}, [
|
|
23
|
+
}, [e, i.state, s.getRefState(e), c, r?.root]), e ? u : {
|
|
24
24
|
stuck: u,
|
|
25
25
|
ref: i
|
|
26
26
|
};
|
|
27
|
-
};
|
|
27
|
+
});
|
|
28
28
|
export {
|
|
29
29
|
R as useSticky
|
|
30
30
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSticky.mjs","sources":["../../../../src/hooks/useSticky/useSticky.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The use sticky return type */\nexport interface UseStickyReturn {\n stuck: boolean;\n}\n\n/** The use sticky axis type */\nexport type UseStickyAxis = 'horizontal' | 'vertical';\n\n/** The use sticky options type */\nexport interface UseStickyOptions {\n axis?: UseStickyAxis;\n root?: HookTarget;\n}\n\nexport interface UseSticky {\n (target: HookTarget, options?: UseStickyOptions): boolean;\n\n <Target extends Element>(\n options?: UseStickyOptions,\n target?: never\n ): {\n ref: StateRef<Target>;\n } & UseStickyReturn;\n}\n\n/**\n * @name UseSticky\n * @description - Hook that allows you to detect that your sticky component is stuck\n * @category Elements\n * @usage low\n *\n * @overload\n * @param {HookTarget} target The target sticky element\n * @param {UseStickyAxis} [options.axis='vertical'] The axis of motion of the sticky component\n * @param {UseStickyRoot} [options.root=document] The element that contains your sticky component\n * @returns {UseStickyReturn} The state of the sticky\n *\n * @example\n * const stuck = useSticky(ref);\n *\n * @overload\n * @param {UseStickyAxis} [options.axis='vertical'] The axis of motion of the sticky component\n * @param {UseStickyRoot} [options.root=document] The element that contains your sticky component\n * @returns {{ stickyRef: StateRef<Target> } & UseStickyReturn} The state of the sticky\n *\n * @example\n * const { stuck, ref } = useSticky();\n */\nexport const useSticky = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = (target ? params[1] : params[0]) as UseStickyOptions;\n const axis = options?.axis ?? 'vertical';\n\n const internalRef = useRefState<Element>();\n const [stuck, setStuck] = useState(false);\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (target ? isTarget.getElement(target) : internalRef.current) as Element;\n\n if (!element) return;\n\n const root = (options?.root ? isTarget.getElement(options.root) : document) as Element;\n const elementOffsetTop =\n element.getBoundingClientRect().top + root.scrollTop - root.getBoundingClientRect().top;\n const elementOffsetLeft =\n element.getBoundingClientRect().left + root.scrollLeft - root.getBoundingClientRect().left;\n\n const onSticky = () => {\n if (axis === 'vertical') {\n const scrollTop = root.scrollTop;\n setStuck(scrollTop >= elementOffsetTop);\n }\n\n if (axis === 'horizontal') {\n const scrollLeft = root.scrollLeft;\n setStuck(scrollLeft >= elementOffsetLeft);\n }\n };\n\n root.addEventListener('scroll', onSticky);\n window.addEventListener('resize', onSticky);\n window.addEventListener('orientationchange', onSticky);\n\n onSticky();\n\n return () => {\n root.removeEventListener('scroll', onSticky);\n window.removeEventListener('resize', onSticky);\n window.removeEventListener('orientationchange', onSticky);\n };\n }, [target, internalRef.state, axis, options?.root]);\n\n if (target) return stuck;\n return {\n stuck,\n ref: internalRef\n };\n}) as UseSticky;\n"],"names":["useSticky","params","target","isTarget","options","axis","internalRef","useRefState","stuck","setStuck","useState","useEffect","element","root","elementOffsetTop","elementOffsetLeft","onSticky","scrollTop","scrollLeft"],"mappings":";;;AA0DO,MAAMA,
|
|
1
|
+
{"version":3,"file":"useSticky.mjs","sources":["../../../../src/hooks/useSticky/useSticky.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The use sticky return type */\nexport interface UseStickyReturn {\n stuck: boolean;\n}\n\n/** The use sticky axis type */\nexport type UseStickyAxis = 'horizontal' | 'vertical';\n\n/** The use sticky options type */\nexport interface UseStickyOptions {\n axis?: UseStickyAxis;\n root?: HookTarget;\n}\n\nexport interface UseSticky {\n (target: HookTarget, options?: UseStickyOptions): boolean;\n\n <Target extends Element>(\n options?: UseStickyOptions,\n target?: never\n ): {\n ref: StateRef<Target>;\n } & UseStickyReturn;\n}\n\n/**\n * @name UseSticky\n * @description - Hook that allows you to detect that your sticky component is stuck\n * @category Elements\n * @usage low\n *\n * @overload\n * @param {HookTarget} target The target sticky element\n * @param {UseStickyAxis} [options.axis='vertical'] The axis of motion of the sticky component\n * @param {UseStickyRoot} [options.root=document] The element that contains your sticky component\n * @returns {UseStickyReturn} The state of the sticky\n *\n * @example\n * const stuck = useSticky(ref);\n *\n * @overload\n * @param {UseStickyAxis} [options.axis='vertical'] The axis of motion of the sticky component\n * @param {UseStickyRoot} [options.root=document] The element that contains your sticky component\n * @returns {{ stickyRef: StateRef<Target> } & UseStickyReturn} The state of the sticky\n *\n * @example\n * const { stuck, ref } = useSticky();\n */\nexport const useSticky = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = (target ? params[1] : params[0]) as UseStickyOptions;\n const axis = options?.axis ?? 'vertical';\n\n const internalRef = useRefState<Element>();\n const [stuck, setStuck] = useState(false);\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (target ? isTarget.getElement(target) : internalRef.current) as Element;\n\n if (!element) return;\n\n const root = (options?.root ? isTarget.getElement(options.root) : document) as Element;\n const elementOffsetTop =\n element.getBoundingClientRect().top + root.scrollTop - root.getBoundingClientRect().top;\n const elementOffsetLeft =\n element.getBoundingClientRect().left + root.scrollLeft - root.getBoundingClientRect().left;\n\n const onSticky = () => {\n if (axis === 'vertical') {\n const scrollTop = root.scrollTop;\n setStuck(scrollTop >= elementOffsetTop);\n }\n\n if (axis === 'horizontal') {\n const scrollLeft = root.scrollLeft;\n setStuck(scrollLeft >= elementOffsetLeft);\n }\n };\n\n root.addEventListener('scroll', onSticky);\n window.addEventListener('resize', onSticky);\n window.addEventListener('orientationchange', onSticky);\n\n onSticky();\n\n return () => {\n root.removeEventListener('scroll', onSticky);\n window.removeEventListener('resize', onSticky);\n window.removeEventListener('orientationchange', onSticky);\n };\n }, [target, internalRef.state, isTarget.getRefState(target), axis, options?.root]);\n\n if (target) return stuck;\n return {\n stuck,\n ref: internalRef\n };\n}) as UseSticky;\n"],"names":["useSticky","params","target","isTarget","options","axis","internalRef","useRefState","stuck","setStuck","useState","useEffect","element","root","elementOffsetTop","elementOffsetLeft","onSticky","scrollTop","scrollLeft"],"mappings":";;;AA0DO,MAAMA,KAAa,IAAIC,MAAkB;AAC9C,QAAMC,IAAUC,EAASF,EAAO,CAAC,CAAC,IAAIA,EAAO,CAAC,IAAI,QAC5CG,IAAWF,IAASD,EAAO,CAAC,IAAIA,EAAO,CAAC,GACxCI,IAAOD,GAAS,QAAQ,YAExBE,IAAcC,EAAA,GACd,CAACC,GAAOC,CAAQ,IAAIC,EAAS,EAAK;AAwCxC,SAtCAC,EAAU,MAAM;AACd,QAAI,CAACT,KAAU,CAACI,EAAY,MAAO;AAEnC,UAAMM,IAAWV,IAASC,EAAS,WAAWD,CAAM,IAAII,EAAY;AAEpE,QAAI,CAACM,EAAS;AAEd,UAAMC,IAAQT,GAAS,OAAOD,EAAS,WAAWC,EAAQ,IAAI,IAAI,UAC5DU,IACJF,EAAQ,wBAAwB,MAAMC,EAAK,YAAYA,EAAK,sBAAA,EAAwB,KAChFE,IACJH,EAAQ,wBAAwB,OAAOC,EAAK,aAAaA,EAAK,sBAAA,EAAwB,MAElFG,IAAW,MAAM;AACrB,UAAIX,MAAS,YAAY;AACvB,cAAMY,IAAYJ,EAAK;AACvB,QAAAJ,EAASQ,KAAaH,CAAgB;AAAA,MAAA;AAGxC,UAAIT,MAAS,cAAc;AACzB,cAAMa,IAAaL,EAAK;AACxB,QAAAJ,EAASS,KAAcH,CAAiB;AAAA,MAAA;AAAA,IAC1C;AAGF,WAAAF,EAAK,iBAAiB,UAAUG,CAAQ,GACxC,OAAO,iBAAiB,UAAUA,CAAQ,GAC1C,OAAO,iBAAiB,qBAAqBA,CAAQ,GAErDA,EAAA,GAEO,MAAM;AACX,MAAAH,EAAK,oBAAoB,UAAUG,CAAQ,GAC3C,OAAO,oBAAoB,UAAUA,CAAQ,GAC7C,OAAO,oBAAoB,qBAAqBA,CAAQ;AAAA,IAAA;AAAA,EAC1D,GACC,CAACd,GAAQI,EAAY,OAAOH,EAAS,YAAYD,CAAM,GAAGG,GAAMD,GAAS,IAAI,CAAC,GAE7EF,IAAeM,IACZ;AAAA,IACL,OAAAA;AAAA,IACA,KAAKF;AAAA,EAAA;AAET;"}
|
|
@@ -10,7 +10,7 @@ const l = (t) => {
|
|
|
10
10
|
};
|
|
11
11
|
const s = Math.floor(t / 86400), d = Math.floor(t % 86400 / 3600), e = Math.floor(t % 3600 / 60), u = Math.floor(t % 60);
|
|
12
12
|
return { days: s, hours: d, minutes: e, seconds: u, count: t };
|
|
13
|
-
}, g = (...t) => {
|
|
13
|
+
}, g = ((...t) => {
|
|
14
14
|
const s = (typeof t[0] == "number" ? t[0] : t[0]?.initialTime) ?? 0, e = (typeof t[0] == "number" ? t[1] : t[0])?.immediately ?? !1, [u, i] = f(l(s)), [c, r] = f(!e && !s);
|
|
15
15
|
return y(() => {
|
|
16
16
|
if (c) return;
|
|
@@ -22,13 +22,13 @@ const l = (t) => {
|
|
|
22
22
|
minutes: n.minutes + 1,
|
|
23
23
|
seconds: 0,
|
|
24
24
|
count: o
|
|
25
|
-
} : o %
|
|
25
|
+
} : o % 3600 === 0 ? {
|
|
26
26
|
...n,
|
|
27
27
|
hours: n.hours + 1,
|
|
28
28
|
minutes: 0,
|
|
29
29
|
seconds: 0,
|
|
30
30
|
count: o
|
|
31
|
-
} : o % (
|
|
31
|
+
} : o % (3600 * 24) === 0 ? {
|
|
32
32
|
...n,
|
|
33
33
|
days: n.days + 1,
|
|
34
34
|
hours: 0,
|
|
@@ -51,7 +51,7 @@ const l = (t) => {
|
|
|
51
51
|
reset: () => i(l(s)),
|
|
52
52
|
toggle: () => r((a) => !a)
|
|
53
53
|
};
|
|
54
|
-
};
|
|
54
|
+
});
|
|
55
55
|
export {
|
|
56
56
|
g as useStopwatch
|
|
57
57
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStopwatch.mjs","sources":["../../../../src/hooks/useStopwatch/useStopwatch.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nconst getStopwatchTime = (time: number) => {\n if (!time)\n return {\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n count: 0\n };\n\n const days = Math.floor(time / 86400);\n const hours = Math.floor((time % 86400) / 3600);\n const minutes = Math.floor((time % 3600) / 60);\n const seconds = Math.floor(time % 60);\n\n return { days, hours, minutes, seconds, count: time };\n};\n\n/** The use stopwatch return type */\nexport interface UseStopwatchReturn {\n /** The total count of the stopwatch */\n count: number;\n /** The day count of the stopwatch */\n days: number;\n /** The hour count of the stopwatch */\n hours: number;\n /** The minute count of the stopwatch */\n minutes: number;\n /** The over state of the stopwatch */\n over: boolean;\n /** The paused state of the stopwatch */\n paused: boolean;\n /** The second count of the stopwatch */\n seconds: number;\n /** The function to pause the stopwatch */\n pause: () => void;\n /** The function to reset the stopwatch */\n reset: () => void;\n /** The function to start the stopwatch */\n start: () => void;\n /** The function to toggle the stopwatch */\n toggle: () => void;\n}\n\n/** The use stopwatch options */\nexport interface UseStopwatchOptions {\n /** The immediately state of the timer */\n immediately?: boolean;\n}\n\ninterface UseStopwatch {\n (initialTime?: number, options?: UseStopwatchOptions): UseStopwatchReturn;\n (options?: UseStopwatchOptions & { initialTime?: number }): UseStopwatchReturn;\n}\n/**\n * @name useStopwatch\n * @description - Hook that creates a stopwatch functionality\n * @category Time\n * @usage high\n *\n * @overload\n * @param {number} [initialTime=0] The initial time of the timer\n * @param {boolean} [options.enabled=true] The enabled state of the timer\n * @returns {UseStopwatchReturn} An object containing the current time and functions to interact with the timer\n *\n * @example\n * const { seconds, minutes, start, pause, reset } = useStopwatch(1000, { enabled: false });\n *\n * @overload\n * @param {number} [options.initialTime=0] -The initial time of the timer\n * @param {boolean} [options.enabled=true] The enabled state of the timer\n * @returns {UseStopwatchReturn} An object containing the current time and functions to interact with the timer\n *\n * @example\n * const { seconds, minutes, start, pause, reset } = useStopwatch({ initialTime: 1000, enabled: false });\n */\nexport const useStopwatch = ((...params: any[]) => {\n const initialTime =\n (typeof params[0] === 'number'\n ? (params[0] as number | undefined)\n : (params[0] as UseStopwatchOptions & { initialTime?: number })?.initialTime) ?? 0;\n\n const options =\n typeof params[0] === 'number'\n ? (params[1] as UseStopwatchOptions | undefined)\n : (params[0] as (UseStopwatchOptions & { initialTime?: number }) | undefined);\n\n const immediately = options?.immediately ?? false;\n\n const [time, setTime] = useState(getStopwatchTime(initialTime));\n const [paused, setPaused] = useState(!immediately && !initialTime);\n\n useEffect(() => {\n if (paused) return;\n const onInterval = () => {\n setTime((prevTime) => {\n const updatedCount = prevTime.count + 1;\n\n if (updatedCount % 60 === 0) {\n return {\n ...prevTime,\n minutes: prevTime.minutes + 1,\n seconds: 0,\n count: updatedCount\n };\n }\n\n if (updatedCount % (60 * 60) === 0) {\n return {\n ...prevTime,\n hours: prevTime.hours + 1,\n minutes: 0,\n seconds: 0,\n count: updatedCount\n };\n }\n\n if (updatedCount % (60 * 60 * 24) === 0) {\n return {\n ...prevTime,\n days: prevTime.days + 1,\n hours: 0,\n minutes: 0,\n seconds: 0,\n count: updatedCount\n };\n }\n\n return {\n ...prevTime,\n seconds: prevTime.seconds + 1,\n count: updatedCount\n };\n });\n };\n\n const interval = setInterval(() => onInterval(), 1000);\n return () => clearInterval(interval);\n }, [paused]);\n\n return {\n ...time,\n paused,\n pause: () => setPaused(true),\n start: () => setPaused(false),\n reset: () => setTime(getStopwatchTime(initialTime)),\n toggle: () => setPaused((prevPause) => !prevPause)\n };\n}) as UseStopwatch;\n"],"names":["getStopwatchTime","time","days","hours","minutes","seconds","useStopwatch","params","initialTime","immediately","setTime","useState","paused","setPaused","useEffect","onInterval","prevTime","updatedCount","interval","prevPause"],"mappings":";AAEA,MAAMA,IAAmB,CAACC,MAAiB;AACzC,MAAI,CAACA;AACH,WAAO;AAAA,MACL,MAAM;AAAA,MACN,OAAO;AAAA,MACP,SAAS;AAAA,MACT,SAAS;AAAA,MACT,OAAO;AAAA,IAAA;AAGX,QAAMC,IAAO,KAAK,MAAMD,IAAO,KAAK,GAC9BE,IAAQ,KAAK,MAAOF,IAAO,QAAS,IAAI,GACxCG,IAAU,KAAK,MAAOH,IAAO,OAAQ,EAAE,GACvCI,IAAU,KAAK,MAAMJ,IAAO,EAAE;AAEpC,SAAO,EAAE,MAAAC,GAAM,OAAAC,GAAO,SAAAC,GAAS,SAAAC,GAAS,OAAOJ,EAAA;AACjD,GA4DaK,
|
|
1
|
+
{"version":3,"file":"useStopwatch.mjs","sources":["../../../../src/hooks/useStopwatch/useStopwatch.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nconst getStopwatchTime = (time: number) => {\n if (!time)\n return {\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n count: 0\n };\n\n const days = Math.floor(time / 86400);\n const hours = Math.floor((time % 86400) / 3600);\n const minutes = Math.floor((time % 3600) / 60);\n const seconds = Math.floor(time % 60);\n\n return { days, hours, minutes, seconds, count: time };\n};\n\n/** The use stopwatch return type */\nexport interface UseStopwatchReturn {\n /** The total count of the stopwatch */\n count: number;\n /** The day count of the stopwatch */\n days: number;\n /** The hour count of the stopwatch */\n hours: number;\n /** The minute count of the stopwatch */\n minutes: number;\n /** The over state of the stopwatch */\n over: boolean;\n /** The paused state of the stopwatch */\n paused: boolean;\n /** The second count of the stopwatch */\n seconds: number;\n /** The function to pause the stopwatch */\n pause: () => void;\n /** The function to reset the stopwatch */\n reset: () => void;\n /** The function to start the stopwatch */\n start: () => void;\n /** The function to toggle the stopwatch */\n toggle: () => void;\n}\n\n/** The use stopwatch options */\nexport interface UseStopwatchOptions {\n /** The immediately state of the timer */\n immediately?: boolean;\n}\n\ninterface UseStopwatch {\n (initialTime?: number, options?: UseStopwatchOptions): UseStopwatchReturn;\n (options?: UseStopwatchOptions & { initialTime?: number }): UseStopwatchReturn;\n}\n/**\n * @name useStopwatch\n * @description - Hook that creates a stopwatch functionality\n * @category Time\n * @usage high\n *\n * @overload\n * @param {number} [initialTime=0] The initial time of the timer\n * @param {boolean} [options.enabled=true] The enabled state of the timer\n * @returns {UseStopwatchReturn} An object containing the current time and functions to interact with the timer\n *\n * @example\n * const { seconds, minutes, start, pause, reset } = useStopwatch(1000, { enabled: false });\n *\n * @overload\n * @param {number} [options.initialTime=0] -The initial time of the timer\n * @param {boolean} [options.enabled=true] The enabled state of the timer\n * @returns {UseStopwatchReturn} An object containing the current time and functions to interact with the timer\n *\n * @example\n * const { seconds, minutes, start, pause, reset } = useStopwatch({ initialTime: 1000, enabled: false });\n */\nexport const useStopwatch = ((...params: any[]) => {\n const initialTime =\n (typeof params[0] === 'number'\n ? (params[0] as number | undefined)\n : (params[0] as UseStopwatchOptions & { initialTime?: number })?.initialTime) ?? 0;\n\n const options =\n typeof params[0] === 'number'\n ? (params[1] as UseStopwatchOptions | undefined)\n : (params[0] as (UseStopwatchOptions & { initialTime?: number }) | undefined);\n\n const immediately = options?.immediately ?? false;\n\n const [time, setTime] = useState(getStopwatchTime(initialTime));\n const [paused, setPaused] = useState(!immediately && !initialTime);\n\n useEffect(() => {\n if (paused) return;\n const onInterval = () => {\n setTime((prevTime) => {\n const updatedCount = prevTime.count + 1;\n\n if (updatedCount % 60 === 0) {\n return {\n ...prevTime,\n minutes: prevTime.minutes + 1,\n seconds: 0,\n count: updatedCount\n };\n }\n\n if (updatedCount % (60 * 60) === 0) {\n return {\n ...prevTime,\n hours: prevTime.hours + 1,\n minutes: 0,\n seconds: 0,\n count: updatedCount\n };\n }\n\n if (updatedCount % (60 * 60 * 24) === 0) {\n return {\n ...prevTime,\n days: prevTime.days + 1,\n hours: 0,\n minutes: 0,\n seconds: 0,\n count: updatedCount\n };\n }\n\n return {\n ...prevTime,\n seconds: prevTime.seconds + 1,\n count: updatedCount\n };\n });\n };\n\n const interval = setInterval(() => onInterval(), 1000);\n return () => clearInterval(interval);\n }, [paused]);\n\n return {\n ...time,\n paused,\n pause: () => setPaused(true),\n start: () => setPaused(false),\n reset: () => setTime(getStopwatchTime(initialTime)),\n toggle: () => setPaused((prevPause) => !prevPause)\n };\n}) as UseStopwatch;\n"],"names":["getStopwatchTime","time","days","hours","minutes","seconds","useStopwatch","params","initialTime","immediately","setTime","useState","paused","setPaused","useEffect","onInterval","prevTime","updatedCount","interval","prevPause"],"mappings":";AAEA,MAAMA,IAAmB,CAACC,MAAiB;AACzC,MAAI,CAACA;AACH,WAAO;AAAA,MACL,MAAM;AAAA,MACN,OAAO;AAAA,MACP,SAAS;AAAA,MACT,SAAS;AAAA,MACT,OAAO;AAAA,IAAA;AAGX,QAAMC,IAAO,KAAK,MAAMD,IAAO,KAAK,GAC9BE,IAAQ,KAAK,MAAOF,IAAO,QAAS,IAAI,GACxCG,IAAU,KAAK,MAAOH,IAAO,OAAQ,EAAE,GACvCI,IAAU,KAAK,MAAMJ,IAAO,EAAE;AAEpC,SAAO,EAAE,MAAAC,GAAM,OAAAC,GAAO,SAAAC,GAAS,SAAAC,GAAS,OAAOJ,EAAA;AACjD,GA4DaK,KAAgB,IAAIC,MAAkB;AACjD,QAAMC,KACH,OAAOD,EAAO,CAAC,KAAM,WACjBA,EAAO,CAAC,IACRA,EAAO,CAAC,GAAsD,gBAAgB,GAO/EE,KAJJ,OAAOF,EAAO,CAAC,KAAM,WAChBA,EAAO,CAAC,IACRA,EAAO,CAAC,IAEc,eAAe,IAEtC,CAACN,GAAMS,CAAO,IAAIC,EAASX,EAAiBQ,CAAW,CAAC,GACxD,CAACI,GAAQC,CAAS,IAAIF,EAAS,CAACF,KAAe,CAACD,CAAW;AAEjE,SAAAM,EAAU,MAAM;AACd,QAAIF,EAAQ;AACZ,UAAMG,IAAa,MAAM;AACvB,MAAAL,EAAQ,CAACM,MAAa;AACpB,cAAMC,IAAeD,EAAS,QAAQ;AAEtC,eAAIC,IAAe,OAAO,IACjB;AAAA,UACL,GAAGD;AAAA,UACH,SAASA,EAAS,UAAU;AAAA,UAC5B,SAAS;AAAA,UACT,OAAOC;AAAA,QAAA,IAIPA,IAAgB,SAAa,IACxB;AAAA,UACL,GAAGD;AAAA,UACH,OAAOA,EAAS,QAAQ;AAAA,UACxB,SAAS;AAAA,UACT,SAAS;AAAA,UACT,OAAOC;AAAA,QAAA,IAIPA,KAAgB,OAAU,QAAQ,IAC7B;AAAA,UACL,GAAGD;AAAA,UACH,MAAMA,EAAS,OAAO;AAAA,UACtB,OAAO;AAAA,UACP,SAAS;AAAA,UACT,SAAS;AAAA,UACT,OAAOC;AAAA,QAAA,IAIJ;AAAA,UACL,GAAGD;AAAA,UACH,SAASA,EAAS,UAAU;AAAA,UAC5B,OAAOC;AAAA,QAAA;AAAA,MACT,CACD;AAAA,IAAA,GAGGC,IAAW,YAAY,MAAMH,EAAA,GAAc,GAAI;AACrD,WAAO,MAAM,cAAcG,CAAQ;AAAA,EAAA,GAClC,CAACN,CAAM,CAAC,GAEJ;AAAA,IACL,GAAGX;AAAA,IACH,QAAAW;AAAA,IACA,OAAO,MAAMC,EAAU,EAAI;AAAA,IAC3B,OAAO,MAAMA,EAAU,EAAK;AAAA,IAC5B,OAAO,MAAMH,EAAQV,EAAiBQ,CAAW,CAAC;AAAA,IAClD,QAAQ,MAAMK,EAAU,CAACM,MAAc,CAACA,CAAS;AAAA,EAAA;AAErD;"}
|
|
@@ -14,7 +14,7 @@ const c = "reactuse-storage", f = (t) => window.dispatchEvent(new StorageEvent(c
|
|
|
14
14
|
const o = t.getItem(n);
|
|
15
15
|
if (o)
|
|
16
16
|
return o;
|
|
17
|
-
}, I = (t, n) => {
|
|
17
|
+
}, I = ((t, n) => {
|
|
18
18
|
const o = typeof n == "object" && n && ("serializer" in n || "deserializer" in n || "initialValue" in n || "storage" in n) ? n : void 0, r = o ? o?.initialValue : n;
|
|
19
19
|
if (typeof window > "u")
|
|
20
20
|
return {
|
|
@@ -51,7 +51,7 @@ const c = "reactuse-storage", f = (t) => window.dispatchEvent(new StorageEvent(c
|
|
|
51
51
|
set: g,
|
|
52
52
|
remove: v
|
|
53
53
|
};
|
|
54
|
-
};
|
|
54
|
+
});
|
|
55
55
|
export {
|
|
56
56
|
c as STORAGE_EVENT,
|
|
57
57
|
f as dispatchStorageEvent,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStorage.mjs","sources":["../../../../src/hooks/useStorage/useStorage.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/* The use storage initial value type */\nexport type UseStorageInitialValue<Value> = (() => Value) | Value;\n\n/* The use storage options type */\nexport interface UseStorageOptions<Value> {\n /* The initial value of the storage */\n initialValue?: UseStorageInitialValue<Value>;\n /* The storage to be used */\n storage?: Storage;\n /* The deserializer function to be invoked */\n deserializer?: (value: string) => Value;\n /* The serializer function to be invoked */\n serializer?: (value: Value) => string;\n}\n\n/* The use storage return type */\nexport interface UseStorageReturn<Value> {\n /* The value of the storage */\n value: Value;\n /* The error state of the storage */\n remove: () => void;\n /* The loading state of the storage */\n set: (value: Value) => void;\n}\n\nexport interface UseStorage {\n <Value>(key: string, options?: UseStorageOptions<Value>): UseStorageReturn<Value | undefined>;\n\n <Value>(\n key: string,\n initialValue?: UseStorageInitialValue<Value>\n ): UseStorageReturn<Value | undefined>;\n}\n\nexport const STORAGE_EVENT = 'reactuse-storage';\n\nexport const dispatchStorageEvent = (params: Partial<StorageEvent>) =>\n window.dispatchEvent(new StorageEvent(STORAGE_EVENT, params));\n\nconst setStorageItem = (storage: Storage, key: string, value: string) => {\n const oldValue = storage.getItem(key);\n\n storage.setItem(key, value);\n dispatchStorageEvent({\n key,\n oldValue,\n newValue: value,\n storageArea: storage\n });\n};\n\nconst removeStorageItem = (storage: Storage, key: string) => {\n const oldValue = storage.getItem(key);\n\n storage.removeItem(key);\n dispatchStorageEvent({ key, oldValue, newValue: null, storageArea: storage });\n};\n\nconst getStorageItem = (storage: Storage, key: string) => {\n const value = storage.getItem(key);\n if (!value) return undefined;\n return value;\n};\n\n/**\n * @name useStorage\n * @description - Hook that manages storage value\n * @category State\n * @usage high\n *\n * @overload\n * @param {string} key The key of the storage\n * @param {UseStorageInitialValue<Value>} [initialValue] The initial value of the storage\n * @returns {UseStorageReturn<Value>} The value and the set function\n *\n * @overload\n * @param {string} key The key of the storage\n * @param {(value: Value) => string} [params.serializer] The serializer function\n * @param {(value: string) => Value} [params.deserializer] The deserializer function\n * @param {Storage} [params.storage] The storage\n * @param {UseStorageInitialValue<Value>} [params.initialValue] The initial value of the storage\n * @returns {UseStorageReturn<Value>} The value and the set function\n *\n * @example\n * const { value, set, remove } = useStorage('key', 'value');\n */\nexport const useStorage = (<Value>(key: string, params?: any): UseStorageReturn<Value> => {\n const options = (\n typeof params === 'object' &&\n params &&\n ('serializer' in params ||\n 'deserializer' in params ||\n 'initialValue' in params ||\n 'storage' in params)\n ? params\n : undefined\n ) as UseStorageOptions<Value>;\n\n const initialValue = (options ? options?.initialValue : params) as UseStorageInitialValue<Value>;\n\n if (typeof window === 'undefined') {\n const value = typeof initialValue === 'function' ? (initialValue as () => any)() : initialValue;\n return {\n value,\n set: () => {},\n remove: () => {}\n };\n }\n\n const serializer = (value: Value) => {\n if (options?.serializer) return options.serializer(value);\n if (typeof value === 'string') return value;\n return JSON.stringify(value);\n };\n\n const storage = options?.storage ?? window?.localStorage;\n\n const set = (value: Value) => setStorageItem(storage, key, serializer(value));\n const remove = () => removeStorageItem(storage, key);\n\n const deserializer = (value: string) => {\n if (options?.deserializer) return options.deserializer(value);\n if (value === 'undefined') return undefined as unknown as Value;\n\n try {\n return JSON.parse(value) as Value;\n } catch {\n return value as Value;\n }\n };\n\n const [value, setValue] = useState<Value | undefined>(() => {\n const storageValue = getStorageItem(storage, key);\n if (storageValue === undefined && initialValue !== undefined) {\n const value =\n typeof initialValue === 'function' ? (initialValue as () => Value)() : initialValue;\n setStorageItem(storage, key, serializer(value));\n return value;\n }\n return storageValue ? deserializer(storageValue) : undefined;\n });\n\n useEffect(() => {\n const onChange = () => {\n const storageValue = getStorageItem(storage, key);\n setValue(storageValue ? deserializer(storageValue) : undefined);\n };\n window.addEventListener(STORAGE_EVENT, onChange);\n return () => window.removeEventListener(STORAGE_EVENT, onChange);\n }, [key]);\n\n return {\n value: value as Value,\n set,\n remove\n };\n}) as UseStorage;\n"],"names":["STORAGE_EVENT","dispatchStorageEvent","params","setStorageItem","storage","key","value","oldValue","removeStorageItem","getStorageItem","useStorage","options","initialValue","serializer","set","remove","deserializer","setValue","useState","storageValue","useEffect","onChange"],"mappings":";AAoCO,MAAMA,IAAgB,oBAEhBC,IAAuB,CAACC,MACnC,OAAO,cAAc,IAAI,aAAaF,GAAeE,CAAM,CAAC,GAExDC,IAAiB,CAACC,GAAkBC,GAAaC,MAAkB;AACvE,QAAMC,IAAWH,EAAQ,QAAQC,CAAG;AAEpC,EAAAD,EAAQ,QAAQC,GAAKC,CAAK,GAC1BL,EAAqB;AAAA,IACnB,KAAAI;AAAA,IACA,UAAAE;AAAA,IACA,UAAUD;AAAA,IACV,aAAaF;AAAA,EAAA,CACd;AACH,GAEMI,IAAoB,CAACJ,GAAkBC,MAAgB;AAC3D,QAAME,IAAWH,EAAQ,QAAQC,CAAG;AAEpC,EAAAD,EAAQ,WAAWC,CAAG,GACtBJ,EAAqB,EAAE,KAAAI,GAAK,UAAAE,GAAU,UAAU,MAAM,aAAaH,GAAS;AAC9E,GAEMK,IAAiB,CAACL,GAAkBC,MAAgB;AACxD,QAAMC,IAAQF,EAAQ,QAAQC,CAAG;AACjC,MAAKC;AACL,WAAOA;AACT,GAwBaI,
|
|
1
|
+
{"version":3,"file":"useStorage.mjs","sources":["../../../../src/hooks/useStorage/useStorage.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/* The use storage initial value type */\nexport type UseStorageInitialValue<Value> = (() => Value) | Value;\n\n/* The use storage options type */\nexport interface UseStorageOptions<Value> {\n /* The initial value of the storage */\n initialValue?: UseStorageInitialValue<Value>;\n /* The storage to be used */\n storage?: Storage;\n /* The deserializer function to be invoked */\n deserializer?: (value: string) => Value;\n /* The serializer function to be invoked */\n serializer?: (value: Value) => string;\n}\n\n/* The use storage return type */\nexport interface UseStorageReturn<Value> {\n /* The value of the storage */\n value: Value;\n /* The error state of the storage */\n remove: () => void;\n /* The loading state of the storage */\n set: (value: Value) => void;\n}\n\nexport interface UseStorage {\n <Value>(key: string, options?: UseStorageOptions<Value>): UseStorageReturn<Value | undefined>;\n\n <Value>(\n key: string,\n initialValue?: UseStorageInitialValue<Value>\n ): UseStorageReturn<Value | undefined>;\n}\n\nexport const STORAGE_EVENT = 'reactuse-storage';\n\nexport const dispatchStorageEvent = (params: Partial<StorageEvent>) =>\n window.dispatchEvent(new StorageEvent(STORAGE_EVENT, params));\n\nconst setStorageItem = (storage: Storage, key: string, value: string) => {\n const oldValue = storage.getItem(key);\n\n storage.setItem(key, value);\n dispatchStorageEvent({\n key,\n oldValue,\n newValue: value,\n storageArea: storage\n });\n};\n\nconst removeStorageItem = (storage: Storage, key: string) => {\n const oldValue = storage.getItem(key);\n\n storage.removeItem(key);\n dispatchStorageEvent({ key, oldValue, newValue: null, storageArea: storage });\n};\n\nconst getStorageItem = (storage: Storage, key: string) => {\n const value = storage.getItem(key);\n if (!value) return undefined;\n return value;\n};\n\n/**\n * @name useStorage\n * @description - Hook that manages storage value\n * @category State\n * @usage high\n *\n * @overload\n * @param {string} key The key of the storage\n * @param {UseStorageInitialValue<Value>} [initialValue] The initial value of the storage\n * @returns {UseStorageReturn<Value>} The value and the set function\n *\n * @overload\n * @param {string} key The key of the storage\n * @param {(value: Value) => string} [params.serializer] The serializer function\n * @param {(value: string) => Value} [params.deserializer] The deserializer function\n * @param {Storage} [params.storage] The storage\n * @param {UseStorageInitialValue<Value>} [params.initialValue] The initial value of the storage\n * @returns {UseStorageReturn<Value>} The value and the set function\n *\n * @example\n * const { value, set, remove } = useStorage('key', 'value');\n */\nexport const useStorage = (<Value>(key: string, params?: any): UseStorageReturn<Value> => {\n const options = (\n typeof params === 'object' &&\n params &&\n ('serializer' in params ||\n 'deserializer' in params ||\n 'initialValue' in params ||\n 'storage' in params)\n ? params\n : undefined\n ) as UseStorageOptions<Value>;\n\n const initialValue = (options ? options?.initialValue : params) as UseStorageInitialValue<Value>;\n\n if (typeof window === 'undefined') {\n const value = typeof initialValue === 'function' ? (initialValue as () => any)() : initialValue;\n return {\n value,\n set: () => {},\n remove: () => {}\n };\n }\n\n const serializer = (value: Value) => {\n if (options?.serializer) return options.serializer(value);\n if (typeof value === 'string') return value;\n return JSON.stringify(value);\n };\n\n const storage = options?.storage ?? window?.localStorage;\n\n const set = (value: Value) => setStorageItem(storage, key, serializer(value));\n const remove = () => removeStorageItem(storage, key);\n\n const deserializer = (value: string) => {\n if (options?.deserializer) return options.deserializer(value);\n if (value === 'undefined') return undefined as unknown as Value;\n\n try {\n return JSON.parse(value) as Value;\n } catch {\n return value as Value;\n }\n };\n\n const [value, setValue] = useState<Value | undefined>(() => {\n const storageValue = getStorageItem(storage, key);\n if (storageValue === undefined && initialValue !== undefined) {\n const value =\n typeof initialValue === 'function' ? (initialValue as () => Value)() : initialValue;\n setStorageItem(storage, key, serializer(value));\n return value;\n }\n return storageValue ? deserializer(storageValue) : undefined;\n });\n\n useEffect(() => {\n const onChange = () => {\n const storageValue = getStorageItem(storage, key);\n setValue(storageValue ? deserializer(storageValue) : undefined);\n };\n window.addEventListener(STORAGE_EVENT, onChange);\n return () => window.removeEventListener(STORAGE_EVENT, onChange);\n }, [key]);\n\n return {\n value: value as Value,\n set,\n remove\n };\n}) as UseStorage;\n"],"names":["STORAGE_EVENT","dispatchStorageEvent","params","setStorageItem","storage","key","value","oldValue","removeStorageItem","getStorageItem","useStorage","options","initialValue","serializer","set","remove","deserializer","setValue","useState","storageValue","useEffect","onChange"],"mappings":";AAoCO,MAAMA,IAAgB,oBAEhBC,IAAuB,CAACC,MACnC,OAAO,cAAc,IAAI,aAAaF,GAAeE,CAAM,CAAC,GAExDC,IAAiB,CAACC,GAAkBC,GAAaC,MAAkB;AACvE,QAAMC,IAAWH,EAAQ,QAAQC,CAAG;AAEpC,EAAAD,EAAQ,QAAQC,GAAKC,CAAK,GAC1BL,EAAqB;AAAA,IACnB,KAAAI;AAAA,IACA,UAAAE;AAAA,IACA,UAAUD;AAAA,IACV,aAAaF;AAAA,EAAA,CACd;AACH,GAEMI,IAAoB,CAACJ,GAAkBC,MAAgB;AAC3D,QAAME,IAAWH,EAAQ,QAAQC,CAAG;AAEpC,EAAAD,EAAQ,WAAWC,CAAG,GACtBJ,EAAqB,EAAE,KAAAI,GAAK,UAAAE,GAAU,UAAU,MAAM,aAAaH,GAAS;AAC9E,GAEMK,IAAiB,CAACL,GAAkBC,MAAgB;AACxD,QAAMC,IAAQF,EAAQ,QAAQC,CAAG;AACjC,MAAKC;AACL,WAAOA;AACT,GAwBaI,KAAc,CAAQL,GAAaH,MAA0C;AACxF,QAAMS,IACJ,OAAOT,KAAW,YAClBA,MACC,gBAAgBA,KACf,kBAAkBA,KAClB,kBAAkBA,KAClB,aAAaA,KACXA,IACA,QAGAU,IAAgBD,IAAUA,GAAS,eAAeT;AAExD,MAAI,OAAO,SAAW;AAEpB,WAAO;AAAA,MACL,OAFY,OAAOU,KAAiB,aAAcA,MAA+BA;AAAA,MAGjF,KAAK,MAAM;AAAA,MAAA;AAAA,MACX,QAAQ,MAAM;AAAA,MAAA;AAAA,IAAC;AAInB,QAAMC,IAAa,CAACP,MACdK,GAAS,aAAmBA,EAAQ,WAAWL,CAAK,IACpD,OAAOA,KAAU,WAAiBA,IAC/B,KAAK,UAAUA,CAAK,GAGvBF,IAAUO,GAAS,WAAW,QAAQ,cAEtCG,IAAM,CAACR,MAAiBH,EAAeC,GAASC,GAAKQ,EAAWP,CAAK,CAAC,GACtES,IAAS,MAAMP,EAAkBJ,GAASC,CAAG,GAE7CW,IAAe,CAACV,MAAkB;AACtC,QAAIK,GAAS,aAAc,QAAOA,EAAQ,aAAaL,CAAK;AAC5D,QAAIA,MAAU;AAEd,UAAI;AACF,eAAO,KAAK,MAAMA,CAAK;AAAA,MAAA,QACjB;AACN,eAAOA;AAAAA,MAAA;AAAA,EACT,GAGI,CAACA,GAAOW,CAAQ,IAAIC,EAA4B,MAAM;AAC1D,UAAMC,IAAeV,EAAeL,GAASC,CAAG;AAChD,QAAIc,MAAiB,UAAaP,MAAiB,QAAW;AAC5D,YAAMN,IACJ,OAAOM,KAAiB,aAAcA,MAAiCA;AACzE,aAAAT,EAAeC,GAASC,GAAKQ,EAAWP,CAAK,CAAC,GACvCA;AAAAA,IAAA;AAET,WAAOa,IAAeH,EAAaG,CAAY,IAAI;AAAA,EAAA,CACpD;AAED,SAAAC,EAAU,MAAM;AACd,UAAMC,IAAW,MAAM;AACrB,YAAMF,IAAeV,EAAeL,GAASC,CAAG;AAChD,MAAAY,EAASE,IAAeH,EAAaG,CAAY,IAAI,MAAS;AAAA,IAAA;AAEhE,kBAAO,iBAAiBnB,GAAeqB,CAAQ,GACxC,MAAM,OAAO,oBAAoBrB,GAAeqB,CAAQ;AAAA,EAAA,GAC9D,CAAChB,CAAG,CAAC,GAED;AAAA,IACL,OAAAC;AAAA,IACA,KAAAQ;AAAA,IACA,QAAAC;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import { useRef as a, useState as g, useEffect as v } from "react";
|
|
2
|
-
import { useRefState as
|
|
3
|
-
import { isTarget as
|
|
4
|
-
const
|
|
5
|
-
const e =
|
|
2
|
+
import { useRefState as R } from "../useRefState/useRefState.mjs";
|
|
3
|
+
import { isTarget as o } from "../../utils/helpers/isTarget.mjs";
|
|
4
|
+
const E = ((...i) => {
|
|
5
|
+
const e = o(i[0]) ? i[0] : void 0, s = (e ? i[1] : i[0]) ?? "ltr", r = R(), n = a(null), u = () => typeof window > "u" ? s : (e ? o.getElement(e) : r.current)?.getAttribute("dir") ?? s, [f, c] = g(u()), l = () => {
|
|
6
6
|
n.current && n.current?.removeAttribute("dir");
|
|
7
7
|
}, m = (t) => {
|
|
8
|
-
n.current && (
|
|
8
|
+
n.current && (c(t), n.current.setAttribute("dir", t));
|
|
9
9
|
};
|
|
10
10
|
return v(() => {
|
|
11
11
|
if (!e && !r.state) return;
|
|
12
|
-
const t = (e ?
|
|
12
|
+
const t = (e ? o.getElement(e) : r.current) ?? document.querySelector("html");
|
|
13
13
|
if (!t) return;
|
|
14
14
|
n.current = t;
|
|
15
|
-
const d =
|
|
16
|
-
t.setAttribute("dir", d),
|
|
17
|
-
const b = new MutationObserver(() => u(
|
|
15
|
+
const d = u();
|
|
16
|
+
t.setAttribute("dir", d), c(d);
|
|
17
|
+
const b = new MutationObserver(() => c(u()));
|
|
18
18
|
return b.observe(t, { attributes: !0 }), () => {
|
|
19
19
|
b.disconnect();
|
|
20
20
|
};
|
|
21
|
-
}, [r.state, e]), e ? { value: f, set: m, remove: l } : {
|
|
21
|
+
}, [r.state, e, o.getRefState(e)]), e ? { value: f, set: m, remove: l } : {
|
|
22
22
|
ref: r,
|
|
23
23
|
value: f,
|
|
24
24
|
set: m,
|
|
25
25
|
remove: l
|
|
26
26
|
};
|
|
27
|
-
};
|
|
27
|
+
});
|
|
28
28
|
export {
|
|
29
|
-
|
|
29
|
+
E as useTextDirection
|
|
30
30
|
};
|
|
31
31
|
//# sourceMappingURL=useTextDirection.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTextDirection.mjs","sources":["../../../../src/hooks/useTextDirection/useTextDirection.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The use text direction value type */\nexport type UseTextDirectionValue = 'auto' | 'ltr' | 'rtl';\n\n/** The use text direction return type */\nexport interface UseTextDirectionReturn {\n /** The current direction */\n value: UseTextDirectionValue;\n /*** The function to remove the direction */\n remove: () => void;\n /*** The function to set the direction */\n set: (value: UseTextDirectionValue | null) => void;\n}\n\nexport interface UseTextDirection {\n (target: HookTarget, initialValue?: UseTextDirectionValue): UseTextDirectionReturn;\n\n <Target extends Element>(\n initialValue?: UseTextDirectionValue,\n target?: never\n ): UseTextDirectionReturn & { ref: StateRef<Target> };\n}\n\n/**\n * @name useTextDirection\n * @description - Hook that can get and set the direction of the element\n * @category Elements\n * @usage medium\n *\n * @overload\n * @param {HookTarget} [target=document.querySelector('html')] The target element to observe\n * @param {UseTextDirectionValue} [initialValue = 'ltr'] The initial direction of the element\n * @returns {UseTextDirectionReturn} An object containing the current text direction of the element\n *\n * @example\n * const { value, set, remove } = useTextDirection(ref);\n *\n * @overload\n * @template Target The target element type\n * @param {UseTextDirectionValue} [initialValue = 'ltr'] The initial direction of the element\n * @returns { { ref: StateRef<Target> } & UseTextDirectionReturn } An object containing the current text direction of the element\n *\n * @example\n * const { ref, value, set, remove } = useTextDirection();\n */\nexport const useTextDirection = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const initialValue = ((target ? params[1] : params[0]) as UseTextDirectionValue) ?? 'ltr';\n\n const internalRef = useRefState();\n const elementRef = useRef<Element>(null);\n\n const getDirection = () => {\n if (typeof window === 'undefined') return initialValue;\n const element = (target ? isTarget.getElement(target) : internalRef.current) as Element;\n return (element?.getAttribute('dir') as UseTextDirectionValue) ?? initialValue;\n };\n\n const [value, setValue] = useState<UseTextDirectionValue>(getDirection());\n\n const remove = () => {\n if (!elementRef.current) return;\n\n elementRef.current?.removeAttribute('dir');\n };\n\n const set = (value: UseTextDirectionValue) => {\n if (!elementRef.current) return;\n\n setValue(value);\n elementRef.current.setAttribute('dir', value);\n };\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element =\n ((target ? isTarget.getElement(target) : internalRef.current) as Element) ??\n document.querySelector('html');\n if (!element) return;\n\n elementRef.current = element;\n\n const direction = getDirection();\n element.setAttribute('dir', direction);\n setValue(direction);\n\n const observer = new MutationObserver(() => setValue(getDirection()));\n\n observer.observe(element, { attributes: true });\n\n return () => {\n observer.disconnect();\n };\n }, [internalRef.state, target]);\n\n if (target) return { value, set, remove };\n return {\n ref: internalRef,\n value,\n set,\n remove\n };\n}) as UseTextDirection;\n"],"names":["useTextDirection","params","target","isTarget","initialValue","internalRef","useRefState","elementRef","useRef","getDirection","value","setValue","useState","remove","set","useEffect","element","direction","observer"],"mappings":";;;AAsDO,MAAMA,
|
|
1
|
+
{"version":3,"file":"useTextDirection.mjs","sources":["../../../../src/hooks/useTextDirection/useTextDirection.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The use text direction value type */\nexport type UseTextDirectionValue = 'auto' | 'ltr' | 'rtl';\n\n/** The use text direction return type */\nexport interface UseTextDirectionReturn {\n /** The current direction */\n value: UseTextDirectionValue;\n /*** The function to remove the direction */\n remove: () => void;\n /*** The function to set the direction */\n set: (value: UseTextDirectionValue | null) => void;\n}\n\nexport interface UseTextDirection {\n (target: HookTarget, initialValue?: UseTextDirectionValue): UseTextDirectionReturn;\n\n <Target extends Element>(\n initialValue?: UseTextDirectionValue,\n target?: never\n ): UseTextDirectionReturn & { ref: StateRef<Target> };\n}\n\n/**\n * @name useTextDirection\n * @description - Hook that can get and set the direction of the element\n * @category Elements\n * @usage medium\n *\n * @overload\n * @param {HookTarget} [target=document.querySelector('html')] The target element to observe\n * @param {UseTextDirectionValue} [initialValue = 'ltr'] The initial direction of the element\n * @returns {UseTextDirectionReturn} An object containing the current text direction of the element\n *\n * @example\n * const { value, set, remove } = useTextDirection(ref);\n *\n * @overload\n * @template Target The target element type\n * @param {UseTextDirectionValue} [initialValue = 'ltr'] The initial direction of the element\n * @returns { { ref: StateRef<Target> } & UseTextDirectionReturn } An object containing the current text direction of the element\n *\n * @example\n * const { ref, value, set, remove } = useTextDirection();\n */\nexport const useTextDirection = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const initialValue = ((target ? params[1] : params[0]) as UseTextDirectionValue) ?? 'ltr';\n\n const internalRef = useRefState();\n const elementRef = useRef<Element>(null);\n\n const getDirection = () => {\n if (typeof window === 'undefined') return initialValue;\n const element = (target ? isTarget.getElement(target) : internalRef.current) as Element;\n return (element?.getAttribute('dir') as UseTextDirectionValue) ?? initialValue;\n };\n\n const [value, setValue] = useState<UseTextDirectionValue>(getDirection());\n\n const remove = () => {\n if (!elementRef.current) return;\n\n elementRef.current?.removeAttribute('dir');\n };\n\n const set = (value: UseTextDirectionValue) => {\n if (!elementRef.current) return;\n\n setValue(value);\n elementRef.current.setAttribute('dir', value);\n };\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element =\n ((target ? isTarget.getElement(target) : internalRef.current) as Element) ??\n document.querySelector('html');\n if (!element) return;\n\n elementRef.current = element;\n\n const direction = getDirection();\n element.setAttribute('dir', direction);\n setValue(direction);\n\n const observer = new MutationObserver(() => setValue(getDirection()));\n\n observer.observe(element, { attributes: true });\n\n return () => {\n observer.disconnect();\n };\n }, [internalRef.state, target, isTarget.getRefState(target)]);\n\n if (target) return { value, set, remove };\n return {\n ref: internalRef,\n value,\n set,\n remove\n };\n}) as UseTextDirection;\n"],"names":["useTextDirection","params","target","isTarget","initialValue","internalRef","useRefState","elementRef","useRef","getDirection","value","setValue","useState","remove","set","useEffect","element","direction","observer"],"mappings":";;;AAsDO,MAAMA,KAAoB,IAAIC,MAAkB;AACrD,QAAMC,IAAUC,EAASF,EAAO,CAAC,CAAC,IAAIA,EAAO,CAAC,IAAI,QAC5CG,KAAiBF,IAASD,EAAO,CAAC,IAAIA,EAAO,CAAC,MAAgC,OAE9EI,IAAcC,EAAA,GACdC,IAAaC,EAAgB,IAAI,GAEjCC,IAAe,MACf,OAAO,SAAW,MAAoBL,KACzBF,IAASC,EAAS,WAAWD,CAAM,IAAIG,EAAY,UACnD,aAAa,KAAK,KAA+BD,GAG9D,CAACM,GAAOC,CAAQ,IAAIC,EAAgCH,GAAc,GAElEI,IAAS,MAAM;AACnB,IAAKN,EAAW,WAEhBA,EAAW,SAAS,gBAAgB,KAAK;AAAA,EAAA,GAGrCO,IAAM,CAACJ,MAAiC;AAC5C,IAAKH,EAAW,YAEhBI,EAASD,CAAK,GACdH,EAAW,QAAQ,aAAa,OAAOG,CAAK;AAAA,EAAA;AA0B9C,SAvBAK,EAAU,MAAM;AACd,QAAI,CAACb,KAAU,CAACG,EAAY,MAAO;AAEnC,UAAMW,KACFd,IAASC,EAAS,WAAWD,CAAM,IAAIG,EAAY,YACrD,SAAS,cAAc,MAAM;AAC/B,QAAI,CAACW,EAAS;AAEd,IAAAT,EAAW,UAAUS;AAErB,UAAMC,IAAYR,EAAA;AAClB,IAAAO,EAAQ,aAAa,OAAOC,CAAS,GACrCN,EAASM,CAAS;AAElB,UAAMC,IAAW,IAAI,iBAAiB,MAAMP,EAASF,EAAA,CAAc,CAAC;AAEpE,WAAAS,EAAS,QAAQF,GAAS,EAAE,YAAY,IAAM,GAEvC,MAAM;AACX,MAAAE,EAAS,WAAA;AAAA,IAAW;AAAA,EACtB,GACC,CAACb,EAAY,OAAOH,GAAQC,EAAS,YAAYD,CAAM,CAAC,CAAC,GAExDA,IAAe,EAAE,OAAAQ,GAAO,KAAAI,GAAK,QAAAD,EAAA,IAC1B;AAAA,IACL,KAAKR;AAAA,IACL,OAAAK;AAAA,IACA,KAAAI;AAAA,IACA,QAAAD;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { useState as H, useRef as y, useEffect as
|
|
1
|
+
import { useState as H, useRef as y, useEffect as x } from "react";
|
|
2
2
|
import { useRefState as V } from "../useRefState/useRefState.mjs";
|
|
3
|
-
import { isTarget as
|
|
4
|
-
const d = (...
|
|
5
|
-
const
|
|
3
|
+
import { isTarget as f } from "../../utils/helpers/isTarget.mjs";
|
|
4
|
+
const d = ((...i) => {
|
|
5
|
+
const t = f(i[0]) ? i[0] : void 0, r = t ? i[1] : typeof i[0] == "string" ? { initialValue: i[0] } : i[0], [a, c] = H(r?.initialValue ?? ""), o = V(), u = y(null), s = () => {
|
|
6
6
|
const e = u.current;
|
|
7
7
|
if (!e) return;
|
|
8
|
-
const
|
|
8
|
+
const n = e.style.minHeight, l = e.style.maxHeight;
|
|
9
9
|
e.style.height = "auto", e.style.minHeight = "auto", e.style.maxHeight = "none";
|
|
10
10
|
const g = e.scrollHeight;
|
|
11
|
-
e.style.height = `${g}px`, e.style.minHeight =
|
|
11
|
+
e.style.height = `${g}px`, e.style.minHeight = n, e.style.maxHeight = l, r?.onResize?.();
|
|
12
12
|
};
|
|
13
|
-
|
|
14
|
-
if (!
|
|
15
|
-
const e =
|
|
13
|
+
x(() => {
|
|
14
|
+
if (!t && !o.state) return;
|
|
15
|
+
const e = t ? f.getElement(t) : o.current;
|
|
16
16
|
if (!e) return;
|
|
17
17
|
u.current = e, r?.initialValue && (e.value = r.initialValue), s();
|
|
18
|
-
const
|
|
18
|
+
const n = (g) => {
|
|
19
19
|
const v = g.target.value;
|
|
20
20
|
c(v), requestAnimationFrame(() => {
|
|
21
21
|
s();
|
|
@@ -25,10 +25,10 @@ const d = (...n) => {
|
|
|
25
25
|
s();
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
|
-
return e.addEventListener("input",
|
|
29
|
-
e.removeEventListener("input",
|
|
28
|
+
return e.addEventListener("input", n), e.addEventListener("resize", l), () => {
|
|
29
|
+
e.removeEventListener("input", n), e.removeEventListener("resize", l);
|
|
30
30
|
};
|
|
31
|
-
}, [
|
|
31
|
+
}, [t, o.state, f.getRefState(t), r?.initialValue]), x(() => {
|
|
32
32
|
const e = u.current;
|
|
33
33
|
e && (e.value = a, requestAnimationFrame(() => {
|
|
34
34
|
s();
|
|
@@ -36,22 +36,22 @@ const d = (...n) => {
|
|
|
36
36
|
}, [a]);
|
|
37
37
|
const m = (e) => {
|
|
38
38
|
c(e);
|
|
39
|
-
const
|
|
40
|
-
|
|
39
|
+
const n = u.current;
|
|
40
|
+
n && (n.value = e, requestAnimationFrame(() => {
|
|
41
41
|
s();
|
|
42
42
|
}));
|
|
43
|
-
},
|
|
44
|
-
return
|
|
43
|
+
}, h = () => c("");
|
|
44
|
+
return t ? {
|
|
45
45
|
value: a,
|
|
46
46
|
setValue: m,
|
|
47
|
-
clear:
|
|
47
|
+
clear: h
|
|
48
48
|
} : {
|
|
49
49
|
ref: o,
|
|
50
50
|
value: a,
|
|
51
51
|
setValue: m,
|
|
52
|
-
clear:
|
|
52
|
+
clear: h
|
|
53
53
|
};
|
|
54
|
-
};
|
|
54
|
+
});
|
|
55
55
|
export {
|
|
56
56
|
d as useTextareaAutosize
|
|
57
57
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTextareaAutosize.mjs","sources":["../../../../src/hooks/useTextareaAutosize/useTextareaAutosize.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The use textarea autosize options */\nexport interface UseTextareaAutosizeOptions {\n /** The initial value for the textarea */\n initialValue?: string;\n /** Callback function called when the textarea size changes */\n onResize?: () => void;\n}\n\n/** The use textarea autosize return type */\nexport interface UseTextareaAutosizeReturn {\n /** The current value of the textarea */\n value: string;\n /** Function to clear the textarea value */\n clear: () => void;\n /** Function to set the textarea value */\n setValue: (value: string) => void;\n}\n\nexport interface UseTextareaAutosize {\n (target: HookTarget, options?: UseTextareaAutosizeOptions): UseTextareaAutosizeReturn;\n\n <Target extends HTMLTextAreaElement = HTMLTextAreaElement>(\n initialValue: string,\n target?: never\n ): UseTextareaAutosizeReturn & {\n ref: StateRef<Target>;\n };\n\n <Target extends HTMLTextAreaElement = HTMLTextAreaElement>(\n options?: UseTextareaAutosizeOptions,\n target?: never\n ): UseTextareaAutosizeReturn & {\n ref: StateRef<Target>;\n };\n}\n\n/**\n * @name useTextareaAutosize\n * @description - Hook that automatically adjusts textarea height based on content\n * @category Elements\n * @usage medium\n *\n * @overload\n * @param {HookTarget} target The target textarea element\n * @param {string} [options.initialValue] The initial value for the textarea\n * @param {Function} [options.onResize] Callback function called when the textarea size changes\n * @returns {UseTextareaAutosizeReturn} An object containing value, setValue and clear\n *\n * @example\n * const { value, setValue, clear } = useTextareaAutosize(ref);\n *\n * @overload\n * @template Target The textarea element type\n * @param {string} initialValue The initial value for the textarea\n * @returns {UseTextareaAutosizeReturn & { ref: StateRef<Target> }} An object containing ref, value, setValue and clear\n *\n * @example\n * const { ref, value, setValue, clear } = useTextareaAutosize('initial');\n *\n * @overload\n * @template Target The textarea element type\n * @param {string} [options.initialValue] The initial value for the textarea\n * @param {Function} [options.onResize] Callback function called when the textarea size changes\n * @returns {UseTextareaAutosizeReturn & { ref: StateRef<Target> }} An object containing ref, value, setValue and clear\n *\n * @example\n * const { ref, value, setValue, clear } = useTextareaAutosize();\n */\nexport const useTextareaAutosize = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n\n const options = (\n target ? params[1] : typeof params[0] === 'string' ? { initialValue: params[0] } : params[0]\n ) as UseTextareaAutosizeOptions | undefined;\n\n const [value, setValue] = useState(options?.initialValue ?? '');\n const internalRef = useRefState<HTMLTextAreaElement>();\n const textareaRef = useRef<HTMLTextAreaElement | null>(null);\n\n const onTextareaResize = () => {\n const textarea = textareaRef.current;\n if (!textarea) return;\n\n const originalMinHeight = textarea.style.minHeight;\n const originalMaxHeight = textarea.style.maxHeight;\n\n textarea.style.height = 'auto';\n textarea.style.minHeight = 'auto';\n textarea.style.maxHeight = 'none';\n\n const scrollHeight = textarea.scrollHeight;\n\n textarea.style.height = `${scrollHeight}px`;\n textarea.style.minHeight = originalMinHeight;\n textarea.style.maxHeight = originalMaxHeight;\n\n options?.onResize?.();\n };\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (\n target ? isTarget.getElement(target) : internalRef.current\n ) as HTMLTextAreaElement;\n if (!element) return;\n\n textareaRef.current = element;\n if (options?.initialValue) element.value = options.initialValue;\n\n onTextareaResize();\n\n const onInput = (event: InputEvent) => {\n const newValue = (event.target as HTMLTextAreaElement).value;\n setValue(newValue);\n\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n };\n\n const onResize = () => {\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n };\n\n element.addEventListener('input', onInput as EventListener);\n element.addEventListener('resize', onResize as EventListener);\n\n return () => {\n element.removeEventListener('input', onInput as EventListener);\n element.removeEventListener('resize', onResize as EventListener);\n };\n }, [target, internalRef.state, options?.initialValue]);\n\n useEffect(() => {\n const textarea = textareaRef.current;\n if (!textarea) return;\n textarea.value = value;\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n }, [value]);\n\n const setTextareaValue = (newValue: string) => {\n setValue(newValue);\n const textarea = textareaRef.current;\n if (textarea) {\n textarea.value = newValue;\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n }\n };\n\n const clear = () => setValue('');\n\n if (target)\n return {\n value,\n setValue: setTextareaValue,\n clear\n };\n return {\n ref: internalRef,\n value,\n setValue: setTextareaValue,\n clear\n };\n}) as UseTextareaAutosize;\n"],"names":["useTextareaAutosize","params","target","isTarget","options","value","setValue","useState","internalRef","useRefState","textareaRef","useRef","onTextareaResize","textarea","originalMinHeight","originalMaxHeight","scrollHeight","useEffect","element","onInput","event","newValue","onResize","setTextareaValue","clear"],"mappings":";;;AA8EO,MAAMA,
|
|
1
|
+
{"version":3,"file":"useTextareaAutosize.mjs","sources":["../../../../src/hooks/useTextareaAutosize/useTextareaAutosize.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useRefState } from '../useRefState/useRefState';\n\n/** The use textarea autosize options */\nexport interface UseTextareaAutosizeOptions {\n /** The initial value for the textarea */\n initialValue?: string;\n /** Callback function called when the textarea size changes */\n onResize?: () => void;\n}\n\n/** The use textarea autosize return type */\nexport interface UseTextareaAutosizeReturn {\n /** The current value of the textarea */\n value: string;\n /** Function to clear the textarea value */\n clear: () => void;\n /** Function to set the textarea value */\n setValue: (value: string) => void;\n}\n\nexport interface UseTextareaAutosize {\n (target: HookTarget, options?: UseTextareaAutosizeOptions): UseTextareaAutosizeReturn;\n\n <Target extends HTMLTextAreaElement = HTMLTextAreaElement>(\n initialValue: string,\n target?: never\n ): UseTextareaAutosizeReturn & {\n ref: StateRef<Target>;\n };\n\n <Target extends HTMLTextAreaElement = HTMLTextAreaElement>(\n options?: UseTextareaAutosizeOptions,\n target?: never\n ): UseTextareaAutosizeReturn & {\n ref: StateRef<Target>;\n };\n}\n\n/**\n * @name useTextareaAutosize\n * @description - Hook that automatically adjusts textarea height based on content\n * @category Elements\n * @usage medium\n *\n * @overload\n * @param {HookTarget} target The target textarea element\n * @param {string} [options.initialValue] The initial value for the textarea\n * @param {Function} [options.onResize] Callback function called when the textarea size changes\n * @returns {UseTextareaAutosizeReturn} An object containing value, setValue and clear\n *\n * @example\n * const { value, setValue, clear } = useTextareaAutosize(ref);\n *\n * @overload\n * @template Target The textarea element type\n * @param {string} initialValue The initial value for the textarea\n * @returns {UseTextareaAutosizeReturn & { ref: StateRef<Target> }} An object containing ref, value, setValue and clear\n *\n * @example\n * const { ref, value, setValue, clear } = useTextareaAutosize('initial');\n *\n * @overload\n * @template Target The textarea element type\n * @param {string} [options.initialValue] The initial value for the textarea\n * @param {Function} [options.onResize] Callback function called when the textarea size changes\n * @returns {UseTextareaAutosizeReturn & { ref: StateRef<Target> }} An object containing ref, value, setValue and clear\n *\n * @example\n * const { ref, value, setValue, clear } = useTextareaAutosize();\n */\nexport const useTextareaAutosize = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n\n const options = (\n target ? params[1] : typeof params[0] === 'string' ? { initialValue: params[0] } : params[0]\n ) as UseTextareaAutosizeOptions | undefined;\n\n const [value, setValue] = useState(options?.initialValue ?? '');\n const internalRef = useRefState<HTMLTextAreaElement>();\n const textareaRef = useRef<HTMLTextAreaElement | null>(null);\n\n const onTextareaResize = () => {\n const textarea = textareaRef.current;\n if (!textarea) return;\n\n const originalMinHeight = textarea.style.minHeight;\n const originalMaxHeight = textarea.style.maxHeight;\n\n textarea.style.height = 'auto';\n textarea.style.minHeight = 'auto';\n textarea.style.maxHeight = 'none';\n\n const scrollHeight = textarea.scrollHeight;\n\n textarea.style.height = `${scrollHeight}px`;\n textarea.style.minHeight = originalMinHeight;\n textarea.style.maxHeight = originalMaxHeight;\n\n options?.onResize?.();\n };\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (\n target ? isTarget.getElement(target) : internalRef.current\n ) as HTMLTextAreaElement;\n if (!element) return;\n\n textareaRef.current = element;\n if (options?.initialValue) element.value = options.initialValue;\n\n onTextareaResize();\n\n const onInput = (event: InputEvent) => {\n const newValue = (event.target as HTMLTextAreaElement).value;\n setValue(newValue);\n\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n };\n\n const onResize = () => {\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n };\n\n element.addEventListener('input', onInput as EventListener);\n element.addEventListener('resize', onResize as EventListener);\n\n return () => {\n element.removeEventListener('input', onInput as EventListener);\n element.removeEventListener('resize', onResize as EventListener);\n };\n }, [target, internalRef.state, isTarget.getRefState(target), options?.initialValue]);\n\n useEffect(() => {\n const textarea = textareaRef.current;\n if (!textarea) return;\n textarea.value = value;\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n }, [value]);\n\n const setTextareaValue = (newValue: string) => {\n setValue(newValue);\n const textarea = textareaRef.current;\n if (textarea) {\n textarea.value = newValue;\n requestAnimationFrame(() => {\n onTextareaResize();\n });\n }\n };\n\n const clear = () => setValue('');\n\n if (target)\n return {\n value,\n setValue: setTextareaValue,\n clear\n };\n return {\n ref: internalRef,\n value,\n setValue: setTextareaValue,\n clear\n };\n}) as UseTextareaAutosize;\n"],"names":["useTextareaAutosize","params","target","isTarget","options","value","setValue","useState","internalRef","useRefState","textareaRef","useRef","onTextareaResize","textarea","originalMinHeight","originalMaxHeight","scrollHeight","useEffect","element","onInput","event","newValue","onResize","setTextareaValue","clear"],"mappings":";;;AA8EO,MAAMA,KAAuB,IAAIC,MAAkB;AACxD,QAAMC,IAAUC,EAASF,EAAO,CAAC,CAAC,IAAIA,EAAO,CAAC,IAAI,QAE5CG,IACJF,IAASD,EAAO,CAAC,IAAI,OAAOA,EAAO,CAAC,KAAM,WAAW,EAAE,cAAcA,EAAO,CAAC,EAAA,IAAMA,EAAO,CAAC,GAGvF,CAACI,GAAOC,CAAQ,IAAIC,EAASH,GAAS,gBAAgB,EAAE,GACxDI,IAAcC,EAAA,GACdC,IAAcC,EAAmC,IAAI,GAErDC,IAAmB,MAAM;AAC7B,UAAMC,IAAWH,EAAY;AAC7B,QAAI,CAACG,EAAU;AAEf,UAAMC,IAAoBD,EAAS,MAAM,WACnCE,IAAoBF,EAAS,MAAM;AAEzC,IAAAA,EAAS,MAAM,SAAS,QACxBA,EAAS,MAAM,YAAY,QAC3BA,EAAS,MAAM,YAAY;AAE3B,UAAMG,IAAeH,EAAS;AAE9B,IAAAA,EAAS,MAAM,SAAS,GAAGG,CAAY,MACvCH,EAAS,MAAM,YAAYC,GAC3BD,EAAS,MAAM,YAAYE,GAE3BX,GAAS,WAAA;AAAA,EAAW;AAGtB,EAAAa,EAAU,MAAM;AACd,QAAI,CAACf,KAAU,CAACM,EAAY,MAAO;AAEnC,UAAMU,IACJhB,IAASC,EAAS,WAAWD,CAAM,IAAIM,EAAY;AAErD,QAAI,CAACU,EAAS;AAEd,IAAAR,EAAY,UAAUQ,GAClBd,GAAS,iBAAcc,EAAQ,QAAQd,EAAQ,eAEnDQ,EAAA;AAEA,UAAMO,IAAU,CAACC,MAAsB;AACrC,YAAMC,IAAYD,EAAM,OAA+B;AACvD,MAAAd,EAASe,CAAQ,GAEjB,sBAAsB,MAAM;AAC1B,QAAAT,EAAA;AAAA,MAAiB,CAClB;AAAA,IAAA,GAGGU,IAAW,MAAM;AACrB,4BAAsB,MAAM;AAC1B,QAAAV,EAAA;AAAA,MAAiB,CAClB;AAAA,IAAA;AAGH,WAAAM,EAAQ,iBAAiB,SAASC,CAAwB,GAC1DD,EAAQ,iBAAiB,UAAUI,CAAyB,GAErD,MAAM;AACX,MAAAJ,EAAQ,oBAAoB,SAASC,CAAwB,GAC7DD,EAAQ,oBAAoB,UAAUI,CAAyB;AAAA,IAAA;AAAA,EACjE,GACC,CAACpB,GAAQM,EAAY,OAAOL,EAAS,YAAYD,CAAM,GAAGE,GAAS,YAAY,CAAC,GAEnFa,EAAU,MAAM;AACd,UAAMJ,IAAWH,EAAY;AAC7B,IAAKG,MACLA,EAAS,QAAQR,GACjB,sBAAsB,MAAM;AAC1B,MAAAO,EAAA;AAAA,IAAiB,CAClB;AAAA,EAAA,GACA,CAACP,CAAK,CAAC;AAEV,QAAMkB,IAAmB,CAACF,MAAqB;AAC7C,IAAAf,EAASe,CAAQ;AACjB,UAAMR,IAAWH,EAAY;AAC7B,IAAIG,MACFA,EAAS,QAAQQ,GACjB,sBAAsB,MAAM;AAC1B,MAAAT,EAAA;AAAA,IAAiB,CAClB;AAAA,EACH,GAGIY,IAAQ,MAAMlB,EAAS,EAAE;AAE/B,SAAIJ,IACK;AAAA,IACL,OAAAG;AAAA,IACA,UAAUkB;AAAA,IACV,OAAAC;AAAA,EAAA,IAEG;AAAA,IACL,KAAKhB;AAAA,IACL,OAAAH;AAAA,IACA,UAAUkB;AAAA,IACV,OAAAC;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { useState as l, useRef as m, useEffect as g } from "react";
|
|
2
2
|
import { useDidUpdate as y } from "../useDidUpdate/useDidUpdate.mjs";
|
|
3
3
|
const E = (s) => {
|
|
4
|
-
const t = Math.ceil(s), u = Math.floor(t / (
|
|
4
|
+
const t = Math.ceil(s), u = Math.floor(t / (3600 * 24)), c = Math.floor(t % (3600 * 24) / 3600), e = Math.floor(t % 3600 / 60);
|
|
5
5
|
return {
|
|
6
6
|
seconds: Math.floor(t % 60),
|
|
7
7
|
minutes: e,
|
|
8
8
|
hours: c,
|
|
9
9
|
days: u
|
|
10
10
|
};
|
|
11
|
-
}, j = (...s) => {
|
|
11
|
+
}, j = ((...s) => {
|
|
12
12
|
const t = Math.max(s[0] ?? 0, 0), u = typeof s[1] == "object" ? s[1] : { onExpire: s[1] }, [c, e] = l(t > 0 && (u?.immediately ?? !0)), [i, n] = l(t), d = m(void 0), f = m(u);
|
|
13
13
|
f.current = u ?? {}, y(() => {
|
|
14
14
|
if (t <= 0) {
|
|
@@ -59,7 +59,7 @@ const E = (s) => {
|
|
|
59
59
|
increase: I,
|
|
60
60
|
decrease: T
|
|
61
61
|
};
|
|
62
|
-
};
|
|
62
|
+
});
|
|
63
63
|
export {
|
|
64
64
|
E as getTimeFromSeconds,
|
|
65
65
|
j as useTimer
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTimer.mjs","sources":["../../../../src/hooks/useTimer/useTimer.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { useDidUpdate } from '../useDidUpdate/useDidUpdate';\n\nexport type PositiveInteger<Value extends number> = `${Value}` extends `-${any}` | `${any}.${any}`\n ? never\n : Value;\n\nexport const getTimeFromSeconds = (timestamp: number) => {\n const roundedTimestamp = Math.ceil(timestamp);\n const days = Math.floor(roundedTimestamp / (60 * 60 * 24));\n const hours = Math.floor((roundedTimestamp % (60 * 60 * 24)) / (60 * 60));\n const minutes = Math.floor((roundedTimestamp % (60 * 60)) / 60);\n const seconds = Math.floor(roundedTimestamp % 60);\n\n return {\n seconds,\n minutes,\n hours,\n days\n };\n};\n\n/** The use timer options type */\nexport interface UseTimerOptions {\n /** Whether the timer should start automatically */\n immediately?: boolean;\n /** The function to be executed when the timer is expired */\n onExpire?: () => void;\n /** The function to be executed when the timer is started */\n onStart?: () => void;\n /** Callback function to be executed on each tick of the timer */\n onTick?: (seconds: number) => void;\n}\n\n/** The use timer return type */\nexport interface UseTimerReturn {\n /** flag to indicate if timer is active or not */\n active: boolean;\n /** The total count of the timer */\n count: number;\n /** The day count of the timer */\n days: number;\n /** The hour count of the timer */\n hours: number;\n /** The minute count of the timer */\n minutes: number;\n /** The second count of the timer */\n seconds: number;\n /** The function to clear the timer */\n clear: () => void;\n /** The function to decrease the timer */\n decrease: (seconds: PositiveInteger<number>) => void;\n /** The function to increase the timer */\n increase: (seconds: PositiveInteger<number>) => void;\n /** The function to pause the timer */\n pause: () => void;\n /** The function to restart the timer */\n restart: (time: PositiveInteger<number>, immediately?: boolean) => void;\n /** The function to resume the timer */\n resume: () => void;\n /** The function to start the timer */\n start: () => void;\n /** The function to toggle the timer */\n toggle: () => void;\n}\n\nexport interface UseTimer {\n (): UseTimerReturn;\n\n (seconds: PositiveInteger<number>, callback: () => void): UseTimerReturn;\n\n (seconds: PositiveInteger<number>, options?: UseTimerOptions): UseTimerReturn;\n}\n\n/**\n * @name useTimer\n * @description - Hook that creates a timer functionality\n * @category Time\n * @usage medium\n *\n * @overload\n * @returns {UseTimerReturn} An object containing the timer properties and functions\n *\n * @example\n * const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer();\n *\n * @overload\n * @param {number} seconds The seconds value that define for how long the timer will be running\n * @param {() => void} callback The function to be executed once countdown timer is expired\n * @returns {UseTimerReturn} An object containing the timer properties and functions\n *\n * @example\n * const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer(1000, () => console.log('ready'));\n *\n * @overload\n * @param {number} seconds The seconds value that define for how long the timer will be running\n * @param {boolean} [options.immediately=true] The flag to decide if timer should start automatically\n * @param {() => void} [options.onExpire] The function to be executed when the timer is expired\n * @param {(timestamp: number) => void} [options.onTick] The function to be executed on each tick of the timer\n * @returns {UseTimerReturn} An object containing the timer properties and functions\n *\n * @example\n * const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer(1000);\n */\nexport const useTimer = ((...params: any[]) => {\n const initialSeconds = Math.max((params[0] ?? 0) as PositiveInteger<number>, 0);\n const options = (typeof params[1] === 'object' ? params[1] : { onExpire: params[1] }) as\n | UseTimerOptions\n | undefined;\n\n const [active, setActive] = useState(initialSeconds > 0 && (options?.immediately ?? true));\n const [seconds, setSeconds] = useState(initialSeconds);\n\n const intervalIdRef = useRef<ReturnType<typeof setInterval>>(undefined);\n const optionsRef = useRef<UseTimerOptions>(options);\n optionsRef.current = options ?? {};\n\n useDidUpdate(() => {\n if (initialSeconds <= 0) {\n setActive(false);\n setSeconds(0);\n return;\n }\n\n setActive(true);\n setSeconds(initialSeconds);\n }, [initialSeconds]);\n\n useEffect(() => {\n if (!active) return;\n\n optionsRef.current?.onStart?.();\n const onInterval = () => {\n setSeconds((prevSeconds) => {\n optionsRef.current?.onTick?.(prevSeconds);\n const updatedSeconds = prevSeconds - 1;\n if (updatedSeconds === 0) {\n setActive(false);\n optionsRef.current?.onExpire?.();\n }\n return updatedSeconds;\n });\n };\n\n intervalIdRef.current = setInterval(onInterval, 1000);\n return () => {\n clearInterval(intervalIdRef.current);\n };\n }, [active]);\n\n const pause = () => setActive(false);\n const resume = () => {\n if (seconds <= 0) return;\n setActive(true);\n };\n\n const toggle = () => {\n if (seconds <= 0) return;\n setActive(!active);\n };\n\n const restart = (seconds: PositiveInteger<number>, immediately = true) => {\n setSeconds(seconds);\n if (immediately) setActive(true);\n };\n\n const start = () => {\n if (initialSeconds <= 0) return;\n\n setActive(true);\n setSeconds(initialSeconds);\n };\n\n const clear = () => {\n setActive(false);\n setSeconds(0);\n };\n\n const increase = (seconds: PositiveInteger<number>) =>\n setSeconds((prevSeconds) => prevSeconds + seconds);\n const decrease = (seconds: PositiveInteger<number>) => {\n setSeconds((prevSeconds) => {\n const updatedSeconds = prevSeconds - seconds;\n if (updatedSeconds <= 0) {\n setActive(false);\n return 0;\n } else {\n return updatedSeconds;\n }\n });\n };\n\n return {\n ...getTimeFromSeconds(seconds),\n count: seconds,\n pause,\n active,\n resume,\n toggle,\n start,\n restart,\n clear,\n increase,\n decrease\n };\n}) as UseTimer;\n"],"names":["getTimeFromSeconds","timestamp","roundedTimestamp","days","hours","minutes","useTimer","params","initialSeconds","options","active","setActive","useState","seconds","setSeconds","intervalIdRef","useRef","optionsRef","useDidUpdate","useEffect","onInterval","prevSeconds","updatedSeconds","pause","resume","toggle","restart","immediately","start","clear","increase","decrease"],"mappings":";;AAQO,MAAMA,IAAqB,CAACC,MAAsB;AACvD,QAAMC,IAAmB,KAAK,KAAKD,CAAS,GACtCE,IAAO,KAAK,MAAMD,KAAoB,
|
|
1
|
+
{"version":3,"file":"useTimer.mjs","sources":["../../../../src/hooks/useTimer/useTimer.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { useDidUpdate } from '../useDidUpdate/useDidUpdate';\n\nexport type PositiveInteger<Value extends number> = `${Value}` extends `-${any}` | `${any}.${any}`\n ? never\n : Value;\n\nexport const getTimeFromSeconds = (timestamp: number) => {\n const roundedTimestamp = Math.ceil(timestamp);\n const days = Math.floor(roundedTimestamp / (60 * 60 * 24));\n const hours = Math.floor((roundedTimestamp % (60 * 60 * 24)) / (60 * 60));\n const minutes = Math.floor((roundedTimestamp % (60 * 60)) / 60);\n const seconds = Math.floor(roundedTimestamp % 60);\n\n return {\n seconds,\n minutes,\n hours,\n days\n };\n};\n\n/** The use timer options type */\nexport interface UseTimerOptions {\n /** Whether the timer should start automatically */\n immediately?: boolean;\n /** The function to be executed when the timer is expired */\n onExpire?: () => void;\n /** The function to be executed when the timer is started */\n onStart?: () => void;\n /** Callback function to be executed on each tick of the timer */\n onTick?: (seconds: number) => void;\n}\n\n/** The use timer return type */\nexport interface UseTimerReturn {\n /** flag to indicate if timer is active or not */\n active: boolean;\n /** The total count of the timer */\n count: number;\n /** The day count of the timer */\n days: number;\n /** The hour count of the timer */\n hours: number;\n /** The minute count of the timer */\n minutes: number;\n /** The second count of the timer */\n seconds: number;\n /** The function to clear the timer */\n clear: () => void;\n /** The function to decrease the timer */\n decrease: (seconds: PositiveInteger<number>) => void;\n /** The function to increase the timer */\n increase: (seconds: PositiveInteger<number>) => void;\n /** The function to pause the timer */\n pause: () => void;\n /** The function to restart the timer */\n restart: (time: PositiveInteger<number>, immediately?: boolean) => void;\n /** The function to resume the timer */\n resume: () => void;\n /** The function to start the timer */\n start: () => void;\n /** The function to toggle the timer */\n toggle: () => void;\n}\n\nexport interface UseTimer {\n (): UseTimerReturn;\n\n (seconds: PositiveInteger<number>, callback: () => void): UseTimerReturn;\n\n (seconds: PositiveInteger<number>, options?: UseTimerOptions): UseTimerReturn;\n}\n\n/**\n * @name useTimer\n * @description - Hook that creates a timer functionality\n * @category Time\n * @usage medium\n *\n * @overload\n * @returns {UseTimerReturn} An object containing the timer properties and functions\n *\n * @example\n * const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer();\n *\n * @overload\n * @param {number} seconds The seconds value that define for how long the timer will be running\n * @param {() => void} callback The function to be executed once countdown timer is expired\n * @returns {UseTimerReturn} An object containing the timer properties and functions\n *\n * @example\n * const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer(1000, () => console.log('ready'));\n *\n * @overload\n * @param {number} seconds The seconds value that define for how long the timer will be running\n * @param {boolean} [options.immediately=true] The flag to decide if timer should start automatically\n * @param {() => void} [options.onExpire] The function to be executed when the timer is expired\n * @param {(timestamp: number) => void} [options.onTick] The function to be executed on each tick of the timer\n * @returns {UseTimerReturn} An object containing the timer properties and functions\n *\n * @example\n * const { days, hours, minutes, seconds, toggle, pause, start, restart, resume, active, decrease, increase } = useTimer(1000);\n */\nexport const useTimer = ((...params: any[]) => {\n const initialSeconds = Math.max((params[0] ?? 0) as PositiveInteger<number>, 0);\n const options = (typeof params[1] === 'object' ? params[1] : { onExpire: params[1] }) as\n | UseTimerOptions\n | undefined;\n\n const [active, setActive] = useState(initialSeconds > 0 && (options?.immediately ?? true));\n const [seconds, setSeconds] = useState(initialSeconds);\n\n const intervalIdRef = useRef<ReturnType<typeof setInterval>>(undefined);\n const optionsRef = useRef<UseTimerOptions>(options);\n optionsRef.current = options ?? {};\n\n useDidUpdate(() => {\n if (initialSeconds <= 0) {\n setActive(false);\n setSeconds(0);\n return;\n }\n\n setActive(true);\n setSeconds(initialSeconds);\n }, [initialSeconds]);\n\n useEffect(() => {\n if (!active) return;\n\n optionsRef.current?.onStart?.();\n const onInterval = () => {\n setSeconds((prevSeconds) => {\n optionsRef.current?.onTick?.(prevSeconds);\n const updatedSeconds = prevSeconds - 1;\n if (updatedSeconds === 0) {\n setActive(false);\n optionsRef.current?.onExpire?.();\n }\n return updatedSeconds;\n });\n };\n\n intervalIdRef.current = setInterval(onInterval, 1000);\n return () => {\n clearInterval(intervalIdRef.current);\n };\n }, [active]);\n\n const pause = () => setActive(false);\n const resume = () => {\n if (seconds <= 0) return;\n setActive(true);\n };\n\n const toggle = () => {\n if (seconds <= 0) return;\n setActive(!active);\n };\n\n const restart = (seconds: PositiveInteger<number>, immediately = true) => {\n setSeconds(seconds);\n if (immediately) setActive(true);\n };\n\n const start = () => {\n if (initialSeconds <= 0) return;\n\n setActive(true);\n setSeconds(initialSeconds);\n };\n\n const clear = () => {\n setActive(false);\n setSeconds(0);\n };\n\n const increase = (seconds: PositiveInteger<number>) =>\n setSeconds((prevSeconds) => prevSeconds + seconds);\n const decrease = (seconds: PositiveInteger<number>) => {\n setSeconds((prevSeconds) => {\n const updatedSeconds = prevSeconds - seconds;\n if (updatedSeconds <= 0) {\n setActive(false);\n return 0;\n } else {\n return updatedSeconds;\n }\n });\n };\n\n return {\n ...getTimeFromSeconds(seconds),\n count: seconds,\n pause,\n active,\n resume,\n toggle,\n start,\n restart,\n clear,\n increase,\n decrease\n };\n}) as UseTimer;\n"],"names":["getTimeFromSeconds","timestamp","roundedTimestamp","days","hours","minutes","useTimer","params","initialSeconds","options","active","setActive","useState","seconds","setSeconds","intervalIdRef","useRef","optionsRef","useDidUpdate","useEffect","onInterval","prevSeconds","updatedSeconds","pause","resume","toggle","restart","immediately","start","clear","increase","decrease"],"mappings":";;AAQO,MAAMA,IAAqB,CAACC,MAAsB;AACvD,QAAMC,IAAmB,KAAK,KAAKD,CAAS,GACtCE,IAAO,KAAK,MAAMD,KAAoB,OAAU,GAAG,GACnDE,IAAQ,KAAK,MAAOF,KAAoB,OAAU,MAAQ,IAAQ,GAClEG,IAAU,KAAK,MAAOH,IAAoB,OAAY,EAAE;AAG9D,SAAO;AAAA,IACL,SAHc,KAAK,MAAMA,IAAmB,EAAE;AAAA,IAI9C,SAAAG;AAAA,IACA,OAAAD;AAAA,IACA,MAAAD;AAAA,EAAA;AAEJ,GAoFaG,KAAY,IAAIC,MAAkB;AAC7C,QAAMC,IAAiB,KAAK,IAAKD,EAAO,CAAC,KAAK,GAA+B,CAAC,GACxEE,IAAW,OAAOF,EAAO,CAAC,KAAM,WAAWA,EAAO,CAAC,IAAI,EAAE,UAAUA,EAAO,CAAC,EAAA,GAI3E,CAACG,GAAQC,CAAS,IAAIC,EAASJ,IAAiB,MAAMC,GAAS,eAAe,GAAK,GACnF,CAACI,GAASC,CAAU,IAAIF,EAASJ,CAAc,GAE/CO,IAAgBC,EAAuC,MAAS,GAChEC,IAAaD,EAAwBP,CAAO;AAClD,EAAAQ,EAAW,UAAUR,KAAW,CAAA,GAEhCS,EAAa,MAAM;AACjB,QAAIV,KAAkB,GAAG;AACvB,MAAAG,EAAU,EAAK,GACfG,EAAW,CAAC;AACZ;AAAA,IAAA;AAGF,IAAAH,EAAU,EAAI,GACdG,EAAWN,CAAc;AAAA,EAAA,GACxB,CAACA,CAAc,CAAC,GAEnBW,EAAU,MAAM;AACd,QAAI,CAACT,EAAQ;AAEb,IAAAO,EAAW,SAAS,UAAA;AACpB,UAAMG,IAAa,MAAM;AACvB,MAAAN,EAAW,CAACO,MAAgB;AAC1B,QAAAJ,EAAW,SAAS,SAASI,CAAW;AACxC,cAAMC,IAAiBD,IAAc;AACrC,eAAIC,MAAmB,MACrBX,EAAU,EAAK,GACfM,EAAW,SAAS,WAAA,IAEfK;AAAA,MAAA,CACR;AAAA,IAAA;AAGH,WAAAP,EAAc,UAAU,YAAYK,GAAY,GAAI,GAC7C,MAAM;AACX,oBAAcL,EAAc,OAAO;AAAA,IAAA;AAAA,EACrC,GACC,CAACL,CAAM,CAAC;AAEX,QAAMa,IAAQ,MAAMZ,EAAU,EAAK,GAC7Ba,IAAS,MAAM;AACnB,IAAIX,KAAW,KACfF,EAAU,EAAI;AAAA,EAAA,GAGVc,IAAS,MAAM;AACnB,IAAIZ,KAAW,KACfF,EAAU,CAACD,CAAM;AAAA,EAAA,GAGbgB,IAAU,CAACb,GAAkCc,IAAc,OAAS;AACxE,IAAAb,EAAWD,CAAO,GACdc,OAAuB,EAAI;AAAA,EAAA,GAG3BC,IAAQ,MAAM;AAClB,IAAIpB,KAAkB,MAEtBG,EAAU,EAAI,GACdG,EAAWN,CAAc;AAAA,EAAA,GAGrBqB,IAAQ,MAAM;AAClB,IAAAlB,EAAU,EAAK,GACfG,EAAW,CAAC;AAAA,EAAA,GAGRgB,IAAW,CAACjB,MAChBC,EAAW,CAACO,MAAgBA,IAAcR,CAAO,GAC7CkB,IAAW,CAAClB,MAAqC;AACrD,IAAAC,EAAW,CAACO,MAAgB;AAC1B,YAAMC,IAAiBD,IAAcR;AACrC,aAAIS,KAAkB,KACpBX,EAAU,EAAK,GACR,KAEAW;AAAA,IACT,CACD;AAAA,EAAA;AAGH,SAAO;AAAA,IACL,GAAGtB,EAAmBa,CAAO;AAAA,IAC7B,OAAOA;AAAA,IACP,OAAAU;AAAA,IACA,QAAAb;AAAA,IACA,QAAAc;AAAA,IACA,QAAAC;AAAA,IACA,OAAAG;AAAA,IACA,SAAAF;AAAA,IACA,OAAAG;AAAA,IACA,UAAAC;AAAA,IACA,UAAAC;AAAA,EAAA;AAEJ;"}
|
|
@@ -17,7 +17,7 @@ const f = "reactuse-url-search-params-event", w = (n = "history") => {
|
|
|
17
17
|
return `${s}${h}${r ? `?${r}` : ""}`;
|
|
18
18
|
}
|
|
19
19
|
throw new Error("Invalid mode");
|
|
20
|
-
}, L = () => window.dispatchEvent(new Event(f)), U = (n, i) => {
|
|
20
|
+
}, L = () => window.dispatchEvent(new Event(f)), U = ((n, i) => {
|
|
21
21
|
const r = typeof i == "object" && i && ("serializer" in i || "deserializer" in i || "initialValue" in i || "mode" in i || "write" in i) ? i : void 0, s = r ? r?.initialValue : i, { mode: t = "history", write: a = "replace" } = r ?? {};
|
|
22
22
|
if (typeof window > "u")
|
|
23
23
|
return {
|
|
@@ -66,7 +66,7 @@ const f = "reactuse-url-search-params-event", w = (n = "history") => {
|
|
|
66
66
|
remove: g,
|
|
67
67
|
set: P
|
|
68
68
|
};
|
|
69
|
-
};
|
|
69
|
+
});
|
|
70
70
|
export {
|
|
71
71
|
f as URL_SEARCH_PARAMS_EVENT,
|
|
72
72
|
z as createQueryString,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useUrlSearchParam.mjs","sources":["../../../../src/hooks/useUrlSearchParam/useUrlSearchParam.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/** The url search params mode type */\nexport type UrlSearchParamMode = 'hash-params' | 'hash' | 'history';\n\n/** The use url search param options type */\nexport interface UseUrlSearchParamOptions<Value> {\n /** The initial value of the search param */\n initialValue?: Value;\n /** The mode to use for writing to the URL */\n mode?: UrlSearchParamMode;\n /** The mode to use for writing to the URL */\n write?: 'push' | 'replace';\n /** The deserializer function to be invoked */\n deserializer?: (value: string) => Value;\n /** The serializer function to be invoked */\n serializer?: (value: Value) => string;\n}\n\n/** The use url search params set options type */\nexport interface UseUrlSearchParamsActionOptions {\n /** The mode to use for writing to the URL */\n write?: 'push' | 'replace';\n}\n\n/** The use url search param return type */\nexport interface UseUrlSearchParamReturn<Value> {\n /** Current search param value */\n value: Value | undefined;\n /** Function to remove the search param */\n remove: (options?: UseUrlSearchParamsActionOptions) => void;\n /** Function to update search param */\n set: (value: Value, options?: UseUrlSearchParamsActionOptions) => void;\n}\n\nexport const URL_SEARCH_PARAMS_EVENT = 'reactuse-url-search-params-event';\n\nexport const getUrlSearchParams = (mode: UrlSearchParamMode = 'history') => {\n const { search, hash } = window.location;\n\n let path = '';\n\n if (mode === 'history') path = search;\n if (mode === 'hash-params') path = hash.replace(/^#/, '');\n if (mode === 'hash') {\n const index = hash.indexOf('?');\n path = ~index ? hash.slice(index) : '';\n }\n\n return new URLSearchParams(path);\n};\n\nexport const createQueryString = (searchParams: URLSearchParams, mode: UrlSearchParamMode) => {\n const searchParamsString = searchParams.toString();\n const { search, hash } = window.location;\n\n if (mode === 'history') return `${searchParamsString ? `?${searchParamsString}` : ''}${hash}`;\n if (mode === 'hash-params')\n return `${search}${searchParamsString ? `#${searchParamsString}` : ''}`;\n if (mode === 'hash') {\n const index = hash.indexOf('?');\n const base = index > -1 ? hash.slice(0, index) : hash;\n return `${search}${base}${searchParamsString ? `?${searchParamsString}` : ''}`;\n }\n\n throw new Error('Invalid mode');\n};\n\nexport const dispatchUrlSearchParamsEvent = () =>\n window.dispatchEvent(new Event(URL_SEARCH_PARAMS_EVENT));\n\nexport interface UseUrlSearchParam {\n <Value>(\n key: string,\n options: UseUrlSearchParamOptions<Value> & { initialValue: Value }\n ): UseUrlSearchParamReturn<Value>;\n\n <Value>(\n key: string,\n options?: UseUrlSearchParamOptions<Value>\n ): UseUrlSearchParamReturn<Value | undefined>;\n\n <Value>(key: string, initialValue: Value): UseUrlSearchParamReturn<Value>;\n\n <Value>(key: string): UseUrlSearchParamReturn<Value | undefined>;\n}\n\n/**\n * @name useUrlSearchParam\n * @description - Hook that provides reactive URLSearchParams for a single key\n * @category State\n * @usage high\n *\n * @overload\n * @template Value The type of the url param values\n * @param {string} key The key of the url param\n * @param {UseUrlSearchParamOptions<Value> & { initialValue: Value }} options The options object with required initialValue\n * @param {Value} options.initialValue The initial value for the url param\n * @param {UrlSearchParamsMode} [options.mode='history'] The mode to use for the URL ('history' | 'hash-params' | 'hash')\n * @param {'push' | 'replace'} [options.write='replace'] The mode to use for writing to the URL\n * @param {(value: Value) => string} [options.serializer] Custom serializer function to convert value to string\n * @param {(value: string) => Value} [options.deserializer] Custom deserializer function to convert string to value\n * @returns {UseUrlSearchParamReturn<Value>} The object with value and function for change value\n *\n * @example\n * const { value, set } = useUrlSearchParam('page', { initialValue: 1 });\n *\n * @overload\n * @template Value The type of the url param values\n * @param {string} key The key of the url param\n * @param {Value} [initialValue] The initial value for the url param\n * @returns {UseUrlSearchParamReturn<Value>} The object with value and function for change value\n *\n * @example\n * const { value, set } = useUrlSearchParam('page', 1);\n */\nexport const useUrlSearchParam = (<Value>(key: string, params?: any) => {\n const options = (\n typeof params === 'object' &&\n params &&\n ('serializer' in params ||\n 'deserializer' in params ||\n 'initialValue' in params ||\n 'mode' in params ||\n 'write' in params)\n ? params\n : undefined\n ) as UseUrlSearchParamOptions<Value>;\n\n const initialValue = (options ? options?.initialValue : params) as Value;\n const { mode = 'history', write: writeMode = 'replace' } = options ?? {};\n\n if (typeof window === 'undefined') {\n return {\n value: initialValue,\n remove: () => {},\n set: () => {}\n } as UseUrlSearchParamReturn<Value>;\n }\n\n const serializer = (value: Value) => {\n if (options?.serializer) return options.serializer(value);\n if (typeof value === 'string') return value;\n\n return JSON.stringify(value);\n };\n\n const deserializer = (value: string) => {\n if (options?.deserializer) return options.deserializer(value);\n if (value === 'undefined' || value === 'null') return undefined as unknown as Value;\n\n try {\n return JSON.parse(value) as Value;\n } catch {\n return value as Value;\n }\n };\n\n const setUrlSearchParam = (\n key: string,\n value: Value | undefined,\n mode: UrlSearchParamMode,\n write: 'push' | 'replace' = 'replace'\n ) => {\n const urlSearchParams = getUrlSearchParams(mode);\n\n if (value === undefined) {\n urlSearchParams.delete(key);\n } else {\n const serializedValue = serializer ? serializer(value) : String(value);\n urlSearchParams.set(key, serializedValue);\n }\n\n const query = createQueryString(urlSearchParams, mode);\n if (write === 'replace') window.history.replaceState({}, '', query);\n if (write === 'push') window.history.pushState({}, '', query);\n\n dispatchUrlSearchParamsEvent();\n };\n\n const [value, setValue] = useState<Value | undefined>(() => {\n const urlSearchParams = getUrlSearchParams(mode);\n const currentValue = urlSearchParams.get(key);\n\n if (currentValue === null && initialValue !== undefined) {\n setUrlSearchParam(key, initialValue, mode, writeMode);\n return initialValue;\n }\n\n return currentValue ? deserializer(currentValue) : undefined;\n });\n\n const set = (value: Value, options?: UseUrlSearchParamsActionOptions) => {\n setUrlSearchParam(key, value, mode, options?.write ?? writeMode);\n setValue(value);\n };\n\n const remove = (options?: UseUrlSearchParamsActionOptions) => {\n setUrlSearchParam(key, undefined, mode, options?.write ?? writeMode);\n setValue(undefined);\n };\n\n useEffect(() => {\n const onParamsChange = () => {\n const urlSearchParams = getUrlSearchParams(mode);\n const newValue = urlSearchParams.get(key);\n setValue(newValue ? deserializer(newValue) : undefined);\n };\n\n window.addEventListener(URL_SEARCH_PARAMS_EVENT, onParamsChange);\n window.addEventListener('popstate', onParamsChange);\n if (mode !== 'history') {\n window.addEventListener('hashchange', onParamsChange);\n }\n\n return () => {\n window.removeEventListener(URL_SEARCH_PARAMS_EVENT, onParamsChange);\n window.removeEventListener('popstate', onParamsChange);\n if (mode !== 'history') {\n window.removeEventListener('hashchange', onParamsChange);\n }\n };\n }, [key, mode]);\n\n return {\n value,\n remove,\n set\n };\n}) as UseUrlSearchParam;\n"],"names":["URL_SEARCH_PARAMS_EVENT","getUrlSearchParams","mode","search","hash","path","index","createQueryString","searchParams","searchParamsString","base","dispatchUrlSearchParamsEvent","useUrlSearchParam","key","params","options","initialValue","writeMode","serializer","value","deserializer","setUrlSearchParam","write","urlSearchParams","serializedValue","query","setValue","useState","currentValue","set","remove","useEffect","onParamsChange","newValue"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"useUrlSearchParam.mjs","sources":["../../../../src/hooks/useUrlSearchParam/useUrlSearchParam.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/** The url search params mode type */\nexport type UrlSearchParamMode = 'hash-params' | 'hash' | 'history';\n\n/** The use url search param options type */\nexport interface UseUrlSearchParamOptions<Value> {\n /** The initial value of the search param */\n initialValue?: Value;\n /** The mode to use for writing to the URL */\n mode?: UrlSearchParamMode;\n /** The mode to use for writing to the URL */\n write?: 'push' | 'replace';\n /** The deserializer function to be invoked */\n deserializer?: (value: string) => Value;\n /** The serializer function to be invoked */\n serializer?: (value: Value) => string;\n}\n\n/** The use url search params set options type */\nexport interface UseUrlSearchParamsActionOptions {\n /** The mode to use for writing to the URL */\n write?: 'push' | 'replace';\n}\n\n/** The use url search param return type */\nexport interface UseUrlSearchParamReturn<Value> {\n /** Current search param value */\n value: Value | undefined;\n /** Function to remove the search param */\n remove: (options?: UseUrlSearchParamsActionOptions) => void;\n /** Function to update search param */\n set: (value: Value, options?: UseUrlSearchParamsActionOptions) => void;\n}\n\nexport const URL_SEARCH_PARAMS_EVENT = 'reactuse-url-search-params-event';\n\nexport const getUrlSearchParams = (mode: UrlSearchParamMode = 'history') => {\n const { search, hash } = window.location;\n\n let path = '';\n\n if (mode === 'history') path = search;\n if (mode === 'hash-params') path = hash.replace(/^#/, '');\n if (mode === 'hash') {\n const index = hash.indexOf('?');\n path = ~index ? hash.slice(index) : '';\n }\n\n return new URLSearchParams(path);\n};\n\nexport const createQueryString = (searchParams: URLSearchParams, mode: UrlSearchParamMode) => {\n const searchParamsString = searchParams.toString();\n const { search, hash } = window.location;\n\n if (mode === 'history') return `${searchParamsString ? `?${searchParamsString}` : ''}${hash}`;\n if (mode === 'hash-params')\n return `${search}${searchParamsString ? `#${searchParamsString}` : ''}`;\n if (mode === 'hash') {\n const index = hash.indexOf('?');\n const base = index > -1 ? hash.slice(0, index) : hash;\n return `${search}${base}${searchParamsString ? `?${searchParamsString}` : ''}`;\n }\n\n throw new Error('Invalid mode');\n};\n\nexport const dispatchUrlSearchParamsEvent = () =>\n window.dispatchEvent(new Event(URL_SEARCH_PARAMS_EVENT));\n\nexport interface UseUrlSearchParam {\n <Value>(\n key: string,\n options: UseUrlSearchParamOptions<Value> & { initialValue: Value }\n ): UseUrlSearchParamReturn<Value>;\n\n <Value>(\n key: string,\n options?: UseUrlSearchParamOptions<Value>\n ): UseUrlSearchParamReturn<Value | undefined>;\n\n <Value>(key: string, initialValue: Value): UseUrlSearchParamReturn<Value>;\n\n <Value>(key: string): UseUrlSearchParamReturn<Value | undefined>;\n}\n\n/**\n * @name useUrlSearchParam\n * @description - Hook that provides reactive URLSearchParams for a single key\n * @category State\n * @usage high\n *\n * @overload\n * @template Value The type of the url param values\n * @param {string} key The key of the url param\n * @param {UseUrlSearchParamOptions<Value> & { initialValue: Value }} options The options object with required initialValue\n * @param {Value} options.initialValue The initial value for the url param\n * @param {UrlSearchParamsMode} [options.mode='history'] The mode to use for the URL ('history' | 'hash-params' | 'hash')\n * @param {'push' | 'replace'} [options.write='replace'] The mode to use for writing to the URL\n * @param {(value: Value) => string} [options.serializer] Custom serializer function to convert value to string\n * @param {(value: string) => Value} [options.deserializer] Custom deserializer function to convert string to value\n * @returns {UseUrlSearchParamReturn<Value>} The object with value and function for change value\n *\n * @example\n * const { value, set } = useUrlSearchParam('page', { initialValue: 1 });\n *\n * @overload\n * @template Value The type of the url param values\n * @param {string} key The key of the url param\n * @param {Value} [initialValue] The initial value for the url param\n * @returns {UseUrlSearchParamReturn<Value>} The object with value and function for change value\n *\n * @example\n * const { value, set } = useUrlSearchParam('page', 1);\n */\nexport const useUrlSearchParam = (<Value>(key: string, params?: any) => {\n const options = (\n typeof params === 'object' &&\n params &&\n ('serializer' in params ||\n 'deserializer' in params ||\n 'initialValue' in params ||\n 'mode' in params ||\n 'write' in params)\n ? params\n : undefined\n ) as UseUrlSearchParamOptions<Value>;\n\n const initialValue = (options ? options?.initialValue : params) as Value;\n const { mode = 'history', write: writeMode = 'replace' } = options ?? {};\n\n if (typeof window === 'undefined') {\n return {\n value: initialValue,\n remove: () => {},\n set: () => {}\n } as UseUrlSearchParamReturn<Value>;\n }\n\n const serializer = (value: Value) => {\n if (options?.serializer) return options.serializer(value);\n if (typeof value === 'string') return value;\n\n return JSON.stringify(value);\n };\n\n const deserializer = (value: string) => {\n if (options?.deserializer) return options.deserializer(value);\n if (value === 'undefined' || value === 'null') return undefined as unknown as Value;\n\n try {\n return JSON.parse(value) as Value;\n } catch {\n return value as Value;\n }\n };\n\n const setUrlSearchParam = (\n key: string,\n value: Value | undefined,\n mode: UrlSearchParamMode,\n write: 'push' | 'replace' = 'replace'\n ) => {\n const urlSearchParams = getUrlSearchParams(mode);\n\n if (value === undefined) {\n urlSearchParams.delete(key);\n } else {\n const serializedValue = serializer ? serializer(value) : String(value);\n urlSearchParams.set(key, serializedValue);\n }\n\n const query = createQueryString(urlSearchParams, mode);\n if (write === 'replace') window.history.replaceState({}, '', query);\n if (write === 'push') window.history.pushState({}, '', query);\n\n dispatchUrlSearchParamsEvent();\n };\n\n const [value, setValue] = useState<Value | undefined>(() => {\n const urlSearchParams = getUrlSearchParams(mode);\n const currentValue = urlSearchParams.get(key);\n\n if (currentValue === null && initialValue !== undefined) {\n setUrlSearchParam(key, initialValue, mode, writeMode);\n return initialValue;\n }\n\n return currentValue ? deserializer(currentValue) : undefined;\n });\n\n const set = (value: Value, options?: UseUrlSearchParamsActionOptions) => {\n setUrlSearchParam(key, value, mode, options?.write ?? writeMode);\n setValue(value);\n };\n\n const remove = (options?: UseUrlSearchParamsActionOptions) => {\n setUrlSearchParam(key, undefined, mode, options?.write ?? writeMode);\n setValue(undefined);\n };\n\n useEffect(() => {\n const onParamsChange = () => {\n const urlSearchParams = getUrlSearchParams(mode);\n const newValue = urlSearchParams.get(key);\n setValue(newValue ? deserializer(newValue) : undefined);\n };\n\n window.addEventListener(URL_SEARCH_PARAMS_EVENT, onParamsChange);\n window.addEventListener('popstate', onParamsChange);\n if (mode !== 'history') {\n window.addEventListener('hashchange', onParamsChange);\n }\n\n return () => {\n window.removeEventListener(URL_SEARCH_PARAMS_EVENT, onParamsChange);\n window.removeEventListener('popstate', onParamsChange);\n if (mode !== 'history') {\n window.removeEventListener('hashchange', onParamsChange);\n }\n };\n }, [key, mode]);\n\n return {\n value,\n remove,\n set\n };\n}) as UseUrlSearchParam;\n"],"names":["URL_SEARCH_PARAMS_EVENT","getUrlSearchParams","mode","search","hash","path","index","createQueryString","searchParams","searchParamsString","base","dispatchUrlSearchParamsEvent","useUrlSearchParam","key","params","options","initialValue","writeMode","serializer","value","deserializer","setUrlSearchParam","write","urlSearchParams","serializedValue","query","setValue","useState","currentValue","set","remove","useEffect","onParamsChange","newValue"],"mappings":"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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState as L, useEffect as v } from "react";
|
|
2
2
|
import { getUrlSearchParams as u, URL_SEARCH_PARAMS_EVENT as g, createQueryString as U, dispatchUrlSearchParamsEvent as m } from "../useUrlSearchParam/useUrlSearchParam.mjs";
|
|
3
|
-
const A = (t) => {
|
|
3
|
+
const A = ((t) => {
|
|
4
4
|
const i = typeof t == "object" && t && ("serializer" in t || "deserializer" in t || "initialValue" in t || "mode" in t || "write" in t) ? t : {}, c = i ? i?.initialValue : t, { mode: n = "history", write: f = "replace" } = i, l = (e) => i?.serializer ? i.serializer(e) : typeof e == "string" ? e : JSON.stringify(e), p = (e) => {
|
|
5
5
|
if (i?.deserializer) return i.deserializer(e);
|
|
6
6
|
if (e !== "undefined")
|
|
@@ -51,7 +51,7 @@ const A = (t) => {
|
|
|
51
51
|
value: w,
|
|
52
52
|
set: P
|
|
53
53
|
};
|
|
54
|
-
};
|
|
54
|
+
});
|
|
55
55
|
export {
|
|
56
56
|
U as createQueryString,
|
|
57
57
|
m as dispatchUrlSearchParamsEvent,
|