@siberiacancode/reactuse 0.2.11 → 0.2.13
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/README.md +71 -71
- package/dist/cjs/helpers/createContext/createContext.cjs.map +1 -1
- package/dist/cjs/helpers/createReactiveContext/createReactiveContext.cjs.map +1 -1
- package/dist/cjs/helpers/createStore/createStore.cjs.map +1 -1
- package/dist/cjs/hooks/useActiveElement/useActiveElement.cjs +1 -1
- package/dist/cjs/hooks/useActiveElement/useActiveElement.cjs.map +1 -1
- package/dist/cjs/hooks/useAsync/useAsync.cjs.map +1 -1
- package/dist/cjs/hooks/useBattery/useBattery.cjs +1 -1
- package/dist/cjs/hooks/useBattery/useBattery.cjs.map +1 -1
- package/dist/cjs/hooks/useBluetooth/useBluetooth.cjs +1 -1
- package/dist/cjs/hooks/useBluetooth/useBluetooth.cjs.map +1 -1
- package/dist/cjs/hooks/useBoolean/useBoolean.cjs.map +1 -1
- package/dist/cjs/hooks/useBreakpoints/useBreakpoints.cjs.map +1 -1
- package/dist/cjs/hooks/useBrowserLanguage/useBrowserLanguage.cjs.map +1 -1
- package/dist/cjs/hooks/useClickOutside/useClickOutside.cjs.map +1 -1
- package/dist/cjs/hooks/useClipboard/useClipboard.cjs +1 -1
- package/dist/cjs/hooks/useClipboard/useClipboard.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/useCookies/useCookies.cjs +1 -1
- package/dist/cjs/hooks/useCookies/useCookies.cjs.map +1 -1
- package/dist/cjs/hooks/useCopy/useCopy.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/useDebounceCallback/useDebounceCallback.cjs.map +1 -1
- package/dist/cjs/hooks/useDebounceValue/useDebounceValue.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/useDeviceOrientation/useDeviceOrientation.cjs.map +1 -1
- package/dist/cjs/hooks/useDevicePixelRatio/useDevicePixelRatio.cjs.map +1 -1
- package/dist/cjs/hooks/useDidUpdate/useDidUpdate.cjs.map +1 -1
- package/dist/cjs/hooks/useDisclosure/useDisclosure.cjs +1 -1
- package/dist/cjs/hooks/useDisclosure/useDisclosure.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/useDocumentEvent/useDocumentEvent.cjs.map +1 -1
- package/dist/cjs/hooks/useDocumentTitle/useDocumentTitle.cjs +1 -1
- package/dist/cjs/hooks/useDocumentTitle/useDocumentTitle.cjs.map +1 -1
- package/dist/cjs/hooks/useDocumentVisibility/useDocumentVisibility.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.map +1 -1
- package/dist/cjs/hooks/useEvent/useEvent.cjs.map +1 -1
- package/dist/cjs/hooks/useEventListener/useEventListener.cjs.map +1 -1
- package/dist/cjs/hooks/useEventSource/useEventSource.cjs +1 -1
- package/dist/cjs/hooks/useEventSource/useEventSource.cjs.map +1 -1
- package/dist/cjs/hooks/useEyeDropper/useEyeDropper.cjs.map +1 -1
- package/dist/cjs/hooks/useFavicon/useFavicon.cjs +1 -1
- package/dist/cjs/hooks/useFavicon/useFavicon.cjs.map +1 -1
- package/dist/cjs/hooks/useField/useField.cjs +1 -1
- package/dist/cjs/hooks/useField/useField.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/useFps/useFps.cjs.map +1 -1
- package/dist/cjs/hooks/useFul/useFul.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/useGamepad/useGamepad.cjs.map +1 -1
- package/dist/cjs/hooks/useGeolocation/useGeolocation.cjs +1 -1
- package/dist/cjs/hooks/useGeolocation/useGeolocation.cjs.map +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/useIdle/useIdle.cjs.map +1 -1
- package/dist/cjs/hooks/useImage/useImage.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/useIsFirstRender/useIsFirstRender.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/useLastChanged/useLastChanged.cjs +1 -1
- package/dist/cjs/hooks/useLastChanged/useLastChanged.cjs.map +1 -1
- package/dist/cjs/hooks/useLatest/useLatest.cjs +1 -1
- package/dist/cjs/hooks/useLatest/useLatest.cjs.map +1 -1
- package/dist/cjs/hooks/useLess/useLess.cjs.map +1 -1
- package/dist/cjs/hooks/useList/useList.cjs.map +1 -1
- package/dist/cjs/hooks/useLockCallback/useLockCallback.cjs.map +1 -1
- package/dist/cjs/hooks/useLogger/useLogger.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/useMap/useMap.cjs.map +1 -1
- package/dist/cjs/hooks/useMeasure/useMeasure.cjs.map +1 -1
- package/dist/cjs/hooks/useMediaQuery/useMediaQuery.cjs.map +1 -1
- package/dist/cjs/hooks/useMemory/useMemory.cjs +1 -1
- package/dist/cjs/hooks/useMemory/useMemory.cjs.map +1 -1
- package/dist/cjs/hooks/useMount/useMount.cjs.map +1 -1
- package/dist/cjs/hooks/useMouse/useMouse.cjs.map +1 -1
- package/dist/cjs/hooks/useMutation/useMutation.cjs +1 -1
- package/dist/cjs/hooks/useMutation/useMutation.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/useNetwork/useNetwork.cjs +1 -1
- package/dist/cjs/hooks/useNetwork/useNetwork.cjs.map +1 -1
- package/dist/cjs/hooks/useOffsetPagination/useOffsetPagination.cjs.map +1 -1
- package/dist/cjs/hooks/useOnce/useOnce.cjs.map +1 -1
- package/dist/cjs/hooks/useOnline/useOnline.cjs.map +1 -1
- package/dist/cjs/hooks/useOperatingSystem/useOperatingSystem.cjs.map +1 -1
- package/dist/cjs/hooks/useOptimistic/useOptimistic.cjs.map +1 -1
- package/dist/cjs/hooks/useOrientation/useOrientation.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/usePageLeave/usePageLeave.cjs +1 -1
- package/dist/cjs/hooks/usePageLeave/usePageLeave.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.map +1 -1
- package/dist/cjs/hooks/usePerformanceObserver/usePerformanceObserver.cjs +1 -1
- package/dist/cjs/hooks/usePerformanceObserver/usePerformanceObserver.cjs.map +1 -1
- package/dist/cjs/hooks/usePermission/usePermission.cjs +1 -1
- package/dist/cjs/hooks/usePermission/usePermission.cjs.map +1 -1
- package/dist/cjs/hooks/usePointerLock/usePointerLock.cjs.map +1 -1
- package/dist/cjs/hooks/usePostMessage/usePostMessage.cjs.map +1 -1
- package/dist/cjs/hooks/usePreferredColorScheme/usePreferredColorScheme.cjs.map +1 -1
- package/dist/cjs/hooks/usePreferredContrast/usePreferredContrast.cjs.map +1 -1
- package/dist/cjs/hooks/usePreferredLanguages/usePreferredLanguages.cjs.map +1 -1
- package/dist/cjs/hooks/usePreferredReducedMotion/usePreferredReducedMotion.cjs.map +1 -1
- package/dist/cjs/hooks/usePrevious/usePrevious.cjs +1 -1
- package/dist/cjs/hooks/usePrevious/usePrevious.cjs.map +1 -1
- package/dist/cjs/hooks/useQuery/useQuery.cjs +1 -1
- package/dist/cjs/hooks/useQuery/useQuery.cjs.map +1 -1
- package/dist/cjs/hooks/useQueue/useQueue.cjs.map +1 -1
- package/dist/cjs/hooks/useRaf/useRaf.cjs +1 -1
- package/dist/cjs/hooks/useRaf/useRaf.cjs.map +1 -1
- package/dist/cjs/hooks/useRafValue/useRafValue.cjs.map +1 -1
- package/dist/cjs/hooks/useRefState/useRefState.cjs.map +1 -1
- package/dist/cjs/hooks/useRenderCount/useRenderCount.cjs.map +1 -1
- package/dist/cjs/hooks/useRenderInfo/useRenderInfo.cjs.map +1 -1
- package/dist/cjs/hooks/useRerender/useRerender.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/useScreenOrientation/useScreenOrientation.cjs +1 -1
- package/dist/cjs/hooks/useScreenOrientation/useScreenOrientation.cjs.map +1 -1
- package/dist/cjs/hooks/useScript/useScript.cjs +1 -1
- package/dist/cjs/hooks/useScript/useScript.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.map +1 -1
- package/dist/cjs/hooks/useScrollTo/useScrollTo.cjs.map +1 -1
- package/dist/cjs/hooks/useSet/useSet.cjs.map +1 -1
- package/dist/cjs/hooks/useShare/useShare.cjs.map +1 -1
- package/dist/cjs/hooks/useSpeechRecognition/useSpeechRecognition.cjs +1 -1
- package/dist/cjs/hooks/useSpeechRecognition/useSpeechRecognition.cjs.map +1 -1
- package/dist/cjs/hooks/useSpeechSynthesis/useSpeechSynthesis.cjs +1 -1
- package/dist/cjs/hooks/useSpeechSynthesis/useSpeechSynthesis.cjs.map +1 -1
- package/dist/cjs/hooks/useStateHistory/useStateHistory.cjs.map +1 -1
- package/dist/cjs/hooks/useStep/useStep.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/useTextSelection/useTextSelection.cjs +1 -1
- package/dist/cjs/hooks/useTextSelection/useTextSelection.cjs.map +1 -1
- package/dist/cjs/hooks/useThrottleCallback/useThrottleCallback.cjs.map +1 -1
- package/dist/cjs/hooks/useThrottleValue/useThrottleValue.cjs.map +1 -1
- package/dist/cjs/hooks/useTime/useTime.cjs.map +1 -1
- package/dist/cjs/hooks/useTimeout/useTimeout.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/useToggle/useToggle.cjs.map +1 -1
- package/dist/cjs/hooks/useUnmount/useUnmount.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/useVibrate/useVibrate.cjs.map +1 -1
- package/dist/cjs/hooks/useWakeLock/useWakeLock.cjs +1 -1
- package/dist/cjs/hooks/useWakeLock/useWakeLock.cjs.map +1 -1
- package/dist/cjs/hooks/useWebSocket/useWebSocket.cjs +1 -1
- package/dist/cjs/hooks/useWebSocket/useWebSocket.cjs.map +1 -1
- package/dist/cjs/hooks/useWindowEvent/useWindowEvent.cjs.map +1 -1
- package/dist/cjs/hooks/useWindowFocus/useWindowFocus.cjs.map +1 -1
- package/dist/cjs/hooks/useWindowScroll/useWindowScroll.cjs.map +1 -1
- package/dist/cjs/hooks/useWindowSize/useWindowSize.cjs +1 -1
- package/dist/cjs/hooks/useWindowSize/useWindowSize.cjs.map +1 -1
- package/dist/cjs/hooks/useWizard/useWizard.cjs +1 -1
- package/dist/cjs/hooks/useWizard/useWizard.cjs.map +1 -1
- package/dist/cjs/utils/helpers/copy.cjs.map +1 -1
- package/dist/cjs/utils/helpers/debounce.cjs.map +1 -1
- package/dist/cjs/utils/helpers/getDate.cjs.map +1 -1
- package/dist/cjs/utils/helpers/getElement.cjs.map +1 -1
- package/dist/cjs/utils/helpers/getRetry.cjs.map +1 -1
- package/dist/cjs/utils/helpers/isTarget.cjs.map +1 -1
- package/dist/cjs/utils/helpers/throttle.cjs.map +1 -1
- package/dist/esm/helpers/createContext/createContext.mjs.map +1 -1
- package/dist/esm/helpers/createReactiveContext/createReactiveContext.mjs.map +1 -1
- package/dist/esm/helpers/createStore/createStore.mjs.map +1 -1
- package/dist/esm/hooks/useActiveElement/useActiveElement.mjs +1 -1
- package/dist/esm/hooks/useActiveElement/useActiveElement.mjs.map +1 -1
- package/dist/esm/hooks/useAsync/useAsync.mjs.map +1 -1
- package/dist/esm/hooks/useBattery/useBattery.mjs +10 -10
- package/dist/esm/hooks/useBattery/useBattery.mjs.map +1 -1
- package/dist/esm/hooks/useBluetooth/useBluetooth.mjs +14 -15
- package/dist/esm/hooks/useBluetooth/useBluetooth.mjs.map +1 -1
- package/dist/esm/hooks/useBoolean/useBoolean.mjs.map +1 -1
- package/dist/esm/hooks/useBreakpoints/useBreakpoints.mjs.map +1 -1
- package/dist/esm/hooks/useBrowserLanguage/useBrowserLanguage.mjs.map +1 -1
- package/dist/esm/hooks/useClickOutside/useClickOutside.mjs.map +1 -1
- package/dist/esm/hooks/useClipboard/useClipboard.mjs +13 -14
- package/dist/esm/hooks/useClipboard/useClipboard.mjs.map +1 -1
- package/dist/esm/hooks/useConst/useConst.mjs.map +1 -1
- package/dist/esm/hooks/useCookie/useCookie.mjs +27 -27
- package/dist/esm/hooks/useCookie/useCookie.mjs.map +1 -1
- package/dist/esm/hooks/useCookies/useCookies.mjs +19 -19
- package/dist/esm/hooks/useCookies/useCookies.mjs.map +1 -1
- package/dist/esm/hooks/useCopy/useCopy.mjs.map +1 -1
- package/dist/esm/hooks/useCounter/useCounter.mjs +5 -6
- package/dist/esm/hooks/useCounter/useCounter.mjs.map +1 -1
- package/dist/esm/hooks/useCssVar/useCssVar.mjs +13 -14
- package/dist/esm/hooks/useCssVar/useCssVar.mjs.map +1 -1
- package/dist/esm/hooks/useDebounceCallback/useDebounceCallback.mjs.map +1 -1
- package/dist/esm/hooks/useDebounceValue/useDebounceValue.mjs.map +1 -1
- package/dist/esm/hooks/useDefault/useDefault.mjs.map +1 -1
- package/dist/esm/hooks/useDeviceMotion/useDeviceMotion.mjs +18 -19
- package/dist/esm/hooks/useDeviceMotion/useDeviceMotion.mjs.map +1 -1
- package/dist/esm/hooks/useDeviceOrientation/useDeviceOrientation.mjs.map +1 -1
- package/dist/esm/hooks/useDevicePixelRatio/useDevicePixelRatio.mjs.map +1 -1
- package/dist/esm/hooks/useDidUpdate/useDidUpdate.mjs.map +1 -1
- package/dist/esm/hooks/useDisclosure/useDisclosure.mjs +5 -11
- package/dist/esm/hooks/useDisclosure/useDisclosure.mjs.map +1 -1
- package/dist/esm/hooks/useDisplayMedia/useDisplayMedia.mjs +28 -28
- package/dist/esm/hooks/useDisplayMedia/useDisplayMedia.mjs.map +1 -1
- package/dist/esm/hooks/useDocumentEvent/useDocumentEvent.mjs.map +1 -1
- package/dist/esm/hooks/useDocumentTitle/useDocumentTitle.mjs +15 -15
- package/dist/esm/hooks/useDocumentTitle/useDocumentTitle.mjs.map +1 -1
- package/dist/esm/hooks/useDocumentVisibility/useDocumentVisibility.mjs.map +1 -1
- package/dist/esm/hooks/useDoubleClick/useDoubleClick.mjs +11 -13
- package/dist/esm/hooks/useDoubleClick/useDoubleClick.mjs.map +1 -1
- package/dist/esm/hooks/useDropZone/useDropZone.mjs +27 -28
- package/dist/esm/hooks/useDropZone/useDropZone.mjs.map +1 -1
- package/dist/esm/hooks/useElementSize/useElementSize.mjs.map +1 -1
- package/dist/esm/hooks/useEvent/useEvent.mjs.map +1 -1
- package/dist/esm/hooks/useEventListener/useEventListener.mjs.map +1 -1
- package/dist/esm/hooks/useEventSource/useEventSource.mjs +37 -40
- package/dist/esm/hooks/useEventSource/useEventSource.mjs.map +1 -1
- package/dist/esm/hooks/useEyeDropper/useEyeDropper.mjs.map +1 -1
- package/dist/esm/hooks/useFavicon/useFavicon.mjs +11 -12
- package/dist/esm/hooks/useFavicon/useFavicon.mjs.map +1 -1
- package/dist/esm/hooks/useField/useField.mjs +45 -49
- package/dist/esm/hooks/useField/useField.mjs.map +1 -1
- package/dist/esm/hooks/useFileDialog/useFileDialog.mjs +22 -25
- package/dist/esm/hooks/useFileDialog/useFileDialog.mjs.map +1 -1
- package/dist/esm/hooks/useFocus/useFocus.mjs +24 -32
- package/dist/esm/hooks/useFocus/useFocus.mjs.map +1 -1
- package/dist/esm/hooks/useFps/useFps.mjs.map +1 -1
- package/dist/esm/hooks/useFul/useFul.mjs.map +1 -1
- package/dist/esm/hooks/useFullscreen/useFullscreen.mjs +23 -24
- package/dist/esm/hooks/useFullscreen/useFullscreen.mjs.map +1 -1
- package/dist/esm/hooks/useGamepad/useGamepad.mjs.map +1 -1
- package/dist/esm/hooks/useGeolocation/useGeolocation.mjs +24 -24
- package/dist/esm/hooks/useGeolocation/useGeolocation.mjs.map +1 -1
- package/dist/esm/hooks/useHash/useHash.mjs.map +1 -1
- package/dist/esm/hooks/useHotkeys/useHotkeys.mjs +24 -25
- package/dist/esm/hooks/useHotkeys/useHotkeys.mjs.map +1 -1
- package/dist/esm/hooks/useHover/useHover.mjs +20 -22
- package/dist/esm/hooks/useHover/useHover.mjs.map +1 -1
- package/dist/esm/hooks/useIdle/useIdle.mjs.map +1 -1
- package/dist/esm/hooks/useImage/useImage.mjs.map +1 -1
- package/dist/esm/hooks/useInfiniteScroll/useInfiniteScroll.mjs +14 -14
- package/dist/esm/hooks/useInfiniteScroll/useInfiniteScroll.mjs.map +1 -1
- package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs +19 -20
- package/dist/esm/hooks/useIntersectionObserver/useIntersectionObserver.mjs.map +1 -1
- package/dist/esm/hooks/useInterval/useInterval.mjs +8 -8
- package/dist/esm/hooks/useInterval/useInterval.mjs.map +1 -1
- package/dist/esm/hooks/useIsFirstRender/useIsFirstRender.mjs.map +1 -1
- package/dist/esm/hooks/useKeyPress/useKeyPress.mjs +22 -24
- package/dist/esm/hooks/useKeyPress/useKeyPress.mjs.map +1 -1
- package/dist/esm/hooks/useKeyPressEvent/useKeyPressEvent.mjs +23 -23
- package/dist/esm/hooks/useKeyPressEvent/useKeyPressEvent.mjs.map +1 -1
- package/dist/esm/hooks/useKeyboard/useKeyboard.mjs +16 -22
- package/dist/esm/hooks/useKeyboard/useKeyboard.mjs.map +1 -1
- package/dist/esm/hooks/useKeysPressed/useKeysPressed.mjs +20 -20
- package/dist/esm/hooks/useKeysPressed/useKeysPressed.mjs.map +1 -1
- package/dist/esm/hooks/useLastChanged/useLastChanged.mjs +6 -6
- package/dist/esm/hooks/useLastChanged/useLastChanged.mjs.map +1 -1
- package/dist/esm/hooks/useLatest/useLatest.mjs +12 -4
- package/dist/esm/hooks/useLatest/useLatest.mjs.map +1 -1
- package/dist/esm/hooks/useLess/useLess.mjs.map +1 -1
- package/dist/esm/hooks/useList/useList.mjs.map +1 -1
- package/dist/esm/hooks/useLockCallback/useLockCallback.mjs.map +1 -1
- package/dist/esm/hooks/useLogger/useLogger.mjs.map +1 -1
- package/dist/esm/hooks/useLongPress/useLongPress.mjs +23 -27
- package/dist/esm/hooks/useLongPress/useLongPress.mjs.map +1 -1
- package/dist/esm/hooks/useMap/useMap.mjs.map +1 -1
- package/dist/esm/hooks/useMeasure/useMeasure.mjs.map +1 -1
- package/dist/esm/hooks/useMediaQuery/useMediaQuery.mjs.map +1 -1
- package/dist/esm/hooks/useMemory/useMemory.mjs +1 -1
- package/dist/esm/hooks/useMemory/useMemory.mjs.map +1 -1
- package/dist/esm/hooks/useMount/useMount.mjs.map +1 -1
- package/dist/esm/hooks/useMouse/useMouse.mjs.map +1 -1
- package/dist/esm/hooks/useMutation/useMutation.mjs +38 -42
- package/dist/esm/hooks/useMutation/useMutation.mjs.map +1 -1
- package/dist/esm/hooks/useMutationObserver/useMutationObserver.mjs +12 -12
- package/dist/esm/hooks/useMutationObserver/useMutationObserver.mjs.map +1 -1
- package/dist/esm/hooks/useNetwork/useNetwork.mjs +28 -28
- package/dist/esm/hooks/useNetwork/useNetwork.mjs.map +1 -1
- package/dist/esm/hooks/useOffsetPagination/useOffsetPagination.mjs.map +1 -1
- package/dist/esm/hooks/useOnce/useOnce.mjs.map +1 -1
- package/dist/esm/hooks/useOnline/useOnline.mjs.map +1 -1
- package/dist/esm/hooks/useOperatingSystem/useOperatingSystem.mjs.map +1 -1
- package/dist/esm/hooks/useOptimistic/useOptimistic.mjs.map +1 -1
- package/dist/esm/hooks/useOrientation/useOrientation.mjs.map +1 -1
- package/dist/esm/hooks/useOtpCredential/useOtpCredential.mjs +12 -13
- package/dist/esm/hooks/useOtpCredential/useOtpCredential.mjs.map +1 -1
- package/dist/esm/hooks/usePageLeave/usePageLeave.mjs +6 -6
- package/dist/esm/hooks/usePageLeave/usePageLeave.mjs.map +1 -1
- package/dist/esm/hooks/usePaint/usePaint.mjs +87 -93
- package/dist/esm/hooks/usePaint/usePaint.mjs.map +1 -1
- package/dist/esm/hooks/useParallax/useParallax.mjs.map +1 -1
- package/dist/esm/hooks/usePerformanceObserver/usePerformanceObserver.mjs +17 -19
- package/dist/esm/hooks/usePerformanceObserver/usePerformanceObserver.mjs.map +1 -1
- package/dist/esm/hooks/usePermission/usePermission.mjs +13 -13
- package/dist/esm/hooks/usePermission/usePermission.mjs.map +1 -1
- package/dist/esm/hooks/usePointerLock/usePointerLock.mjs.map +1 -1
- package/dist/esm/hooks/usePostMessage/usePostMessage.mjs.map +1 -1
- package/dist/esm/hooks/usePreferredColorScheme/usePreferredColorScheme.mjs.map +1 -1
- package/dist/esm/hooks/usePreferredContrast/usePreferredContrast.mjs.map +1 -1
- package/dist/esm/hooks/usePreferredDark/usePreferredDark.mjs.map +1 -1
- package/dist/esm/hooks/usePreferredLanguages/usePreferredLanguages.mjs.map +1 -1
- package/dist/esm/hooks/usePrevious/usePrevious.mjs +4 -4
- package/dist/esm/hooks/usePrevious/usePrevious.mjs.map +1 -1
- package/dist/esm/hooks/useQuery/useQuery.mjs +39 -41
- package/dist/esm/hooks/useQuery/useQuery.mjs.map +1 -1
- package/dist/esm/hooks/useQueue/useQueue.mjs.map +1 -1
- package/dist/esm/hooks/useRaf/useRaf.mjs +21 -21
- package/dist/esm/hooks/useRaf/useRaf.mjs.map +1 -1
- package/dist/esm/hooks/useRafValue/useRafValue.mjs.map +1 -1
- package/dist/esm/hooks/useRefState/useRefState.mjs.map +1 -1
- package/dist/esm/hooks/useRenderCount/useRenderCount.mjs.map +1 -1
- package/dist/esm/hooks/useRenderInfo/useRenderInfo.mjs.map +1 -1
- package/dist/esm/hooks/useRerender/useRerender.mjs.map +1 -1
- package/dist/esm/hooks/useResizeObserver/useResizeObserver.mjs +16 -17
- package/dist/esm/hooks/useResizeObserver/useResizeObserver.mjs.map +1 -1
- package/dist/esm/hooks/useScreenOrientation/useScreenOrientation.mjs +11 -11
- package/dist/esm/hooks/useScreenOrientation/useScreenOrientation.mjs.map +1 -1
- package/dist/esm/hooks/useScript/useScript.mjs +16 -16
- package/dist/esm/hooks/useScript/useScript.mjs.map +1 -1
- package/dist/esm/hooks/useScroll/useScroll.mjs +37 -39
- package/dist/esm/hooks/useScroll/useScroll.mjs.map +1 -1
- package/dist/esm/hooks/useScrollIntoView/useScrollIntoView.mjs.map +1 -1
- package/dist/esm/hooks/useScrollTo/useScrollTo.mjs.map +1 -1
- package/dist/esm/hooks/useSet/useSet.mjs.map +1 -1
- package/dist/esm/hooks/useShare/useShare.mjs.map +1 -1
- package/dist/esm/hooks/useSpeechRecognition/useSpeechRecognition.mjs +35 -35
- package/dist/esm/hooks/useSpeechRecognition/useSpeechRecognition.mjs.map +1 -1
- package/dist/esm/hooks/useSpeechSynthesis/useSpeechSynthesis.mjs +36 -42
- package/dist/esm/hooks/useSpeechSynthesis/useSpeechSynthesis.mjs.map +1 -1
- package/dist/esm/hooks/useStateHistory/useStateHistory.mjs.map +1 -1
- package/dist/esm/hooks/useStep/useStep.mjs.map +1 -1
- package/dist/esm/hooks/useSticky/useSticky.mjs +12 -12
- package/dist/esm/hooks/useSticky/useSticky.mjs.map +1 -1
- package/dist/esm/hooks/useStopwatch/useStopwatch.mjs +10 -11
- package/dist/esm/hooks/useStopwatch/useStopwatch.mjs.map +1 -1
- package/dist/esm/hooks/useStorage/useStorage.mjs +27 -27
- package/dist/esm/hooks/useStorage/useStorage.mjs.map +1 -1
- package/dist/esm/hooks/useTextDirection/useTextDirection.mjs +21 -24
- package/dist/esm/hooks/useTextDirection/useTextDirection.mjs.map +1 -1
- package/dist/esm/hooks/useTextSelection/useTextSelection.mjs +7 -7
- package/dist/esm/hooks/useTextSelection/useTextSelection.mjs.map +1 -1
- package/dist/esm/hooks/useThrottleCallback/useThrottleCallback.mjs.map +1 -1
- package/dist/esm/hooks/useThrottleValue/useThrottleValue.mjs.map +1 -1
- package/dist/esm/hooks/useTime/useTime.mjs.map +1 -1
- package/dist/esm/hooks/useTimeout/useTimeout.mjs.map +1 -1
- package/dist/esm/hooks/useTimer/useTimer.mjs +51 -54
- package/dist/esm/hooks/useTimer/useTimer.mjs.map +1 -1
- package/dist/esm/hooks/useToggle/useToggle.mjs.map +1 -1
- package/dist/esm/hooks/useUnmount/useUnmount.mjs.map +1 -1
- package/dist/esm/hooks/useUrlSearchParam/useUrlSearchParam.mjs +38 -38
- package/dist/esm/hooks/useUrlSearchParam/useUrlSearchParam.mjs.map +1 -1
- package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs +29 -29
- package/dist/esm/hooks/useUrlSearchParams/useUrlSearchParams.mjs.map +1 -1
- package/dist/esm/hooks/useVibrate/useVibrate.mjs.map +1 -1
- package/dist/esm/hooks/useWakeLock/useWakeLock.mjs +16 -16
- package/dist/esm/hooks/useWakeLock/useWakeLock.mjs.map +1 -1
- package/dist/esm/hooks/useWebSocket/useWebSocket.mjs +28 -38
- package/dist/esm/hooks/useWebSocket/useWebSocket.mjs.map +1 -1
- package/dist/esm/hooks/useWindowEvent/useWindowEvent.mjs.map +1 -1
- package/dist/esm/hooks/useWindowFocus/useWindowFocus.mjs.map +1 -1
- package/dist/esm/hooks/useWindowScroll/useWindowScroll.mjs.map +1 -1
- package/dist/esm/hooks/useWindowSize/useWindowSize.mjs +9 -9
- package/dist/esm/hooks/useWindowSize/useWindowSize.mjs.map +1 -1
- package/dist/esm/hooks/useWizard/useWizard.mjs +9 -11
- package/dist/esm/hooks/useWizard/useWizard.mjs.map +1 -1
- package/dist/esm/utils/helpers/copy.mjs.map +1 -1
- package/dist/esm/utils/helpers/debounce.mjs.map +1 -1
- package/dist/esm/utils/helpers/getDate.mjs.map +1 -1
- package/dist/esm/utils/helpers/getElement.mjs.map +1 -1
- package/dist/esm/utils/helpers/getRetry.mjs.map +1 -1
- package/dist/esm/utils/helpers/isTarget.mjs.map +1 -1
- package/dist/esm/utils/helpers/throttle.mjs.map +1 -1
- package/dist/types/hooks/useLatest/useLatest.d.ts +7 -2
- package/package.json +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOffsetPagination.mjs","sources":["../../../../src/hooks/useOffsetPagination/useOffsetPagination.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\n/** The use offset pagination return type */\nexport interface UseOffsetPaginationOptions {\n /** The initial page number */\n initialPage?: number;\n /** The number of items per page */\n pageSize?: number;\n /** The total number of items */\n total?: number;\n /** The callback function to be invoked when page changes */\n onPageChange?: ({ page, pageSize }: { page: number; pageSize: number }) => void;\n /** The callback function to be invoked when page count changes */\n onPageCountChange?: ({ page, pageSize }: { page: number; pageSize: number }) => void;\n /** The callback function to be invoked when page size changes */\n onPageSizeChange?: ({ page, pageSize }: { page: number; pageSize: number }) => void;\n}\n\n/** The use offset pagination return type */\nexport interface UseOffsetPaginationReturn {\n /** The number of items per page */\n currentPageSize: number;\n /** Whether the current page is the first page */\n isFirstPage: boolean;\n /** Whether the current page is the last page */\n isLastPage: boolean;\n /** The current page number */\n page: number;\n /** The total number of pages */\n pageCount: number;\n /** The callback function to go to the next page */\n next: () => void;\n /** The callback function to go to the previous page */\n prev: () => void;\n /** The callback function to set the current page */\n set: (page: number) => void;\n}\n\n/**\n * @name useOffsetPagination\n * @description - Hook that defines the logic when pagination\n * @category Utilities\n *\n * @param {UseOffsetPaginationOptions} options - The options for the hook\n * @param {number} [options.total] - The total number of items\n * @param {number} [options.pageSize] - The number of items per page\n * @param {number} [options.initialPage] - The current page\n * @param {({ page, pageSize }: { page: number; pageSize: number }) => void} [options.onPageChange] - The callback function to be invoked when page changes\n * @param {({ page, pageSize }: { page: number; pageSize: number }) => void} [options.onPageCountChange] - The callback function to be invoked when page count changes\n * @param {({ page, pageSize }: { page: number; pageSize: number }) => void} [options.onPageSizeChange] - The callback function to be invoked when page size changes\n * @returns {UseOffsetPaginationReturn} - The state of the hook\n *\n * @example\n * const { currentPage, currentPageSize, pageCount, isFirstPage, isLastPage, prev, next } = useOffsetPagination({\n * total: 100,\n * pageSize: 10,\n * page: 1,\n * onPageChange: (page) => {},\n * onPageCountChange: (pageCount) => {},\n * onPageSizeChange: (pageSize) => {}\n * });\n */\nexport const useOffsetPagination = ({\n total = Number.POSITIVE_INFINITY,\n pageSize = 10,\n initialPage = 1,\n onPageChange = () => {},\n onPageCountChange = () => {},\n onPageSizeChange = () => {}\n}: UseOffsetPaginationOptions = {}): UseOffsetPaginationReturn => {\n const [page, setPage] = useState(initialPage);\n\n const onPageChangeRef = useRef(onPageChange);\n const onPageCountChangeRef = useRef(onPageCountChange);\n const onPageSizeChangeRef = useRef(onPageSizeChange);\n\n onPageChangeRef.current = onPageChange;\n onPageCountChangeRef.current = onPageCountChange;\n onPageSizeChangeRef.current = onPageSizeChange;\n\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n const isFirstPage = page === 1;\n const isLastPage = page === pageCount;\n\n const next = () => {\n if (isLastPage) return onPageChange({ page: pageCount, pageSize });\n\n setPage((prevPage) => {\n const page = prevPage + 1;\n onPageChange({ page, pageSize });\n return page;\n });\n };\n\n const prev = () => {\n if (isFirstPage) return onPageChange({ page: 1, pageSize });\n\n setPage((prevPage) => {\n const page = prevPage - 1;\n onPageChange({ page, pageSize });\n return page;\n });\n };\n\n const set = (page: number) => {\n setPage(page);\n onPageChange({ page, pageSize });\n };\n\n useEffect(() => {\n onPageCountChangeRef.current({ page, pageSize });\n }, [pageCount]);\n\n useEffect(() => {\n onPageSizeChangeRef.current({ page, pageSize });\n }, [pageSize]);\n\n return {\n page,\n set,\n currentPageSize: pageSize,\n isFirstPage,\n isLastPage,\n pageCount,\n next,\n prev\n };\n};\n"],"names":["useOffsetPagination","total","pageSize","initialPage","onPageChange","onPageCountChange","onPageSizeChange","page","setPage","useState","onPageChangeRef","useRef","onPageCountChangeRef","onPageSizeChangeRef","pageCount","isFirstPage","isLastPage","next","prevPage","prev","set","useEffect"],"mappings":";AA8DO,MAAMA,IAAsB,CAAC;AAAA,EAClC,OAAAC,IAAQ,OAAO;AAAA,EACf,UAAAC,IAAW;AAAA,EACX,aAAAC,IAAc;AAAA,EACd,cAAAC,IAAe,MAAM;AAAA,
|
|
1
|
+
{"version":3,"file":"useOffsetPagination.mjs","sources":["../../../../src/hooks/useOffsetPagination/useOffsetPagination.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\n/** The use offset pagination return type */\nexport interface UseOffsetPaginationOptions {\n /** The initial page number */\n initialPage?: number;\n /** The number of items per page */\n pageSize?: number;\n /** The total number of items */\n total?: number;\n /** The callback function to be invoked when page changes */\n onPageChange?: ({ page, pageSize }: { page: number; pageSize: number }) => void;\n /** The callback function to be invoked when page count changes */\n onPageCountChange?: ({ page, pageSize }: { page: number; pageSize: number }) => void;\n /** The callback function to be invoked when page size changes */\n onPageSizeChange?: ({ page, pageSize }: { page: number; pageSize: number }) => void;\n}\n\n/** The use offset pagination return type */\nexport interface UseOffsetPaginationReturn {\n /** The number of items per page */\n currentPageSize: number;\n /** Whether the current page is the first page */\n isFirstPage: boolean;\n /** Whether the current page is the last page */\n isLastPage: boolean;\n /** The current page number */\n page: number;\n /** The total number of pages */\n pageCount: number;\n /** The callback function to go to the next page */\n next: () => void;\n /** The callback function to go to the previous page */\n prev: () => void;\n /** The callback function to set the current page */\n set: (page: number) => void;\n}\n\n/**\n * @name useOffsetPagination\n * @description - Hook that defines the logic when pagination\n * @category Utilities\n *\n * @param {UseOffsetPaginationOptions} options - The options for the hook\n * @param {number} [options.total] - The total number of items\n * @param {number} [options.pageSize] - The number of items per page\n * @param {number} [options.initialPage] - The current page\n * @param {({ page, pageSize }: { page: number; pageSize: number }) => void} [options.onPageChange] - The callback function to be invoked when page changes\n * @param {({ page, pageSize }: { page: number; pageSize: number }) => void} [options.onPageCountChange] - The callback function to be invoked when page count changes\n * @param {({ page, pageSize }: { page: number; pageSize: number }) => void} [options.onPageSizeChange] - The callback function to be invoked when page size changes\n * @returns {UseOffsetPaginationReturn} - The state of the hook\n *\n * @example\n * const { currentPage, currentPageSize, pageCount, isFirstPage, isLastPage, prev, next } = useOffsetPagination({\n * total: 100,\n * pageSize: 10,\n * page: 1,\n * onPageChange: (page) => {},\n * onPageCountChange: (pageCount) => {},\n * onPageSizeChange: (pageSize) => {}\n * });\n */\nexport const useOffsetPagination = ({\n total = Number.POSITIVE_INFINITY,\n pageSize = 10,\n initialPage = 1,\n onPageChange = () => {},\n onPageCountChange = () => {},\n onPageSizeChange = () => {}\n}: UseOffsetPaginationOptions = {}): UseOffsetPaginationReturn => {\n const [page, setPage] = useState(initialPage);\n\n const onPageChangeRef = useRef(onPageChange);\n const onPageCountChangeRef = useRef(onPageCountChange);\n const onPageSizeChangeRef = useRef(onPageSizeChange);\n\n onPageChangeRef.current = onPageChange;\n onPageCountChangeRef.current = onPageCountChange;\n onPageSizeChangeRef.current = onPageSizeChange;\n\n const pageCount = Math.max(1, Math.ceil(total / pageSize));\n\n const isFirstPage = page === 1;\n const isLastPage = page === pageCount;\n\n const next = () => {\n if (isLastPage) return onPageChange({ page: pageCount, pageSize });\n\n setPage((prevPage) => {\n const page = prevPage + 1;\n onPageChange({ page, pageSize });\n return page;\n });\n };\n\n const prev = () => {\n if (isFirstPage) return onPageChange({ page: 1, pageSize });\n\n setPage((prevPage) => {\n const page = prevPage - 1;\n onPageChange({ page, pageSize });\n return page;\n });\n };\n\n const set = (page: number) => {\n setPage(page);\n onPageChange({ page, pageSize });\n };\n\n useEffect(() => {\n onPageCountChangeRef.current({ page, pageSize });\n }, [pageCount]);\n\n useEffect(() => {\n onPageSizeChangeRef.current({ page, pageSize });\n }, [pageSize]);\n\n return {\n page,\n set,\n currentPageSize: pageSize,\n isFirstPage,\n isLastPage,\n pageCount,\n next,\n prev\n };\n};\n"],"names":["useOffsetPagination","total","pageSize","initialPage","onPageChange","onPageCountChange","onPageSizeChange","page","setPage","useState","onPageChangeRef","useRef","onPageCountChangeRef","onPageSizeChangeRef","pageCount","isFirstPage","isLastPage","next","prevPage","prev","set","useEffect"],"mappings":";AA8DO,MAAMA,IAAsB,CAAC;AAAA,EAClC,OAAAC,IAAQ,OAAO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AAAA,EAAA,GAC9C,CAACY,CAAS,CAAC,GAEdO,EAAU,MAAM;AACd,IAAAR,EAAoB,QAAQ,EAAE,MAAAN,GAAM,UAAAL,EAAA,CAAU;AAAA,EAAA,GAC7C,CAACA,CAAQ,CAAC,GAEN;AAAA,IACL,MAAAK;AAAA,IACA,KAAAa;AAAA,IACA,iBAAiBlB;AAAA,IACjB,aAAAa;AAAA,IACA,YAAAC;AAAA,IACA,WAAAF;AAAA,IACA,MAAAG;AAAA,IACA,MAAAE;AAAA,EAAA;AAEJ;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOnce.mjs","sources":["../../../../src/hooks/useOnce/useOnce.ts"],"sourcesContent":["import type { EffectCallback } from 'react';\n\nimport { useEffect, useRef } from 'react';\n\n/**\n * @name useEffectOnce\n * @description - Hook that runs an effect only once. Please do not use it in production code!\n * @category Humor\n *\n * @warning - This hook will run effect only once even in strict mode. Please do not use it in production code!\n *\n * @param {EffectCallback} effect The effect to run\n *\n * @example\n * useOnce(() => console.log('effect once'));\n */\nexport function useOnce(effect: EffectCallback) {\n const cleanupRef = useRef<ReturnType<EffectCallback>>(undefined);\n const hasRunRef = useRef(false);\n const hasRenderedAfterRun = useRef(false);\n\n if (hasRunRef.current) {\n hasRenderedAfterRun.current = true;\n }\n\n useEffect(() => {\n if (hasRunRef.current) return;\n\n hasRunRef.current = true;\n cleanupRef.current = effect();\n\n return () => {\n if (!hasRenderedAfterRun.current) return;\n\n if (typeof cleanupRef.current === 'function') {\n cleanupRef.current();\n }\n };\n }, []);\n}\n"],"names":["useOnce","effect","cleanupRef","useRef","hasRunRef","hasRenderedAfterRun","useEffect"],"mappings":";AAgBO,SAASA,EAAQC,GAAwB;
|
|
1
|
+
{"version":3,"file":"useOnce.mjs","sources":["../../../../src/hooks/useOnce/useOnce.ts"],"sourcesContent":["import type { EffectCallback } from 'react';\n\nimport { useEffect, useRef } from 'react';\n\n/**\n * @name useEffectOnce\n * @description - Hook that runs an effect only once. Please do not use it in production code!\n * @category Humor\n *\n * @warning - This hook will run effect only once even in strict mode. Please do not use it in production code!\n *\n * @param {EffectCallback} effect The effect to run\n *\n * @example\n * useOnce(() => console.log('effect once'));\n */\nexport function useOnce(effect: EffectCallback) {\n const cleanupRef = useRef<ReturnType<EffectCallback>>(undefined);\n const hasRunRef = useRef(false);\n const hasRenderedAfterRun = useRef(false);\n\n if (hasRunRef.current) {\n hasRenderedAfterRun.current = true;\n }\n\n useEffect(() => {\n if (hasRunRef.current) return;\n\n hasRunRef.current = true;\n cleanupRef.current = effect();\n\n return () => {\n if (!hasRenderedAfterRun.current) return;\n\n if (typeof cleanupRef.current === 'function') {\n cleanupRef.current();\n }\n };\n }, []);\n}\n"],"names":["useOnce","effect","cleanupRef","useRef","hasRunRef","hasRenderedAfterRun","useEffect"],"mappings":";AAgBO,SAASA,EAAQC,GAAwB;AAC9C,QAAMC,IAAaC,EAAmC,MAAS,GACzDC,IAAYD,EAAO,EAAK,GACxBE,IAAsBF,EAAO,EAAK;AAExC,EAAIC,EAAU,YACZC,EAAoB,UAAU,KAGhCC,EAAU,MAAM;AACd,QAAI,CAAAF,EAAU;AAEd,aAAAA,EAAU,UAAU,IACpBF,EAAW,UAAUD,EAAA,GAEd,MAAM;AACX,QAAKI,EAAoB,WAErB,OAAOH,EAAW,WAAY,cAChCA,EAAW,QAAA;AAAA,MACb;AAAA,EACF,GACC,EAAE;AACP;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOnline.mjs","sources":["../../../../src/hooks/useOnline/useOnline.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\n\nconst getSnapshot = () => navigator.onLine;\nconst getServerSnapshot = () => false;\nconst subscribe = (callback: () => void) => {\n window.addEventListener('online', callback);\n window.addEventListener('offline', callback);\n return () => {\n window.removeEventListener('online', callback);\n window.removeEventListener('offline', callback);\n };\n};\n\n/**\n * @name useOnline\n * @description - Hook that manages if the user is online\n * @category Sensors\n *\n * @browserapi navigator.onLine https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine\n *\n * @returns {boolean} A boolean indicating if the user is online\n *\n * @example\n * const online = useOnline();\n */\nexport const useOnline = () => useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n"],"names":["getSnapshot","getServerSnapshot","subscribe","callback","useOnline","useSyncExternalStore"],"mappings":";AAEA,MAAMA,IAAc,MAAM,UAAU,QAC9BC,IAAoB,MAAM,IAC1BC,IAAY,CAACC,
|
|
1
|
+
{"version":3,"file":"useOnline.mjs","sources":["../../../../src/hooks/useOnline/useOnline.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\n\nconst getSnapshot = () => navigator.onLine;\nconst getServerSnapshot = () => false;\nconst subscribe = (callback: () => void) => {\n window.addEventListener('online', callback);\n window.addEventListener('offline', callback);\n return () => {\n window.removeEventListener('online', callback);\n window.removeEventListener('offline', callback);\n };\n};\n\n/**\n * @name useOnline\n * @description - Hook that manages if the user is online\n * @category Sensors\n *\n * @browserapi navigator.onLine https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine\n *\n * @returns {boolean} A boolean indicating if the user is online\n *\n * @example\n * const online = useOnline();\n */\nexport const useOnline = () => useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n"],"names":["getSnapshot","getServerSnapshot","subscribe","callback","useOnline","useSyncExternalStore"],"mappings":";AAEA,MAAMA,IAAc,MAAM,UAAU,QAC9BC,IAAoB,MAAM,IAC1BC,IAAY,CAACC,OACjB,OAAO,iBAAiB,UAAUA,CAAQ,GAC1C,OAAO,iBAAiB,WAAWA,CAAQ,GACpC,MAAM;AACX,SAAO,oBAAoB,UAAUA,CAAQ,GAC7C,OAAO,oBAAoB,WAAWA,CAAQ;AAAA,IAgBrCC,IAAY,MAAMC,EAAqBH,GAAWF,GAAaC,CAAiB;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOperatingSystem.mjs","sources":["../../../../src/hooks/useOperatingSystem/useOperatingSystem.ts"],"sourcesContent":["import { useState } from 'react';\n\n/** The operating system type */\nexport type OperatingSystem = 'android' | 'ios' | 'linux' | 'macos' | 'undetermined' | 'windows';\n\nexport const getOperatingSystem = (): OperatingSystem => {\n if (typeof window === 'undefined') return 'undetermined';\n\n const { userAgent } = window.navigator;\n\n if (/Macintosh|MacIntel|MacPPC|Mac68K/i.test(userAgent)) return 'macos';\n if (/iPhone|iPad|iPod/i.test(userAgent)) return 'ios';\n if (/Win32|Win64|Windows|WinCE/i.test(userAgent)) return 'windows';\n if (/Android/i.test(userAgent)) return 'android';\n if (/Linux/i.test(userAgent)) return 'linux';\n\n return 'undetermined';\n};\n\n/**\n * @name useOperatingSystem\n * @description - Hook that returns the operating system of the current browser\n * @category Browser\n *\n * @returns {OperatingSystem} The operating system\n *\n * @example\n * const operatingSystem = useOperatingSystem();\n */\nexport const useOperatingSystem = () => {\n const [osOperatingSystem] = useState<OperatingSystem>(getOperatingSystem());\n return osOperatingSystem;\n};\n"],"names":["getOperatingSystem","userAgent","useOperatingSystem","osOperatingSystem","useState"],"mappings":";AAKO,MAAMA,IAAqB,MAAuB;
|
|
1
|
+
{"version":3,"file":"useOperatingSystem.mjs","sources":["../../../../src/hooks/useOperatingSystem/useOperatingSystem.ts"],"sourcesContent":["import { useState } from 'react';\n\n/** The operating system type */\nexport type OperatingSystem = 'android' | 'ios' | 'linux' | 'macos' | 'undetermined' | 'windows';\n\nexport const getOperatingSystem = (): OperatingSystem => {\n if (typeof window === 'undefined') return 'undetermined';\n\n const { userAgent } = window.navigator;\n\n if (/Macintosh|MacIntel|MacPPC|Mac68K/i.test(userAgent)) return 'macos';\n if (/iPhone|iPad|iPod/i.test(userAgent)) return 'ios';\n if (/Win32|Win64|Windows|WinCE/i.test(userAgent)) return 'windows';\n if (/Android/i.test(userAgent)) return 'android';\n if (/Linux/i.test(userAgent)) return 'linux';\n\n return 'undetermined';\n};\n\n/**\n * @name useOperatingSystem\n * @description - Hook that returns the operating system of the current browser\n * @category Browser\n *\n * @returns {OperatingSystem} The operating system\n *\n * @example\n * const operatingSystem = useOperatingSystem();\n */\nexport const useOperatingSystem = () => {\n const [osOperatingSystem] = useState<OperatingSystem>(getOperatingSystem());\n return osOperatingSystem;\n};\n"],"names":["getOperatingSystem","userAgent","useOperatingSystem","osOperatingSystem","useState"],"mappings":";AAKO,MAAMA,IAAqB,MAAuB;AACvD,MAAI,OAAO,SAAW,IAAa,QAAO;AAE1C,QAAM,EAAE,WAAAC,MAAc,OAAO;AAE7B,SAAI,oCAAoC,KAAKA,CAAS,IAAU,UAC5D,oBAAoB,KAAKA,CAAS,IAAU,QAC5C,6BAA6B,KAAKA,CAAS,IAAU,YACrD,WAAW,KAAKA,CAAS,IAAU,YACnC,SAAS,KAAKA,CAAS,IAAU,UAE9B;AACT,GAYaC,IAAqB,MAAM;AACtC,QAAM,CAACC,CAAiB,IAAIC,EAA0BJ,GAAoB;AAC1E,SAAOG;AACT;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOptimistic.mjs","sources":["../../../../src/hooks/useOptimistic/useOptimistic.ts"],"sourcesContent":["import { useRef, useState } from 'react';\n\nimport { useDidUpdate } from '../useDidUpdate/useDidUpdate';\n\nexport type UseOptimisticReturn<State> = [\n State,\n (optimisticValue: State, promise: Promise<any>) => void\n];\n\n/**\n * @name useOptimistic\n * @description - Hook that allows get optimistic value before its update\n * @category Utilities\n *\n * @template State The type of the state\n * @param {State} state The value to be returned initially and whenever no action is pending\n * @param {(currentState: State, optimisticValue: State) => State} update A pure function that takes the current state and the optimistic value passed to updateOptimistic and returns the resulting optimistic state\n * @returns {UseOptimisticReturn<State>} The resulting optimistic state, and the function to update it\n *\n * @example\n * const [optimisticValue, updateOptimistic] = useOptimistic<number>(count, (currentState, optimisticValue) => currentState + optimisticValue);\n */\nexport const useOptimistic = <State, OptimisticState = State>(\n externalState: State,\n update: (currentState: State, optimisticState: OptimisticState) => State\n) => {\n const [state, setState] = useState<State>(externalState);\n const internalUpdateRef = useRef(update);\n internalUpdateRef.current = update;\n\n const [promised, setPromised] = useState(false);\n\n useDidUpdate(() => {\n if (!promised) return;\n setState(externalState);\n setPromised(false);\n }, [promised]);\n\n const updateState = (optimisticValue: OptimisticState, promise: Promise<any>) => {\n setState((currentState) => internalUpdateRef.current(currentState, optimisticValue));\n return promise.finally(() => setPromised(true));\n };\n\n return [state, updateState] as const;\n};\n"],"names":["useOptimistic","externalState","update","state","setState","useState","internalUpdateRef","useRef","promised","setPromised","useDidUpdate","optimisticValue","promise","currentState"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"useOptimistic.mjs","sources":["../../../../src/hooks/useOptimistic/useOptimistic.ts"],"sourcesContent":["import { useRef, useState } from 'react';\n\nimport { useDidUpdate } from '../useDidUpdate/useDidUpdate';\n\nexport type UseOptimisticReturn<State> = [\n State,\n (optimisticValue: State, promise: Promise<any>) => void\n];\n\n/**\n * @name useOptimistic\n * @description - Hook that allows get optimistic value before its update\n * @category Utilities\n *\n * @template State The type of the state\n * @param {State} state The value to be returned initially and whenever no action is pending\n * @param {(currentState: State, optimisticValue: State) => State} update A pure function that takes the current state and the optimistic value passed to updateOptimistic and returns the resulting optimistic state\n * @returns {UseOptimisticReturn<State>} The resulting optimistic state, and the function to update it\n *\n * @example\n * const [optimisticValue, updateOptimistic] = useOptimistic<number>(count, (currentState, optimisticValue) => currentState + optimisticValue);\n */\nexport const useOptimistic = <State, OptimisticState = State>(\n externalState: State,\n update: (currentState: State, optimisticState: OptimisticState) => State\n) => {\n const [state, setState] = useState<State>(externalState);\n const internalUpdateRef = useRef(update);\n internalUpdateRef.current = update;\n\n const [promised, setPromised] = useState(false);\n\n useDidUpdate(() => {\n if (!promised) return;\n setState(externalState);\n setPromised(false);\n }, [promised]);\n\n const updateState = (optimisticValue: OptimisticState, promise: Promise<any>) => {\n setState((currentState) => internalUpdateRef.current(currentState, optimisticValue));\n return promise.finally(() => setPromised(true));\n };\n\n return [state, updateState] as const;\n};\n"],"names":["useOptimistic","externalState","update","state","setState","useState","internalUpdateRef","useRef","promised","setPromised","useDidUpdate","optimisticValue","promise","currentState"],"mappings":";;AAsBO,MAAMA,IAAgB,CAC3BC,GACAC,MACG;AACH,QAAM,CAACC,GAAOC,CAAQ,IAAIC,EAAgBJ,CAAa,GACjDK,IAAoBC,EAAOL,CAAM;AACvC,EAAAI,EAAkB,UAAUJ;AAE5B,QAAM,CAACM,GAAUC,CAAW,IAAIJ,EAAS,EAAK;AAE9C,SAAAK,EAAa,MAAM;AACjB,IAAKF,MACLJ,EAASH,CAAa,GACtBQ,EAAY,EAAK;AAAA,EAAA,GAChB,CAACD,CAAQ,CAAC,GAON,CAACL,GALY,CAACQ,GAAkCC,OACrDR,EAAS,CAACS,MAAiBP,EAAkB,QAAQO,GAAcF,CAAe,CAAC,GAC5EC,EAAQ,QAAQ,MAAMH,EAAY,EAAI,CAAC,EAGtB;AAC5B;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOrientation.mjs","sources":["../../../../src/hooks/useOrientation/useOrientation.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/** The use orientation return type */\nexport interface UseOrientationReturn {\n /** The current screen orientation angle */\n angle: number;\n /** The screen orientation type */\n type: OrientationType;\n}\n\n/**\n * @name useOrientation\n * @description - Hook that returns the current screen orientation\n * @category Browser\n *\n * @browserapi window.screen.orientation https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation\n *\n * @returns {UseOrientationReturn} An object containing the current screen orientation\n *\n * @example\n * const { angle, type } = useOrientation();\n */\nexport const useOrientation = (): UseOrientationReturn => {\n const [orientation, setOrientation] = useState<{\n angle: number;\n type: OrientationType;\n }>({ angle: 0, type: 'landscape-primary' });\n\n useEffect(() => {\n const onChange = () => setOrientation(window.screen.orientation);\n window.screen.orientation.addEventListener('change', onChange);\n return () => {\n window.screen.orientation.removeEventListener('change', onChange);\n };\n }, []);\n\n return orientation;\n};\n"],"names":["useOrientation","orientation","setOrientation","useState","useEffect","onChange"],"mappings":";AAsBO,MAAMA,IAAiB,MAA4B;
|
|
1
|
+
{"version":3,"file":"useOrientation.mjs","sources":["../../../../src/hooks/useOrientation/useOrientation.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\n/** The use orientation return type */\nexport interface UseOrientationReturn {\n /** The current screen orientation angle */\n angle: number;\n /** The screen orientation type */\n type: OrientationType;\n}\n\n/**\n * @name useOrientation\n * @description - Hook that returns the current screen orientation\n * @category Browser\n *\n * @browserapi window.screen.orientation https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation\n *\n * @returns {UseOrientationReturn} An object containing the current screen orientation\n *\n * @example\n * const { angle, type } = useOrientation();\n */\nexport const useOrientation = (): UseOrientationReturn => {\n const [orientation, setOrientation] = useState<{\n angle: number;\n type: OrientationType;\n }>({ angle: 0, type: 'landscape-primary' });\n\n useEffect(() => {\n const onChange = () => setOrientation(window.screen.orientation);\n window.screen.orientation.addEventListener('change', onChange);\n return () => {\n window.screen.orientation.removeEventListener('change', onChange);\n };\n }, []);\n\n return orientation;\n};\n"],"names":["useOrientation","orientation","setOrientation","useState","useEffect","onChange"],"mappings":";AAsBO,MAAMA,IAAiB,MAA4B;AACxD,QAAM,CAACC,GAAaC,CAAc,IAAIC,EAGnC,EAAE,OAAO,GAAG,MAAM,qBAAqB;AAE1C,SAAAC,EAAU,MAAM;AACd,UAAMC,IAAW,MAAMH,EAAe,OAAO,OAAO,WAAW;AAC/D,kBAAO,OAAO,YAAY,iBAAiB,UAAUG,CAAQ,GACtD,MAAM;AACX,aAAO,OAAO,YAAY,oBAAoB,UAAUA,CAAQ;AAAA,IAAA;AAAA,EAClE,GACC,EAAE,GAEEJ;AACT;"}
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import { useState as u, useRef as
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (c) {
|
|
1
|
+
import { useState as u, useRef as a } from "react";
|
|
2
|
+
const b = (...t) => {
|
|
3
|
+
const c = typeof t[0] == "function" ? t[0] : t[0]?.onSuccess, s = typeof t[0] == "function" ? t[0]?.onError : void 0, e = typeof navigator < "u" && "OTPCredential" in window, [i, n] = u(!1), r = a(new AbortController());
|
|
4
|
+
return { supported: e, abort: () => {
|
|
5
|
+
r.current.abort(), r.current = new AbortController(), r.current.signal.onabort = () => n(!0);
|
|
6
|
+
}, aborted: i, get: async () => {
|
|
7
|
+
if (e) {
|
|
9
8
|
r.current = new AbortController();
|
|
10
9
|
try {
|
|
11
|
-
const
|
|
10
|
+
const o = await navigator.credentials.get({
|
|
12
11
|
otp: { transport: ["sms"] },
|
|
13
12
|
signal: r.current.signal
|
|
14
13
|
});
|
|
15
|
-
return
|
|
16
|
-
} catch (
|
|
17
|
-
o
|
|
14
|
+
return c?.(o), n(!1), o;
|
|
15
|
+
} catch (o) {
|
|
16
|
+
s?.(o);
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
} };
|
|
21
20
|
};
|
|
22
21
|
export {
|
|
23
|
-
|
|
22
|
+
b as useOtpCredential
|
|
24
23
|
};
|
|
25
24
|
//# sourceMappingURL=useOtpCredential.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOtpCredential.mjs","sources":["../../../../src/hooks/useOtpCredential/useOtpCredential.ts"],"sourcesContent":["import { useRef, useState } from 'react';\n\ndeclare global {\n interface OTPOptions {\n readonly transport: string[];\n }\n\n interface CredentialRequestOptions {\n readonly otp: OTPOptions;\n }\n\n interface Credential {\n readonly code: string;\n }\n}\n\n/* The use otp credential callback type */\nexport type UseOtpCredentialCallback = (otp: Credential | null) => void;\n\n/* The use otp credential options type */\nexport interface UseOtpCredentialParams {\n /* The callback function to be invoked on error */\n onError: (error: any) => void;\n /* The callback function to be invoked on success */\n onSuccess: (credential: Credential | null) => void;\n}\n\n/* The use otp credential return type */\nexport interface UseOtpCredentialReturn {\n /* The abort function */\n abort: AbortController['abort'];\n /* The aborted state of the query */\n aborted: boolean;\n /* The supported state of the otp credential */\n supported: boolean;\n /* The get otp credential function */\n get: () => Promise<Credential | null>;\n}\n\nexport interface UseOtpCredential {\n (callback?: UseOtpCredentialCallback): UseOtpCredentialReturn;\n\n (params?: UseOtpCredentialParams): UseOtpCredentialReturn;\n}\n\n/**\n * @name useOtpCredential\n * @description - Hook that creates an otp credential\n * @category Browser\n *\n * @browserapi navigator.credentials https://developer.mozilla.org/en-US/docs/Web/API/Navigator/credentials\n *\n * @overload\n * @param {UseOtpCredentialCallback} callback The callback function to be invoked\n * @returns {UseOtpCredentialReturn}\n *\n * @example\n * useOtpCredential((credential) => console.log(credential));\n *\n * @overload\n * @param {UseOtpCredentialCallback} params.onSuccess The callback function to be invoked on success\n * @param {UseOtpCredentialCallback} params.onError The callback function to be invoked on error\n * @returns {UseOtpCredentialReturn}\n *\n * @example\n * useOtpCredential({ onSuccess: (credential) => console.log(credential), onError: (error) => console.log(error) });\n */\nexport const useOtpCredential = ((...params: any[]) => {\n const onSuccess =\n typeof params[0] === 'function'\n ? (params[0] as UseOtpCredentialCallback | undefined)\n : (params[0] as UseOtpCredentialParams | undefined)?.onSuccess;\n\n const onError =\n typeof params[0] === 'function'\n ? (params[0] as UseOtpCredentialParams | undefined)?.onError\n : undefined;\n\n const supported = typeof navigator !== 'undefined' && 'OTPCredential' in window;\n const [aborted, setAborted] = useState(false);\n\n const abortControllerRef = useRef<AbortController>(new AbortController());\n\n const get = async () => {\n if (!supported) return;\n\n abortControllerRef.current = new AbortController();\n try {\n const credential = await navigator.credentials.get({\n otp: { transport: ['sms'] },\n signal: abortControllerRef.current.signal\n });\n onSuccess?.(credential);\n setAborted(false);\n return credential;\n } catch (error) {\n onError?.(error);\n }\n };\n\n const abort = () => {\n abortControllerRef.current.abort();\n abortControllerRef.current = new AbortController();\n abortControllerRef.current.signal.onabort = () => setAborted(true);\n };\n\n return { supported, abort, aborted, get };\n}) as UseOtpCredential;\n"],"names":["useOtpCredential","params","onSuccess","
|
|
1
|
+
{"version":3,"file":"useOtpCredential.mjs","sources":["../../../../src/hooks/useOtpCredential/useOtpCredential.ts"],"sourcesContent":["import { useRef, useState } from 'react';\n\ndeclare global {\n interface OTPOptions {\n readonly transport: string[];\n }\n\n interface CredentialRequestOptions {\n readonly otp: OTPOptions;\n }\n\n interface Credential {\n readonly code: string;\n }\n}\n\n/* The use otp credential callback type */\nexport type UseOtpCredentialCallback = (otp: Credential | null) => void;\n\n/* The use otp credential options type */\nexport interface UseOtpCredentialParams {\n /* The callback function to be invoked on error */\n onError: (error: any) => void;\n /* The callback function to be invoked on success */\n onSuccess: (credential: Credential | null) => void;\n}\n\n/* The use otp credential return type */\nexport interface UseOtpCredentialReturn {\n /* The abort function */\n abort: AbortController['abort'];\n /* The aborted state of the query */\n aborted: boolean;\n /* The supported state of the otp credential */\n supported: boolean;\n /* The get otp credential function */\n get: () => Promise<Credential | null>;\n}\n\nexport interface UseOtpCredential {\n (callback?: UseOtpCredentialCallback): UseOtpCredentialReturn;\n\n (params?: UseOtpCredentialParams): UseOtpCredentialReturn;\n}\n\n/**\n * @name useOtpCredential\n * @description - Hook that creates an otp credential\n * @category Browser\n *\n * @browserapi navigator.credentials https://developer.mozilla.org/en-US/docs/Web/API/Navigator/credentials\n *\n * @overload\n * @param {UseOtpCredentialCallback} callback The callback function to be invoked\n * @returns {UseOtpCredentialReturn}\n *\n * @example\n * useOtpCredential((credential) => console.log(credential));\n *\n * @overload\n * @param {UseOtpCredentialCallback} params.onSuccess The callback function to be invoked on success\n * @param {UseOtpCredentialCallback} params.onError The callback function to be invoked on error\n * @returns {UseOtpCredentialReturn}\n *\n * @example\n * useOtpCredential({ onSuccess: (credential) => console.log(credential), onError: (error) => console.log(error) });\n */\nexport const useOtpCredential = ((...params: any[]) => {\n const onSuccess =\n typeof params[0] === 'function'\n ? (params[0] as UseOtpCredentialCallback | undefined)\n : (params[0] as UseOtpCredentialParams | undefined)?.onSuccess;\n\n const onError =\n typeof params[0] === 'function'\n ? (params[0] as UseOtpCredentialParams | undefined)?.onError\n : undefined;\n\n const supported = typeof navigator !== 'undefined' && 'OTPCredential' in window;\n const [aborted, setAborted] = useState(false);\n\n const abortControllerRef = useRef<AbortController>(new AbortController());\n\n const get = async () => {\n if (!supported) return;\n\n abortControllerRef.current = new AbortController();\n try {\n const credential = await navigator.credentials.get({\n otp: { transport: ['sms'] },\n signal: abortControllerRef.current.signal\n });\n onSuccess?.(credential);\n setAborted(false);\n return credential;\n } catch (error) {\n onError?.(error);\n }\n };\n\n const abort = () => {\n abortControllerRef.current.abort();\n abortControllerRef.current = new AbortController();\n abortControllerRef.current.signal.onabort = () => setAborted(true);\n };\n\n return { supported, abort, aborted, get };\n}) as UseOtpCredential;\n"],"names":["useOtpCredential","params","onSuccess","onError","supported","aborted","setAborted","useState","abortControllerRef","useRef","credential","error"],"mappings":";AAmEO,MAAMA,IAAoB,IAAIC,MAAkB;AACrD,QAAMC,IACJ,OAAOD,EAAO,CAAC,KAAM,aAChBA,EAAO,CAAC,IACRA,EAAO,CAAC,GAA0C,WAEnDE,IACJ,OAAOF,EAAO,CAAC,KAAM,aAChBA,EAAO,CAAC,GAA0C,UACnD,QAEAG,IAAY,OAAO,YAAc,OAAe,mBAAmB,QACnE,CAACC,GAASC,CAAU,IAAIC,EAAS,EAAK,GAEtCC,IAAqBC,EAAwB,IAAI,iBAAiB;AAyBxE,SAAO,EAAE,WAAAL,GAAW,OANN,MAAM;AAClB,IAAAI,EAAmB,QAAQ,MAAA,GAC3BA,EAAmB,UAAU,IAAI,gBAAA,GACjCA,EAAmB,QAAQ,OAAO,UAAU,MAAMF,EAAW,EAAI;AAAA,EAAA,GAGxC,SAAAD,GAAS,KAvBxB,YAAY;AACtB,QAAKD,GAEL;AAAA,MAAAI,EAAmB,UAAU,IAAI,gBAAA;AACjC,UAAI;AACF,cAAME,IAAa,MAAM,UAAU,YAAY,IAAI;AAAA,UACjD,KAAK,EAAE,WAAW,CAAC,KAAK,EAAA;AAAA,UACxB,QAAQF,EAAmB,QAAQ;AAAA,QAAA,CACpC;AACD,eAAAN,IAAYQ,CAAU,GACtBJ,EAAW,EAAK,GACTI;AAAA,MAAA,SACAC,GAAO;AACd,QAAAR,IAAUQ,CAAK;AAAA,MAAA;AAAA;AAAA,EACjB,EASkC;AACtC;"}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { useState as r, useEffect as o } from "react";
|
|
2
2
|
import { useEvent as u } from "../useEvent/useEvent.mjs";
|
|
3
|
-
const
|
|
4
|
-
const [
|
|
5
|
-
if (
|
|
6
|
-
|
|
3
|
+
const a = (n) => {
|
|
4
|
+
const [t, s] = r(!1), e = u(() => {
|
|
5
|
+
if (t) return s(!1);
|
|
6
|
+
n?.(), s(!0);
|
|
7
7
|
});
|
|
8
8
|
return o(() => (document.addEventListener("mouseleave", e, { passive: !0 }), document.addEventListener("mouseenter", e, { passive: !0 }), () => {
|
|
9
9
|
document.removeEventListener("mouseenter", e), document.removeEventListener("mouseleave", e);
|
|
10
|
-
}), []),
|
|
10
|
+
}), []), t;
|
|
11
11
|
};
|
|
12
12
|
export {
|
|
13
|
-
|
|
13
|
+
a as usePageLeave
|
|
14
14
|
};
|
|
15
15
|
//# sourceMappingURL=usePageLeave.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePageLeave.mjs","sources":["../../../../src/hooks/usePageLeave/usePageLeave.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { useEvent } from '../useEvent/useEvent';\n\n/**\n * @name usePageLeave\n * @description - Hook what calls given function when mouse leaves the page\n * @category Sensors\n *\n * @param {() => void} [callback] The callback function what calls then mouse leaves the page\n * @returns {boolean} A boolean which determines if the mouse left the page\n *\n * @example\n * const isLeft = usePageLeave(() => console.log('on leave'))\n */\nexport const usePageLeave = (callback?: () => void) => {\n const [isLeft, setIsLeft] = useState(false);\n\n const onMouse = useEvent(() => {\n if (isLeft) return setIsLeft(false);\n callback?.();\n setIsLeft(true);\n });\n\n useEffect(() => {\n document.addEventListener('mouseleave', onMouse, { passive: true });\n document.addEventListener('mouseenter', onMouse, { passive: true });\n\n return () => {\n document.removeEventListener('mouseenter', onMouse);\n document.removeEventListener('mouseleave', onMouse);\n };\n }, []);\n\n return isLeft;\n};\n"],"names":["usePageLeave","callback","isLeft","setIsLeft","useState","onMouse","useEvent","useEffect"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"usePageLeave.mjs","sources":["../../../../src/hooks/usePageLeave/usePageLeave.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { useEvent } from '../useEvent/useEvent';\n\n/**\n * @name usePageLeave\n * @description - Hook what calls given function when mouse leaves the page\n * @category Sensors\n *\n * @param {() => void} [callback] The callback function what calls then mouse leaves the page\n * @returns {boolean} A boolean which determines if the mouse left the page\n *\n * @example\n * const isLeft = usePageLeave(() => console.log('on leave'))\n */\nexport const usePageLeave = (callback?: () => void) => {\n const [isLeft, setIsLeft] = useState(false);\n\n const onMouse = useEvent(() => {\n if (isLeft) return setIsLeft(false);\n callback?.();\n setIsLeft(true);\n });\n\n useEffect(() => {\n document.addEventListener('mouseleave', onMouse, { passive: true });\n document.addEventListener('mouseenter', onMouse, { passive: true });\n\n return () => {\n document.removeEventListener('mouseenter', onMouse);\n document.removeEventListener('mouseleave', onMouse);\n };\n }, []);\n\n return isLeft;\n};\n"],"names":["usePageLeave","callback","isLeft","setIsLeft","useState","onMouse","useEvent","useEffect"],"mappings":";;AAeO,MAAMA,IAAe,CAACC,MAA0B;AACrD,QAAM,CAACC,GAAQC,CAAS,IAAIC,EAAS,EAAK,GAEpCC,IAAUC,EAAS,MAAM;AAC7B,QAAIJ,EAAQ,QAAOC,EAAU,EAAK;AAClC,IAAAF,IAAA,GACAE,EAAU,EAAI;AAAA,EAAA,CACf;AAED,SAAAI,EAAU,OACR,SAAS,iBAAiB,cAAcF,GAAS,EAAE,SAAS,IAAM,GAClE,SAAS,iBAAiB,cAAcA,GAAS,EAAE,SAAS,IAAM,GAE3D,MAAM;AACX,aAAS,oBAAoB,cAAcA,CAAO,GAClD,SAAS,oBAAoB,cAAcA,CAAO;AAAA,EAAA,IAEnD,EAAE,GAEEH;AACT;"}
|
|
@@ -1,49 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
h(this, "x");
|
|
13
|
-
h(this, "y");
|
|
14
|
-
this.x = e, this.y = t;
|
|
1
|
+
import { useRef as C, useState as U, useEffect as A } from "react";
|
|
2
|
+
import { useEvent as M } from "../useEvent/useEvent.mjs";
|
|
3
|
+
import { useRefState as B } from "../useRefState/useRefState.mjs";
|
|
4
|
+
import { isTarget as S } from "../../utils/helpers/isTarget.mjs";
|
|
5
|
+
import { getElement as q } from "../../utils/helpers/getElement.mjs";
|
|
6
|
+
const P = 10;
|
|
7
|
+
class x {
|
|
8
|
+
x;
|
|
9
|
+
y;
|
|
10
|
+
constructor(t, e) {
|
|
11
|
+
this.x = t, this.y = e;
|
|
15
12
|
}
|
|
16
|
-
update(
|
|
17
|
-
this.x =
|
|
13
|
+
update(t) {
|
|
14
|
+
this.x = t.x, this.y = t.y;
|
|
18
15
|
}
|
|
19
|
-
getDifferenceTo(
|
|
20
|
-
return new
|
|
16
|
+
getDifferenceTo(t) {
|
|
17
|
+
return new x(this.x - t.x, this.y - t.y);
|
|
21
18
|
}
|
|
22
|
-
getDistanceTo(
|
|
23
|
-
const
|
|
24
|
-
return Math.sqrt(
|
|
19
|
+
getDistanceTo(t) {
|
|
20
|
+
const e = this.getDifferenceTo(t);
|
|
21
|
+
return Math.sqrt(e.x ** 2 + e.y ** 2);
|
|
25
22
|
}
|
|
26
|
-
getAngleTo(
|
|
27
|
-
const
|
|
28
|
-
return Math.atan2(
|
|
23
|
+
getAngleTo(t) {
|
|
24
|
+
const e = this.getDifferenceTo(t);
|
|
25
|
+
return Math.atan2(e.y, e.x);
|
|
29
26
|
}
|
|
30
|
-
equalsTo(
|
|
31
|
-
return this.x ===
|
|
27
|
+
equalsTo(t) {
|
|
28
|
+
return this.x === t.x && this.y === t.y;
|
|
32
29
|
}
|
|
33
|
-
moveByAngle(
|
|
34
|
-
const o =
|
|
35
|
-
return this.x += Math.sin(o) *
|
|
30
|
+
moveByAngle(t, e) {
|
|
31
|
+
const o = t + Math.PI / 2;
|
|
32
|
+
return this.x += Math.sin(o) * e, this.y -= Math.cos(o) * e, this;
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
|
-
class
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.smooth =
|
|
35
|
+
class I {
|
|
36
|
+
pointer;
|
|
37
|
+
brush;
|
|
38
|
+
radius;
|
|
39
|
+
smooth = !1;
|
|
40
|
+
points = [];
|
|
41
|
+
lines = [];
|
|
42
|
+
constructor({ x: t, y: e, radius: o, smooth: h }) {
|
|
43
|
+
this.smooth = h, this.pointer = new x(t, e), this.brush = new x(t, e), this.radius = o, this.points = [], this.lines = [];
|
|
47
44
|
}
|
|
48
45
|
getBrushCoordinates() {
|
|
49
46
|
return {
|
|
@@ -57,91 +54,88 @@ class X {
|
|
|
57
54
|
y: this.pointer.y
|
|
58
55
|
};
|
|
59
56
|
}
|
|
60
|
-
update(
|
|
61
|
-
if (this.pointer.equalsTo(
|
|
62
|
-
if (this.pointer.update(
|
|
63
|
-
return this.brush.update(
|
|
64
|
-
const
|
|
65
|
-
if (Math.round((
|
|
66
|
-
const
|
|
57
|
+
update(t) {
|
|
58
|
+
if (this.pointer.equalsTo(t)) return !1;
|
|
59
|
+
if (this.pointer.update(t), !this.smooth)
|
|
60
|
+
return this.brush.update(t), this.points.push(this.getBrushCoordinates()), !0;
|
|
61
|
+
const e = this.pointer.getDistanceTo(this.brush), o = this.pointer.getAngleTo(this.brush);
|
|
62
|
+
if (Math.round((e - this.radius) * 10) / 10 > 0) {
|
|
63
|
+
const f = o + Math.PI / 2;
|
|
67
64
|
return this.brush.update({
|
|
68
|
-
x: this.brush.x + Math.sin(
|
|
69
|
-
y: this.brush.y - Math.cos(
|
|
65
|
+
x: this.brush.x + Math.sin(f) * (e - this.radius),
|
|
66
|
+
y: this.brush.y - Math.cos(f) * (e - this.radius)
|
|
70
67
|
}), this.points.push(this.getBrushCoordinates()), !0;
|
|
71
68
|
}
|
|
72
69
|
return !1;
|
|
73
70
|
}
|
|
74
71
|
}
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
new
|
|
72
|
+
const H = (...p) => {
|
|
73
|
+
const t = S(p[0]) ? p[0] : void 0, e = (t ? p[1] : p[0]) ?? {}, o = e?.color ?? "black", h = e?.opacity ?? 1, f = e?.radius ?? P, s = C(
|
|
74
|
+
new I({
|
|
78
75
|
x: 0,
|
|
79
76
|
y: 0,
|
|
80
|
-
radius:
|
|
81
|
-
smooth:
|
|
77
|
+
radius: e?.radius ?? P,
|
|
78
|
+
smooth: e?.smooth ?? !1
|
|
82
79
|
})
|
|
83
|
-
), [
|
|
80
|
+
), [m, b] = U(!1), g = B(), n = C(null), c = (r, u, l, a) => {
|
|
84
81
|
if (!n.current) return;
|
|
85
|
-
n.current.globalAlpha =
|
|
86
|
-
let
|
|
82
|
+
n.current.globalAlpha = l, n.current.strokeStyle = u, n.current.lineWidth = a * 2, n.current.lineCap = "round", n.current.lineJoin = "round";
|
|
83
|
+
let i = r[0], y = r[1];
|
|
87
84
|
n.current.beginPath();
|
|
88
|
-
for (let
|
|
89
|
-
const
|
|
90
|
-
x:
|
|
91
|
-
y:
|
|
85
|
+
for (let d = 1; d < r.length; d += 1) {
|
|
86
|
+
const L = {
|
|
87
|
+
x: i.x + (y.x - i.x) / 2,
|
|
88
|
+
y: i.y + (y.y - i.y) / 2
|
|
92
89
|
};
|
|
93
|
-
n.current.quadraticCurveTo(
|
|
90
|
+
n.current.quadraticCurveTo(i.x, i.y, L.x, L.y), i = r[d], y = r[d + 1];
|
|
94
91
|
}
|
|
95
|
-
n.current.lineTo(
|
|
96
|
-
},
|
|
92
|
+
n.current.lineTo(i.x, i.y), n.current.stroke();
|
|
93
|
+
}, v = () => {
|
|
97
94
|
n.current && n.current.clearRect(
|
|
98
95
|
0,
|
|
99
96
|
0,
|
|
100
97
|
n.current.canvas.width,
|
|
101
98
|
n.current.canvas.height
|
|
102
99
|
);
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
s.current.update(i) && n.current && (b(), n.current.clearRect(
|
|
100
|
+
}, w = M((r) => {
|
|
101
|
+
if (!m) return;
|
|
102
|
+
e?.onMouseMove?.(r, s.current);
|
|
103
|
+
const u = { x: r.offsetX, y: r.offsetY };
|
|
104
|
+
s.current.update(u) && n.current && (v(), n.current.clearRect(
|
|
109
105
|
0,
|
|
110
106
|
0,
|
|
111
107
|
n.current.canvas.width,
|
|
112
108
|
n.current.canvas.height
|
|
113
109
|
), s.current.lines.forEach(
|
|
114
|
-
({ points:
|
|
115
|
-
),
|
|
116
|
-
}),
|
|
117
|
-
var a;
|
|
110
|
+
({ points: a, color: i, opacity: y, radius: d }) => c(a, i, y, d)
|
|
111
|
+
), c(s.current.points, o, h, f));
|
|
112
|
+
}), E = M((r) => {
|
|
118
113
|
if (!n.current) return;
|
|
119
|
-
const
|
|
120
|
-
s.current.brush.update(
|
|
121
|
-
}),
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
({ points: r, color: i, opacity: a, radius: f }) => l(r, i, a, f)
|
|
114
|
+
const u = { x: r.offsetX, y: r.offsetY };
|
|
115
|
+
s.current.brush.update(u), s.current.points.push(u), c(s.current.points, o, h, f), e?.onMouseDown?.(r, s.current), b(!0);
|
|
116
|
+
}), T = M((r) => {
|
|
117
|
+
n.current && (s.current.points.length && (s.current.lines.push({ points: s.current.points, color: o, opacity: h, radius: f }), s.current.points = []), e?.onMouseUp?.(r, s.current), b(!1));
|
|
118
|
+
}), R = () => {
|
|
119
|
+
n.current && (v(), s.current.lines = [], s.current.points = []);
|
|
120
|
+
}, D = () => {
|
|
121
|
+
n.current && (v(), s.current.lines.pop(), s.current.lines.forEach(
|
|
122
|
+
({ points: r, color: u, opacity: l, radius: a }) => c(r, u, l, a)
|
|
129
123
|
));
|
|
130
124
|
};
|
|
131
|
-
return
|
|
132
|
-
if (!
|
|
133
|
-
const r =
|
|
125
|
+
return A(() => {
|
|
126
|
+
if (!t && !g.state) return;
|
|
127
|
+
const r = t ? q(t) : g.current;
|
|
134
128
|
if (r)
|
|
135
|
-
return n.current = r.getContext("2d"),
|
|
136
|
-
({ points:
|
|
137
|
-
)), r.addEventListener("mousedown",
|
|
138
|
-
r && (r.removeEventListener("mousedown",
|
|
129
|
+
return n.current = r.getContext("2d"), e?.initialLines && (s.current.lines = e.initialLines, e.initialLines.forEach(
|
|
130
|
+
({ points: u, color: l, opacity: a, radius: i }) => c(u, l, a, i)
|
|
131
|
+
)), r.addEventListener("mousedown", E), r.addEventListener("mousemove", w), r.addEventListener("mouseup", T), () => {
|
|
132
|
+
r && (r.removeEventListener("mousedown", E), r.removeEventListener("mousemove", w), r.removeEventListener("mouseup", T));
|
|
139
133
|
};
|
|
140
|
-
}, [
|
|
134
|
+
}, [t, g.state]), t ? { drawing: m, clear: R, undo: D, draw: c, lines: s.current.lines } : { ref: g, drawing: m, clear: R, undo: D, draw: c, lines: s.current.lines };
|
|
141
135
|
};
|
|
142
136
|
export {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
I as Paint,
|
|
138
|
+
x as Pointer,
|
|
139
|
+
H as usePaint
|
|
146
140
|
};
|
|
147
141
|
//# sourceMappingURL=usePaint.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePaint.mjs","sources":["../../../../src/hooks/usePaint/usePaint.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { getElement, isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useEvent } from '../useEvent/useEvent';\nimport { useRefState } from '../useRefState/useRefState';\n\nconst DEFAULT_BRUSH_RADIUS = 10;\n\nexport interface Point {\n x: number;\n y: number;\n}\n\nexport class Pointer implements Point {\n x: number;\n\n y: number;\n\n constructor(x: number, y: number) {\n this.x = x;\n this.y = y;\n }\n\n update(point: Point) {\n this.x = point.x;\n this.y = point.y;\n }\n\n getDifferenceTo(point: Point) {\n return new Pointer(this.x - point.x, this.y - point.y);\n }\n\n getDistanceTo(point: Point) {\n const diff = this.getDifferenceTo(point);\n return Math.sqrt(diff.x ** 2 + diff.y ** 2);\n }\n\n getAngleTo(point: Point) {\n const diff = this.getDifferenceTo(point);\n return Math.atan2(diff.y, diff.x);\n }\n\n equalsTo(point: Point) {\n return this.x === point.x && this.y === point.y;\n }\n\n moveByAngle(\n // The angle in radians\n angle: number,\n // How much the point should be moved\n distance: number\n ) {\n // Rotate the angle based on the browser coordinate system ([0,0] in the top left)\n const angleRotated = angle + Math.PI / 2;\n\n this.x += Math.sin(angleRotated) * distance;\n this.y -= Math.cos(angleRotated) * distance;\n\n return this;\n }\n}\n\nexport class Paint {\n pointer: Pointer;\n\n brush: Pointer;\n\n radius: number;\n\n smooth: boolean = false;\n\n points: Point[] = [];\n\n lines: { points: Point[]; color: string; radius: number; opacity: number }[] = [];\n\n constructor({ x, y, radius, smooth }: { x: number; y: number; radius: number; smooth: boolean }) {\n this.smooth = smooth;\n this.pointer = new Pointer(x, y);\n this.brush = new Pointer(x, y);\n this.radius = radius;\n this.points = [];\n this.lines = [];\n }\n\n getBrushCoordinates() {\n return {\n x: this.brush.x,\n y: this.brush.y\n };\n }\n\n getPointerCoordinates() {\n return {\n x: this.pointer.x,\n y: this.pointer.y\n };\n }\n\n update(point: Point) {\n if (this.pointer.equalsTo(point)) return false;\n this.pointer.update(point);\n\n if (!this.smooth) {\n this.brush.update(point);\n this.points.push(this.getBrushCoordinates());\n return true;\n }\n\n const distance = this.pointer.getDistanceTo(this.brush);\n const angle = this.pointer.getAngleTo(this.brush);\n const isOutside = Math.round((distance - this.radius) * 10) / 10 > 0;\n\n if (isOutside) {\n const angleRotated = angle + Math.PI / 2;\n this.brush.update({\n x: this.brush.x + Math.sin(angleRotated) * (distance - this.radius),\n y: this.brush.y - Math.cos(angleRotated) * (distance - this.radius)\n });\n this.points.push(this.getBrushCoordinates());\n return true;\n }\n\n return false;\n }\n}\n\nexport interface UsePaintOptions {\n /** Brush color */\n color?: string;\n /** Initial lines */\n initialLines?: Paint['lines'];\n /** Brush opacity */\n opacity?: number;\n /** Brush radius */\n radius?: number;\n /** Smooth brush movement */\n smooth?: boolean;\n /** Callback when the mouse is down */\n onMouseDown?: (event: MouseEvent, paint: Paint) => void;\n /** Callback when the mouse is moved */\n onMouseMove?: (event: MouseEvent, paint: Paint) => void;\n /** Callback when the mouse is up */\n onMouseUp?: (event: MouseEvent, paint: Paint) => void;\n}\n\nexport interface UsePaintReturn {\n drawing: boolean;\n lines: Paint['lines'];\n clear: () => void;\n draw: (points: Point[], color: string, opacity: number, radius: number) => void;\n undo: () => void;\n}\n\nexport interface UsePaint {\n (target: HookTarget, options?: UsePaintOptions): UsePaintReturn;\n\n <Target extends HTMLCanvasElement>(\n options?: UsePaintOptions,\n target?: never\n ): UsePaintReturn & { ref: StateRef<Target> };\n}\n\n/**\n * @name usePaint\n * @description - Hook that allows you to draw in a specific area\n * @category Browser\n *\n * @overload\n * @param {HookTarget} target The target element to be painted\n * @param {UsePaintOptions} [options] The options to be used\n * @returns {UsePaintReturn} An object containing the current pencil options and functions to interact with the paint\n *\n * @example\n * const drawing = usePaint(canvasRef);\n *\n * @overload\n * @param {UsePaintOptions} [options] The options to be used\n * @returns {UsePaintReturn & { ref: StateRef<HTMLCanvasElement> }} An object containing the current pencil options and functions to interact with the paint\n *\n * @example\n * const { ref, drawing } = usePaint();\n */\nexport const usePaint = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = ((target ? params[1] : params[0]) as UsePaintOptions) ?? {};\n\n const color = options?.color ?? 'black';\n const opacity = options?.opacity ?? 1;\n const radius = options?.radius ?? DEFAULT_BRUSH_RADIUS;\n\n const paintRef = useRef<Paint>(\n new Paint({\n x: 0,\n y: 0,\n radius: options?.radius ?? DEFAULT_BRUSH_RADIUS,\n smooth: options?.smooth ?? false\n })\n );\n const [drawing, setIsDrawing] = useState(false);\n const internalRef = useRefState<HTMLCanvasElement>();\n const contextRef = useRef<CanvasRenderingContext2D | null>(null);\n\n const draw = (points: Point[], color: string, opacity: number, radius: number) => {\n if (!contextRef.current) return;\n\n contextRef.current.globalAlpha = opacity;\n contextRef.current.strokeStyle = color;\n contextRef.current.lineWidth = radius * 2;\n contextRef.current.lineCap = 'round';\n contextRef.current.lineJoin = 'round';\n\n let p1 = points[0];\n let p2 = points[1];\n\n contextRef.current.beginPath();\n\n for (let i = 1; i < points.length; i += 1) {\n const midPoint = {\n x: p1.x + (p2.x - p1.x) / 2,\n y: p1.y + (p2.y - p1.y) / 2\n };\n contextRef.current.quadraticCurveTo(p1.x, p1.y, midPoint.x, midPoint.y);\n p1 = points[i];\n p2 = points[i + 1];\n }\n contextRef.current.lineTo(p1.x, p1.y);\n contextRef.current.stroke();\n };\n\n const clearCanvas = () => {\n if (!contextRef.current) return;\n contextRef.current.clearRect(\n 0,\n 0,\n contextRef.current.canvas.width,\n contextRef.current.canvas.height\n );\n };\n\n const onMouseMove = useEvent((event: MouseEvent) => {\n if (!drawing) return;\n options?.onMouseMove?.(event, paintRef.current);\n\n const point = { x: event.offsetX, y: event.offsetY };\n\n const isUpdated = paintRef.current.update(point);\n\n if (!isUpdated) return;\n if (!contextRef.current) return;\n clearCanvas();\n contextRef.current.clearRect(\n 0,\n 0,\n contextRef.current.canvas.width,\n contextRef.current.canvas.height\n );\n\n // const brush = paintRef.current.getBrushCoordinates();\n // // Draw brush point\n // contextRef.current.beginPath();\n // contextRef.current.fillStyle = 'red';\n // contextRef.current.arc(brush.x, brush.y, radius, 0, Math.PI * 2, true);\n // contextRef.current.fill();\n\n // // Draw the lazy radius.\n // contextRef.current.beginPath();\n // contextRef.current.strokeStyle = '#ccc';\n // contextRef.current.arc(brush.x, brush.y, radius * 2, 0, Math.PI * 2, true);\n // contextRef.current.stroke();\n\n paintRef.current.lines.forEach(({ points, color, opacity, radius }) =>\n draw(points, color, opacity, radius)\n );\n draw(paintRef.current.points, color, opacity, radius);\n });\n\n const onMouseDown = useEvent((event: MouseEvent) => {\n if (!contextRef.current) return;\n\n const point = { x: event.offsetX, y: event.offsetY };\n paintRef.current.brush.update(point);\n paintRef.current.points.push(point);\n draw(paintRef.current.points, color, opacity, radius);\n\n options?.onMouseDown?.(event, paintRef.current);\n setIsDrawing(true);\n });\n\n const onMouseUp = useEvent((event: MouseEvent) => {\n if (!contextRef.current) return;\n\n if (paintRef.current.points.length) {\n paintRef.current.lines.push({ points: paintRef.current.points, color, opacity, radius });\n paintRef.current.points = [];\n }\n\n options?.onMouseUp?.(event, paintRef.current);\n setIsDrawing(false);\n });\n\n const clear = () => {\n if (!contextRef.current) return;\n clearCanvas();\n paintRef.current.lines = [];\n paintRef.current.points = [];\n };\n\n const undo = () => {\n if (!contextRef.current) return;\n clearCanvas();\n\n paintRef.current.lines.pop();\n paintRef.current.lines.forEach(({ points, color, opacity, radius }) =>\n draw(points, color, opacity, radius)\n );\n };\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (target ? getElement(target) : internalRef.current) as HTMLCanvasElement;\n if (!element) return;\n contextRef.current = element.getContext('2d');\n\n if (options?.initialLines) {\n paintRef.current.lines = options.initialLines;\n options.initialLines.forEach(({ points, color, opacity, radius }) =>\n draw(points, color, opacity, radius)\n );\n }\n\n element.addEventListener('mousedown', onMouseDown);\n element.addEventListener('mousemove', onMouseMove);\n element.addEventListener('mouseup', onMouseUp);\n\n return () => {\n if (!element) return;\n element.removeEventListener('mousedown', onMouseDown);\n element.removeEventListener('mousemove', onMouseMove);\n element.removeEventListener('mouseup', onMouseUp);\n };\n }, [target, internalRef.state]);\n\n if (target) return { drawing, clear, undo, draw, lines: paintRef.current.lines };\n return { ref: internalRef, drawing, clear, undo, draw, lines: paintRef.current.lines };\n}) as UsePaint;\n"],"names":["DEFAULT_BRUSH_RADIUS","Pointer","x","y","__publicField","point","diff","angle","distance","angleRotated","Paint","radius","smooth","usePaint","params","target","isTarget","options","color","opacity","paintRef","useRef","drawing","setIsDrawing","useState","internalRef","useRefState","contextRef","draw","points","p1","p2","i","midPoint","clearCanvas","onMouseMove","useEvent","event","_a","onMouseDown","onMouseUp","clear","undo","useEffect","element","getElement"],"mappings":";;;;;;;;AAWA,MAAMA,IAAuB;AAOtB,MAAMC,EAAyB;AAAA,EAKpC,YAAYC,GAAWC,GAAW;AAJlC,IAAAC,EAAA;AAEA,IAAAA,EAAA;AAGE,SAAK,IAAIF,GACT,KAAK,IAAIC;AAAA,EAAA;AAAA,EAGX,OAAOE,GAAc;AACnB,SAAK,IAAIA,EAAM,GACf,KAAK,IAAIA,EAAM;AAAA,EAAA;AAAA,EAGjB,gBAAgBA,GAAc;AACrB,WAAA,IAAIJ,EAAQ,KAAK,IAAII,EAAM,GAAG,KAAK,IAAIA,EAAM,CAAC;AAAA,EAAA;AAAA,EAGvD,cAAcA,GAAc;AACpB,UAAAC,IAAO,KAAK,gBAAgBD,CAAK;AACvC,WAAO,KAAK,KAAKC,EAAK,KAAK,IAAIA,EAAK,KAAK,CAAC;AAAA,EAAA;AAAA,EAG5C,WAAWD,GAAc;AACjB,UAAAC,IAAO,KAAK,gBAAgBD,CAAK;AACvC,WAAO,KAAK,MAAMC,EAAK,GAAGA,EAAK,CAAC;AAAA,EAAA;AAAA,EAGlC,SAASD,GAAc;AACrB,WAAO,KAAK,M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ASR,CAAM;AAAA,IACrC;AAAA,EACF;AA4BI,SA1BJgC,EAAU,MAAM;AACd,QAAI,CAAC5B,KAAU,CAACU,EAAY,MAAO;AAEnC,UAAMmB,IAAW7B,IAAS8B,EAAW9B,CAAM,IAAIU,EAAY;AAC3D,QAAKmB;AACM,aAAAjB,EAAA,UAAUiB,EAAQ,WAAW,IAAI,GAExC3B,KAAA,QAAAA,EAAS,iBACFG,EAAA,QAAQ,QAAQH,EAAQ,cACjCA,EAAQ,aAAa;AAAA,QAAQ,CAAC,EAAE,QAAAY,GAAQ,OAAAX,GAAO,SAAAC,GAAS,QAAAR,EACtD,MAAAiB,EAAKC,GAAQX,GAAOC,GAASR,CAAM;AAAA,MACrC,IAGMiC,EAAA,iBAAiB,aAAaL,CAAW,GACzCK,EAAA,iBAAiB,aAAaT,CAAW,GACzCS,EAAA,iBAAiB,WAAWJ,CAAS,GAEtC,MAAM;AACX,QAAKI,MACGA,EAAA,oBAAoB,aAAaL,CAAW,GAC5CK,EAAA,oBAAoB,aAAaT,CAAW,GAC5CS,EAAA,oBAAoB,WAAWJ,CAAS;AAAA,MAClD;AAAA,EACC,GAAA,CAACzB,GAAQU,EAAY,KAAK,CAAC,GAE1BV,IAAe,EAAE,SAAAO,GAAS,OAAAmB,GAAO,MAAAC,GAAM,MAAAd,GAAM,OAAOR,EAAS,QAAQ,MAAM,IACxE,EAAE,KAAKK,GAAa,SAAAH,GAAS,OAAAmB,GAAO,MAAAC,GAAM,MAAAd,GAAM,OAAOR,EAAS,QAAQ,MAAM;AACvF;"}
|
|
1
|
+
{"version":3,"file":"usePaint.mjs","sources":["../../../../src/hooks/usePaint/usePaint.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { getElement, isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useEvent } from '../useEvent/useEvent';\nimport { useRefState } from '../useRefState/useRefState';\n\nconst DEFAULT_BRUSH_RADIUS = 10;\n\nexport interface Point {\n x: number;\n y: number;\n}\n\nexport class Pointer implements Point {\n x: number;\n\n y: number;\n\n constructor(x: number, y: number) {\n this.x = x;\n this.y = y;\n }\n\n update(point: Point) {\n this.x = point.x;\n this.y = point.y;\n }\n\n getDifferenceTo(point: Point) {\n return new Pointer(this.x - point.x, this.y - point.y);\n }\n\n getDistanceTo(point: Point) {\n const diff = this.getDifferenceTo(point);\n return Math.sqrt(diff.x ** 2 + diff.y ** 2);\n }\n\n getAngleTo(point: Point) {\n const diff = this.getDifferenceTo(point);\n return Math.atan2(diff.y, diff.x);\n }\n\n equalsTo(point: Point) {\n return this.x === point.x && this.y === point.y;\n }\n\n moveByAngle(\n // The angle in radians\n angle: number,\n // How much the point should be moved\n distance: number\n ) {\n // Rotate the angle based on the browser coordinate system ([0,0] in the top left)\n const angleRotated = angle + Math.PI / 2;\n\n this.x += Math.sin(angleRotated) * distance;\n this.y -= Math.cos(angleRotated) * distance;\n\n return this;\n }\n}\n\nexport class Paint {\n pointer: Pointer;\n\n brush: Pointer;\n\n radius: number;\n\n smooth: boolean = false;\n\n points: Point[] = [];\n\n lines: { points: Point[]; color: string; radius: number; opacity: number }[] = [];\n\n constructor({ x, y, radius, smooth }: { x: number; y: number; radius: number; smooth: boolean }) {\n this.smooth = smooth;\n this.pointer = new Pointer(x, y);\n this.brush = new Pointer(x, y);\n this.radius = radius;\n this.points = [];\n this.lines = [];\n }\n\n getBrushCoordinates() {\n return {\n x: this.brush.x,\n y: this.brush.y\n };\n }\n\n getPointerCoordinates() {\n return {\n x: this.pointer.x,\n y: this.pointer.y\n };\n }\n\n update(point: Point) {\n if (this.pointer.equalsTo(point)) return false;\n this.pointer.update(point);\n\n if (!this.smooth) {\n this.brush.update(point);\n this.points.push(this.getBrushCoordinates());\n return true;\n }\n\n const distance = this.pointer.getDistanceTo(this.brush);\n const angle = this.pointer.getAngleTo(this.brush);\n const isOutside = Math.round((distance - this.radius) * 10) / 10 > 0;\n\n if (isOutside) {\n const angleRotated = angle + Math.PI / 2;\n this.brush.update({\n x: this.brush.x + Math.sin(angleRotated) * (distance - this.radius),\n y: this.brush.y - Math.cos(angleRotated) * (distance - this.radius)\n });\n this.points.push(this.getBrushCoordinates());\n return true;\n }\n\n return false;\n }\n}\n\nexport interface UsePaintOptions {\n /** Brush color */\n color?: string;\n /** Initial lines */\n initialLines?: Paint['lines'];\n /** Brush opacity */\n opacity?: number;\n /** Brush radius */\n radius?: number;\n /** Smooth brush movement */\n smooth?: boolean;\n /** Callback when the mouse is down */\n onMouseDown?: (event: MouseEvent, paint: Paint) => void;\n /** Callback when the mouse is moved */\n onMouseMove?: (event: MouseEvent, paint: Paint) => void;\n /** Callback when the mouse is up */\n onMouseUp?: (event: MouseEvent, paint: Paint) => void;\n}\n\nexport interface UsePaintReturn {\n drawing: boolean;\n lines: Paint['lines'];\n clear: () => void;\n draw: (points: Point[], color: string, opacity: number, radius: number) => void;\n undo: () => void;\n}\n\nexport interface UsePaint {\n (target: HookTarget, options?: UsePaintOptions): UsePaintReturn;\n\n <Target extends HTMLCanvasElement>(\n options?: UsePaintOptions,\n target?: never\n ): UsePaintReturn & { ref: StateRef<Target> };\n}\n\n/**\n * @name usePaint\n * @description - Hook that allows you to draw in a specific area\n * @category Browser\n *\n * @overload\n * @param {HookTarget} target The target element to be painted\n * @param {UsePaintOptions} [options] The options to be used\n * @returns {UsePaintReturn} An object containing the current pencil options and functions to interact with the paint\n *\n * @example\n * const drawing = usePaint(canvasRef);\n *\n * @overload\n * @param {UsePaintOptions} [options] The options to be used\n * @returns {UsePaintReturn & { ref: StateRef<HTMLCanvasElement> }} An object containing the current pencil options and functions to interact with the paint\n *\n * @example\n * const { ref, drawing } = usePaint();\n */\nexport const usePaint = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = ((target ? params[1] : params[0]) as UsePaintOptions) ?? {};\n\n const color = options?.color ?? 'black';\n const opacity = options?.opacity ?? 1;\n const radius = options?.radius ?? DEFAULT_BRUSH_RADIUS;\n\n const paintRef = useRef<Paint>(\n new Paint({\n x: 0,\n y: 0,\n radius: options?.radius ?? DEFAULT_BRUSH_RADIUS,\n smooth: options?.smooth ?? false\n })\n );\n const [drawing, setIsDrawing] = useState(false);\n const internalRef = useRefState<HTMLCanvasElement>();\n const contextRef = useRef<CanvasRenderingContext2D | null>(null);\n\n const draw = (points: Point[], color: string, opacity: number, radius: number) => {\n if (!contextRef.current) return;\n\n contextRef.current.globalAlpha = opacity;\n contextRef.current.strokeStyle = color;\n contextRef.current.lineWidth = radius * 2;\n contextRef.current.lineCap = 'round';\n contextRef.current.lineJoin = 'round';\n\n let p1 = points[0];\n let p2 = points[1];\n\n contextRef.current.beginPath();\n\n for (let i = 1; i < points.length; i += 1) {\n const midPoint = {\n x: p1.x + (p2.x - p1.x) / 2,\n y: p1.y + (p2.y - p1.y) / 2\n };\n contextRef.current.quadraticCurveTo(p1.x, p1.y, midPoint.x, midPoint.y);\n p1 = points[i];\n p2 = points[i + 1];\n }\n contextRef.current.lineTo(p1.x, p1.y);\n contextRef.current.stroke();\n };\n\n const clearCanvas = () => {\n if (!contextRef.current) return;\n contextRef.current.clearRect(\n 0,\n 0,\n contextRef.current.canvas.width,\n contextRef.current.canvas.height\n );\n };\n\n const onMouseMove = useEvent((event: MouseEvent) => {\n if (!drawing) return;\n options?.onMouseMove?.(event, paintRef.current);\n\n const point = { x: event.offsetX, y: event.offsetY };\n\n const isUpdated = paintRef.current.update(point);\n\n if (!isUpdated) return;\n if (!contextRef.current) return;\n clearCanvas();\n contextRef.current.clearRect(\n 0,\n 0,\n contextRef.current.canvas.width,\n contextRef.current.canvas.height\n );\n\n // const brush = paintRef.current.getBrushCoordinates();\n // // Draw brush point\n // contextRef.current.beginPath();\n // contextRef.current.fillStyle = 'red';\n // contextRef.current.arc(brush.x, brush.y, radius, 0, Math.PI * 2, true);\n // contextRef.current.fill();\n\n // // Draw the lazy radius.\n // contextRef.current.beginPath();\n // contextRef.current.strokeStyle = '#ccc';\n // contextRef.current.arc(brush.x, brush.y, radius * 2, 0, Math.PI * 2, true);\n // contextRef.current.stroke();\n\n paintRef.current.lines.forEach(({ points, color, opacity, radius }) =>\n draw(points, color, opacity, radius)\n );\n draw(paintRef.current.points, color, opacity, radius);\n });\n\n const onMouseDown = useEvent((event: MouseEvent) => {\n if (!contextRef.current) return;\n\n const point = { x: event.offsetX, y: event.offsetY };\n paintRef.current.brush.update(point);\n paintRef.current.points.push(point);\n draw(paintRef.current.points, color, opacity, radius);\n\n options?.onMouseDown?.(event, paintRef.current);\n setIsDrawing(true);\n });\n\n const onMouseUp = useEvent((event: MouseEvent) => {\n if (!contextRef.current) return;\n\n if (paintRef.current.points.length) {\n paintRef.current.lines.push({ points: paintRef.current.points, color, opacity, radius });\n paintRef.current.points = [];\n }\n\n options?.onMouseUp?.(event, paintRef.current);\n setIsDrawing(false);\n });\n\n const clear = () => {\n if (!contextRef.current) return;\n clearCanvas();\n paintRef.current.lines = [];\n paintRef.current.points = [];\n };\n\n const undo = () => {\n if (!contextRef.current) return;\n clearCanvas();\n\n paintRef.current.lines.pop();\n paintRef.current.lines.forEach(({ points, color, opacity, radius }) =>\n draw(points, color, opacity, radius)\n );\n };\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (target ? getElement(target) : internalRef.current) as HTMLCanvasElement;\n if (!element) return;\n contextRef.current = element.getContext('2d');\n\n if (options?.initialLines) {\n paintRef.current.lines = options.initialLines;\n options.initialLines.forEach(({ points, color, opacity, radius }) =>\n draw(points, color, opacity, radius)\n );\n }\n\n element.addEventListener('mousedown', onMouseDown);\n element.addEventListener('mousemove', onMouseMove);\n element.addEventListener('mouseup', onMouseUp);\n\n return () => {\n if (!element) return;\n element.removeEventListener('mousedown', onMouseDown);\n element.removeEventListener('mousemove', onMouseMove);\n element.removeEventListener('mouseup', onMouseUp);\n };\n }, [target, internalRef.state]);\n\n if (target) return { drawing, clear, undo, draw, lines: paintRef.current.lines };\n return { ref: internalRef, drawing, clear, undo, draw, lines: paintRef.current.lines };\n}) as UsePaint;\n"],"names":["DEFAULT_BRUSH_RADIUS","Pointer","x","y","point","diff","angle","distance","angleRotated","Paint","radius","smooth","usePaint","params","target","isTarget","options","color","opacity","paintRef","useRef","drawing","setIsDrawing","useState","internalRef","useRefState","contextRef","draw","points","p1","p2","i","midPoint","clearCanvas","onMouseMove","useEvent","event","onMouseDown","onMouseUp","clear","undo","useEffect","element","getElement"],"mappings":";;;;;AAWA,MAAMA,IAAuB;AAOtB,MAAMC,EAAyB;AAAA,EACpC;AAAA,EAEA;AAAA,EAEA,YAAYC,GAAWC,GAAW;AAChC,SAAK,IAAID,GACT,KAAK,IAAIC;AAAA,EAAA;AAAA,EAGX,OAAOC,GAAc;AACnB,SAAK,IAAIA,EAAM,GACf,KAAK,IAAIA,EAAM;AAAA,EAAA;AAAA,EAGjB,gBAAgBA,GAAc;AAC5B,WAAO,IAAIH,EAAQ,KAAK,IAAIG,EAAM,GAAG,KAAK,IAAIA,EAAM,CAAC;AAAA,EAAA;AAAA,EAGvD,cAAcA,GAAc;AAC1B,UAAMC,IAAO,KAAK,gBAAgBD,CAAK;AACvC,WAAO,KAAK,KAAKC,EAAK,KAAK,IAAIA,EAAK,KAAK,CAAC;AAAA,EAAA;AAAA,EAG5C,WAAWD,GAAc;AACvB,UAAMC,IAAO,KAAK,gBAAgBD,CAAK;AACvC,WAAO,KAAK,MAAMC,EAAK,GAAGA,EAAK,CAAC;AAAA,EAAA;AAAA,EAGlC,SAASD,GAAc;AACrB,WAAO,KAAK,MAAMA,EAAM,KAAK,KAAK,MAAMA,EAAM;AAAA,EAAA;AAAA,EAGhD,YAEEE,GAEAC,GACA;AAEA,UAAMC,IAAeF,IAAQ,KAAK,KAAK;AAEvC,gBAAK,KAAK,KAAK,IAAIE,CAAY,IAAID,GACnC,KAAK,KAAK,KAAK,IAAIC,CAAY,IAAID,GAE5B;AAAA,EAAA;AAEX;AAEO,MAAME,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AAaR,CAAW,GACjDQ,EAAQ,iBAAiB,WAAWJ,CAAS,GAEtC,MAAM;AACX,QAAKI,MACLA,EAAQ,oBAAoB,aAAaL,CAAW,GACpDK,EAAQ,oBAAoB,aAAaR,CAAW,GACpDQ,EAAQ,oBAAoB,WAAWJ,CAAS;AAAA,MAAA;AAAA,EAClD,GACC,CAACxB,GAAQU,EAAY,KAAK,CAAC,GAE1BV,IAAe,EAAE,SAAAO,GAAS,OAAAkB,GAAO,MAAAC,GAAM,MAAAb,GAAM,OAAOR,EAAS,QAAQ,MAAA,IAClE,EAAE,KAAKK,GAAa,SAAAH,GAAS,OAAAkB,GAAO,MAAAC,GAAM,MAAAb,GAAM,OAAOR,EAAS,QAAQ,MAAA;AACjF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useParallax.mjs","sources":["../../../../src/hooks/useParallax/useParallax.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { getElement, isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useDeviceOrientation } from '../useDeviceOrientation/useDeviceOrientation';\nimport { useRefState } from '../useRefState/useRefState';\nimport { useScreenOrientation } from '../useScreenOrientation/useScreenOrientation';\n\n/** The use parallax value type */\nexport interface UseParallaxValue {\n /** Roll value. Scaled to `-0.5 ~ 0.5` */\n roll: number;\n /** Sensor source, can be `mouse` or `deviceOrientation` */\n source: 'deviceOrientation' | 'mouse';\n /** Tilt value. Scaled to `-0.5 ~ 0.5` */\n tilt: number;\n}\n\n/** The use parallax options type */\nexport interface UseParallaxOptions {\n /** Device orientation roll adjust function */\n deviceOrientationRollAdjust?: (value: number) => number;\n /** Device orientation tilt adjust function */\n deviceOrientationTiltAdjust?: (value: number) => number;\n /** Mouse roll adjust function */\n mouseRollAdjust?: (value: number) => number;\n /** Mouse tilt adjust function */\n mouseTiltAdjust?: (value: number) => number;\n}\n\ninterface UseParallaxReturn {\n value: UseParallaxValue;\n}\n\nexport interface UseParallax {\n (target: HookTarget, options?: UseParallaxOptions): UseParallaxReturn;\n\n <Target extends Element>(\n options?: UseParallaxOptions,\n target?: never\n ): UseParallaxReturn & {\n ref: StateRef<Target>;\n };\n}\n\n/**\n * @name useParallax\n * @description - Hook to help create parallax effect\n * @category Sensors\n *\n * @overload\n * @param {HookTarget} target The target element for the parallax effect\n * @param {UseParallaxOptions} options The options for the parallax effect\n * @returns {UseParallaxReturn} An object of parallax values\n *\n * @example\n * const { value } = useParallax(ref);\n *\n * @overload\n * @template Target The target element for the parallax effect\n * @param {UseParallaxOptions} options The options for the parallax effect\n * @returns {UseParallaxReturn & { ref: StateRef<Target> }} An object of parallax values\n *\n * @example\n * const { ref, value } = useParallax();\n */\nexport const useParallax = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = (params[1] ? params[1] : params[0]) as UseParallaxOptions | undefined;\n\n const internalRef = useRefState<Element>();\n\n const screenOrientation = useScreenOrientation();\n const deviceOrientation = useDeviceOrientation();\n\n const {\n deviceOrientationRollAdjust = (value) => value,\n deviceOrientationTiltAdjust = (value) => value,\n mouseRollAdjust = (value) => value,\n mouseTiltAdjust = (value) => value\n } = (options ?? {}) as UseParallaxOptions;\n\n const [value, setValue] = useState({\n roll: 0,\n tilt: 0,\n source: 'mouse'\n });\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (target ? getElement(target) : internalRef.current) as Element;\n if (!element) return;\n\n const onMouseMove = (event: MouseEvent) => {\n const { left, top } = element.getBoundingClientRect();\n const elementPositionX = left + window.scrollX;\n const elementPositionY = top + window.scrollY;\n\n const getSource = () => {\n const isDeviceOrientation =\n deviceOrientation.supported &&\n (deviceOrientation.value.alpha || deviceOrientation.value.gamma);\n\n if (isDeviceOrientation) return 'deviceOrientation';\n return 'mouse';\n };\n\n const getRoll = () => {\n const source = getSource();\n if (source === 'deviceOrientation') {\n let value: number;\n switch (screenOrientation.value.orientationType) {\n case 'landscape-primary':\n value = deviceOrientation.value.gamma! / 90;\n break;\n case 'landscape-secondary':\n value = -deviceOrientation.value.gamma! / 90;\n break;\n case 'portrait-primary':\n value = -deviceOrientation.value.beta! / 90;\n break;\n case 'portrait-secondary':\n value = deviceOrientation.value.beta! / 90;\n break;\n default:\n value = -deviceOrientation.value.beta! / 90;\n }\n return deviceOrientationRollAdjust(value);\n } else {\n const y = event.pageY - elementPositionY;\n const height = element.getBoundingClientRect().height;\n const value = -(y - height / 2) / height;\n return mouseRollAdjust(value);\n }\n };\n\n const getTilt = () => {\n const source = getSource();\n if (source === 'deviceOrientation') {\n let value: number;\n switch (screenOrientation.value.orientationType) {\n case 'landscape-primary':\n value = deviceOrientation.value.beta! / 90;\n break;\n case 'landscape-secondary':\n value = -deviceOrientation.value.beta! / 90;\n break;\n case 'portrait-primary':\n value = deviceOrientation.value.gamma! / 90;\n break;\n case 'portrait-secondary':\n value = -deviceOrientation.value.gamma! / 90;\n break;\n default:\n value = deviceOrientation.value.gamma! / 90;\n }\n return deviceOrientationTiltAdjust(value);\n } else {\n const x = event.pageX - elementPositionX;\n const width = element.getBoundingClientRect().width;\n const value = (x - width / 2) / width;\n return mouseTiltAdjust(value);\n }\n };\n\n const source = getSource();\n const roll = getRoll();\n const tilt = getTilt();\n\n setValue({\n roll,\n source,\n tilt\n });\n };\n\n document.addEventListener('mousemove', onMouseMove);\n return () => {\n document.removeEventListener('mousemove', onMouseMove);\n };\n }, [\n target,\n internalRef.state,\n screenOrientation.value.angle,\n screenOrientation.value.orientationType,\n deviceOrientation.value.gamma,\n deviceOrientation.value.beta,\n deviceOrientation.value.alpha,\n deviceOrientation.value.absolute\n ]);\n\n if (target) return { value };\n\n return {\n ref: internalRef,\n value\n };\n}) as UseParallax;\n"],"names":["useParallax","params","target","isTarget","options","internalRef","useRefState","screenOrientation","useScreenOrientation","deviceOrientation","useDeviceOrientation","deviceOrientationRollAdjust","value","deviceOrientationTiltAdjust","mouseRollAdjust","mouseTiltAdjust","setValue","useState","useEffect","element","getElement","onMouseMove","event","left","top","elementPositionX","elementPositionY","getSource","getRoll","y","height","getTilt","x","width","source","roll","tilt"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"useParallax.mjs","sources":["../../../../src/hooks/useParallax/useParallax.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport type { HookTarget } from '@/utils/helpers';\n\nimport { getElement, isTarget } from '@/utils/helpers';\n\nimport type { StateRef } from '../useRefState/useRefState';\n\nimport { useDeviceOrientation } from '../useDeviceOrientation/useDeviceOrientation';\nimport { useRefState } from '../useRefState/useRefState';\nimport { useScreenOrientation } from '../useScreenOrientation/useScreenOrientation';\n\n/** The use parallax value type */\nexport interface UseParallaxValue {\n /** Roll value. Scaled to `-0.5 ~ 0.5` */\n roll: number;\n /** Sensor source, can be `mouse` or `deviceOrientation` */\n source: 'deviceOrientation' | 'mouse';\n /** Tilt value. Scaled to `-0.5 ~ 0.5` */\n tilt: number;\n}\n\n/** The use parallax options type */\nexport interface UseParallaxOptions {\n /** Device orientation roll adjust function */\n deviceOrientationRollAdjust?: (value: number) => number;\n /** Device orientation tilt adjust function */\n deviceOrientationTiltAdjust?: (value: number) => number;\n /** Mouse roll adjust function */\n mouseRollAdjust?: (value: number) => number;\n /** Mouse tilt adjust function */\n mouseTiltAdjust?: (value: number) => number;\n}\n\ninterface UseParallaxReturn {\n value: UseParallaxValue;\n}\n\nexport interface UseParallax {\n (target: HookTarget, options?: UseParallaxOptions): UseParallaxReturn;\n\n <Target extends Element>(\n options?: UseParallaxOptions,\n target?: never\n ): UseParallaxReturn & {\n ref: StateRef<Target>;\n };\n}\n\n/**\n * @name useParallax\n * @description - Hook to help create parallax effect\n * @category Sensors\n *\n * @overload\n * @param {HookTarget} target The target element for the parallax effect\n * @param {UseParallaxOptions} options The options for the parallax effect\n * @returns {UseParallaxReturn} An object of parallax values\n *\n * @example\n * const { value } = useParallax(ref);\n *\n * @overload\n * @template Target The target element for the parallax effect\n * @param {UseParallaxOptions} options The options for the parallax effect\n * @returns {UseParallaxReturn & { ref: StateRef<Target> }} An object of parallax values\n *\n * @example\n * const { ref, value } = useParallax();\n */\nexport const useParallax = ((...params: any[]) => {\n const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;\n const options = (params[1] ? params[1] : params[0]) as UseParallaxOptions | undefined;\n\n const internalRef = useRefState<Element>();\n\n const screenOrientation = useScreenOrientation();\n const deviceOrientation = useDeviceOrientation();\n\n const {\n deviceOrientationRollAdjust = (value) => value,\n deviceOrientationTiltAdjust = (value) => value,\n mouseRollAdjust = (value) => value,\n mouseTiltAdjust = (value) => value\n } = (options ?? {}) as UseParallaxOptions;\n\n const [value, setValue] = useState({\n roll: 0,\n tilt: 0,\n source: 'mouse'\n });\n\n useEffect(() => {\n if (!target && !internalRef.state) return;\n\n const element = (target ? getElement(target) : internalRef.current) as Element;\n if (!element) return;\n\n const onMouseMove = (event: MouseEvent) => {\n const { left, top } = element.getBoundingClientRect();\n const elementPositionX = left + window.scrollX;\n const elementPositionY = top + window.scrollY;\n\n const getSource = () => {\n const isDeviceOrientation =\n deviceOrientation.supported &&\n (deviceOrientation.value.alpha || deviceOrientation.value.gamma);\n\n if (isDeviceOrientation) return 'deviceOrientation';\n return 'mouse';\n };\n\n const getRoll = () => {\n const source = getSource();\n if (source === 'deviceOrientation') {\n let value: number;\n switch (screenOrientation.value.orientationType) {\n case 'landscape-primary':\n value = deviceOrientation.value.gamma! / 90;\n break;\n case 'landscape-secondary':\n value = -deviceOrientation.value.gamma! / 90;\n break;\n case 'portrait-primary':\n value = -deviceOrientation.value.beta! / 90;\n break;\n case 'portrait-secondary':\n value = deviceOrientation.value.beta! / 90;\n break;\n default:\n value = -deviceOrientation.value.beta! / 90;\n }\n return deviceOrientationRollAdjust(value);\n } else {\n const y = event.pageY - elementPositionY;\n const height = element.getBoundingClientRect().height;\n const value = -(y - height / 2) / height;\n return mouseRollAdjust(value);\n }\n };\n\n const getTilt = () => {\n const source = getSource();\n if (source === 'deviceOrientation') {\n let value: number;\n switch (screenOrientation.value.orientationType) {\n case 'landscape-primary':\n value = deviceOrientation.value.beta! / 90;\n break;\n case 'landscape-secondary':\n value = -deviceOrientation.value.beta! / 90;\n break;\n case 'portrait-primary':\n value = deviceOrientation.value.gamma! / 90;\n break;\n case 'portrait-secondary':\n value = -deviceOrientation.value.gamma! / 90;\n break;\n default:\n value = deviceOrientation.value.gamma! / 90;\n }\n return deviceOrientationTiltAdjust(value);\n } else {\n const x = event.pageX - elementPositionX;\n const width = element.getBoundingClientRect().width;\n const value = (x - width / 2) / width;\n return mouseTiltAdjust(value);\n }\n };\n\n const source = getSource();\n const roll = getRoll();\n const tilt = getTilt();\n\n setValue({\n roll,\n source,\n tilt\n });\n };\n\n document.addEventListener('mousemove', onMouseMove);\n return () => {\n document.removeEventListener('mousemove', onMouseMove);\n };\n }, [\n target,\n internalRef.state,\n screenOrientation.value.angle,\n screenOrientation.value.orientationType,\n deviceOrientation.value.gamma,\n deviceOrientation.value.beta,\n deviceOrientation.value.alpha,\n deviceOrientation.value.absolute\n ]);\n\n if (target) return { value };\n\n return {\n ref: internalRef,\n value\n };\n}) as UseParallax;\n"],"names":["useParallax","params","target","isTarget","options","internalRef","useRefState","screenOrientation","useScreenOrientation","deviceOrientation","useDeviceOrientation","deviceOrientationRollAdjust","value","deviceOrientationTiltAdjust","mouseRollAdjust","mouseTiltAdjust","setValue","useState","useEffect","element","getElement","onMouseMove","event","left","top","elementPositionX","elementPositionY","getSource","getRoll","y","height","getTilt","x","width","source","roll","tilt"],"mappings":";;;;;;AAsEO,MAAMA,IAAe,IAAIC,MAAkB;AAChD,QAAMC,IAAUC,EAASF,EAAO,CAAC,CAAC,IAAIA,EAAO,CAAC,IAAI,QAC5CG,IAAWH,EAAO,CAAC,IAAIA,EAAO,CAAC,IAAIA,EAAO,CAAC,GAE3CI,IAAcC,EAAA,GAEdC,IAAoBC,EAAA,GACpBC,IAAoBC,EAAA,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