@oneplatformdev/hooks 0.1.0-7 → 0.1.0-9
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/.babelrc +12 -0
- package/CHANGELOG.md +23 -0
- package/dist/CHANGELOG.md +8 -0
- package/dist/package.json +2 -1
- package/node_modules/@types/lodash.debounce/LICENSE +21 -0
- package/node_modules/@types/lodash.debounce/README.md +23 -0
- package/node_modules/@types/lodash.debounce/index.d.ts +4 -0
- package/node_modules/@types/lodash.debounce/package.json +32 -0
- package/node_modules/@types/react/LICENSE +21 -0
- package/node_modules/@types/react/README.md +15 -0
- package/node_modules/@types/react/canary.d.ts +35 -0
- package/node_modules/@types/react/compiler-runtime.d.ts +4 -0
- package/node_modules/@types/react/experimental.d.ts +125 -0
- package/node_modules/@types/react/global.d.ts +160 -0
- package/node_modules/@types/react/index.d.ts +4206 -0
- package/node_modules/@types/react/jsx-dev-runtime.d.ts +45 -0
- package/node_modules/@types/react/jsx-runtime.d.ts +36 -0
- package/node_modules/@types/react/package.json +210 -0
- package/node_modules/@types/react/ts5.0/canary.d.ts +35 -0
- package/node_modules/@types/react/ts5.0/experimental.d.ts +125 -0
- package/node_modules/@types/react/ts5.0/global.d.ts +160 -0
- package/node_modules/@types/react/ts5.0/index.d.ts +4193 -0
- package/node_modules/@types/react/ts5.0/jsx-dev-runtime.d.ts +44 -0
- package/node_modules/@types/react/ts5.0/jsx-runtime.d.ts +35 -0
- package/node_modules/@types/react/ts5.0/v18/global.d.ts +160 -0
- package/node_modules/@types/react/ts5.0/v18/index.d.ts +4543 -0
- package/node_modules/@types/react/ts5.0/v18/jsx-dev-runtime.d.ts +45 -0
- package/node_modules/@types/react/ts5.0/v18/jsx-runtime.d.ts +36 -0
- package/node_modules/@types/react/ts5.0/v18/ts5.0/global.d.ts +160 -0
- package/node_modules/@types/react/ts5.0/v18/ts5.0/index.d.ts +4530 -0
- package/node_modules/@types/react/ts5.0/v18/ts5.0/jsx-dev-runtime.d.ts +44 -0
- package/node_modules/@types/react/ts5.0/v18/ts5.0/jsx-runtime.d.ts +35 -0
- package/package.json +2 -1
- package/src/index.ts +36 -0
- package/src/useBoolean/index.ts +1 -0
- package/src/useBoolean/useBoolean.ts +50 -0
- package/src/useClickAnyWhere/index.ts +1 -0
- package/src/useClickAnyWhere/useClickAnyWhere.ts +22 -0
- package/src/useCopyToClipboard/index.ts +1 -0
- package/src/useCopyToClipboard/useCopyToClipboard.ts +58 -0
- package/src/useCountdown/index.ts +1 -0
- package/src/useCountdown/useCountdown.ts +102 -0
- package/src/useCounter/index.ts +1 -0
- package/src/useCounter/useCounter.ts +52 -0
- package/src/useDarkMode/index.ts +1 -0
- package/src/useDarkMode/useDarkMode.ts +92 -0
- package/src/useDebounceCallback/index.ts +2 -0
- package/src/useDebounceCallback/useDebounceCallback.ts +115 -0
- package/src/useDebounceValue/index.ts +1 -0
- package/src/useDebounceValue/useDebounceValue.ts +67 -0
- package/src/useDocumentTitle/index.ts +1 -0
- package/src/useDocumentTitle/useDocumentTitle.ts +43 -0
- package/src/useEventCallback/index.ts +1 -0
- package/src/useEventCallback/useEventCallback.ts +40 -0
- package/src/useEventListener/index.ts +1 -0
- package/src/useEventListener/useEventListener.ts +121 -0
- package/src/useHover/index.ts +1 -0
- package/src/useHover/useHover.ts +37 -0
- package/src/useIntersectionObserver/index.ts +1 -0
- package/src/useIntersectionObserver/useIntersectionObserver.ts +186 -0
- package/src/useInterval/index.ts +1 -0
- package/src/useInterval/useInterval.ts +43 -0
- package/src/useIsClient/index.ts +1 -0
- package/src/useIsClient/useIsClient.ts +22 -0
- package/src/useIsMobile/index.ts +2 -0
- package/src/useIsMobile/types.ts +3 -0
- package/src/useIsMobile/useIsMobile.tsx +23 -0
- package/src/useIsMounted/index.ts +1 -0
- package/src/useIsMounted/useIsMounted.ts +15 -0
- package/src/useIsomorphicLayoutEffect/index.ts +1 -0
- package/src/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.ts +17 -0
- package/src/useLocalStorage/index.ts +1 -0
- package/src/useLocalStorage/useLocalStorage.ts +191 -0
- package/src/useLockBody/index.ts +1 -0
- package/src/useLockBody/useLockBody.tsx +12 -0
- package/src/useMap/index.ts +1 -0
- package/src/useMap/useMap.ts +83 -0
- package/src/useMediaQuery/index.ts +1 -0
- package/src/useMediaQuery/useMediaQuery.ts +83 -0
- package/src/useOnClickOutside/index.ts +1 -0
- package/src/useOnClickOutside/useOnClickOutside.ts +61 -0
- package/src/useQueryString/index.ts +1 -0
- package/src/useQueryString/useQueryString.ts +26 -0
- package/src/useReadLocalStorage/index.ts +1 -0
- package/src/useReadLocalStorage/useReadLocalStorage.ts +122 -0
- package/src/useResizeObserver/index.ts +1 -0
- package/src/useResizeObserver/useResizeObserver.ts +131 -0
- package/src/useScreen/index.ts +1 -0
- package/src/useScreen/useScreen.ts +106 -0
- package/src/useScript/index.ts +1 -0
- package/src/useScript/useScript.ts +142 -0
- package/src/useScrollLock/index.ts +1 -0
- package/src/useScrollLock/useScrollLock.ts +141 -0
- package/src/useSessionStorage/index.ts +1 -0
- package/src/useSessionStorage/useSessionStorage.ts +191 -0
- package/src/useStep/index.ts +1 -0
- package/src/useStep/useStep.ts +83 -0
- package/src/useTernaryDarkMode/index.ts +1 -0
- package/src/useTernaryDarkMode/useTernaryDarkMode.ts +81 -0
- package/src/useTimeout/index.ts +1 -0
- package/src/useTimeout/useTimeout.ts +44 -0
- package/src/useToggle/index.ts +1 -0
- package/src/useToggle/useToggle.ts +30 -0
- package/src/useUnmount/index.ts +1 -0
- package/src/useUnmount/useUnmount.ts +26 -0
- package/src/useWindowSize/index.ts +1 -0
- package/src/useWindowSize/useWindowSize.ts +100 -0
- package/tsconfig.json +10 -0
- package/tsconfig.lib.json +33 -0
- package/vite.config.ts +62 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { DebouncedState } from '../useDebounceCallback'
|
|
4
|
+
import { useDebounceCallback } from '../useDebounceCallback'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook options.
|
|
8
|
+
* @template T - The type of the value.
|
|
9
|
+
*/
|
|
10
|
+
type UseDebounceValueOptions<T> = {
|
|
11
|
+
/**
|
|
12
|
+
* Determines whether the function should be invoked on the leading edge of the timeout.
|
|
13
|
+
* @default false
|
|
14
|
+
*/
|
|
15
|
+
leading?: boolean
|
|
16
|
+
/**
|
|
17
|
+
* Determines whether the function should be invoked on the trailing edge of the timeout.
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
trailing?: boolean
|
|
21
|
+
/**
|
|
22
|
+
* The maximum time the specified function is allowed to be delayed before it is invoked.
|
|
23
|
+
*/
|
|
24
|
+
maxWait?: number
|
|
25
|
+
/** A function to determine if the value has changed. Defaults to a function that checks if the value is strictly equal to the previous value. */
|
|
26
|
+
equalityFn?: (left: T, right: T) => boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Custom hook that returns a debounced version of the provided value, along with a function to update it.
|
|
31
|
+
* @template T - The type of the value.
|
|
32
|
+
* @param {T | (() => T)} initialValue - The value to be debounced.
|
|
33
|
+
* @param {number} delay - The delay in milliseconds before the value is updated (default is 500ms).
|
|
34
|
+
* @param {object} [options] - Optional configurations for the debouncing behavior.
|
|
35
|
+
* @returns {[T, DebouncedState<(value: T) => void>]} An array containing the debounced value and the function to update it.
|
|
36
|
+
* @public
|
|
37
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-debounce-value)
|
|
38
|
+
* @example
|
|
39
|
+
* ```tsx
|
|
40
|
+
* const [debouncedValue, updateDebouncedValue] = useDebounceValue(inputValue, 500, { leading: true });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function useDebounceValue<T>(
|
|
44
|
+
initialValue: T | (() => T),
|
|
45
|
+
delay: number,
|
|
46
|
+
options?: UseDebounceValueOptions<T>,
|
|
47
|
+
): [T, DebouncedState<(value: T) => void>] {
|
|
48
|
+
const eq = options?.equalityFn ?? ((left: T, right: T) => left === right)
|
|
49
|
+
const unwrappedInitialValue =
|
|
50
|
+
initialValue instanceof Function ? initialValue() : initialValue
|
|
51
|
+
const [debouncedValue, setDebouncedValue] = useState<T>(unwrappedInitialValue)
|
|
52
|
+
const previousValueRef = useRef<T | undefined>(unwrappedInitialValue)
|
|
53
|
+
|
|
54
|
+
const updateDebouncedValue = useDebounceCallback(
|
|
55
|
+
setDebouncedValue,
|
|
56
|
+
delay,
|
|
57
|
+
options,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
// Update the debounced value if the initial value changes
|
|
61
|
+
if (!eq(previousValueRef.current as T, unwrappedInitialValue)) {
|
|
62
|
+
updateDebouncedValue(unwrappedInitialValue)
|
|
63
|
+
previousValueRef.current = unwrappedInitialValue
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return [debouncedValue, updateDebouncedValue]
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useDocumentTitle'
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'
|
|
4
|
+
import { useUnmount } from '../useUnmount'
|
|
5
|
+
|
|
6
|
+
/** Hook options. */
|
|
7
|
+
type UseDocumentTitleOptions = {
|
|
8
|
+
/** Whether to keep the title after unmounting the component (default is `true`). */
|
|
9
|
+
preserveTitleOnUnmount?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Custom hook that sets the document title.
|
|
14
|
+
* @param {string} title - The title to set.
|
|
15
|
+
* @param {?UseDocumentTitleOptions} [options] - The options.
|
|
16
|
+
* @public
|
|
17
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-document-title)
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* useDocumentTitle('My new title');
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useDocumentTitle(
|
|
24
|
+
title: string,
|
|
25
|
+
options: UseDocumentTitleOptions = {},
|
|
26
|
+
): void {
|
|
27
|
+
const { preserveTitleOnUnmount = true } = options
|
|
28
|
+
const defaultTitle = useRef<string | null>(null)
|
|
29
|
+
|
|
30
|
+
useIsomorphicLayoutEffect(() => {
|
|
31
|
+
defaultTitle.current = window.document.title
|
|
32
|
+
}, [])
|
|
33
|
+
|
|
34
|
+
useIsomorphicLayoutEffect(() => {
|
|
35
|
+
window.document.title = title
|
|
36
|
+
}, [title])
|
|
37
|
+
|
|
38
|
+
useUnmount(() => {
|
|
39
|
+
if (!preserveTitleOnUnmount && defaultTitle.current) {
|
|
40
|
+
window.document.title = defaultTitle.current
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useEventCallback'
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useCallback, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Custom hook that creates a memoized event callback.
|
|
7
|
+
* @template Args - An array of argument types for the event callback.
|
|
8
|
+
* @template R - The return type of the event callback.
|
|
9
|
+
* @param {(...args: Args) => R} fn - The callback function.
|
|
10
|
+
* @returns {(...args: Args) => R} A memoized event callback function.
|
|
11
|
+
* @public
|
|
12
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-event-callback)
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const handleClick = useEventCallback((event) => {
|
|
16
|
+
* // Handle the event here
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function useEventCallback<Args extends unknown[], R>(
|
|
21
|
+
fn: (...args: Args) => R,
|
|
22
|
+
): (...args: Args) => R
|
|
23
|
+
export function useEventCallback<Args extends unknown[], R>(
|
|
24
|
+
fn: ((...args: Args) => R) | undefined,
|
|
25
|
+
): ((...args: Args) => R) | undefined
|
|
26
|
+
export function useEventCallback<Args extends unknown[], R>(
|
|
27
|
+
fn: ((...args: Args) => R) | undefined,
|
|
28
|
+
): ((...args: Args) => R) | undefined {
|
|
29
|
+
const ref = useRef<typeof fn>(() => {
|
|
30
|
+
throw new Error('Cannot call an event handler while rendering.')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
useIsomorphicLayoutEffect(() => {
|
|
34
|
+
ref.current = fn
|
|
35
|
+
}, [fn])
|
|
36
|
+
|
|
37
|
+
return useCallback((...args: Args) => ref.current?.(...args), [ref]) as (
|
|
38
|
+
...args: Args
|
|
39
|
+
) => R
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useEventListener'
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { RefObject } from 'react'
|
|
4
|
+
|
|
5
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect/useIsomorphicLayoutEffect'
|
|
6
|
+
|
|
7
|
+
// MediaQueryList Event based useEventListener interface
|
|
8
|
+
function useEventListener<K extends keyof MediaQueryListEventMap>(
|
|
9
|
+
eventName: K,
|
|
10
|
+
handler: (event: MediaQueryListEventMap[K]) => void,
|
|
11
|
+
element: RefObject<MediaQueryList>,
|
|
12
|
+
options?: boolean | AddEventListenerOptions,
|
|
13
|
+
): void
|
|
14
|
+
|
|
15
|
+
// Window Event based useEventListener interface
|
|
16
|
+
function useEventListener<K extends keyof WindowEventMap>(
|
|
17
|
+
eventName: K,
|
|
18
|
+
handler: (event: WindowEventMap[K]) => void,
|
|
19
|
+
element?: undefined,
|
|
20
|
+
options?: boolean | AddEventListenerOptions,
|
|
21
|
+
): void
|
|
22
|
+
|
|
23
|
+
// Element Event based useEventListener interface
|
|
24
|
+
function useEventListener<
|
|
25
|
+
K extends keyof HTMLElementEventMap & keyof SVGElementEventMap,
|
|
26
|
+
T extends Element = K extends keyof HTMLElementEventMap
|
|
27
|
+
? HTMLDivElement
|
|
28
|
+
: SVGElement,
|
|
29
|
+
>(
|
|
30
|
+
eventName: K,
|
|
31
|
+
handler:
|
|
32
|
+
| ((event: HTMLElementEventMap[K]) => void)
|
|
33
|
+
| ((event: SVGElementEventMap[K]) => void),
|
|
34
|
+
element: RefObject<T>,
|
|
35
|
+
options?: boolean | AddEventListenerOptions,
|
|
36
|
+
): void
|
|
37
|
+
|
|
38
|
+
// Document Event based useEventListener interface
|
|
39
|
+
function useEventListener<K extends keyof DocumentEventMap>(
|
|
40
|
+
eventName: K,
|
|
41
|
+
handler: (event: DocumentEventMap[K]) => void,
|
|
42
|
+
element: RefObject<Document>,
|
|
43
|
+
options?: boolean | AddEventListenerOptions,
|
|
44
|
+
): void
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Custom hook that attaches event listeners to DOM elements, the window, or media query lists.
|
|
48
|
+
* @template KW - The type of event for window events.
|
|
49
|
+
* @template KH - The type of event for HTML or SVG element events.
|
|
50
|
+
* @template KM - The type of event for media query list events.
|
|
51
|
+
* @template T - The type of the DOM element (default is `HTMLElement`).
|
|
52
|
+
* @param {KW | KH | KM} eventName - The name of the event to listen for.
|
|
53
|
+
* @param {(event: WindowEventMap[KW] | HTMLElementEventMap[KH] | SVGElementEventMap[KH] | MediaQueryListEventMap[KM] | Event) => void} handler - The event handler function.
|
|
54
|
+
* @param {RefObject<T>} [element] - The DOM element or media query list to attach the event listener to (optional).
|
|
55
|
+
* @param {boolean | AddEventListenerOptions} [options] - An options object that specifies characteristics about the event listener (optional).
|
|
56
|
+
* @public
|
|
57
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-event-listener)
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* // Example 1: Attach a window event listener
|
|
61
|
+
* useEventListener('resize', handleResize);
|
|
62
|
+
* ```
|
|
63
|
+
* @example
|
|
64
|
+
* ```tsx
|
|
65
|
+
* // Example 2: Attach a document event listener with options
|
|
66
|
+
* const elementRef = useRef(document);
|
|
67
|
+
* useEventListener('click', handleClick, elementRef, { capture: true });
|
|
68
|
+
* ```
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* // Example 3: Attach an element event listener
|
|
72
|
+
* const buttonRef = useRef<HTMLButtonElement>(null);
|
|
73
|
+
* useEventListener('click', handleButtonClick, buttonRef);
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function useEventListener<
|
|
77
|
+
KW extends keyof WindowEventMap,
|
|
78
|
+
KH extends keyof HTMLElementEventMap & keyof SVGElementEventMap,
|
|
79
|
+
KM extends keyof MediaQueryListEventMap,
|
|
80
|
+
T extends HTMLElement | SVGAElement | MediaQueryList = HTMLElement,
|
|
81
|
+
>(
|
|
82
|
+
eventName: KW | KH | KM,
|
|
83
|
+
handler: (
|
|
84
|
+
event:
|
|
85
|
+
| WindowEventMap[KW]
|
|
86
|
+
| HTMLElementEventMap[KH]
|
|
87
|
+
| SVGElementEventMap[KH]
|
|
88
|
+
| MediaQueryListEventMap[KM]
|
|
89
|
+
| Event,
|
|
90
|
+
) => void,
|
|
91
|
+
element?: RefObject<T>,
|
|
92
|
+
options?: boolean | AddEventListenerOptions,
|
|
93
|
+
) {
|
|
94
|
+
// Create a ref that stores handler
|
|
95
|
+
const savedHandler = useRef(handler)
|
|
96
|
+
|
|
97
|
+
useIsomorphicLayoutEffect(() => {
|
|
98
|
+
savedHandler.current = handler
|
|
99
|
+
}, [handler])
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
// Define the listening target
|
|
103
|
+
const targetElement: T | Window = element?.current ?? window
|
|
104
|
+
|
|
105
|
+
if (!(targetElement && targetElement.addEventListener)) return
|
|
106
|
+
|
|
107
|
+
// Create event listener that calls handler function stored in ref
|
|
108
|
+
const listener: typeof handler = event => {
|
|
109
|
+
savedHandler.current(event)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
targetElement.addEventListener(eventName, listener, options)
|
|
113
|
+
|
|
114
|
+
// Remove event listener on cleanup
|
|
115
|
+
return () => {
|
|
116
|
+
targetElement.removeEventListener(eventName, listener, options)
|
|
117
|
+
}
|
|
118
|
+
}, [eventName, element, options])
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { useEventListener }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useHover'
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { RefObject } from 'react'
|
|
4
|
+
|
|
5
|
+
import { useEventListener } from '../useEventListener'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Custom hook that tracks whether a DOM element is being hovered over.
|
|
9
|
+
* @template T - The type of the DOM element. Defaults to `HTMLElement`.
|
|
10
|
+
* @param {RefObject<T>} elementRef - The ref object for the DOM element to track.
|
|
11
|
+
* @returns {boolean} A boolean value indicating whether the element is being hovered over.
|
|
12
|
+
* @public
|
|
13
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-hover)
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const buttonRef = useRef<HTMLButtonElement>(null);
|
|
17
|
+
* const isHovered = useHover(buttonRef);
|
|
18
|
+
* // Access the isHovered variable to determine if the button is being hovered over.
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function useHover<T extends HTMLElement = HTMLElement>(
|
|
22
|
+
elementRef: RefObject<T>,
|
|
23
|
+
): boolean {
|
|
24
|
+
const [value, setValue] = useState<boolean>(false)
|
|
25
|
+
|
|
26
|
+
const handleMouseEnter = () => {
|
|
27
|
+
setValue(true)
|
|
28
|
+
}
|
|
29
|
+
const handleMouseLeave = () => {
|
|
30
|
+
setValue(false)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
useEventListener('mouseenter', handleMouseEnter, elementRef)
|
|
34
|
+
useEventListener('mouseleave', handleMouseLeave, elementRef)
|
|
35
|
+
|
|
36
|
+
return value
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useIntersectionObserver'
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/** The hook internal state. */
|
|
4
|
+
type State = {
|
|
5
|
+
/** A boolean indicating if the element is intersecting. */
|
|
6
|
+
isIntersecting: boolean
|
|
7
|
+
/** The intersection observer entry. */
|
|
8
|
+
entry?: IntersectionObserverEntry
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Represents the options for configuring the Intersection Observer. */
|
|
12
|
+
type UseIntersectionObserverOptions = {
|
|
13
|
+
/**
|
|
14
|
+
* The element that is used as the viewport for checking visibility of the target.
|
|
15
|
+
* @default null
|
|
16
|
+
*/
|
|
17
|
+
root?: Element | Document | null
|
|
18
|
+
/**
|
|
19
|
+
* A margin around the root.
|
|
20
|
+
* @default '0%'
|
|
21
|
+
*/
|
|
22
|
+
rootMargin?: string
|
|
23
|
+
/**
|
|
24
|
+
* A threshold indicating the percentage of the target's visibility needed to trigger the callback.
|
|
25
|
+
* @default 0
|
|
26
|
+
*/
|
|
27
|
+
threshold?: number | number[]
|
|
28
|
+
/**
|
|
29
|
+
* If true, freezes the intersection state once the element becomes visible.
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
32
|
+
freezeOnceVisible?: boolean
|
|
33
|
+
/**
|
|
34
|
+
* A callback function to be invoked when the intersection state changes.
|
|
35
|
+
* @param {boolean} isIntersecting - A boolean indicating if the element is intersecting.
|
|
36
|
+
* @param {IntersectionObserverEntry} entry - The intersection observer Entry.
|
|
37
|
+
* @default undefined
|
|
38
|
+
*/
|
|
39
|
+
onChange?: (isIntersecting: boolean, entry: IntersectionObserverEntry) => void
|
|
40
|
+
/**
|
|
41
|
+
* The initial state of the intersection.
|
|
42
|
+
* @default false
|
|
43
|
+
*/
|
|
44
|
+
initialIsIntersecting?: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The return type of the useIntersectionObserver hook.
|
|
49
|
+
*
|
|
50
|
+
* Supports both tuple and object destructing.
|
|
51
|
+
* @param {(node: Element | null) => void} ref - The ref callback function.
|
|
52
|
+
* @param {boolean} isIntersecting - A boolean indicating if the element is intersecting.
|
|
53
|
+
* @param {IntersectionObserverEntry | undefined} entry - The intersection observer Entry.
|
|
54
|
+
*/
|
|
55
|
+
type IntersectionReturn = [
|
|
56
|
+
(node?: Element | null) => void,
|
|
57
|
+
boolean,
|
|
58
|
+
IntersectionObserverEntry | undefined,
|
|
59
|
+
] & {
|
|
60
|
+
ref: (node?: Element | null) => void
|
|
61
|
+
isIntersecting: boolean
|
|
62
|
+
entry?: IntersectionObserverEntry
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Custom hook that tracks the intersection of a DOM element with its containing element or the viewport using the [`Intersection Observer API`](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
|
|
67
|
+
* @param {UseIntersectionObserverOptions} options - The options for the Intersection Observer.
|
|
68
|
+
* @returns {IntersectionReturn} The ref callback, a boolean indicating if the element is intersecting, and the intersection observer entry.
|
|
69
|
+
* @public
|
|
70
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-intersection-observer)
|
|
71
|
+
* @example
|
|
72
|
+
* ```tsx
|
|
73
|
+
* // Example 1
|
|
74
|
+
* const [ref, isIntersecting, entry] = useIntersectionObserver({ threshold: 0.5 });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* ```tsx
|
|
78
|
+
* // Example 2
|
|
79
|
+
* const { ref, isIntersecting, entry } = useIntersectionObserver({ threshold: 0.5 });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function useIntersectionObserver({
|
|
83
|
+
threshold = 0,
|
|
84
|
+
root = null,
|
|
85
|
+
rootMargin = '0%',
|
|
86
|
+
freezeOnceVisible = false,
|
|
87
|
+
initialIsIntersecting = false,
|
|
88
|
+
onChange,
|
|
89
|
+
}: UseIntersectionObserverOptions = {}): IntersectionReturn {
|
|
90
|
+
const [ref, setRef] = useState<Element | null>(null)
|
|
91
|
+
|
|
92
|
+
const [state, setState] = useState<State>(() => ({
|
|
93
|
+
isIntersecting: initialIsIntersecting,
|
|
94
|
+
entry: undefined,
|
|
95
|
+
}))
|
|
96
|
+
|
|
97
|
+
const callbackRef = useRef<UseIntersectionObserverOptions['onChange']>(null)
|
|
98
|
+
|
|
99
|
+
callbackRef.current = onChange
|
|
100
|
+
|
|
101
|
+
const frozen = state.entry?.isIntersecting && freezeOnceVisible
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
// Ensure we have a ref to observe
|
|
105
|
+
if (!ref) return
|
|
106
|
+
|
|
107
|
+
// Ensure the browser supports the Intersection Observer API
|
|
108
|
+
if (!('IntersectionObserver' in window)) return
|
|
109
|
+
|
|
110
|
+
// Skip if frozen
|
|
111
|
+
if (frozen) return
|
|
112
|
+
|
|
113
|
+
let unobserve: (() => void) | undefined
|
|
114
|
+
|
|
115
|
+
const observer = new IntersectionObserver(
|
|
116
|
+
(entries: IntersectionObserverEntry[]): void => {
|
|
117
|
+
const thresholds = Array.isArray(observer.thresholds)
|
|
118
|
+
? observer.thresholds
|
|
119
|
+
: [observer.thresholds]
|
|
120
|
+
|
|
121
|
+
entries.forEach(entry => {
|
|
122
|
+
const isIntersecting =
|
|
123
|
+
entry.isIntersecting &&
|
|
124
|
+
thresholds.some(threshold => entry.intersectionRatio >= threshold)
|
|
125
|
+
|
|
126
|
+
setState({ isIntersecting, entry })
|
|
127
|
+
|
|
128
|
+
if (callbackRef.current) {
|
|
129
|
+
callbackRef.current(isIntersecting, entry)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (isIntersecting && freezeOnceVisible && unobserve) {
|
|
133
|
+
unobserve()
|
|
134
|
+
unobserve = undefined
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
},
|
|
138
|
+
{ threshold, root, rootMargin },
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
observer.observe(ref)
|
|
142
|
+
|
|
143
|
+
return () => {
|
|
144
|
+
observer.disconnect()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
148
|
+
}, [
|
|
149
|
+
ref,
|
|
150
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
151
|
+
JSON.stringify(threshold),
|
|
152
|
+
root,
|
|
153
|
+
rootMargin,
|
|
154
|
+
frozen,
|
|
155
|
+
freezeOnceVisible,
|
|
156
|
+
])
|
|
157
|
+
|
|
158
|
+
// ensures that if the observed element changes, the intersection observer is reinitialized
|
|
159
|
+
const prevRef = useRef<Element | null>(null)
|
|
160
|
+
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
if (
|
|
163
|
+
!ref &&
|
|
164
|
+
state.entry?.target &&
|
|
165
|
+
!freezeOnceVisible &&
|
|
166
|
+
!frozen &&
|
|
167
|
+
prevRef.current !== state.entry.target
|
|
168
|
+
) {
|
|
169
|
+
prevRef.current = state.entry.target
|
|
170
|
+
setState({ isIntersecting: initialIsIntersecting, entry: undefined })
|
|
171
|
+
}
|
|
172
|
+
}, [ref, state.entry, freezeOnceVisible, frozen, initialIsIntersecting])
|
|
173
|
+
|
|
174
|
+
const result = [
|
|
175
|
+
setRef,
|
|
176
|
+
!!state.isIntersecting,
|
|
177
|
+
state.entry,
|
|
178
|
+
] as IntersectionReturn
|
|
179
|
+
|
|
180
|
+
// Support object destructuring, by adding the specific values.
|
|
181
|
+
result.ref = result[0]
|
|
182
|
+
result.isIntersecting = result[1]
|
|
183
|
+
result.entry = result[2]
|
|
184
|
+
|
|
185
|
+
return result
|
|
186
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useInterval'
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Custom hook that creates an interval that invokes a callback function at a specified delay using the [`setInterval API`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval).
|
|
7
|
+
* @param {() => void} callback - The function to be invoked at each interval.
|
|
8
|
+
* @param {number | null} delay - The time, in milliseconds, between each invocation of the callback. Use `null` to clear the interval.
|
|
9
|
+
* @public
|
|
10
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-interval)
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const handleInterval = () => {
|
|
14
|
+
* // Code to be executed at each interval
|
|
15
|
+
* };
|
|
16
|
+
* useInterval(handleInterval, 1000);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function useInterval(callback: () => void, delay: number | null) {
|
|
20
|
+
const savedCallback = useRef(callback)
|
|
21
|
+
|
|
22
|
+
// Remember the latest callback if it changes.
|
|
23
|
+
useIsomorphicLayoutEffect(() => {
|
|
24
|
+
savedCallback.current = callback
|
|
25
|
+
}, [callback])
|
|
26
|
+
|
|
27
|
+
// Set up the interval.
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
// Don't schedule if no delay is specified.
|
|
30
|
+
// Note: 0 is a valid value for delay.
|
|
31
|
+
if (delay === null) {
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const id = setInterval(() => {
|
|
36
|
+
savedCallback.current()
|
|
37
|
+
}, delay)
|
|
38
|
+
|
|
39
|
+
return () => {
|
|
40
|
+
clearInterval(id)
|
|
41
|
+
}
|
|
42
|
+
}, [delay])
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useIsClient'
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that determines if the code is running on the client side (in the browser).
|
|
5
|
+
* @returns {boolean} A boolean value indicating whether the code is running on the client side.
|
|
6
|
+
* @public
|
|
7
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-is-client)
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const isClient = useIsClient();
|
|
11
|
+
* // Use isClient to conditionally render or execute code specific to the client side.
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function useIsClient() {
|
|
15
|
+
const [isClient, setClient] = useState(false)
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
setClient(true)
|
|
19
|
+
}, [])
|
|
20
|
+
|
|
21
|
+
return isClient
|
|
22
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { IUseIsMobileParams } from './types';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_MOBILE_BREAKPOINT = 768
|
|
5
|
+
|
|
6
|
+
export function useIsMobile(params: IUseIsMobileParams = {}) {
|
|
7
|
+
const {
|
|
8
|
+
mobileBreakpoint = DEFAULT_MOBILE_BREAKPOINT
|
|
9
|
+
} = params;
|
|
10
|
+
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
|
|
11
|
+
|
|
12
|
+
React.useEffect(() => {
|
|
13
|
+
const mql = window.matchMedia(`(max-width: ${mobileBreakpoint - 1}px)`)
|
|
14
|
+
const onChange = () => {
|
|
15
|
+
setIsMobile(window.innerWidth < mobileBreakpoint)
|
|
16
|
+
}
|
|
17
|
+
mql.addEventListener("change", onChange)
|
|
18
|
+
setIsMobile(window.innerWidth < mobileBreakpoint)
|
|
19
|
+
return () => mql.removeEventListener("change", onChange)
|
|
20
|
+
}, [mobileBreakpoint])
|
|
21
|
+
|
|
22
|
+
return !!isMobile
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useIsMounted'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
export function useIsMounted(): () => boolean {
|
|
4
|
+
const isMounted = useRef(false)
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
isMounted.current = true
|
|
8
|
+
|
|
9
|
+
return () => {
|
|
10
|
+
isMounted.current = false
|
|
11
|
+
}
|
|
12
|
+
}, [])
|
|
13
|
+
|
|
14
|
+
return useCallback(() => isMounted.current, [])
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useIsomorphicLayoutEffect'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that uses either `useLayoutEffect` or `useEffect` based on the environment (client-side or server-side).
|
|
5
|
+
* @param {Function} effect - The effect function to be executed.
|
|
6
|
+
* @param {Array<any>} [dependencies] - An array of dependencies for the effect (optional).
|
|
7
|
+
* @public
|
|
8
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-isomorphic-layout-effect)
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* useIsomorphicLayoutEffect(() => {
|
|
12
|
+
* // Code to be executed during the layout phase on the client side
|
|
13
|
+
* }, [dependency1, dependency2]);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const useIsomorphicLayoutEffect =
|
|
17
|
+
typeof window !== 'undefined' ? useLayoutEffect : useEffect
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useLocalStorage'
|