@oneplatformdev/hooks 0.1.0-6 → 0.1.0-8
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 +19 -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,44 @@
|
|
|
1
|
+
import * as React from "./";
|
|
2
|
+
export { Fragment } from "./";
|
|
3
|
+
|
|
4
|
+
export namespace JSX {
|
|
5
|
+
interface Element extends React.JSX.Element {}
|
|
6
|
+
interface ElementClass extends React.JSX.ElementClass {}
|
|
7
|
+
interface ElementAttributesProperty extends React.JSX.ElementAttributesProperty {}
|
|
8
|
+
interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}
|
|
9
|
+
type LibraryManagedAttributes<C, P> = React.JSX.LibraryManagedAttributes<C, P>;
|
|
10
|
+
interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}
|
|
11
|
+
interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}
|
|
12
|
+
interface IntrinsicElements extends React.JSX.IntrinsicElements {}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface JSXSource {
|
|
16
|
+
/**
|
|
17
|
+
* The source file where the element originates from.
|
|
18
|
+
*/
|
|
19
|
+
fileName?: string | undefined;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The line number where the element was created.
|
|
23
|
+
*/
|
|
24
|
+
lineNumber?: number | undefined;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The column number where the element was created.
|
|
28
|
+
*/
|
|
29
|
+
columnNumber?: number | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create a React element.
|
|
34
|
+
*
|
|
35
|
+
* You should not use this function directly. Use JSX and a transpiler instead.
|
|
36
|
+
*/
|
|
37
|
+
export function jsxDEV(
|
|
38
|
+
type: React.ElementType,
|
|
39
|
+
props: unknown,
|
|
40
|
+
key: React.Key | undefined,
|
|
41
|
+
isStatic: boolean,
|
|
42
|
+
source?: JSXSource,
|
|
43
|
+
self?: unknown,
|
|
44
|
+
): React.ReactElement;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from "./";
|
|
2
|
+
export { Fragment } from "./";
|
|
3
|
+
|
|
4
|
+
export namespace JSX {
|
|
5
|
+
interface Element extends React.JSX.Element {}
|
|
6
|
+
interface ElementClass extends React.JSX.ElementClass {}
|
|
7
|
+
interface ElementAttributesProperty extends React.JSX.ElementAttributesProperty {}
|
|
8
|
+
interface ElementChildrenAttribute extends React.JSX.ElementChildrenAttribute {}
|
|
9
|
+
type LibraryManagedAttributes<C, P> = React.JSX.LibraryManagedAttributes<C, P>;
|
|
10
|
+
interface IntrinsicAttributes extends React.JSX.IntrinsicAttributes {}
|
|
11
|
+
interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}
|
|
12
|
+
interface IntrinsicElements extends React.JSX.IntrinsicElements {}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a React element.
|
|
17
|
+
*
|
|
18
|
+
* You should not use this function directly. Use JSX and a transpiler instead.
|
|
19
|
+
*/
|
|
20
|
+
export function jsx(
|
|
21
|
+
type: React.ElementType,
|
|
22
|
+
props: unknown,
|
|
23
|
+
key?: React.Key,
|
|
24
|
+
): React.ReactElement;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create a React element.
|
|
28
|
+
*
|
|
29
|
+
* You should not use this function directly. Use JSX and a transpiler instead.
|
|
30
|
+
*/
|
|
31
|
+
export function jsxs(
|
|
32
|
+
type: React.ElementType,
|
|
33
|
+
props: unknown,
|
|
34
|
+
key?: React.Key,
|
|
35
|
+
): React.ReactElement;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oneplatformdev/hooks",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-8",
|
|
4
4
|
"description": "React hook library",
|
|
5
5
|
"author": "One Platform Development Team",
|
|
6
6
|
"keywords": [
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
|
+
"**/*",
|
|
42
43
|
"dist",
|
|
43
44
|
"README.md",
|
|
44
45
|
"LICENSE",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export * from './useBoolean';
|
|
2
|
+
export * from './useClickAnyWhere';
|
|
3
|
+
export * from './useCopyToClipboard';
|
|
4
|
+
export * from './useCountdown';
|
|
5
|
+
export * from './useCounter';
|
|
6
|
+
export * from './useDarkMode';
|
|
7
|
+
export * from './useDebounceCallback';
|
|
8
|
+
export * from './useDebounceValue';
|
|
9
|
+
export * from './useDocumentTitle';
|
|
10
|
+
export * from './useEventCallback';
|
|
11
|
+
export * from './useEventListener';
|
|
12
|
+
export * from './useHover';
|
|
13
|
+
export * from './useIntersectionObserver';
|
|
14
|
+
export * from './useInterval';
|
|
15
|
+
export * from './useIsClient';
|
|
16
|
+
export * from './useIsMobile';
|
|
17
|
+
export * from './useIsMounted';
|
|
18
|
+
export * from './useIsomorphicLayoutEffect';
|
|
19
|
+
export * from './useLocalStorage';
|
|
20
|
+
export * from './useLockBody';
|
|
21
|
+
export * from './useMap';
|
|
22
|
+
export * from './useMediaQuery';
|
|
23
|
+
export * from './useOnClickOutside';
|
|
24
|
+
export * from './useReadLocalStorage';
|
|
25
|
+
export * from './useResizeObserver';
|
|
26
|
+
export * from './useScreen';
|
|
27
|
+
export * from './useScript';
|
|
28
|
+
export * from './useScrollLock';
|
|
29
|
+
export * from './useSessionStorage';
|
|
30
|
+
export * from './useStep';
|
|
31
|
+
export * from './useTernaryDarkMode';
|
|
32
|
+
export * from './useTimeout';
|
|
33
|
+
export * from './useToggle';
|
|
34
|
+
export * from './useUnmount';
|
|
35
|
+
export * from './useWindowSize';
|
|
36
|
+
export * from './useQueryString';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useBoolean'
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { Dispatch, SetStateAction } from 'react'
|
|
4
|
+
|
|
5
|
+
/** The useBoolean return type. */
|
|
6
|
+
type UseBooleanReturn = {
|
|
7
|
+
/** The current boolean state value. */
|
|
8
|
+
value: boolean
|
|
9
|
+
/** Function to set the boolean state directly. */
|
|
10
|
+
setValue: Dispatch<SetStateAction<boolean>>
|
|
11
|
+
/** Function to set the boolean state to `true`. */
|
|
12
|
+
setTrue: () => void
|
|
13
|
+
/** Function to set the boolean state to `false`. */
|
|
14
|
+
setFalse: () => void
|
|
15
|
+
/** Function to toggle the boolean state. */
|
|
16
|
+
toggle: () => void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Custom hook that handles boolean state with useful utility functions.
|
|
21
|
+
* @param {boolean} [defaultValue] - The initial value for the boolean state (default is `false`).
|
|
22
|
+
* @returns {UseBooleanReturn} An object containing the boolean state value and utility functions to manipulate the state.
|
|
23
|
+
* @throws Will throw an error if `defaultValue` is an invalid boolean value.
|
|
24
|
+
* @public
|
|
25
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-boolean)
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* const { value, setTrue, setFalse, toggle } = useBoolean(true);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function useBoolean(defaultValue = false): UseBooleanReturn {
|
|
32
|
+
if (typeof defaultValue !== 'boolean') {
|
|
33
|
+
throw new Error('defaultValue must be `true` or `false`')
|
|
34
|
+
}
|
|
35
|
+
const [value, setValue] = useState(defaultValue)
|
|
36
|
+
|
|
37
|
+
const setTrue = useCallback(() => {
|
|
38
|
+
setValue(true)
|
|
39
|
+
}, [])
|
|
40
|
+
|
|
41
|
+
const setFalse = useCallback(() => {
|
|
42
|
+
setValue(false)
|
|
43
|
+
}, [])
|
|
44
|
+
|
|
45
|
+
const toggle = useCallback(() => {
|
|
46
|
+
setValue(x => !x)
|
|
47
|
+
}, [])
|
|
48
|
+
|
|
49
|
+
return { value, setValue, setTrue, setFalse, toggle }
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useClickAnyWhere'
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEventListener } from '../useEventListener'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that handles click events anywhere on the document.
|
|
5
|
+
* @param {Function} handler - The function to be called when a click event is detected anywhere on the document.
|
|
6
|
+
* @public
|
|
7
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-click-any-where)
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const handleClick = (event) => {
|
|
11
|
+
* console.log('Document clicked!', event);
|
|
12
|
+
* };
|
|
13
|
+
*
|
|
14
|
+
* // Attach click event handler to document
|
|
15
|
+
* useClickAnywhere(handleClick);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function useClickAnyWhere(handler: (event: MouseEvent) => void) {
|
|
19
|
+
useEventListener('click', event => {
|
|
20
|
+
handler(event)
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useCopyToClipboard'
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The copied text as `string` or `null` if nothing has been copied yet.
|
|
5
|
+
*/
|
|
6
|
+
type CopiedValue = string | null
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Function to copy text to the clipboard.
|
|
10
|
+
* @param text - The text to copy to the clipboard.
|
|
11
|
+
* @returns {Promise<boolean>} A promise that resolves to `true` if the text was copied successfully, or `false` otherwise.
|
|
12
|
+
*/
|
|
13
|
+
type CopyFn = (text: string) => Promise<boolean>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Custom hook that copies text to the clipboard using the [`Clipboard API`](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).
|
|
17
|
+
* @returns {[CopiedValue, CopyFn]} An tuple containing the copied text and a function to copy text to the clipboard.
|
|
18
|
+
* @public
|
|
19
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-copy-to-clipboard)
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* const [copiedText, copyToClipboard] = useCopyToClipboard();
|
|
23
|
+
* const textToCopy = 'Hello, world!';
|
|
24
|
+
*
|
|
25
|
+
* // Attempt to copy text to the clipboard
|
|
26
|
+
* copyToClipboard(textToCopy)
|
|
27
|
+
* .then(success => {
|
|
28
|
+
* if (success) {
|
|
29
|
+
* console.log(`Text "${textToCopy}" copied to clipboard successfully.`);
|
|
30
|
+
* } else {
|
|
31
|
+
* console.error('Failed to copy text to clipboard.');
|
|
32
|
+
* }
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
|
37
|
+
const [copiedText, setCopiedText] = useState<CopiedValue>(null)
|
|
38
|
+
|
|
39
|
+
const copy: CopyFn = useCallback(async text => {
|
|
40
|
+
if (!navigator?.clipboard) {
|
|
41
|
+
console.warn('Clipboard not supported')
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Try to save to clipboard then save it in the state if worked
|
|
46
|
+
try {
|
|
47
|
+
await navigator.clipboard.writeText(text)
|
|
48
|
+
setCopiedText(text)
|
|
49
|
+
return true
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.warn('Copy failed', error)
|
|
52
|
+
setCopiedText(null)
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
}, [])
|
|
56
|
+
|
|
57
|
+
return [copiedText, copy]
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useCountdown'
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { useCallback } from 'react'
|
|
2
|
+
|
|
3
|
+
import { useBoolean } from '../useBoolean'
|
|
4
|
+
import { useCounter } from '../useCounter'
|
|
5
|
+
import { useInterval } from '../useInterval'
|
|
6
|
+
|
|
7
|
+
/** The countdown's options. */
|
|
8
|
+
type CountdownOptions = {
|
|
9
|
+
/** The countdown's starting number, initial value of the returned number. */
|
|
10
|
+
countStart: number
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The countdown's interval, in milliseconds.
|
|
14
|
+
* @default 1000
|
|
15
|
+
*/
|
|
16
|
+
intervalMs?: number
|
|
17
|
+
/**
|
|
18
|
+
* True if the countdown is increment.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
isIncrement?: boolean
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The countdown's stopping number. Pass `-Infinity` to decrease forever.
|
|
25
|
+
* @default 0
|
|
26
|
+
*/
|
|
27
|
+
countStop?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The countdown's controllers. */
|
|
31
|
+
type CountdownControllers = {
|
|
32
|
+
/** Start the countdown. */
|
|
33
|
+
startCountdown: () => void
|
|
34
|
+
/** Stop the countdown. */
|
|
35
|
+
stopCountdown: () => void
|
|
36
|
+
/** Reset the countdown. */
|
|
37
|
+
resetCountdown: () => void
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Custom hook that manages countdown.
|
|
42
|
+
* @param {CountdownOptions} countdownOptions - The countdown's options.
|
|
43
|
+
* @returns {[number, CountdownControllers]} An array containing the countdown's count and its controllers.
|
|
44
|
+
* @public
|
|
45
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-countdown)
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* const [counter, { start, stop, reset }] = useCountdown({
|
|
49
|
+
* countStart: 10,
|
|
50
|
+
* intervalMs: 1000,
|
|
51
|
+
* isIncrement: false,
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function useCountdown({
|
|
56
|
+
countStart,
|
|
57
|
+
countStop = 0,
|
|
58
|
+
intervalMs = 1000,
|
|
59
|
+
isIncrement = false,
|
|
60
|
+
}: CountdownOptions): [number, CountdownControllers] {
|
|
61
|
+
const {
|
|
62
|
+
count,
|
|
63
|
+
increment,
|
|
64
|
+
decrement,
|
|
65
|
+
reset: resetCounter,
|
|
66
|
+
} = useCounter(countStart)
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
* Note: used to control the useInterval
|
|
70
|
+
* running: If true, the interval is running
|
|
71
|
+
* start: Should set running true to trigger interval
|
|
72
|
+
* stop: Should set running false to remove interval.
|
|
73
|
+
*/
|
|
74
|
+
const {
|
|
75
|
+
value: isCountdownRunning,
|
|
76
|
+
setTrue: startCountdown,
|
|
77
|
+
setFalse: stopCountdown,
|
|
78
|
+
} = useBoolean(false)
|
|
79
|
+
|
|
80
|
+
// Will set running false and reset the seconds to initial value.
|
|
81
|
+
const resetCountdown = useCallback(() => {
|
|
82
|
+
stopCountdown()
|
|
83
|
+
resetCounter()
|
|
84
|
+
}, [stopCountdown, resetCounter])
|
|
85
|
+
|
|
86
|
+
const countdownCallback = useCallback(() => {
|
|
87
|
+
if (count === countStop) {
|
|
88
|
+
stopCountdown()
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (isIncrement) {
|
|
93
|
+
increment()
|
|
94
|
+
} else {
|
|
95
|
+
decrement()
|
|
96
|
+
}
|
|
97
|
+
}, [count, countStop, decrement, increment, isIncrement, stopCountdown])
|
|
98
|
+
|
|
99
|
+
useInterval(countdownCallback, isCountdownRunning ? intervalMs : null)
|
|
100
|
+
|
|
101
|
+
return [count, { startCountdown, stopCountdown, resetCountdown }]
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useCounter'
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { Dispatch, SetStateAction } from 'react'
|
|
4
|
+
|
|
5
|
+
/** The hook return type. */
|
|
6
|
+
type UseCounterReturn = {
|
|
7
|
+
/** The current count value. */
|
|
8
|
+
count: number
|
|
9
|
+
/** Function to increment the counter by 1. */
|
|
10
|
+
increment: () => void
|
|
11
|
+
/** Function to decrement the counter by 1. */
|
|
12
|
+
decrement: () => void
|
|
13
|
+
/** Function to reset the counter to its initial value. */
|
|
14
|
+
reset: () => void
|
|
15
|
+
/** Function to set a specific value to the counter. */
|
|
16
|
+
setCount: Dispatch<SetStateAction<number>>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Custom hook that manages a counter with increment, decrement, reset, and setCount functionalities.
|
|
21
|
+
* @param {number} [initialValue] - The initial value for the counter.
|
|
22
|
+
* @returns {UseCounterReturn} An object containing the current count and functions to interact with the counter.
|
|
23
|
+
* @public
|
|
24
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-counter)
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* const { count, increment, decrement, reset, setCount } = useCounter(5);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function useCounter(initialValue?: number): UseCounterReturn {
|
|
31
|
+
const [count, setCount] = useState(initialValue ?? 0)
|
|
32
|
+
|
|
33
|
+
const increment = useCallback(() => {
|
|
34
|
+
setCount(x => x + 1)
|
|
35
|
+
}, [])
|
|
36
|
+
|
|
37
|
+
const decrement = useCallback(() => {
|
|
38
|
+
setCount(x => x - 1)
|
|
39
|
+
}, [])
|
|
40
|
+
|
|
41
|
+
const reset = useCallback(() => {
|
|
42
|
+
setCount(initialValue ?? 0)
|
|
43
|
+
}, [initialValue])
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
count,
|
|
47
|
+
increment,
|
|
48
|
+
decrement,
|
|
49
|
+
reset,
|
|
50
|
+
setCount,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useDarkMode'
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'
|
|
2
|
+
import { useLocalStorage } from '../useLocalStorage'
|
|
3
|
+
import { useMediaQuery } from '../useMediaQuery'
|
|
4
|
+
|
|
5
|
+
const COLOR_SCHEME_QUERY = '(prefers-color-scheme: dark)'
|
|
6
|
+
const LOCAL_STORAGE_KEY = 'usehooks-ts-dark-mode'
|
|
7
|
+
|
|
8
|
+
/** The hook options. */
|
|
9
|
+
type DarkModeOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* The initial value of the dark mode.
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
defaultValue?: boolean
|
|
15
|
+
/**
|
|
16
|
+
* The key to use in the local storage.
|
|
17
|
+
* @default 'usehooks-ts-dark-mode'
|
|
18
|
+
*/
|
|
19
|
+
localStorageKey?: string
|
|
20
|
+
/**
|
|
21
|
+
* If `true` (default), the hook will initialize reading `localStorage`.
|
|
22
|
+
* In SSR, you should set it to `false`, returning the `defaultValue` or `false` initially.
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
initializeWithValue?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The hook return type. */
|
|
29
|
+
type DarkModeReturn = {
|
|
30
|
+
/** The current state of the dark mode. */
|
|
31
|
+
isDarkMode: boolean
|
|
32
|
+
/** Function to toggle the dark mode. */
|
|
33
|
+
toggle: () => void
|
|
34
|
+
/** Function to enable the dark mode. */
|
|
35
|
+
enable: () => void
|
|
36
|
+
/** Function to disable the dark mode. */
|
|
37
|
+
disable: () => void
|
|
38
|
+
/** Function to set a specific value to the dark mode. */
|
|
39
|
+
set: (value: boolean) => void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Custom hook that returns the current state of the dark mode.
|
|
44
|
+
* @param {?DarkModeOptions} [options] - The initial value of the dark mode, default `false`.
|
|
45
|
+
* @returns {DarkModeReturn} An object containing the dark mode's state and its controllers.
|
|
46
|
+
* @public
|
|
47
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-dark-mode)
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* const { isDarkMode, toggle, enable, disable, set } = useDarkMode({ defaultValue: true });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function useDarkMode(options: DarkModeOptions = {}): DarkModeReturn {
|
|
54
|
+
const {
|
|
55
|
+
defaultValue,
|
|
56
|
+
localStorageKey = LOCAL_STORAGE_KEY,
|
|
57
|
+
initializeWithValue = true,
|
|
58
|
+
} = options
|
|
59
|
+
|
|
60
|
+
const isDarkOS = useMediaQuery(COLOR_SCHEME_QUERY, {
|
|
61
|
+
initializeWithValue,
|
|
62
|
+
defaultValue,
|
|
63
|
+
})
|
|
64
|
+
const [isDarkMode, setDarkMode] = useLocalStorage<boolean>(
|
|
65
|
+
localStorageKey,
|
|
66
|
+
defaultValue ?? isDarkOS ?? false,
|
|
67
|
+
{ initializeWithValue },
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
// Update darkMode if os prefers changes
|
|
71
|
+
useIsomorphicLayoutEffect(() => {
|
|
72
|
+
if (isDarkOS !== isDarkMode) {
|
|
73
|
+
setDarkMode(isDarkOS)
|
|
74
|
+
}
|
|
75
|
+
}, [isDarkOS])
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
isDarkMode,
|
|
79
|
+
toggle: () => {
|
|
80
|
+
setDarkMode(prev => !prev)
|
|
81
|
+
},
|
|
82
|
+
enable: () => {
|
|
83
|
+
setDarkMode(true)
|
|
84
|
+
},
|
|
85
|
+
disable: () => {
|
|
86
|
+
setDarkMode(false)
|
|
87
|
+
},
|
|
88
|
+
set: value => {
|
|
89
|
+
setDarkMode(value)
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
import debounce from 'lodash.debounce'
|
|
4
|
+
|
|
5
|
+
import { useUnmount } from '../useUnmount'
|
|
6
|
+
|
|
7
|
+
/** Configuration options for controlling the behavior of the debounced function. */
|
|
8
|
+
type DebounceOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* Determines whether the function should be invoked on the leading edge of the timeout.
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
leading?: boolean
|
|
14
|
+
/**
|
|
15
|
+
* Determines whether the function should be invoked on the trailing edge of the timeout.
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
trailing?: boolean
|
|
19
|
+
/**
|
|
20
|
+
* The maximum time the specified function is allowed to be delayed before it is invoked.
|
|
21
|
+
*/
|
|
22
|
+
maxWait?: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Functions to manage a debounced callback. */
|
|
26
|
+
type ControlFunctions = {
|
|
27
|
+
/** Cancels pending function invocations. */
|
|
28
|
+
cancel: () => void
|
|
29
|
+
/** Immediately invokes pending function invocations. */
|
|
30
|
+
flush: () => void
|
|
31
|
+
/**
|
|
32
|
+
* Checks if there are any pending function invocations.
|
|
33
|
+
* @returns `true` if there are pending invocations, otherwise `false`.
|
|
34
|
+
*/
|
|
35
|
+
isPending: () => boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Represents the state and control functions of a debounced callback.
|
|
40
|
+
* Subsequent calls to the debounced function return the result of the last invocation.
|
|
41
|
+
* Note: If there are no previous invocations, the result will be undefined.
|
|
42
|
+
* Ensure proper handling in your code.
|
|
43
|
+
*/
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
+
export type DebouncedState<T extends (...args: any) => ReturnType<T>> = ((
|
|
46
|
+
...args: Parameters<T>
|
|
47
|
+
) => ReturnType<T> | undefined) &
|
|
48
|
+
ControlFunctions
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Custom hook that creates a debounced version of a callback function.
|
|
52
|
+
* @template T - Type of the original callback function.
|
|
53
|
+
* @param {T} func - The callback function to be debounced.
|
|
54
|
+
* @param {number} delay - The delay in milliseconds before the callback is invoked (default is `500` milliseconds).
|
|
55
|
+
* @param {DebounceOptions} [options] - Options to control the behavior of the debounced function.
|
|
56
|
+
* @returns {DebouncedState<T>} A debounced version of the original callback along with control functions.
|
|
57
|
+
* @public
|
|
58
|
+
* @see [Documentation](https://usehooks-ts.com/react-hook/use-debounce-callback)
|
|
59
|
+
* @example
|
|
60
|
+
* ```tsx
|
|
61
|
+
* const debouncedCallback = useDebounceCallback(
|
|
62
|
+
* (searchTerm) => {
|
|
63
|
+
* // Perform search after user stops typing for 500 milliseconds
|
|
64
|
+
* searchApi(searchTerm);
|
|
65
|
+
* },
|
|
66
|
+
* 500
|
|
67
|
+
* );
|
|
68
|
+
*
|
|
69
|
+
* // Later in the component
|
|
70
|
+
* debouncedCallback('react hooks'); // Will invoke the callback after 500 milliseconds of inactivity.
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
export function useDebounceCallback<T extends (...args: any) => ReturnType<T>>(
|
|
75
|
+
func: T,
|
|
76
|
+
delay = 500,
|
|
77
|
+
options?: DebounceOptions,
|
|
78
|
+
): DebouncedState<T> {
|
|
79
|
+
const debouncedFunc = useRef<ReturnType<typeof debounce>>(null)
|
|
80
|
+
|
|
81
|
+
useUnmount(() => {
|
|
82
|
+
if (debouncedFunc.current) {
|
|
83
|
+
debouncedFunc.current.cancel()
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const debounced = useMemo(() => {
|
|
88
|
+
const debouncedFuncInstance = debounce(func, delay, options)
|
|
89
|
+
|
|
90
|
+
const wrappedFunc: DebouncedState<T> = (...args: Parameters<T>) => {
|
|
91
|
+
return debouncedFuncInstance(...args)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
wrappedFunc.cancel = () => {
|
|
95
|
+
debouncedFuncInstance.cancel()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
wrappedFunc.isPending = () => {
|
|
99
|
+
return !!debouncedFunc.current
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
wrappedFunc.flush = () => {
|
|
103
|
+
return debouncedFuncInstance.flush()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return wrappedFunc
|
|
107
|
+
}, [func, delay, options])
|
|
108
|
+
|
|
109
|
+
// Update the debounced function ref whenever func, wait, or options change
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
debouncedFunc.current = debounce(func, delay, options)
|
|
112
|
+
}, [func, delay, options])
|
|
113
|
+
|
|
114
|
+
return debounced
|
|
115
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useDebounceValue'
|