diff --git a/biome.json b/biome.json index c8c063c4..61f999de 100644 --- a/biome.json +++ b/biome.json @@ -26,6 +26,9 @@ "enabled": true, "rules": { "recommended": true, + "style": { + "noInferrableTypes": "off" + }, "correctness": { "useExhaustiveDependencies": { "level": "warn", diff --git a/src/create-single-loading/index.mdx b/src/create-single-loading/index.mdx index d1d4dec6..cb0ad9e9 100644 --- a/src/create-single-loading/index.mdx +++ b/src/create-single-loading/index.mdx @@ -49,13 +49,13 @@ export interface CreateSingleLoadingOptions { /** * Whether set to false on error * - * @default true + * @defaultValue true */ resetOnError?: boolean /** * Initial loading state * - * @default false + * @defaultValue false */ initialState?: boolean } @@ -64,33 +64,49 @@ export interface CreateSingleLoadingOptions { ### Returns ```tsx -export interface useAsyncFnExtendReturn extends UseAsyncFnReturn { +export interface UseAsyncFnExtendReturns extends UseAsyncFnReturns { /** * Set the loading state + * + * @param {boolean} value - `boolean`, the loading value to set + * @returns {void} `void` */ setLoading: (value: boolean) => void } -export interface CreateSingleLoadingReturn { +export interface CreateSingleLoadingReturns { /** - * Use loading state with async function via Hook in React + * A Hook in React to use bound async function with loading state + * + * @param {AnyFunc} asyncFn - `AnyFunc`, the async function to bind, see {@link AnyFunc} + * @returns {UseAsyncFnExtendReturns} see {@link UseAsyncFnExtendReturns} */ - useAsyncFn: (func: T) => useAsyncFnExtendReturn + useAsyncFn: (asyncFn: T) => UseAsyncFnExtendReturns /** - * Use loading state via Hook in React + * A Hook in React to use loading state + * + * @returns {boolean} `boolean`, the loading state */ useLoading(): boolean /** * Set the loading state via store in JS/TS + * + * @param {boolean} value - `boolean`, the value to set + * @returns {void} `void` */ - set(value: boolean): void + set: (value: boolean) => void /** * Get the loading state via store in JS/TS + * + * @returns {boolean} `boolean`, the loading state */ get(): boolean /** * Bind the loading state to the async function in JS/TS + * + * @param {AnyFunc} asyncFn - `AnyFunc`, the async function to bind, see {@link AnyFunc} + * @returns {AnyFunc} `AnyFunc`, same as `asyncFn` param, see {@link AnyFunc} */ - bind: (func: T) => T + bind: (asyncFn: T) => T } ``` diff --git a/src/create-single-loading/index.ts b/src/create-single-loading/index.ts index bd91b0af..3362f1e6 100644 --- a/src/create-single-loading/index.ts +++ b/src/create-single-loading/index.ts @@ -1,60 +1,81 @@ import { create } from '@shined/reactive' import { useAsyncFn as useAsyncFnOrigin } from '../use-async-fn' -import type { ReactNode } from 'react' -import type { UseAsyncFnReturn } from '../use-async-fn' +import type { UseAsyncFnReturns } from '../use-async-fn' import type { AnyFunc } from '../utils/basic' -export interface UseSingleLoadingProviderProps { - children: ReactNode | undefined -} - -export interface useAsyncFnExtendReturn extends UseAsyncFnReturn { +export interface UseAsyncFnExtendReturns extends UseAsyncFnReturns { /** * Set the loading state + * + * @param {boolean} value - `boolean`, the loading value to set + * @returns {void} `void` */ setLoading: (value: boolean) => void } -export interface CreateSingleLoadingReturn { +export interface CreateSingleLoadingReturns { /** * A Hook in React to use bound async function with loading state + * + * @param {AnyFunc} asyncFn - `AnyFunc`, the async function to bind, see {@link AnyFunc} + * @returns {UseAsyncFnExtendReturns} see {@link UseAsyncFnExtendReturns} */ - useAsyncFn: (func: T) => useAsyncFnExtendReturn + useAsyncFn: (asyncFn: T) => UseAsyncFnExtendReturns /** * A Hook in React to use loading state + * + * @returns {boolean} `boolean`, the loading state */ useLoading(): boolean /** * Set the loading state via store in JS/TS + * + * @param {boolean} value - `boolean`, the value to set + * @returns {void} `void` */ set: (value: boolean) => void /** * Get the loading state via store in JS/TS + * + * @returns {boolean} `boolean`, the loading state */ get(): boolean /** * Bind the loading state to the async function in JS/TS + * + * @param {AnyFunc} asyncFn - `AnyFunc`, the async function to bind, see {@link AnyFunc} + * @returns {AnyFunc} `AnyFunc`, same as `asyncFn` param, see {@link AnyFunc} */ - bind: (func: T) => T + bind: (asyncFn: T) => T } export interface CreateSingleLoadingOptions { /** * Whether set to false on error * - * @default true + * @defaultValue true */ resetOnError?: boolean /** * Initial loading state * - * @default false + * @defaultValue false */ initialState?: boolean } -export function createSingleLoading(options: CreateSingleLoadingOptions = {}): CreateSingleLoadingReturn { +/** + * A utility that provide a way to manage single (or global) loading state via multiple Hooks or simple JS/TS functions, using [Reactive](https://sheinsight.github.io/reactive/) under the hood. + * + * You need to install `@shined/reactive` to use this utility. + * + * @param {CreateSingleLoadingOptions} options {@link CreateSingleLoadingOptions} + * @returns {CreateSingleLoadingReturns} `CreateSingleLoadingReturns`, single loading instance, see {@link CreateSingleLoadingReturns} + * + * @see {@link https://sheinsight.github.io/reactive/ | Reactive - Documentation} + */ +export function createSingleLoading(options: CreateSingleLoadingOptions = {}): CreateSingleLoadingReturns { const { resetOnError = true, initialState = false } = options const store = create({ loading: initialState }) diff --git a/src/use-active-element/index.mdx b/src/use-active-element/index.mdx index b10a36b3..66d1252d 100644 --- a/src/use-active-element/index.mdx +++ b/src/use-active-element/index.mdx @@ -4,7 +4,7 @@ import { HooksType } from '@/components' -A React Hook that returns the currently **active element** (via `document.activeElement`). +A React Hook that returns the currently **active element** (via `document.activeElement`), optionally traversing the shadow DOM. ## Demo diff --git a/src/use-active-element/index.ts b/src/use-active-element/index.ts index a09c6fd8..a9ecbd26 100644 --- a/src/use-active-element/index.ts +++ b/src/use-active-element/index.ts @@ -6,13 +6,16 @@ export interface UseActiveElementOptions { /** * Whether to traverse the shadow DOM to find the active element * - * @default true + * @defaultValue false */ deep?: boolean } /** - * use the active element, optionally traversing the shadow DOM + * A React Hook that returns the currently **active element** (via `document.activeElement`), optionally traversing the shadow DOM. + * + * @param {UseActiveElementOptions} options - `UseActiveElementOptions`, options to configure the hook, see {@link UseActiveElementOptions} + * @returns {Element | null} `Element | null`, the currently active element, `null` if there is none, see {@link Element} */ export function useActiveElement(options: UseActiveElementOptions = {}): Element | null { const [element, setElement] = useSafeState(null) @@ -36,9 +39,11 @@ export function useActiveElement(options: UseActiveElementOptions = {}): Element } /** - * find the active element, optionally traversing the shadow DOM + * Get the currently active element, optionally traversing the shadow DOM. + * + * @param {boolean} [deep=false] - `boolean`, Whether to traverse the shadow DOM to find the active element */ -function getActiveElement(deep = false): Element | null { +function getActiveElement(deep: boolean = false): Element | null { let element = document.activeElement if (deep) { diff --git a/src/use-adaptive-textarea/index.mdx b/src/use-adaptive-textarea/index.mdx index 2d736ccb..fe9cb78c 100644 --- a/src/use-adaptive-textarea/index.mdx +++ b/src/use-adaptive-textarea/index.mdx @@ -4,11 +4,11 @@ import { HooksType } from '@/components' -A React Hook that helps to create **adaptive** textarea. +A React Hook that helps to create **adaptive** textarea that automatically adjust height based on its content. ## Demo -Input and feel free to wrap the text in the textarea. +Input in the textarea, and see the height of the textarea automatically adjust. import { App } from './demo' @@ -36,41 +36,41 @@ const { ref, height, resize } = useAdaptiveTextarea(options) ### Options ```tsx -export type UseAdaptiveTextareaOptions = { +export interface UseAdaptiveTextareaOptions { /** * a list of dependencies to trigger the resize * - * @default [] + * @defaultValue [] */ watch?: DependencyList /** * a list of events to trigger the resize * - * @default ['input'] + * @defaultValue ['input'] */ events?: (keyof HTMLElementEventMap)[] /** * whether to automatically adjust the height of the `textarea` element * - * @default true + * @defaultValue true */ autoHeight?: boolean /** * a target element to apply the height style * - * @default undefined + * @defaultValue undefined */ styleTarget?: ElementTarget /** * a style property to apply the height value * - * @default 'height' + * @defaultValue 'height' */ styleProp?: 'height' | 'minHeight' /** * a callback function to be called when the height of the `textarea` element changes * - * @default undefined + * @defaultValue undefined */ onResize?: (height: number, prevHeight: number) => void } @@ -79,7 +79,7 @@ export type UseAdaptiveTextareaOptions = { ### Returns ```tsx -export type UseAdaptiveTextareaReturn = { +export interface UseAdaptiveTextareaReturns { /** * a React ref that should be passed to the `textarea` element */ @@ -90,6 +90,8 @@ export type UseAdaptiveTextareaReturn = { height: number /** * a function to manually resize the `textarea` element + * + * @returns {void} `void` */ resize(): void } diff --git a/src/use-adaptive-textarea/index.ts b/src/use-adaptive-textarea/index.ts index 835ee72d..cae855ad 100644 --- a/src/use-adaptive-textarea/index.ts +++ b/src/use-adaptive-textarea/index.ts @@ -13,42 +13,42 @@ export interface UseAdaptiveTextareaOptions /** * a list of dependencies to trigger the resize * - * @default [] + * @defaultValue [] */ watch?: DependencyList /** * a list of events to trigger the resize * - * @default ['input'] + * @defaultValue ['input'] */ events?: (keyof HTMLElementEventMap)[] /** * whether to automatically adjust the height of the `textarea` element * - * @default true + * @defaultValue true */ autoHeight?: boolean /** * a target element to apply the height style * - * @default undefined + * @defaultValue undefined */ styleTarget?: ElementTarget /** * a style property to apply the height value * - * @default 'height' + * @defaultValue 'height' */ styleProp?: 'height' | 'minHeight' /** * a callback function to be called when the height of the `textarea` element changes * - * @default undefined + * @defaultValue undefined */ onResize?: (height: number, prevHeight: number) => void } -export interface UseAdaptiveTextareaReturn { +export interface UseAdaptiveTextareaReturns { /** * a React ref that should be passed to the `textarea` element */ @@ -59,18 +59,21 @@ export interface UseAdaptiveTextareaReturn { height: number /** * a function to manually resize the `textarea` element + * + * @returns {void} `void` */ resize(): void } /** - * Automatically adjust the height of a `textarea` element based on its content + * A React Hook that helps to create **adaptive** textarea that automatically adjust height based on its content. * - * @tip set `rows={1}` attribute & `resize: none` style to the `textarea` before using this Hook + * @param {UseAdaptiveTextareaOptions} options - `UseAdaptiveTextareaOptions`, see {@link UseAdaptiveTextareaOptions} + * @returns {UseAdaptiveTextareaReturns} `UseAdaptiveTextareaReturns`, see {@link UseAdaptiveTextareaReturns} */ export function useAdaptiveTextarea( options: UseAdaptiveTextareaOptions = {}, -): UseAdaptiveTextareaReturn { +): UseAdaptiveTextareaReturns { const { onResize, events = ['input'], autoHeight = true, watch = [], styleTarget, styleProp = 'height' } = options const taRef = useRef(null) diff --git a/src/use-async-effect/index.mdx b/src/use-async-effect/index.mdx index f0adf438..74992173 100644 --- a/src/use-async-effect/index.mdx +++ b/src/use-async-effect/index.mdx @@ -6,6 +6,12 @@ import { HooksType } from '@/components' A React Hook which is like [React.useEffect](https://react.dev/reference/react/useEffect) but support **cancellable** **async** function. +:::note +It should not return a cleanup function as it's `async` which cannot cleanup as expected. + +If you need to cleanup something, use `isCancelled` in `UseAsyncEffectCallback` instead to check effect status. +::: + ## Demo Toggle the status several times, and you will find that all **obsolete** timers are deleted as expected, although we do not return a cleanup function in effect. diff --git a/src/use-async-effect/index.ts b/src/use-async-effect/index.ts index 22a37a0c..10bbe527 100644 --- a/src/use-async-effect/index.ts +++ b/src/use-async-effect/index.ts @@ -6,8 +6,13 @@ import type { AsyncEffectCallback } from '../utils/create-effect/async' export type UseAsyncEffectCallback = AsyncEffectCallback /** - * like `React.useEffect` but it's can accept an `async` function + * A React Hook which is like [React.useEffect](https://react.dev/reference/react/useEffect) but support **cancellable** **async** function. * - * @tip it should not return a cleanup function as it's `async` which cannot cleanup as expected + * It should not return a cleanup function as it's `async` which cannot cleanup as expected. + * + * If you need to cleanup something, use `isCancelled` in {@link UseAsyncEffectCallback} instead to check effect status. + * + * @param {AsyncEffectCallback} asyncEffectCallback - `AsyncEffectCallback`, a callback function for async effect, see {@link AsyncEffectCallback} + * @param {DependencyList} [deps] - `DependencyList`, an array of dependencies, see [React.useEffect](https://react.dev/reference/react/useEffect) */ export const useAsyncEffect = createAsyncEffect(useEffect) diff --git a/src/use-async-fn/index.mdx b/src/use-async-fn/index.mdx index ce229a4a..476db7b7 100644 --- a/src/use-async-fn/index.mdx +++ b/src/use-async-fn/index.mdx @@ -44,7 +44,7 @@ export type UseAsyncFnOptions = { /** * whether to clear the value before running the function * - * @default false + * @defaultValue false */ clearBeforeRun?: boolean } diff --git a/src/use-async-fn/index.ts b/src/use-async-fn/index.ts index f2d60c4f..8793e98e 100644 --- a/src/use-async-fn/index.ts +++ b/src/use-async-fn/index.ts @@ -8,12 +8,12 @@ export interface UseAsyncFnOptions { /** * whether to clear the value before running the function * - * @default false + * @defaultValue false */ clearBeforeRun?: boolean } -export interface UseAsyncFnReturn { +export interface UseAsyncFnReturns { /** * a function to run the async function */ @@ -32,7 +32,13 @@ export interface UseAsyncFnReturn { value: Awaited> | undefined } -export function useAsyncFn(fn: T, options: UseAsyncFnOptions = {}): UseAsyncFnReturn { +/** + * A React Hook to run **async** functions with extra **loading** state that indicates whether the promise is pending. + * + * @param {AnyFunc} fn - `AnyFunc`, the async function to run, see {@link AnyFunc} + * @returns {UseAsyncFnReturns} `UseAsyncFnReturns`, see {@link UseAsyncFnReturns} + */ +export function useAsyncFn(fn: T, options: UseAsyncFnOptions = {}): UseAsyncFnReturns { const { clearBeforeRun } = options const [state, setState] = useSetState({ diff --git a/src/use-async-lock/index.ts b/src/use-async-lock/index.ts index b21c8d83..3785439c 100644 --- a/src/use-async-lock/index.ts +++ b/src/use-async-lock/index.ts @@ -4,13 +4,23 @@ import { useStableFn } from '../use-stable-fn' import type { AnyFunc } from '../utils/basic' -export type UseAsyncLockReturn = ( +export type UseAsyncLockReturns = ( ...args: Parameters ) => R extends undefined ? Promise> | undefined> : Promise> | Awaited>> -export function useAsyncLock(asyncFn: T, onMeetLock?: R) { +/** + * A React Hook to create a function that can be used to ensure that **only one is running at a time**, and provides an additional invalid operation callback function. + * + * @param {AnyFunc} asyncFn - `AnyFunc`, the async function to run, see {@link AnyFunc} + * @param {AnyFunc} [onMeetLock] - `AnyFunc`, the callback function to run when the lock is met, see {@link AnyFunc} + * @returns {UseAsyncLockReturns} `UseAsyncLockReturns`, see {@link UseAsyncLockReturns} + */ +export function useAsyncLock( + asyncFn: T, + onMeetLock?: R, +): UseAsyncLockReturns { const [isLockedRef, isLocked] = useGetterRef(false) const latest = useLatest({ asyncFn, onMeetLock }) @@ -30,5 +40,5 @@ export function useAsyncLock(asyncFn: T, o } }) - return lockedAsync as UseAsyncLockReturn + return lockedAsync as UseAsyncLockReturns } diff --git a/src/use-async-update-effect/index.mdx b/src/use-async-update-effect/index.mdx index 0e426596..7989e5a3 100644 --- a/src/use-async-update-effect/index.mdx +++ b/src/use-async-update-effect/index.mdx @@ -6,6 +6,12 @@ import { HooksType } from '@/components' A React Hook which is like [useAsyncEffect](/reference/use-async-effect) but **ignore the execution at first mount**. +:::note +It should not return a cleanup function as it's `async` which cannot cleanup as expected. + +If you need to cleanup something, use `isCancelled` in `UseAsyncEffectCallback` instead to check effect status. +::: + ## Demo It will **NOT** execute at first mount before you toggle the status to trigger the effect to start the interval. diff --git a/src/use-async-update-effect/index.ts b/src/use-async-update-effect/index.ts index cd985c63..eaecc55b 100644 --- a/src/use-async-update-effect/index.ts +++ b/src/use-async-update-effect/index.ts @@ -6,8 +6,14 @@ import type { AsyncEffectCallback } from '../utils/create-effect/async' export interface UseAsyncUpdateEffectCallback extends AsyncEffectCallback {} /** - * like `useUpdateEffect` but it's can accept an `async` function + * A React Hook which is like `useAsyncEffect` but **ignore the execution at first mount**. + * + * It should not return a cleanup function as it's `async` which cannot cleanup as expected. + * + * If you need to cleanup something, use `isCancelled` in {@link UseAsyncEffectCallback} instead to check effect status. + * + * @param {UseAsyncUpdateEffectCallback} asyncEffectCallback - `UseAsyncUpdateEffectCallback`, a callback function for async effect, see {@link UseAsyncUpdateEffectCallback} + * @param {DependencyList} [deps] - `DependencyList`, an array of dependencies, see [React.useEffect](https://react.dev/reference/react/useEffect) * - * @tip it should not return a cleanup function as it's `async` which cannot cleanup as expected */ export const useAsyncUpdateEffect = createAsyncEffect(useUpdateEffect) diff --git a/src/use-battery/index.mdx b/src/use-battery/index.mdx index a4c566d9..e585cbc3 100644 --- a/src/use-battery/index.mdx +++ b/src/use-battery/index.mdx @@ -36,7 +36,7 @@ const battery = useBattery() ### Returns ```tsx -export type UseBatteryReturn = { +export interface UseBatteryReturns { /** * Whether the battery is charging */ @@ -46,11 +46,11 @@ export type UseBatteryReturn = { */ level: number /** - * The seconds remaining to charge the battery fully + * The time remaining to charge the battery fully */ chargingTime: number /** - * The seconds remaining to discharge the battery fully + * The time remaining to discharge the battery fully */ dischargingTime: number /** diff --git a/src/use-battery/index.ts b/src/use-battery/index.ts index fba71d75..79c996b9 100644 --- a/src/use-battery/index.ts +++ b/src/use-battery/index.ts @@ -5,18 +5,7 @@ import { useEventListener } from '../use-event-listener' import { useSafeState } from '../use-safe-state' import { useSupported } from '../use-supported' -export interface BatteryManager extends EventTarget { - charging: boolean - chargingTime: number - dischargingTime: number - level: number -} - -export interface NavigatorWithBattery extends Navigator { - getBattery(): Promise -} - -export interface UseBatteryReturn { +export interface UseBatteryReturns { /** * Whether the battery is charging */ @@ -43,9 +32,41 @@ export interface UseBatteryReturn { update(): void } +interface BatteryManager extends EventTarget { + /** + * Whether the battery is charging + */ + charging: boolean + /** + * The time remaining to charge the battery fully + */ + chargingTime: number + /** + * The time remaining to discharge the battery fully + */ + dischargingTime: number + /** + * The battery level as a floating point number between 0 and 1 + */ + level: number +} + +interface NavigatorWithBattery extends Navigator { + /** + * Get the battery manager + */ + getBattery(): Promise +} + const batteryEvents = ['chargingchange', 'chargingtimechange', 'dischargingtimechange', 'levelchange'] -export function useBattery(): UseBatteryReturn { +/** + * A React Hook that get [Battery](https://developer.mozilla.org/zh-CN/docs/Web/API/Battery_Status_API) state with ease. + * + * @returns {UseBatteryReturns} `UseBatteryReturns`, see {@link UseBatteryReturns} + * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/Battery_Status_API | Battery Status API - MDN} + */ +export function useBattery(): UseBatteryReturns { const isSupported = useSupported(() => 'getBattery' in navigator) const [state, setState] = useSafeState( diff --git a/src/use-before-unload/index.mdx b/src/use-before-unload/index.mdx index 8e9c7544..2417284a 100644 --- a/src/use-before-unload/index.mdx +++ b/src/use-before-unload/index.mdx @@ -4,7 +4,7 @@ import { HooksType } from '@/components' -A React Hook to perform actions **before the page is unloaded** (e.g., closing, refreshing the page) and optionally trigger a browser-native confirmation dialog. +A React Hook to perform actions **before the page is unloaded** (e.g., closing, refreshing the page) and optionally trigger a browser-native confirmation dialog. set `preventDefault` to `true` to show the confirm leave dialog. ## Demo @@ -38,20 +38,30 @@ useBeforeUnload(callback, options) A function that will be called right before the page is potentially unloaded. ```tsx -export type UseBeforeUnloadCallback = (e: BeforeUnloadEvent) => void +/** + * A callback function to run before the page is unloaded + * + * @param {BeforeUnloadEvent} event - `BeforeUnloadEvent`, the event object + * @returns {void} `void` + */ +export type UseBeforeUnloadCallback = (event: BeforeUnloadEvent) => void ``` ### Options ```tsx -export type UseBeforeUnloadOptions = { +export interface UseBeforeUnloadOptions { /** * Whether to prevent the default behavior of the event, which leads to showing a browser-native message * * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event * - * @default false + * @defaultValue false */ preventDefault?: boolean } ``` + +### Returns + +A clear function to remove the event listener. diff --git a/src/use-before-unload/index.ts b/src/use-before-unload/index.ts index a8dda686..ecddf92a 100644 --- a/src/use-before-unload/index.ts +++ b/src/use-before-unload/index.ts @@ -10,21 +10,29 @@ export interface UseBeforeUnloadOptions { * * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event * - * @default false + * @defaultValue false */ preventDefault?: boolean } -export type UseBeforeUnloadCallback = (e: BeforeUnloadEvent) => void +/** + * A callback function to run before the page is unloaded + * + * @param {BeforeUnloadEvent} event - `BeforeUnloadEvent`, the event object + * @returns {void} `void` + */ +export type UseBeforeUnloadCallback = (event: BeforeUnloadEvent) => void /** - * Do something before page unload (close or refresh and etc.), and optionally show a dialog to confirm. * - * You can set `preventDefault` to `true` to show the confirm leave dialog. + * A React Hook to perform actions **before the page is unloaded** (e.g., closing, refreshing the page) and optionally trigger a browser-native confirmation dialog. * - * NOTICE: Dialog only shows after user interacts with the page by default, like clicking a link, closing the tab, etc. + * set `preventDefault` to `true` to show the confirm leave dialog. * - * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event + * @param {UseBeforeUnloadCallback} callback - `UseBeforeUnloadCallback`, the callback function to run before the page is unloaded, see {@link UseBeforeUnloadCallback} + * @param {UseBeforeUnloadOptions} [options={}] - `UseBeforeUnloadOptions`, options to configure the hook, see {@link UseBeforeUnloadOptions} + * @returns {Noop} `Noop`, a function that clear the event listener, see {@link Noop} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event | BeforeUnloadEvent - MDN} */ export function useBeforeUnload(callback: UseBeforeUnloadCallback, options: UseBeforeUnloadOptions = {}): Noop { const { preventDefault = false } = options diff --git a/src/use-bluetooth/index.mdx b/src/use-bluetooth/index.mdx index efdd2efd..8320aba1 100644 --- a/src/use-bluetooth/index.mdx +++ b/src/use-bluetooth/index.mdx @@ -36,32 +36,33 @@ const bluetooth = useBlueTooth(options) ### Options ```tsx -export type UseBluetoothRequestDeviceOptions = { +export interface BluetoothRequestDeviceOptions { /** * An array of filter objects indicating the properties of devices that will be matched. * - * @see https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#filters + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#filters | requestDevice#filters - MDN} */ filters?: BluetoothLEScanFilter[] | undefined /** * A list of services that the application wishes to access on the remote device. * - * @see https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTService/uuid + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTService/uuid | BluetoothRemoteGATTService/uuid - MDN} */ optionalServices?: BluetoothServiceUUID[] | undefined } -export type UseBluetoothOptions = UseBluetoothRequestDeviceOptions & { +export interface UseBluetoothOptions extends BluetoothRequestDeviceOptions { /** * Whether to accept all devices * - * @default false + * @defaultValue false + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#acceptAllDevices | requestDevice#acceptAllDevices - MDN} */ acceptAllDevices?: boolean /** * Whether to connect immediately * - * @default true + * @defaultValue true */ immediate?: boolean } @@ -70,7 +71,7 @@ export type UseBluetoothOptions = UseBluetoothRequestDeviceOptions & { ### Returns ```tsx -export type UseBlueToothReturn = { +export interface UseBlueToothReturns { /** * Whether the device is connected */ @@ -93,10 +94,16 @@ export type UseBlueToothReturn = { device: BluetoothDevice | undefined /** * Connect to the Bluetooth GATT server + * + * @returns {Promise} The GATT server, see {@link BluetoothRemoteGATTServer} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTServer | BluetoothRemoteGATTServer - MDN} */ connect(): Promise /** * Disconnect from the Bluetooth GATT server + * + * @returns {void} `void` + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTServer/disconnect | BluetoothRemoteGATTServer.disconnect - MDN} */ disconnect(): void /** @@ -105,6 +112,9 @@ export type UseBlueToothReturn = { isSupported: boolean /** * Request a Bluetooth device + * + * @returns {Promise} The Bluetooth device, see {@link BluetoothDevice} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice | Bluetooth.requestDevice - MDN} */ requestDevice(): Promise } diff --git a/src/use-bluetooth/index.ts b/src/use-bluetooth/index.ts index a6fdc2e5..f72837ff 100644 --- a/src/use-bluetooth/index.ts +++ b/src/use-bluetooth/index.ts @@ -9,13 +9,13 @@ export interface BluetoothRequestDeviceOptions { /** * An array of filter objects indicating the properties of devices that will be matched. * - * @see https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#filters + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#filters | requestDevice#filters - MDN} */ filters?: BluetoothLEScanFilter[] | undefined /** * A list of services that the application wishes to access on the remote device. * - * @see https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTService/uuid + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTService/uuid | BluetoothRemoteGATTService/uuid - MDN} */ optionalServices?: BluetoothServiceUUID[] | undefined } @@ -24,18 +24,19 @@ export interface UseBluetoothOptions extends BluetoothRequestDeviceOptions { /** * Whether to accept all devices * - * @default false + * @defaultValue false + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#acceptAllDevices | requestDevice#acceptAllDevices - MDN} */ acceptAllDevices?: boolean /** * Whether to connect immediately * - * @default true + * @defaultValue true */ immediate?: boolean } -export interface UseBlueToothReturn { +export interface UseBlueToothReturns { /** * Whether the device is connected */ @@ -58,10 +59,16 @@ export interface UseBlueToothReturn { device: BluetoothDevice | undefined /** * Connect to the Bluetooth GATT server + * + * @returns {Promise} The GATT server, see {@link BluetoothRemoteGATTServer} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTServer | BluetoothRemoteGATTServer - MDN} */ connect(): Promise /** * Disconnect from the Bluetooth GATT server + * + * @returns {void} `void` + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTServer/disconnect | BluetoothRemoteGATTServer.disconnect - MDN} */ disconnect(): void /** @@ -70,11 +77,20 @@ export interface UseBlueToothReturn { isSupported: boolean /** * Request a Bluetooth device + * + * @returns {Promise} The Bluetooth device, see {@link BluetoothDevice} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice | Bluetooth.requestDevice - MDN} */ requestDevice(): Promise } -export function useBluetooth(options?: UseBluetoothOptions): UseBlueToothReturn { +/** + * A React Hook that provides a simple API to interact with the Web [Bluetooth API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API). + * + * @param {UseBluetoothOptions} [options={}] - `UseBluetoothOptions`, options to configure the hook, see {@link UseBluetoothOptions} + * @returns {UseBlueToothReturns} `UseBlueToothReturns`, see {@link UseBlueToothReturns} + */ +export function useBluetooth(options?: UseBluetoothOptions): UseBlueToothReturns { const { filters, immediate = true, optionalServices, acceptAllDevices = false } = options || {} const isSupported = useSupported(() => 'bluetooth' in navigator) diff --git a/src/use-breakpoints/index.mdx b/src/use-breakpoints/index.mdx index 575253af..23b3587c 100644 --- a/src/use-breakpoints/index.mdx +++ b/src/use-breakpoints/index.mdx @@ -4,7 +4,7 @@ import { HooksType } from '@/components' -A React Hook that hepls to do with the current breakpoints. +A React Hook that provides a simple API to interact with the viewport breakpoints. ## Demo @@ -36,20 +36,23 @@ const bps = useBreakpoints(breakpoints, options) ### Breakpoints ```tsx +/** + * A object that contains different breakpoints + */ export type Breakpoints = Record ``` ### Options ```tsx -export type UseBreakpointsOptions = { +export interface UseBreakpointsOptions { /** * The query strategy to use for the generated shortcut methods like `.lg` * * 'min-width' - .lg will be true when the viewport is greater than or equal to the lg breakpoint (mobile-first) * 'max-width' - .lg will be true when the viewport is smaller than the xl breakpoint (desktop-first) * - * @default "min-width" + * @defaultValue "min-width" */ strategy?: 'min-width' | 'max-width' } @@ -58,29 +61,45 @@ export type UseBreakpointsOptions = { ### Returns ```tsx -export type useBreakpointsReturn = Record & { +export type UseBreakpointsReturns = Record & { /** * The current breakpoints states */ breakpoints: Record /** * Check if the viewport is greater than the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is greater than the given breakpoint */ isGreater: (k: K) => boolean /** * Check if the viewport is greater or equal to the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is greater or equal to the given breakpoint */ isGreaterOrEqual: (k: K) => boolean /** * Check if the viewport is smaller than the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is smaller than the given breakpoint */ isSmaller: (k: K) => boolean /** * Check if the viewport is smaller or equal to the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is smaller or equal to the given breakpoint */ isSmallerOrEqual: (k: K) => boolean /** * Check if the viewport is between the given breakpoints + * + * @param {K} a - `K`, the breakpoint key + * @param {K} b - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is between the given breakpoints */ isInBetween: (a: K, b: K) => boolean /** @@ -88,4 +107,5 @@ export type useBreakpointsReturn = Record & { */ currents: K[] } + ``` diff --git a/src/use-breakpoints/index.ts b/src/use-breakpoints/index.ts index 01d91415..b1eb1016 100644 --- a/src/use-breakpoints/index.ts +++ b/src/use-breakpoints/index.ts @@ -6,6 +6,9 @@ import { useSafeState } from '../use-safe-state' import { useStableFn } from '../use-stable-fn' import { increaseWithUnit, isNumber } from '../utils/basic' +/** + * A object that contains different breakpoints + */ export type Breakpoints = Record export interface UseBreakpointsOptions { @@ -15,34 +18,50 @@ export interface UseBreakpointsOptions { * 'min-width' - .lg will be true when the viewport is greater than or equal to the lg breakpoint (mobile-first) * 'max-width' - .lg will be true when the viewport is smaller than the xl breakpoint (desktop-first) * - * @default "min-width" + * @defaultValue "min-width" */ strategy?: 'min-width' | 'max-width' } -export type useBreakpointsReturn = Record & { +export type UseBreakpointsReturns = Record & { /** * The current breakpoints states */ breakpoints: Record /** * Check if the viewport is greater than the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is greater than the given breakpoint */ isGreater: (k: K) => boolean /** * Check if the viewport is greater or equal to the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is greater or equal to the given breakpoint */ isGreaterOrEqual: (k: K) => boolean /** * Check if the viewport is smaller than the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is smaller than the given breakpoint */ isSmaller: (k: K) => boolean /** * Check if the viewport is smaller or equal to the given breakpoint + * + * @param {K} k - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is smaller or equal to the given breakpoint */ isSmallerOrEqual: (k: K) => boolean /** * Check if the viewport is between the given breakpoints + * + * @param {K} a - `K`, the breakpoint key + * @param {K} b - `K`, the breakpoint key + * @returns {boolean} `boolean`, whether the viewport is between the given breakpoints */ isInBetween: (a: K, b: K) => boolean /** @@ -54,14 +73,21 @@ export type useBreakpointsReturn = Record & { /** * five breakpoints by default, align with tailwindcss breakpoints * - * @see https://tailwindcss.com/docs/responsive-design + * @see {@link https://tailwindcss.com/docs/responsive-design | Tailwind CSS - Responsive Design} * */ export const defaultBreakpoints = { sm: 640, md: 768, lg: 1024, xl: 1280, xxl: 1536 } +/** + * A React Hook that provides a simple API to interact with the viewport breakpoints. + * + * @param {Breakpoints} [breakpoints=defaultBreakpoints] - `Breakpoints`, the breakpoints to use, see {@link Breakpoints} and {@link defaultBreakpoints} + * @param {UseBreakpointsOptions} [options={}] - `UseBreakpointsOptions`, options to configure the hook, see {@link UseBreakpointsOptions} + * @returns {UseBreakpointsReturns} `UseBreakpointsReturns`, see {@link UseBreakpointsReturns} + */ export function useBreakpoints( breakpoints: Breakpoints = defaultBreakpoints as Breakpoints, options: UseBreakpointsOptions = {}, -): useBreakpointsReturn { +): UseBreakpointsReturns { const { strategy = 'min-width' } = options const latest = useLatest({ breakpoints, strategy, ...options }) diff --git a/src/use-browser-memory/index.mdx b/src/use-browser-memory/index.mdx index c7d853fd..82406896 100644 --- a/src/use-browser-memory/index.mdx +++ b/src/use-browser-memory/index.mdx @@ -4,7 +4,7 @@ import { HooksType } from '@/components' -A React Hook that returns the current browser memory usage. +A React Hook that helps to get the memory info of the browser. ## Demo diff --git a/src/use-browser-memory/index.ts b/src/use-browser-memory/index.ts index 8f34253a..1d332763 100644 --- a/src/use-browser-memory/index.ts +++ b/src/use-browser-memory/index.ts @@ -12,7 +12,7 @@ import type { UseIntervalFnOptions } from '../use-interval-fn' * * @see https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory */ -export interface MemoryInfo { +interface MemoryInfo { /** * The maximum size of the heap, in bytes, that is available to the context. */ @@ -29,15 +29,21 @@ export interface MemoryInfo { [Symbol.toStringTag]: 'MemoryInfo' } -export interface UseBrowserMemoryOptions extends UseIntervalFnOptions { - interval?: number +interface PerformanceWithMemory extends Performance { + /** + * Performance.memory + */ + memory: MemoryInfo } -export interface PerformanceWithMemory extends Performance { - memory: MemoryInfo +export interface UseBrowserMemoryOptions extends UseIntervalFnOptions { + /** + * The interval to update the memory info + */ + interval?: number } -export interface UseBrowserMemoryReturn { +export interface UseBrowserMemoryReturns { /** * The timestamp when the memory info was last updated. */ @@ -68,10 +74,16 @@ export interface UseBrowserMemoryReturn { update(): void } -export function useBrowserMemory(options: UseBrowserMemoryOptions = {}): UseBrowserMemoryReturn { +/** + * A React Hook that helps to get the memory info of the browser. + * + * @param {UseBrowserMemoryOptions} [options={}] - `UseBrowserMemoryOptions`, options to configure the hook, see {@link UseBrowserMemoryOptions} + * @returns {UseBrowserMemoryReturns} `UseBrowserMemoryReturns`, see {@link UseBrowserMemoryReturns} + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory#browser_compatibility | Performance.memory's browser compatibility - MDN} + */ +export function useBrowserMemory(options: UseBrowserMemoryOptions = {}): UseBrowserMemoryReturns { const { interval = 1000, ...useIntervalFnOptions } = options - // https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory#browser_compatibility const isSupported = useSupported(() => 'memory' in performance) const latest = useLatest({ isSupported }) diff --git a/src/use-circular-list/index.mdx b/src/use-circular-list/index.mdx index c1c11a5a..e93576c3 100644 --- a/src/use-circular-list/index.mdx +++ b/src/use-circular-list/index.mdx @@ -4,7 +4,7 @@ import { HooksType } from '@/components' -A React Hook that creates and shifts a circular list. +A React Hook that helps to create a circular list that can cycle through the list. :::tip @@ -51,6 +51,10 @@ export interface UseCycleListOptions { fallbackIndex?: number /** * Custom function to get the index of the value + * + * @param {T} value - `T`, the value to find + * @param {T[]} list - `T[]`, the list to search + * @returns {number} `number`, the index of the value */ getIndexOf?: (value: T, list: T[]) => number } @@ -62,19 +66,28 @@ export interface UseCycleListOptions { export type UseCycleListActions = { /** * Move to the next value + * + * @param {number} [step] - `number`, the step to move + * @returns {T} `T`, the next value */ next: (step?: number) => T /** * Move to the previous value + * + * @param {number} [step] - `number`, the step to move + * @returns {T} `T`, the previous value */ prev: (step?: number) => T /** * Move to the value at the specified index + * + * @param {number} index - `number`, the index to move + * @returns {T} `T`, the value at the specified index */ go: (index: number) => T } -export type UseCycleListReturn = [ +export type UseCycleListReturns = readonly [ /** * Current value */ diff --git a/src/use-circular-list/index.ts b/src/use-circular-list/index.ts index 89b6e81b..6137e89e 100644 --- a/src/use-circular-list/index.ts +++ b/src/use-circular-list/index.ts @@ -15,6 +15,10 @@ export interface UseCycleListOptions { fallbackIndex?: number /** * Custom function to get the index of the value + * + * @param {T} value - `T`, the value to find + * @param {T[]} list - `T[]`, the list to search + * @returns {number} `number`, the index of the value */ getIndexOf?: (value: T, list: T[]) => number } @@ -22,19 +26,28 @@ export interface UseCycleListOptions { export type UseCycleListActions = { /** * Move to the next value + * + * @param {number} [step] - `number`, the step to move + * @returns {T} `T`, the next value */ next: (step?: number) => T /** * Move to the previous value + * + * @param {number} [step] - `number`, the step to move + * @returns {T} `T`, the previous value */ prev: (step?: number) => T /** * Move to the value at the specified index + * + * @param {number} index - `number`, the index to move + * @returns {T} `T`, the value at the specified index */ go: (index: number) => T } -export type UseCycleListReturn = [ +export type UseCycleListReturns = readonly [ /** * Current value */ @@ -49,11 +62,19 @@ export type UseCycleListReturn = [ index: number, ] -// TODO: rewrite using `NoInfer` in TS 5.4, now downgrade temporarily +/** + * A React Hook that helps to create a circular list that can cycle through the list. + * + * TODO: rewrite using `NoInfer` in TS 5.4, now downgrade temporarily + * + * @param {T[]} list - `T[]`, the list to cycle + * @param {UseCycleListOptions} [options] - `UseCycleListOptions`, see {@link UseCycleListOptions} + * @returns {UseCycleListReturns} `UseCycleListReturns`, see {@link UseCycleListReturns} + */ export function useCircularList( list: T[], options: UseCycleListOptions = {}, -): UseCycleListReturn { +): UseCycleListReturns { const { initialValue = list[0], fallbackIndex = 0, getIndexOf } = options const latest = useLatest({ fallbackIndex, list, getIndexOf }) diff --git a/src/use-clamp/index.ts b/src/use-clamp/index.ts index 3e42c6bb..39470e40 100644 --- a/src/use-clamp/index.ts +++ b/src/use-clamp/index.ts @@ -2,14 +2,12 @@ import { useCounter } from '../use-counter' import { useUpdateEffect } from '../use-update-effect' import { unwrapGettable } from '../utils/unwrap' -import type { UseCounterReturnAction } from '../use-counter' +import type { UseCounterReturnsAction } from '../use-counter' import type { Gettable } from '../utils/basic' -export function useClamp( - value: Gettable, - min: Gettable, - max: Gettable, -): [number, UseCounterReturnAction] { +export type UseClampReturns = readonly [number, UseCounterReturnsAction] + +export function useClamp(value: Gettable, min: Gettable, max: Gettable): UseClampReturns { const num = unwrapGettable(value) const [count, actions] = useCounter(num, { diff --git a/src/use-click-outside/index.ts b/src/use-click-outside/index.ts index d8bc0a31..15bdbc4a 100644 --- a/src/use-click-outside/index.ts +++ b/src/use-click-outside/index.ts @@ -19,7 +19,7 @@ export function useClickOutside( target: ElementTarget, handler: UseClickOutsideHandler, options: UseClickOutsideOptions = {}, -) { +): void { const { ignore = [] } = options const el = useTargetElement(target) const latest = useLatest({ ignore, handler }) diff --git a/src/use-clipboard-items/index.mdx b/src/use-clipboard-items/index.mdx index c35a7c76..52158ae2 100644 --- a/src/use-clipboard-items/index.mdx +++ b/src/use-clipboard-items/index.mdx @@ -38,7 +38,7 @@ export type UseClipboardItemsOptions = { /** * Enabled reading for clipboard, need to request permission * - * @default false + * @defaultValue false */ read?: boolean /** @@ -48,7 +48,7 @@ export type UseClipboardItemsOptions = { /** * Milliseconds to reset state of `copied` ref * - * @default 1500 + * @defaultValue 1500 */ copiedDuration?: number } @@ -57,7 +57,7 @@ export type UseClipboardItemsOptions = { ### Returns ```tsx -export type UseClipboardItemsReturn = { +export type UseClipboardItemsReturns = { /** * Check if the browser supports clipboard API */ diff --git a/src/use-clipboard-items/index.ts b/src/use-clipboard-items/index.ts index 9836c320..5bfb9b62 100644 --- a/src/use-clipboard-items/index.ts +++ b/src/use-clipboard-items/index.ts @@ -12,7 +12,7 @@ export interface UseClipboardItemsOptions { /** * Enabled reading for clipboard, need to request permission * - * @default false + * @defaultValue false */ read?: boolean /** @@ -22,12 +22,12 @@ export interface UseClipboardItemsOptions { /** * Milliseconds to reset state of `copied` ref * - * @default 1500 + * @defaultValue 1500 */ copiedDuration?: number } -export interface UseClipboardItemsReturn { +export interface UseClipboardItemsReturns { /** * Check if the browser supports clipboard API */ @@ -54,11 +54,11 @@ function isAllowed(status: UsePermissionReturn) { return status.current && ['granted', 'prompt'].includes(status.current) } -export function useClipboardItems(options?: UseClipboardItemsOptions): UseClipboardItemsReturn -export function useClipboardItems(options: UseClipboardItemsOptions): UseClipboardItemsReturn +export function useClipboardItems(options?: UseClipboardItemsOptions): UseClipboardItemsReturns +export function useClipboardItems(options: UseClipboardItemsOptions): UseClipboardItemsReturns export function useClipboardItems( options: UseClipboardItemsOptions = {}, -): UseClipboardItemsReturn { +): UseClipboardItemsReturns { const { read = false, source, copiedDuration = 1500 } = options const isSupported = useSupported(() => 'clipboard' in navigator) diff --git a/src/use-clipboard/index.mdx b/src/use-clipboard/index.mdx index ddea3bc9..37cbbb9d 100644 --- a/src/use-clipboard/index.mdx +++ b/src/use-clipboard/index.mdx @@ -38,7 +38,7 @@ export type UseClipboardOptions = { /** * Enabled reading for clipboard, need to request permission * - * @default false + * @defaultValue false */ read?: boolean /** @@ -48,7 +48,7 @@ export type UseClipboardOptions = { /** * Milliseconds to reset state of `copied` ref * - * @default 1500 + * @defaultValue 1500 */ copiedDuration?: number } @@ -57,7 +57,7 @@ export type UseClipboardOptions = { ### Returns ```tsx -export interface UseClipboardReturn { +export interface UseClipboardReturns { /** * Whether the clipboard is supported */ diff --git a/src/use-clipboard/index.ts b/src/use-clipboard/index.ts index 00238798..9c61d9e2 100644 --- a/src/use-clipboard/index.ts +++ b/src/use-clipboard/index.ts @@ -14,7 +14,7 @@ export interface UseClipboardOptions { /** * Enabled reading for clipboard, need to request permission * - * @default false + * @defaultValue false */ read?: boolean /** @@ -24,12 +24,12 @@ export interface UseClipboardOptions { /** * Milliseconds to reset state of `copied` ref * - * @default 1500 + * @defaultValue 1500 */ copiedDuration?: number } -export interface UseClipboardReturn { +export interface UseClipboardReturns { /** * Whether the clipboard is supported */ @@ -52,11 +52,11 @@ export interface UseClipboardReturn { clear(): void } -export function useClipboard(options?: UseClipboardOptions): UseClipboardReturn -export function useClipboard(options: UseClipboardOptions>): UseClipboardReturn +export function useClipboard(options?: UseClipboardOptions): UseClipboardReturns +export function useClipboard(options: UseClipboardOptions>): UseClipboardReturns export function useClipboard( options: UseClipboardOptions | undefined> = {}, -): UseClipboardReturn { +): UseClipboardReturns { const { read = false, source, copiedDuration = 1500 } = options const permissionRead = usePermission('clipboard-read') diff --git a/src/use-cloned/index.mdx b/src/use-cloned/index.mdx index 3a18e315..0b7377d4 100644 --- a/src/use-cloned/index.mdx +++ b/src/use-cloned/index.mdx @@ -42,19 +42,19 @@ export type UseClonedOptions = { /** * Clone function * - * @default defaultCloneFn + * @defaultValue defaultCloneFn */ clone?: (source: T) => T /** * Whether to manually trigger the clone function * - * @default false + * @defaultValue false */ manual?: boolean /** * Whether to deep compare the source object * - * @default true + * @defaultValue true */ deep?: boolean } @@ -63,7 +63,7 @@ export type UseClonedOptions = { ### Returns ```tsx -export type UseClonedReturn = [ +export type UseClonedReturns = [ /** * Cloned state */ diff --git a/src/use-cloned/index.ts b/src/use-cloned/index.ts index 7f4801e0..0d656511 100644 --- a/src/use-cloned/index.ts +++ b/src/use-cloned/index.ts @@ -10,24 +10,24 @@ export interface UseClonedOptions { /** * Clone function * - * @default defaultCloneFn + * @defaultValue defaultCloneFn */ clone?: (source: T) => T /** * Whether to manually trigger the clone function * - * @default false + * @defaultValue false */ manual?: boolean /** * Whether to deep compare the source object * - * @default true + * @defaultValue true */ deep?: boolean } -export type UseClonedReturn = [ +export type UseClonedReturns = [ /** * Cloned state */ @@ -48,7 +48,7 @@ export function defaultCloneFn(source: T): T { return JSON.parse(JSON.stringify(source)) } -export function useCloned(source: T, options: UseClonedOptions = {}): UseClonedReturn { +export function useCloned(source: T, options: UseClonedOptions = {}): UseClonedReturns { const { deep = true, manual = false, clone = defaultCloneFn } = options const [cloned, setCloned] = useSafeState(defaultCloneFn(source)) diff --git a/src/use-controlled-component/index.mdx b/src/use-controlled-component/index.mdx index 1eb105b3..71323f35 100644 --- a/src/use-controlled-component/index.mdx +++ b/src/use-controlled-component/index.mdx @@ -76,7 +76,7 @@ export interface UseControlledComponentOptions { +export interface UseControlledComponentReturns { /** * The value of the controlled component */ diff --git a/src/use-controlled-component/index.ts b/src/use-controlled-component/index.ts index 291e46e4..51c7b2a7 100644 --- a/src/use-controlled-component/index.ts +++ b/src/use-controlled-component/index.ts @@ -35,7 +35,7 @@ export interface ControlledComponentProps { onChange: (eventOrValue?: T | { target: { value: T } }) => void } -export interface UseControlledComponentReturn { +export interface UseControlledComponentReturns { /** * The value of the controlled component */ @@ -57,7 +57,7 @@ export interface UseControlledComponentReturn { export function useControlledComponent( initialValue: T = '' as T, options: UseControlledComponentOptions = {}, -): UseControlledComponentReturn { +): UseControlledComponentReturns { const { fallbackValue, onReset, props, onChange, ...stateOptions } = options const [value, setValue] = useSafeState(initialValue as T, stateOptions) diff --git a/src/use-countdown/index.mdx b/src/use-countdown/index.mdx index b7e8613b..89bee567 100644 --- a/src/use-countdown/index.mdx +++ b/src/use-countdown/index.mdx @@ -45,19 +45,19 @@ export type UseCountdownOptions = { /** * whether to start the countdown immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * whether to expose the controls * - * @default false + * @defaultValue false */ controls?: Controls /** * the interval to update the countdown * - * @default 'requestAnimationFrame' + * @defaultValue 'requestAnimationFrame' */ interval?: 'requestAnimationFrame' | number /** @@ -78,5 +78,5 @@ import { Tip } from '@/components' ```tsx -export type UseCountdownReturn = Controls extends true ? { ms: number } & Pausable : number +export type UseCountdownReturns = Controls extends true ? { ms: number } & Pausable : number ``` diff --git a/src/use-countdown/index.ts b/src/use-countdown/index.ts index e0e42162..d77a4bc2 100644 --- a/src/use-countdown/index.ts +++ b/src/use-countdown/index.ts @@ -24,19 +24,19 @@ export interface UseCountdownOptions { /** * whether to start the countdown immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * whether to expose the controls * - * @default false + * @defaultValue false */ controls?: Controls /** * the interval to update the countdown * - * @default 'requestAnimationFrame' + * @defaultValue 'requestAnimationFrame' */ interval?: UseIntervalFnInterval /** @@ -49,7 +49,7 @@ export interface UseCountdownOptions { onStop?(): void } -export type UseCountdownReturn = Controls extends true +export type UseCountdownReturns = Controls extends true ? { ms: number; isStop: boolean } & Pausable : number @@ -62,13 +62,13 @@ function calRemainingTime(date: Gettable) { return 0 } -export function useCountdown(date: Gettable): UseCountdownReturn -export function useCountdown(date: Gettable, options: UseCountdownOptions): UseCountdownReturn -export function useCountdown(date: Gettable, options: UseCountdownOptions): UseCountdownReturn +export function useCountdown(date: Gettable): UseCountdownReturns +export function useCountdown(date: Gettable, options: UseCountdownOptions): UseCountdownReturns +export function useCountdown(date: Gettable, options: UseCountdownOptions): UseCountdownReturns export function useCountdown( date: Gettable, options: UseCountdownOptions = {}, -): UseCountdownReturn { +): UseCountdownReturns { const { immediate = true, controls: exposeControls = false, diff --git a/src/use-counter/index.mdx b/src/use-counter/index.mdx index 0363cb26..96966ae9 100644 --- a/src/use-counter/index.mdx +++ b/src/use-counter/index.mdx @@ -51,7 +51,7 @@ export type UseCounterOptions = { ```tsx export type Count = number -export interface UseCounterReturnAction { +export interface UseCounterReturnsAction { /** * increment the counter */ @@ -74,7 +74,7 @@ export interface UseCounterReturnAction { reset: (n?: number) => void } -export type UseCounterReturn = [ +export type UseCounterReturns = [ /** * The count state of the counter */ @@ -82,7 +82,7 @@ export type UseCounterReturn = [ /** * The actions to control the counter */ - UseCounterReturnAction, + UseCounterReturnsAction, /** * The whole state of the counter */ diff --git a/src/use-counter/index.ts b/src/use-counter/index.ts index 5a7c9f4c..dda8a06a 100644 --- a/src/use-counter/index.ts +++ b/src/use-counter/index.ts @@ -19,7 +19,7 @@ export interface UseCounterOptions { export type Count = number -export interface UseCounterReturnAction { +export interface UseCounterReturnsAction { /** * increment the counter */ @@ -42,7 +42,7 @@ export interface UseCounterReturnAction { reset: (n?: number) => void } -export type UseCounterReturn = [ +export type UseCounterReturns = [ /** * The count state of the counter */ @@ -50,7 +50,7 @@ export type UseCounterReturn = [ /** * The actions to control the counter */ - UseCounterReturnAction, + UseCounterReturnsAction, /** * The whole state of the counter */ @@ -62,7 +62,7 @@ export type UseCounterReturn = [ }, ] -export function useCounter(initialCount?: number, options: UseCounterOptions = {}): UseCounterReturn { +export function useCounter(initialCount?: number, options: UseCounterOptions = {}): UseCounterReturns { const [state, setState] = useSetState( { initialCount: initialCount ?? 0, diff --git a/src/use-css-var/index.mdx b/src/use-css-var/index.mdx index ee919c71..2b446782 100644 --- a/src/use-css-var/index.mdx +++ b/src/use-css-var/index.mdx @@ -44,13 +44,13 @@ export type UseCssVarOptions = { /** * default value of the css variable * - * @default '' + * @defaultValue '' */ defaultValue?: string /** * whether to observe the changes of the css variable * - * @default false + * @defaultValue false */ observe?: boolean } @@ -67,3 +67,8 @@ import { Tip } from '@/components' ### Returns Same as [React.useState](https://react.dev/reference/react/useState), a tuple with the current value of the CSS variable and a function to update it. + +```tsx +export type UseCssVarReturns = readonly [string, ReactSetState] +``` + diff --git a/src/use-css-var/index.ts b/src/use-css-var/index.ts index 04c042c6..b730b9bf 100644 --- a/src/use-css-var/index.ts +++ b/src/use-css-var/index.ts @@ -15,22 +15,24 @@ export interface UseCssVarOptions { /** * default value of the css variable * - * @default '' + * @defaultValue '' */ defaultValue?: string /** * whether to observe the changes of the css variable * - * @default false + * @defaultValue false */ observe?: boolean } +export type UseCssVarReturns = readonly [string, ReactSetState] + export function useCssVar( propName: Gettable, options: UseCssVarOptions = {}, target: ElementTarget = () => document.documentElement as T, -): [string, ReactSetState] { +): UseCssVarReturns { const { defaultValue = '', observe = false } = options const [variable, _setVariable] = useSafeState(defaultValue) diff --git a/src/use-date-format/format-date.ts b/src/use-date-format/format-date.ts index e1eb812a..98b3f083 100644 --- a/src/use-date-format/format-date.ts +++ b/src/use-date-format/format-date.ts @@ -27,7 +27,7 @@ export type FormatDateOptions = { * * @see https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table * - * @default false + * @defaultValue false */ unicodeSymbols?: boolean /** diff --git a/src/use-date-format/index.mdx b/src/use-date-format/index.mdx index b0c71f99..73c6eb78 100644 --- a/src/use-date-format/index.mdx +++ b/src/use-date-format/index.mdx @@ -97,7 +97,7 @@ export type FormatDateOptions = { * * @see https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table * - * @default false + * @defaultValue false */ unicodeSymbols?: boolean /** diff --git a/src/use-date-format/index.ts b/src/use-date-format/index.ts index 13f65fc9..59b65e22 100644 --- a/src/use-date-format/index.ts +++ b/src/use-date-format/index.ts @@ -21,7 +21,7 @@ export function useDateFormat( /** * format string * - * @default 'YYYY-MM-DD HH:mm:ss' + * @defaultValue 'YYYY-MM-DD HH:mm:ss' */ formatStr = 'YYYY-MM-DD HH:mm:ss', /** diff --git a/src/use-debounced-fn/index.mdx b/src/use-debounced-fn/index.mdx index 936a885f..838f9d98 100644 --- a/src/use-debounced-fn/index.mdx +++ b/src/use-debounced-fn/index.mdx @@ -42,19 +42,19 @@ export type DebounceOptions = { /** * Time to wait before invoking the function * - * @default 0 + * @defaultValue 0 */ wait?: number /** * immediately invoke before the timeout * - * @default false + * @defaultValue false */ leading?: boolean /** * invoke the function after the timeout * - * @default true + * @defaultValue true */ trailing?: boolean } diff --git a/src/use-device-list/index.mdx b/src/use-device-list/index.mdx index 16bdac1a..35f849de 100644 --- a/src/use-device-list/index.mdx +++ b/src/use-device-list/index.mdx @@ -42,13 +42,13 @@ export type UseDeviceListOptions = { * Request for permissions immediately if it's not granted, * otherwise label and deviceIds could be empty * - * @default false + * @defaultValue false */ requestPermissions?: boolean /** * Request for types of media permissions * - * @default { audio: true, video: true } + * @defaultValue { audio: true, video: true } */ constraints?: MediaStreamConstraints } diff --git a/src/use-device-list/index.ts b/src/use-device-list/index.ts index b381b65f..8e02ea58 100644 --- a/src/use-device-list/index.ts +++ b/src/use-device-list/index.ts @@ -16,18 +16,18 @@ export interface UseDeviceListOptions { * Request for permissions immediately if it's not granted, * otherwise label and deviceIds could be empty * - * @default false + * @defaultValue false */ requestPermissions?: boolean /** * Request for types of media permissions * - * @default { audio: true, video: true } + * @defaultValue { audio: true, video: true } */ constraints?: MediaStreamConstraints } -export interface UseDeviceListReturn { +export interface UseDeviceListReturns { /** * List of all devices */ @@ -62,7 +62,7 @@ export interface UseDeviceListReturn { update(): Promise } -export function useDeviceList(options: UseDeviceListOptions = {}): UseDeviceListReturn { +export function useDeviceList(options: UseDeviceListOptions = {}): UseDeviceListReturns { const { requestPermissions = false, constraints = { audio: true, video: true }, onUpdated } = options const [state, setState] = useSetState({ diff --git a/src/use-document-visibility/index.ts b/src/use-document-visibility/index.ts index 771ba04c..1d0d2d6d 100644 --- a/src/use-document-visibility/index.ts +++ b/src/use-document-visibility/index.ts @@ -21,7 +21,3 @@ export function useDocumentVisibility(callback?: UseDocumentVisibilityCallback): return visibility } - -function getVisibilityState(): DocumentVisibilityState { - return document.visibilityState -} diff --git a/src/use-draggable/index.mdx b/src/use-draggable/index.mdx index c35ce1f9..91bc9d3d 100644 --- a/src/use-draggable/index.mdx +++ b/src/use-draggable/index.mdx @@ -46,55 +46,55 @@ export type UseDraggableOptions = { /** * Whether to drag the element only when the pointer is exactly on the target element. * - * @default false + * @defaultValue false */ exact?: boolean /** * Whether to prevent the default behavior of the pointer event. * - * @default false + * @defaultValue false */ preventDefault?: boolean /** * Whether to stop the propagation of the pointer event. * - * @default false + * @defaultValue false */ stopPropagation?: boolean /** * Whether to capture the pointer event. * - * @default true + * @defaultValue true */ capture?: boolean /** * The element that should be dragged. * - * @default target + * @defaultValue target */ handle?: ElementTarget /** * The element that should be dragged. * - * @default window + * @defaultValue window */ draggingElement?: ElementTarget | GlobalTarget /** * The element that should contain the draggable element. * - * @default undefined + * @defaultValue undefined */ containerElement?: ElementTarget /** * The types of pointer events that should be handled. * - * @default ['mouse', 'touch', 'pen'] + * @defaultValue ['mouse', 'touch', 'pen'] */ pointerTypes?: PointerType[] /** * The initial position of the draggable element. * - * @default { x: 0; y: 0 } + * @defaultValue { x: 0; y: 0 } */ initialValue?: Position /** @@ -110,11 +110,11 @@ export type UseDraggableOptions = { */ onEnd?: (position: Position, event: PointerEvent) => void /** - * @default 'both' + * @defaultValue 'both' */ axis?: 'x' | 'y' | 'both' /** - * @default false + * @defaultValue false */ disabled?: boolean } @@ -123,7 +123,7 @@ export type UseDraggableOptions = { ### Returns ```tsx -export type UseDraggableReturn = { +export type UseDraggableReturns = { /** * The x-coordinate of the draggable element. */ diff --git a/src/use-draggable/index.ts b/src/use-draggable/index.ts index 56a86bc7..1ac27ae6 100644 --- a/src/use-draggable/index.ts +++ b/src/use-draggable/index.ts @@ -11,55 +11,55 @@ export interface UseDraggableOptions { /** * Whether to drag the element only when the pointer is exactly on the target element. * - * @default false + * @defaultValue false */ exact?: boolean /** * Whether to prevent the default behavior of the pointer event. * - * @default false + * @defaultValue false */ preventDefault?: boolean /** * Whether to stop the propagation of the pointer event. * - * @default false + * @defaultValue false */ stopPropagation?: boolean /** * Whether to capture the pointer event. * - * @default true + * @defaultValue true */ capture?: boolean /** * The element that should be dragged. * - * @default target + * @defaultValue target */ handle?: ElementTarget /** * The element that should be dragged. * - * @default window + * @defaultValue window */ draggingElement?: ElementTarget | GlobalTarget /** * The element that should contain the draggable element. * - * @default undefined + * @defaultValue undefined */ containerElement?: ElementTarget /** * The types of pointer events that should be handled. * - * @default ['mouse', 'touch', 'pen'] + * @defaultValue ['mouse', 'touch', 'pen'] */ pointerTypes?: PointerType[] /** * The initial position of the draggable element. * - * @default { x: 0; y: 0 } + * @defaultValue { x: 0; y: 0 } */ initialValue?: Position /** @@ -75,16 +75,16 @@ export interface UseDraggableOptions { */ onEnd?: (position: Position, event: PointerEvent) => void /** - * @default 'both' + * @defaultValue 'both' */ axis?: 'x' | 'y' | 'both' /** - * @default false + * @defaultValue false */ disabled?: boolean } -export interface UseDraggableReturn { +export interface UseDraggableReturns { /** * The x-coordinate of the draggable element. */ @@ -114,7 +114,7 @@ export interface UseDraggableReturn { /** @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button MDN} */ const MAIN_MOUSE_BUTTON_TYPE = 0 -export function useDraggable(target: ElementTarget, options: UseDraggableOptions = {}): UseDraggableReturn { +export function useDraggable(target: ElementTarget, options: UseDraggableOptions = {}): UseDraggableReturns { const { exact = false, preventDefault = false, diff --git a/src/use-drop-zone/index.mdx b/src/use-drop-zone/index.mdx index b9fcab3e..9927e28e 100644 --- a/src/use-drop-zone/index.mdx +++ b/src/use-drop-zone/index.mdx @@ -69,7 +69,7 @@ export type UseDropZoneOptions = { ### Returns ```tsx -export type UseDropZoneReturn = { +export type UseDropZoneReturns = { /** * The files that were dropped in the drop zone. */ diff --git a/src/use-drop-zone/index.ts b/src/use-drop-zone/index.ts index 145b01d7..fce84aac 100644 --- a/src/use-drop-zone/index.ts +++ b/src/use-drop-zone/index.ts @@ -29,7 +29,7 @@ export interface UseDropZoneOptions { onOver?: (files: File[] | null, event: DragEvent) => void } -export interface UseDropZoneReturn { +export interface UseDropZoneReturns { /** * The files that were dropped in the drop zone. */ @@ -43,7 +43,7 @@ export interface UseDropZoneReturn { export function useDropZone( target: ElementTarget, options: UseDropZoneOptions | UseDropZoneOptions['onDrop'] = {}, -): UseDropZoneReturn { +): UseDropZoneReturns { const el = useTargetElement(target) const [state, setState] = useSetState({ diff --git a/src/use-dynamic-list/index.mdx b/src/use-dynamic-list/index.mdx index e7acef5e..cdeb3c1c 100644 --- a/src/use-dynamic-list/index.mdx +++ b/src/use-dynamic-list/index.mdx @@ -38,7 +38,7 @@ Any array of items, default to `[]` (`unknown[]`). ### Returns ```tsx -export type UseDynamicListReturn = [ +export type UseDynamicListReturns = readonly [ /** * list of items */ diff --git a/src/use-dynamic-list/index.ts b/src/use-dynamic-list/index.ts index a7bffb4b..8355ca77 100644 --- a/src/use-dynamic-list/index.ts +++ b/src/use-dynamic-list/index.ts @@ -3,7 +3,7 @@ import { useCreation } from '../use-creation' import { useSafeState } from '../use-safe-state' import { useStableFn } from '../use-stable-fn' -export type UseDynamicListReturn = [ +export type UseDynamicListReturns = readonly [ /** * list of items */ @@ -64,7 +64,7 @@ export type UseDynamicListReturn = [ }, ] -export function useDynamicList(initialList: T[] = []): UseDynamicListReturn { +export function useDynamicList(initialList: T[] = []): UseDynamicListReturns { const listIdxRef = useRef(0) const keysRef = useRef([]) diff --git a/src/use-element-bounding/index.mdx b/src/use-element-bounding/index.mdx index 818c378e..cc7faf26 100644 --- a/src/use-element-bounding/index.mdx +++ b/src/use-element-bounding/index.mdx @@ -46,19 +46,19 @@ export type UseElementBoundingOptions = { /** * Reset the bounding box when the element is not found * - * @default true + * @defaultValue true */ reset?: boolean /** * Update the bounding box when the window is resized * - * @default true + * @defaultValue true */ windowResize?: boolean /** * Update the bounding box when the window is scrolled * - * @default true + * @defaultValue true */ windowScroll?: boolean } @@ -67,7 +67,7 @@ export type UseElementBoundingOptions = { ### Returns ```tsx -export type UseElementBoundingReturn = [ +export type UseElementBoundingReturns = [ { /** * The height of the element diff --git a/src/use-element-bounding/index.ts b/src/use-element-bounding/index.ts index ea40d391..1165a0c9 100644 --- a/src/use-element-bounding/index.ts +++ b/src/use-element-bounding/index.ts @@ -13,24 +13,24 @@ export interface UseElementBoundingOptions { /** * Reset the bounding box when the element is not found * - * @default true + * @defaultValue true */ reset?: boolean /** * Update the bounding box when the window is resized * - * @default true + * @defaultValue true */ windowResize?: boolean /** * Update the bounding box when the window is scrolled * - * @default true + * @defaultValue true */ windowScroll?: boolean } -export type UseElementBoundingReturn = [ +export type UseElementBoundingReturns = readonly [ { /** * The height of the element @@ -76,7 +76,7 @@ const defaultElBounding = { x: 0, y: 0, height: 0, width: 0, top: 0, right: 0, b export function useElementBounding( target: ElementTarget, options: UseElementBoundingOptions = {}, -): UseElementBoundingReturn { +): UseElementBoundingReturns { const { reset = true, windowResize = true, windowScroll = true } = options const el = useTargetElement(target) @@ -107,7 +107,7 @@ export function useElementBounding( return [bounding, update] as const } -function getElBounding(target: ElementTarget): UseElementBoundingReturn[0] { +function getElBounding(target: ElementTarget): UseElementBoundingReturns[0] { const el = normalizeElement(target) if (!el) return defaultElBounding diff --git a/src/use-element-by-point/index.mdx b/src/use-element-by-point/index.mdx index b9d503e2..f26e6b24 100644 --- a/src/use-element-by-point/index.mdx +++ b/src/use-element-by-point/index.mdx @@ -38,32 +38,32 @@ export type UseElementByPointOptions = Position & { /** * Whether to return multiple elements * - * @default false + * @defaultValue false */ multiple?: M /** * Whether to execute immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * The interval to execute * - * @default 'requestAnimationFrame' + * @defaultValue 'requestAnimationFrame' */ interval?: 'requestAnimationFrame' | number } ``` -### Return +### Returns import { Tip } from '@/components' ```tsx -export type UseElementByPointReturn = Pausable & { +export type UseElementByPointReturns = Pausable & { /** * Whether the browser supports the API */ diff --git a/src/use-element-by-point/index.ts b/src/use-element-by-point/index.ts index 07bfb961..74c1a48f 100644 --- a/src/use-element-by-point/index.ts +++ b/src/use-element-by-point/index.ts @@ -8,7 +8,7 @@ import type { UseIntervalFnInterval } from '../use-interval-fn' import type { Pausable } from '../use-pausable' import type { Position } from '../utils/basic' -export interface UseElementByPointReturn extends Pausable { +export interface UseElementByPointReturns extends Pausable { /** * Whether the browser supports the API */ @@ -23,19 +23,19 @@ export interface UseElementByPointOptions extends Pos /** * Whether to return multiple elements * - * @default false + * @defaultValue false */ multiple?: M /** * Whether to execute immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * The interval to execute * - * @default 'requestAnimationFrame' + * @defaultValue 'requestAnimationFrame' */ interval?: UseIntervalFnInterval } @@ -43,7 +43,7 @@ export interface UseElementByPointOptions extends Pos export function useElementByPoint< M extends boolean = false, E extends Element | Element[] = M extends true ? Element[] : Element, ->(options: UseElementByPointOptions): UseElementByPointReturn { +>(options: UseElementByPointOptions): UseElementByPointReturns { const { x, y, multiple = false, interval = 'requestAnimationFrame', immediate = true } = options const [element, setElement] = useSafeState(null) const latest = useLatest({ x, y, multiple }) diff --git a/src/use-element-size/index.mdx b/src/use-element-size/index.mdx index a67f80a0..486e194a 100644 --- a/src/use-element-size/index.mdx +++ b/src/use-element-size/index.mdx @@ -63,5 +63,5 @@ Options of `useResizeObserver`, see [useResizeObserver#options](/reference/use-r Returns of `useResizeObserver`, see [useResizeObserver#returns](/reference/use-resize-observer/#returns) for more details. ```tsx -export type UseElementSizeReturn = ElementSize & UseResizeObserverReturn +export type UseElementSizeReturns = ElementSize & UseResizeObserverReturn ``` diff --git a/src/use-element-size/index.ts b/src/use-element-size/index.ts index 758d6a9a..b1e99b28 100644 --- a/src/use-element-size/index.ts +++ b/src/use-element-size/index.ts @@ -16,13 +16,13 @@ export interface ElementSize { height: number } -export interface UseElementSizeReturn extends ElementSize, UseResizeObserverReturn {} +export interface UseElementSizeReturns extends ElementSize, UseResizeObserverReturn {} export function useElementSize( target: ElementTarget, defaultValue: ElementSize = { width: 0, height: 0 }, options: UseResizeObserverOptions = {}, -): UseElementSizeReturn { +): UseElementSizeReturns { const el = useTargetElement(target) const [size, setSize] = useSafeState(defaultValue, { deep: true }) const { box = 'content-box' } = options diff --git a/src/use-event-bus/index.mdx b/src/use-event-bus/index.mdx index 363f94b1..fa735e5e 100644 --- a/src/use-event-bus/index.mdx +++ b/src/use-event-bus/index.mdx @@ -46,7 +46,7 @@ export type UseEventBusOptions = { /** * Whether to automatically clean up the listener when the component is unmounted. * - * @default true + * @defaultValue true */ autoCleanup?: boolean } @@ -55,7 +55,7 @@ export type UseEventBusOptions = { ### Returns ```tsx -export type UseEventBusReturn = { +export type UseEventBusReturns = { /** * Subscribe to an event. When calling emit, the listeners will execute. * diff --git a/src/use-event-bus/index.ts b/src/use-event-bus/index.ts index d025a250..7c4538b0 100644 --- a/src/use-event-bus/index.ts +++ b/src/use-event-bus/index.ts @@ -11,7 +11,7 @@ export type EventBusListener = (event: T, payload?: P) => void export type EventBusEvents = Set> export type EventBusIdentifier = symbol | string | number -export interface UseEventBusReturn { +export interface UseEventBusReturns { /** * Subscribe to an event. When calling emit, the listeners will execute. * @@ -52,7 +52,7 @@ export interface UseEventBusOptions { /** * Whether to automatically clean up the listener when the component is unmounted. * - * @default true + * @defaultValue true */ autoCleanup?: boolean } @@ -64,7 +64,7 @@ export const events = /* #__PURE__ */ new Map( key: EventBusIdentifier, options: UseEventBusOptions = {}, -): UseEventBusReturn { +): UseEventBusReturns { const { autoCleanup = true } = options const cleanups = useRef([]) diff --git a/src/use-eye-dropper/index.mdx b/src/use-eye-dropper/index.mdx index 4b031e3f..67e4d63e 100644 --- a/src/use-eye-dropper/index.mdx +++ b/src/use-eye-dropper/index.mdx @@ -38,19 +38,19 @@ export type UseEyeDropperOptions = { /** * Initial sRGBHex. * - * @default '' + * @defaultValue '' */ initialValue?: string /** * Whether to convert the sRGBHex to uppercase. * - * @default true + * @defaultValue true */ upperCase?: boolean /** * Whether to keep the leading hash in the sRGBHex. * - * @default true + * @defaultValue true */ keepLeadingHash?: boolean } @@ -59,7 +59,7 @@ export type UseEyeDropperOptions = { ### Returns ```tsx -export type UseEyeDropperReturn = { +export type UseEyeDropperReturns = { /** * Whether the EyeDropper is supported. */ diff --git a/src/use-eye-dropper/index.ts b/src/use-eye-dropper/index.ts index 2a2fac54..5761f617 100644 --- a/src/use-eye-dropper/index.ts +++ b/src/use-eye-dropper/index.ts @@ -30,24 +30,24 @@ export interface UseEyeDropperOptions { /** * Initial sRGBHex. * - * @default '' + * @defaultValue '' */ initialValue?: string /** * Whether to convert the sRGBHex to uppercase. * - * @default true + * @defaultValue true */ upperCase?: boolean /** * Whether to keep the leading hash in the sRGBHex. * - * @default true + * @defaultValue true */ keepLeadingHash?: boolean } -export interface UseEyeDropperReturn { +export interface UseEyeDropperReturns { /** * Whether the EyeDropper is supported. */ @@ -64,7 +64,7 @@ export interface UseEyeDropperReturn { open: (options?: EyeDropperOpenOptions) => Promise } -export function useEyeDropper(options: UseEyeDropperOptions = {}): UseEyeDropperReturn { +export function useEyeDropper(options: UseEyeDropperOptions = {}): UseEyeDropperReturns { const { initialValue = '', upperCase = true, keepLeadingHash = true } = options const isSupported = useSupported(() => 'EyeDropper' in window) diff --git a/src/use-favicon/index.mdx b/src/use-favicon/index.mdx index cff6fb28..c47082ed 100644 --- a/src/use-favicon/index.mdx +++ b/src/use-favicon/index.mdx @@ -42,19 +42,19 @@ export type UseFaviconOptions = { /** * Base URL for the favicon * - * @default '' + * @defaultValue '' */ baseUrl?: string /** * The rel attribute of the favicon link element * - * @default 'icon' + * @defaultValue 'icon' */ rel?: string /** * Sync the favicon in document `` to hooks state on mount * - * @default true + * @defaultValue true */ syncOnMount?: boolean } @@ -63,7 +63,7 @@ export type UseFaviconOptions = { ### Returns ```tsx -export type UseFaviconReturn = [ +export type UseFaviconReturns = readonly [ /** * The current favicon URL */ diff --git a/src/use-favicon/index.ts b/src/use-favicon/index.ts index e9916794..fa204cd9 100644 --- a/src/use-favicon/index.ts +++ b/src/use-favicon/index.ts @@ -16,24 +16,24 @@ export interface UseFaviconOptions { /** * Base URL for the favicon * - * @default '' + * @defaultValue '' */ baseUrl?: string /** * The rel attribute of the favicon link element * - * @default 'icon' + * @defaultValue 'icon' */ rel?: string /** * Sync the favicon in document `` to hooks state on mount * - * @default true + * @defaultValue true */ syncOnMount?: boolean } -export type UseFaviconReturn = [ +export type UseFaviconReturns = readonly [ /** * The current favicon URL */ @@ -71,7 +71,7 @@ function getFavicon(rel = 'icon'): string | null { return href } -export function useFavicon(newIcon: FaviconType = null, options: UseFaviconOptions = {}): UseFaviconReturn { +export function useFavicon(newIcon: FaviconType = null, options: UseFaviconOptions = {}): UseFaviconReturns { const { baseUrl = '', rel = 'icon', syncOnMount = true } = options const [favicon, setFavicon] = useSafeState(newIcon ?? null) const previousFavicon = usePrevious(favicon) diff --git a/src/use-focus/index.mdx b/src/use-focus/index.mdx index e18dd09d..1fdc7624 100644 --- a/src/use-focus/index.mdx +++ b/src/use-focus/index.mdx @@ -44,7 +44,7 @@ export type UseFocusOptions = { /** * initial focus state * - * @default false + * @defaultValue false */ initialValue?: boolean } @@ -53,7 +53,7 @@ export type UseFocusOptions = { ### Returns ```tsx -export type UseFocusReturn = [ +export type UseFocusReturns = readonly [ /** * focus state */ diff --git a/src/use-focus/index.ts b/src/use-focus/index.ts index fb56069d..82b750ea 100644 --- a/src/use-focus/index.ts +++ b/src/use-focus/index.ts @@ -11,12 +11,12 @@ export interface UseFocusOptions { /** * initial focus state * - * @default false + * @defaultValue false */ initialValue?: boolean } -export type UseFocusReturn = [ +export type UseFocusReturns = readonly [ /** * focus state */ @@ -39,7 +39,7 @@ export type UseFocusReturn = [ export function useFocus( target: ElementTarget, options: UseFocusOptions = {}, -): UseFocusReturn { +): UseFocusReturns { const el = useTargetElement(target) const [focused, setFocused] = useSafeState(options?.initialValue ?? false) diff --git a/src/use-fps/index.mdx b/src/use-fps/index.mdx index 6853792a..cb42d6f4 100644 --- a/src/use-fps/index.mdx +++ b/src/use-fps/index.mdx @@ -38,7 +38,7 @@ export type UseFpsOptions = { /** * Calculate the FPS on every x frames. * - * @default 10 + * @defaultValue 10 */ every?: number } @@ -51,7 +51,7 @@ import { Tip } from '@/components' ```tsx -export type UseFpsReturn = Pausable & { +export type UseFpsReturns = Pausable & { /** * Current FPS */ diff --git a/src/use-fps/index.ts b/src/use-fps/index.ts index f79f8575..d1fc9385 100644 --- a/src/use-fps/index.ts +++ b/src/use-fps/index.ts @@ -9,19 +9,19 @@ export interface UseFpsOptions { /** * Calculate the FPS on every x frames. * - * @default 10 + * @defaultValue 10 */ every?: number } -export interface UseFpsReturn extends Pausable { +export interface UseFpsReturns extends Pausable { /** * Current FPS */ fps: number } -export function useFps(options: UseFpsOptions = {}): UseFpsReturn { +export function useFps(options: UseFpsOptions = {}): UseFpsReturns { const { every = 10 } = options const last = useRef() diff --git a/src/use-fullscreen/index.mdx b/src/use-fullscreen/index.mdx index 2677ba37..e2ff345a 100644 --- a/src/use-fullscreen/index.mdx +++ b/src/use-fullscreen/index.mdx @@ -44,7 +44,7 @@ export type UseFullscreenOptions = { /** * Automatically exit fullscreen when component is unmounted * - * @default false + * @defaultValue false */ autoExit?: boolean } @@ -53,7 +53,7 @@ export type UseFullscreenOptions = { ### Returns ```tsx -export type UseFullscreenReturn = { +export interface UseFullscreenReturns { /** * Whether the browser supports fullscreen API */ @@ -66,10 +66,6 @@ export type UseFullscreenReturn = { * Whether the element itself is in fullscreen mode */ isSelfFullscreen: boolean - /** - * The element that is currently in fullscreen mode - */ - fullscreenElement: Element | null /** * Enter fullscreen mode */ diff --git a/src/use-fullscreen/index.ts b/src/use-fullscreen/index.ts index a07d0b0c..e8e7fb7d 100644 --- a/src/use-fullscreen/index.ts +++ b/src/use-fullscreen/index.ts @@ -15,12 +15,12 @@ export interface UseFullscreenOptions { /** * Automatically exit fullscreen when component is unmounted * - * @default false + * @defaultValue false */ autoExit?: boolean } -export interface UseFullscreenReturn { +export interface UseFullscreenReturns { /** * Whether the browser supports fullscreen API */ @@ -33,10 +33,6 @@ export interface UseFullscreenReturn { * Whether the element itself is in fullscreen mode */ isSelfFullscreen: boolean - /** - * The element that is currently in fullscreen mode - */ - fullscreenElement: Element | null /** * Enter fullscreen mode */ @@ -51,7 +47,10 @@ export interface UseFullscreenReturn { toggle(): Promise } -export function useFullscreen(target: ElementTarget = 'html', options: UseFullscreenOptions = {}) { +export function useFullscreen( + target: ElementTarget = 'html', + options: UseFullscreenOptions = {}, +): UseFullscreenReturns { const { autoExit = false } = options const el = useTargetElement(target) diff --git a/src/use-geolocation/index.mdx b/src/use-geolocation/index.mdx index 8d40f670..15a7b766 100644 --- a/src/use-geolocation/index.mdx +++ b/src/use-geolocation/index.mdx @@ -49,7 +49,7 @@ export type UseGeolocationOptions = PositionOptions & { /** * Whether to start watching the geolocation immediately. * - * @default true + * @defaultValue true */ immediate?: boolean } @@ -58,7 +58,7 @@ export type UseGeolocationOptions = PositionOptions & { ### Returns ```tsx -export type UseGeolocationReturn = Pausable & { +export type UseGeolocationReturns = Pausable & { /** * Whether the geolocation API is supported. */ diff --git a/src/use-geolocation/index.ts b/src/use-geolocation/index.ts index 904a0a11..24f5fe08 100644 --- a/src/use-geolocation/index.ts +++ b/src/use-geolocation/index.ts @@ -10,12 +10,12 @@ export interface UseGeolocationOptions extends PositionOptions { /** * Whether to start watching the geolocation immediately. * - * @default true + * @defaultValue true */ immediate?: boolean } -export interface UseGeolocationReturn extends Pausable { +export interface UseGeolocationReturns extends Pausable { /** * Whether the geolocation API is supported. */ @@ -46,7 +46,7 @@ export interface UseGeolocationReturn extends Pausable { coords: GeolocationPosition['coords'] } -export function useGeolocation(options: UseGeolocationOptions = {}): UseGeolocationReturn { +export function useGeolocation(options: UseGeolocationOptions = {}): UseGeolocationReturns { const { enableHighAccuracy = true, maximumAge = 30000, timeout = 27000, immediate = true } = options const pausable = usePausable( diff --git a/src/use-infinite-scroll/index.mdx b/src/use-infinite-scroll/index.mdx index f60a7f8a..7a8a17a6 100644 --- a/src/use-infinite-scroll/index.mdx +++ b/src/use-infinite-scroll/index.mdx @@ -48,19 +48,19 @@ export type UseInfiniteScrollOptions = UseS /** * distance from the bottom of the scroll container * - * @default 0 + * @defaultValue 0 */ distance?: number /** * scroll direction * - * @default 'bottom' + * @defaultValue 'bottom' */ direction?: 'top' | 'bottom' | 'left' | 'right' /** * interval between each scroll event * - * @default 100 + * @defaultValue 100 */ interval?: number /** diff --git a/src/use-infinite-scroll/index.ts b/src/use-infinite-scroll/index.ts index be6f4dc9..2a2c0a0e 100644 --- a/src/use-infinite-scroll/index.ts +++ b/src/use-infinite-scroll/index.ts @@ -13,25 +13,25 @@ export interface UseInfiniteScrollOptions extends UseScrollOptions { /** * Whether to trigger the first load immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * distance from the bottom of the scroll container * - * @default 0 + * @defaultValue 0 */ distance?: number /** * scroll direction * - * @default 'bottom' + * @defaultValue 'bottom' */ direction?: 'top' | 'bottom' | 'left' | 'right' /** * interval between each scroll event * - * @default 100 + * @defaultValue 100 */ interval?: number /** diff --git a/src/use-intersection-observer/index.mdx b/src/use-intersection-observer/index.mdx index 5afff966..4f3ca26e 100644 --- a/src/use-intersection-observer/index.mdx +++ b/src/use-intersection-observer/index.mdx @@ -46,7 +46,7 @@ export interface UseWebObserverOptions { /** * Start the observer immediate after calling this function * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-interval-fn/index.mdx b/src/use-interval-fn/index.mdx index 17c18c17..fe83ec69 100644 --- a/src/use-interval-fn/index.mdx +++ b/src/use-interval-fn/index.mdx @@ -46,13 +46,13 @@ export interface UseIntervalFnOptions { /** * Whether to start the interval immediately on mounted * - * @default true + * @defaultValue true */ immediate?: boolean /** * Whether to execute the callback immediately before the interval starts * - * @default false + * @defaultValue false */ immediateCallback?: boolean } diff --git a/src/use-interval/index.mdx b/src/use-interval/index.mdx index 67f72c99..26fe1d70 100644 --- a/src/use-interval/index.mdx +++ b/src/use-interval/index.mdx @@ -46,13 +46,13 @@ export type UseIntervalOptions = { /** * Expose more controls * - * @default false + * @defaultValue false */ controls?: Controls /** * Execute the update immediately on calling * - * @default true + * @defaultValue true */ immediate?: boolean /** @@ -69,7 +69,7 @@ import { Tip } from '@/components' ```tsx -export interface UseIntervalAction extends UseCounterReturnAction, Pausable { +export interface UseIntervalAction extends UseCounterReturnsAction, Pausable { /** * Reset the count */ diff --git a/src/use-interval/index.ts b/src/use-interval/index.ts index 79fe3a52..a1fb88ca 100644 --- a/src/use-interval/index.ts +++ b/src/use-interval/index.ts @@ -2,7 +2,7 @@ import { useCounter } from '../use-counter' import { useIntervalFn } from '../use-interval-fn' import { useLatest } from '../use-latest' -import type { UseCounterReturnAction } from '../use-counter' +import type { UseCounterReturnsAction } from '../use-counter' import type { UseIntervalFnInterval } from '../use-interval-fn' import type { Pausable } from '../use-pausable' @@ -10,13 +10,13 @@ export interface UseIntervalOptions { /** * Expose more controls * - * @default false + * @defaultValue false */ controls?: Controls /** * Execute the update immediately on calling * - * @default true + * @defaultValue true */ immediate?: boolean /** @@ -25,7 +25,7 @@ export interface UseIntervalOptions { callback?: (count: number) => void } -export interface UseIntervalAction extends UseCounterReturnAction, Pausable { +export interface UseIntervalAction extends UseCounterReturnsAction, Pausable { /** * Reset the count, optionally set a new count */ diff --git a/src/use-key-modifier/index.mdx b/src/use-key-modifier/index.mdx index 6900cd34..db74757b 100644 --- a/src/use-key-modifier/index.mdx +++ b/src/use-key-modifier/index.mdx @@ -56,13 +56,13 @@ export type UseModifierOptions = { /** * Event names that will prompt update to modifier states * - * @default ['mousedown', 'mouseup', 'keydown', 'keyup'] + * @defaultValue ['mousedown', 'mouseup', 'keydown', 'keyup'] */ events?: WindowEventName[] /** * Initial value of the returned ref * - * @default null + * @defaultValue null */ initial?: Initial } diff --git a/src/use-key-modifier/index.ts b/src/use-key-modifier/index.ts index 588a8085..61d4b497 100644 --- a/src/use-key-modifier/index.ts +++ b/src/use-key-modifier/index.ts @@ -24,13 +24,13 @@ export interface UseModifierOptions { /** * Event names that will prompt update to modifier states * - * @default ['mousedown', 'mouseup', 'keydown', 'keyup'] + * @defaultValue ['mousedown', 'mouseup', 'keydown', 'keyup'] */ events?: WindowEventName[] /** * Initial value of the returned ref * - * @default null + * @defaultValue null */ initial?: Initial } diff --git a/src/use-key-stroke/index.mdx b/src/use-key-stroke/index.mdx index 0351c158..b6e71cd3 100644 --- a/src/use-key-stroke/index.mdx +++ b/src/use-key-stroke/index.mdx @@ -62,25 +62,25 @@ export type UseKeyStrokeOptions = { /** * The event name to listen for. * - * @default 'keydown' + * @defaultValue 'keydown' */ eventName?: KeyStrokeEventName /** * The target to add the event listener to. * - * @default window + * @defaultValue window */ target?: ElementTarget /** * Set to `true` to use passive event listeners. * - * @default false + * @defaultValue false */ passive?: boolean /** * Set to `true` to ignore repeated events when the key is being held down. * - * @default false + * @defaultValue false */ dedupe?: boolean } diff --git a/src/use-key-stroke/index.ts b/src/use-key-stroke/index.ts index 0eb4dd01..9598d341 100644 --- a/src/use-key-stroke/index.ts +++ b/src/use-key-stroke/index.ts @@ -16,31 +16,31 @@ export interface UseKeyStrokeOptions { /** * The event name to listen for. * - * @default 'keydown' + * @defaultValue 'keydown' */ eventName?: KeyStrokeEventName /** * The target to add the event listener to. * - * @default window + * @defaultValue window */ target?: ElementTarget /** * Set to `true` to use passive event listeners. * - * @default false + * @defaultValue false */ passive?: boolean /** * Set to `true` to ignore repeated events when the key is being held down. * - * @default false + * @defaultValue false */ dedupe?: boolean /** * Set to `true` to remove the event listener after the first event. * - * @default false + * @defaultValue false */ once?: boolean } diff --git a/src/use-last-updated/index.mdx b/src/use-last-updated/index.mdx index 4ddd9ad6..d58bfbd9 100644 --- a/src/use-last-updated/index.mdx +++ b/src/use-last-updated/index.mdx @@ -42,13 +42,13 @@ export type UseLastChangedOptions = { /** * The initial value of the last updated timestamp. * - * @default null + * @defaultValue null */ initialValue?: number | null /** * If `true`, deep compares the source object. * - * @default false + * @defaultValue false */ deep?: boolean } diff --git a/src/use-last-updated/index.ts b/src/use-last-updated/index.ts index cbca2934..ad5d9b48 100644 --- a/src/use-last-updated/index.ts +++ b/src/use-last-updated/index.ts @@ -7,13 +7,13 @@ export interface UseLastChangedOptions { /** * The initial value of the last updated timestamp. * - * @default null + * @defaultValue null */ initialValue?: number | null /** * If `true`, deep compares the source object. * - * @default false + * @defaultValue false */ deep?: boolean } diff --git a/src/use-loading-fn/index.ts b/src/use-loading-fn/index.ts index e99a5801..1c582a9d 100644 --- a/src/use-loading-fn/index.ts +++ b/src/use-loading-fn/index.ts @@ -1,10 +1,10 @@ import { useAsyncFn } from '../use-async-fn' -import type { UseAsyncFnOptions, UseAsyncFnReturn } from '../use-async-fn' +import type { UseAsyncFnOptions, UseAsyncFnReturns } from '../use-async-fn' import type { AnyFunc } from '../utils/basic' export interface UseLoadingFnOptions extends UseAsyncFnOptions {} -export interface UseLoadingFnReturn extends UseAsyncFnReturn {} +export interface UseLoadingFnReturn extends UseAsyncFnReturns {} export function useLoadingFn(fn: T, options: UseLoadingFnOptions = {}): UseLoadingFnReturn { return useAsyncFn(fn, options) diff --git a/src/use-long-press/index.mdx b/src/use-long-press/index.mdx index a71d18eb..4b772abf 100644 --- a/src/use-long-press/index.mdx +++ b/src/use-long-press/index.mdx @@ -73,7 +73,7 @@ export interface UseLongPressOptions { /** * Time in ms till `longpress` gets called * - * @default 500 + * @defaultValue 500 */ delay?: number /** @@ -84,7 +84,7 @@ export interface UseLongPressOptions { * Allowance of moving distance in pixels, * The action will get canceled When moving too far from the pointerdown position. * - * @default 10 + * @defaultValue 10 */ distanceThreshold?: number | false } diff --git a/src/use-long-press/index.ts b/src/use-long-press/index.ts index a52f0b65..3c5b3136 100644 --- a/src/use-long-press/index.ts +++ b/src/use-long-press/index.ts @@ -38,7 +38,7 @@ export interface UseLongPressOptions { /** * Time in ms till `longpress` gets called * - * @default 500 + * @defaultValue 500 */ delay?: number /** @@ -49,7 +49,7 @@ export interface UseLongPressOptions { * Allowance of moving distance in pixels, * The action will get canceled When moving too far from the pointerdown position. * - * @default 10 + * @defaultValue 10 */ distanceThreshold?: number | false } diff --git a/src/use-lorem-ipsum/index.mdx b/src/use-lorem-ipsum/index.mdx index 656467f0..12070d42 100644 --- a/src/use-lorem-ipsum/index.mdx +++ b/src/use-lorem-ipsum/index.mdx @@ -43,19 +43,19 @@ export interface UseLoremIpsumOptions { /** * The length of the generated text sentence. * - * @default 1 + * @defaultValue 1 */ length?: number /** * The minimum number of words in a sentence. * - * @default ['.', '!', '?'] + * @defaultValue ['.', '!', '?'] */ sentenceEnds?: string[] /** * Whether to keep the same value between renders. * - * @default true + * @defaultValue true */ stable?: boolean } diff --git a/src/use-lorem-ipsum/index.ts b/src/use-lorem-ipsum/index.ts index d1b9a59b..f913f1b1 100644 --- a/src/use-lorem-ipsum/index.ts +++ b/src/use-lorem-ipsum/index.ts @@ -8,19 +8,19 @@ export interface UseLoremIpsumOptions { /** * The length of the generated text sentence. * - * @default 1 + * @defaultValue 1 */ length?: number /** * The minimum number of words in a sentence. * - * @default ['.', '!', '?'] + * @defaultValue ['.', '!', '?'] */ sentenceEnds?: string[] /** * Whether to keep the same value between renders. * - * @default true + * @defaultValue true */ stable?: boolean } diff --git a/src/use-manual-state-history/index.mdx b/src/use-manual-state-history/index.mdx index 2b92c18a..0a7acd2d 100644 --- a/src/use-manual-state-history/index.mdx +++ b/src/use-manual-state-history/index.mdx @@ -44,37 +44,37 @@ export type UseManualStateHistoryOptions = { /** * The capacity of the history records * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ capacity?: number /** * Whether to clone the source state * - * @default false + * @defaultValue false */ clone?: boolean | CloneFn /** * The throttle options * - * @default undefined + * @defaultValue undefined */ throttle?: number | UseThrottledFnOptions /** * The debounce options * - * @default undefined + * @defaultValue undefined */ debounce?: number | UseDebouncedFnOptions /** * The dump function to serialize the source state * - * @default (v) => v + * @defaultValue (v) => v */ dump?: (v: Raw) => Serialized /** * The parse function to deserialize the serialized state * - * @default (v) => v + * @defaultValue (v) => v */ parse?: (v: Serialized) => Raw } diff --git a/src/use-manual-state-history/index.ts b/src/use-manual-state-history/index.ts index 4dc88adf..9485a170 100644 --- a/src/use-manual-state-history/index.ts +++ b/src/use-manual-state-history/index.ts @@ -25,37 +25,37 @@ export interface UseManualStateHistoryOptions { /** * The capacity of the history records * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ capacity?: number /** * Whether to clone the source state * - * @default false + * @defaultValue false */ clone?: boolean | CloneFn /** * The throttle options * - * @default undefined + * @defaultValue undefined */ throttle?: number | UseThrottledFnOptions /** * The debounce options * - * @default undefined + * @defaultValue undefined */ debounce?: number | UseDebouncedFnOptions /** * The dump function to serialize the source state * - * @default (v) => v + * @defaultValue (v) => v */ dump?: (v: Raw) => Serialized /** * The parse function to deserialize the serialized state * - * @default (v) => v + * @defaultValue (v) => v */ parse?: (v: Serialized) => Raw } diff --git a/src/use-memoize/index.mdx b/src/use-memoize/index.mdx index d4e72b8f..381bd81a 100644 --- a/src/use-memoize/index.mdx +++ b/src/use-memoize/index.mdx @@ -52,13 +52,13 @@ export type UseMemoizeOptions = { /** * Custom cache key generator * - * @default JSON.stringify(args) + * @defaultValue JSON.stringify(args) */ getKey?: (...args: Args) => string | number /** * Custom cache, can be a Map or other object that implements the cache interface * - * @default new Map() + * @defaultValue new Map() */ cache?: UseMemoizeCache } diff --git a/src/use-memoize/index.ts b/src/use-memoize/index.ts index dd761138..7feed106 100644 --- a/src/use-memoize/index.ts +++ b/src/use-memoize/index.ts @@ -16,13 +16,13 @@ export interface UseMemoizeOptions { /** * Custom cache key generator * - * @default JSON.stringify(args) + * @defaultValue JSON.stringify(args) */ getKey?: (...args: Args) => string | number /** * Custom cache, can be a Map or other object that implements the cache interface * - * @default new Map() + * @defaultValue new Map() */ cache?: UseMemoizeCache } diff --git a/src/use-mouse-pressed/index.mdx b/src/use-mouse-pressed/index.mdx index 28ea5759..5842be25 100644 --- a/src/use-mouse-pressed/index.mdx +++ b/src/use-mouse-pressed/index.mdx @@ -44,13 +44,13 @@ export type MousePressedOptions = { /** * Whether to listen to touch events * - * @default true + * @defaultValue true */ touch?: boolean /** * Whether to listen to drag events * - * @default true + * @defaultValue true */ drag?: boolean /** @@ -58,19 +58,19 @@ export type MousePressedOptions = { * * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture MDN - addEventListener#capture} * - * @default false + * @defaultValue false */ capture?: boolean /** * The initial value of the mouse press * - * @default false + * @defaultValue false */ initialValue?: boolean /** * The target element * - * @default () => window + * @defaultValue () => window */ target?: ElementTarget } diff --git a/src/use-mouse-pressed/index.ts b/src/use-mouse-pressed/index.ts index 6cc22059..c21dc1d6 100644 --- a/src/use-mouse-pressed/index.ts +++ b/src/use-mouse-pressed/index.ts @@ -9,13 +9,13 @@ export interface MousePressedOptions { /** * Whether to listen to touch events * - * @default true + * @defaultValue true */ touch?: boolean /** * Whether to listen to drag events * - * @default true + * @defaultValue true */ drag?: boolean /** @@ -23,13 +23,13 @@ export interface MousePressedOptions { * * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture MDN - addEventListener#capture} * - * @default false + * @defaultValue false */ capture?: boolean /** * The initial value of the mouse press * - * @default false + * @defaultValue false */ initialValue?: boolean } diff --git a/src/use-mouse/index.mdx b/src/use-mouse/index.mdx index cb17b5a5..67076827 100644 --- a/src/use-mouse/index.mdx +++ b/src/use-mouse/index.mdx @@ -40,43 +40,43 @@ export type UseMouseOptions = { /** * Type of coordinates to extract from the event. * - * @default 'page' + * @defaultValue 'page' */ type?: UseMouseCoordType | UseMouseEventExtractor /** * Target element to attach the event listeners. * - * @default window + * @defaultValue window */ target?: Window | EventTarget | null | undefined /** * Whether to enable touch events. * - * @default true + * @defaultValue true */ touch?: boolean /** * Whether to enable scroll events. * - * @default true + * @defaultValue true */ scroll?: boolean /** * Whether to reset the position when touch ends. * - * @default false + * @defaultValue false */ resetOnTouchEnds?: boolean /** * Initial position. * - * @default { x: 0, y: 0 } + * @defaultValue { x: 0, y: 0 } */ initialValue?: Position /** * Whether to start to update position immediately. * - * @default false + * @defaultValue false */ immediate?: boolean } diff --git a/src/use-mouse/index.ts b/src/use-mouse/index.ts index 249f4232..c61da335 100644 --- a/src/use-mouse/index.ts +++ b/src/use-mouse/index.ts @@ -17,43 +17,43 @@ export interface UseMouseOptions { /** * Type of coordinates to extract from the event. * - * @default 'page' + * @defaultValue 'page' */ type?: UseMouseCoordType | UseMouseEventExtractor /** * Target element to attach the event listeners. * - * @default window + * @defaultValue window */ target?: Window | EventTarget | null | undefined /** * Whether to enable touch events. * - * @default true + * @defaultValue true */ touch?: boolean /** * Whether to enable scroll events. * - * @default true + * @defaultValue true */ scroll?: boolean /** * Whether to reset the position when touch ends. * - * @default false + * @defaultValue false */ resetOnTouchEnds?: boolean /** * Initial position. * - * @default { x: 0, y: 0 } + * @defaultValue { x: 0, y: 0 } */ initialValue?: Position /** * Whether to start to update position immediately. * - * @default false + * @defaultValue false */ immediate?: boolean } diff --git a/src/use-now/index.mdx b/src/use-now/index.mdx index 5ca9dbc7..14e45f85 100644 --- a/src/use-now/index.mdx +++ b/src/use-now/index.mdx @@ -39,13 +39,13 @@ export type UseNowOptions = UseIntervalFnOptions & { /** * Expose more controls * - * @default false + * @defaultValue false */ controls?: Controls /** * Update interval in milliseconds, or use requestAnimationFrame * - * @default requestAnimationFrame + * @defaultValue requestAnimationFrame */ interval?: UseIntervalFnInterval /** diff --git a/src/use-now/index.ts b/src/use-now/index.ts index 2cc0a27d..68506a72 100644 --- a/src/use-now/index.ts +++ b/src/use-now/index.ts @@ -9,13 +9,13 @@ export interface UseNowOptions extends UseIntervalFnOp /** * Expose more controls * - * @default false + * @defaultValue false */ controls?: Controls /** * Update interval in milliseconds, or use requestAnimationFrame * - * @default requestAnimationFrame + * @defaultValue requestAnimationFrame */ interval?: UseIntervalFnInterval /** diff --git a/src/use-pagination/demo.tsx b/src/use-pagination/demo.tsx index eda63915..ed4d3100 100644 --- a/src/use-pagination/demo.tsx +++ b/src/use-pagination/demo.tsx @@ -7,7 +7,7 @@ export function App() { const pageSizeInput = useControlledComponent('20') const targetPageInput = useControlledComponent('1') - const pagination = usePagination({ pageSize: +pageSizeInput.value, total: TOTAL }) + const [state, actions] = usePagination({ pageSize: +pageSizeInput.value, total: TOTAL }) return ( @@ -16,27 +16,27 @@ export function App() { - - + + - - {Array.from({ length: pagination.pageCount }, (_, i) => { - const isCurrent = i + 1 === pagination.currentPage + {Array.from({ length: state.pageCount }, (_, i) => { + const isCurrent = i + 1 === state.currentPage return ( // biome-ignore lint/suspicious/noArrayIndexKey: for demo - ) })} - - + ) diff --git a/src/use-pagination/index.mdx b/src/use-pagination/index.mdx index d625e55c..d3bfb4ac 100644 --- a/src/use-pagination/index.mdx +++ b/src/use-pagination/index.mdx @@ -28,7 +28,7 @@ import { Source } from '@/components' ## API ```tsx -const pagination = usePagination(options) +const [state, actions] = usePagination(options) ``` ### Options @@ -38,19 +38,19 @@ export type UsePaginationOptions = { /** * Total number of items. * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ total?: number /** * The number of items to display per page. * - * @default 10 + * @defaultValue 10 */ pageSize?: number /** * The current page number. * - * @default 1 + * @defaultValue 1 */ page?: number /** @@ -71,7 +71,7 @@ export type UsePaginationOptions = { ### Returns ```tsx -export type PaginationInfo = { +export interface PaginationInfo { /** * Total number of items. */ @@ -96,23 +96,37 @@ export type PaginationInfo = { * Whether the current page is the last page. */ isLastPage: boolean - /** - * Go to the previous page. - */ - prev(): void - /** - * Go to the next page. - */ - next(): void - /** - * Go to the specified page. - */ - go: (page: number) => void - /** - * Set the number of items to display per page. - */ - setPageSize: (size: number) => void } -export type UsePaginationReturn = PaginationInfo +export type UsePaginationReturns = [ + PaginationInfo, + { + /** + * Go to the previous page. + * + * @returns {void} `void` + */ + prev(): void + /** + * Go to the next page. + * + * @returns {void} `void` + */ + next(): void + /** + * Go to the specified page. + * + * @param {number} page - `number`, the page number + * @returns {void} `void` + */ + go: (page: number) => void + /** + * Set the number of items to display per page. + * + * @param {number} size - `number`, the number of items to display per page + * @returns {void} `void` + */ + setPageSize: (size: number) => void + }, +] ``` diff --git a/src/use-pagination/index.ts b/src/use-pagination/index.ts index 5e87b425..ae98b893 100644 --- a/src/use-pagination/index.ts +++ b/src/use-pagination/index.ts @@ -1,4 +1,5 @@ import { useClamp } from '../use-clamp' +import { useCreation } from '../use-creation' import { useLatest } from '../use-latest' import { useStableFn } from '../use-stable-fn' import { useUpdateEffect } from '../use-update-effect' @@ -7,31 +8,40 @@ export interface UsePaginationOptions { /** * Total number of items. * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ total?: number /** * The number of items to display per page. * - * @default 10 + * @defaultValue 10 */ pageSize?: number /** * The current page number. * - * @default 1 + * @defaultValue 1 */ page?: number /** * Callback when the `page` change. + * + * @param {PaginationInfo} pagination - `PaginationInfo`, the pagination info + * @returns {void} `void` */ onPageChange?: (pagination: PaginationInfo) => void /** * Callback when the `pageSize` change. + * + * @param {PaginationInfo} pagination - `PaginationInfo`, the pagination info + * @returns {void} `void` */ onPageSizeChange?: (pagination: PaginationInfo) => void /** * Callback when the `pageCount` change. + * + * @param {PaginationInfo} pagination - `PaginationInfo`, the pagination info + * @returns {void} `void` */ onPageCountChange?: (pagination: PaginationInfo) => void } @@ -61,27 +71,41 @@ export interface PaginationInfo { * Whether the current page is the last page. */ isLastPage: boolean - /** - * Go to the previous page. - */ - prev(): void - /** - * Go to the next page. - */ - next(): void - /** - * Go to the specified page. - */ - go: (page: number) => void - /** - * Set the number of items to display per page. - */ - setPageSize: (size: number) => void } -export interface UsePaginationReturn extends PaginationInfo {} +export type UsePaginationReturns = [ + PaginationInfo, + { + /** + * Go to the previous page. + * + * @returns {void} `void` + */ + prev(): void + /** + * Go to the next page. + * + * @returns {void} `void` + */ + next(): void + /** + * Go to the specified page. + * + * @param {number} page - `number`, the page number + * @returns {void} `void` + */ + go: (page: number) => void + /** + * Set the number of items to display per page. + * + * @param {number} size - `number`, the number of items to display per page + * @returns {void} `void` + */ + setPageSize: (size: number) => void + }, +] -export function usePagination(options: UsePaginationOptions = {}): UsePaginationReturn { +export function usePagination(options: UsePaginationOptions = {}): UsePaginationReturns { const { total = Number.POSITIVE_INFINITY, page = 1, @@ -108,18 +132,9 @@ export function usePagination(options: UsePaginationOptions = {}): UsePagination pageCount, isFirstPage: currentPage === 1, isLastPage: isInfinity ? false : currentPage === pageCount, - go, - prev, - next, - setPageSize, } - const latest = useLatest({ - pagination, - onPageChange, - onPageCountChange, - onPageSizeChange, - }) + const latest = useLatest({ pagination, onPageChange, onPageCountChange, onPageSizeChange }) // biome-ignore lint/correctness/useExhaustiveDependencies: effect need to re-run when currentPage changes useUpdateEffect(() => { @@ -136,5 +151,7 @@ export function usePagination(options: UsePaginationOptions = {}): UsePagination latest.current.onPageSizeChange?.(latest.current.pagination) }, [currentPageSize]) - return pagination + const actions = useCreation(() => ({ go, prev, next, setPageSize })) + + return [pagination, actions] as const } diff --git a/src/use-performance-observer/index.mdx b/src/use-performance-observer/index.mdx index 2124dc04..3b81fbc0 100644 --- a/src/use-performance-observer/index.mdx +++ b/src/use-performance-observer/index.mdx @@ -44,7 +44,7 @@ export type UseWebObserverOptions = { /** * Start the observer immediate after calling this function * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-permission/index.mdx b/src/use-permission/index.mdx index 7aed2b97..356d7d9d 100644 --- a/src/use-permission/index.mdx +++ b/src/use-permission/index.mdx @@ -64,13 +64,13 @@ export type UsePermissionOptions = { /** * Expose more controls * - * @default false + * @defaultValue false */ controls?: Controls /** * Whether to query immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** diff --git a/src/use-permission/index.ts b/src/use-permission/index.ts index c24280bf..f9594e44 100644 --- a/src/use-permission/index.ts +++ b/src/use-permission/index.ts @@ -32,13 +32,13 @@ export interface UsePermissionOptions { /** * Expose more controls * - * @default false + * @defaultValue false */ controls?: Controls /** * Whether to query immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** diff --git a/src/use-raf-loop/index.ts b/src/use-raf-loop/index.ts index aa45900a..dda3e946 100644 --- a/src/use-raf-loop/index.ts +++ b/src/use-raf-loop/index.ts @@ -20,19 +20,19 @@ export interface UseRafLoopOptions { /** * The maximum fps limit * - * @default undefined + * @defaultValue undefined */ fpsLimit?: number /** * Whether to start the interval immediately on mounted * - * @default true + * @defaultValue true */ immediate?: boolean /** * Whether to execute the callback immediately before the interval starts * - * @default false + * @defaultValue false */ immediateCallback?: boolean } diff --git a/src/use-reporting-observer/index.mdx b/src/use-reporting-observer/index.mdx index d8b6abb6..6be6f9f6 100644 --- a/src/use-reporting-observer/index.mdx +++ b/src/use-reporting-observer/index.mdx @@ -42,7 +42,7 @@ export type UseWebObserverOptions = { /** * Start the observer immediate after calling this function * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-resize-observer/index.mdx b/src/use-resize-observer/index.mdx index 2cdad7b2..acfbf229 100644 --- a/src/use-resize-observer/index.mdx +++ b/src/use-resize-observer/index.mdx @@ -42,7 +42,7 @@ export interface UseWebObserverOptions { /** * Start the observer immediate after calling this function * - * @default true + * @defaultValue true */ immediate?: boolean } @@ -52,7 +52,7 @@ interface ResizeObserverOptions { } export interface UseResizeObserverOptions extends UseWebObserverOptions, ResizeObserverOptions { - /** @default 'content-box' */ + /** @defaultValue 'content-box' */ box?: ResizeObserverBoxOptions } ``` diff --git a/src/use-resize-observer/index.ts b/src/use-resize-observer/index.ts index 6ab24113..be955fee 100644 --- a/src/use-resize-observer/index.ts +++ b/src/use-resize-observer/index.ts @@ -6,7 +6,7 @@ import type { UseWebObserverOptions, UseWebObserverReturn } from '../use-web-obs import type { Arrayable } from '../utils/basic' export interface UseResizeObserverOptions extends UseWebObserverOptions, ResizeObserverOptions { - /** @default 'content-box' */ + /** @defaultValue 'content-box' */ box?: ResizeObserverBoxOptions } diff --git a/src/use-safe-state/index.mdx b/src/use-safe-state/index.mdx index a40de7e1..2e878b65 100644 --- a/src/use-safe-state/index.mdx +++ b/src/use-safe-state/index.mdx @@ -46,7 +46,7 @@ export type UseSafeStateOptions = { * Deeply compare the new state with the old state before updating. * If true, only update the state when the new state is different from the old state. * - * @default false + * @defaultValue false */ deep?: boolean } diff --git a/src/use-safe-state/index.ts b/src/use-safe-state/index.ts index ddc48e15..40362099 100644 --- a/src/use-safe-state/index.ts +++ b/src/use-safe-state/index.ts @@ -15,7 +15,7 @@ export type UseSafeStateOptions = { * Deeply compare the new state with the old state before updating. * If true, only update the state when the new state is different from the old state. * - * @default false + * @defaultValue false */ deep?: boolean } diff --git a/src/use-script-tag/index.mdx b/src/use-script-tag/index.mdx index 5c04650f..9a2ffa1a 100644 --- a/src/use-script-tag/index.mdx +++ b/src/use-script-tag/index.mdx @@ -56,55 +56,55 @@ export interface UseScriptTagOptions { /** * Whether load script immediately at mount * - * @default true + * @defaultValue true */ immediate?: boolean /** * Whether the script is loaded asynchronously * - * @default true + * @defaultValue true */ async?: boolean /** * The type of the script * - * @default 'text/javascript' + * @defaultValue 'text/javascript' */ type?: string /** * Whether to control the loading of the script manually * - * @default false + * @defaultValue false */ manual?: boolean /** * The value of the `crossorigin` attribute * - * @default undefined + * @defaultValue undefined */ crossOrigin?: 'anonymous' | 'use-credentials' /** * The value of the `referrerpolicy` attribute * - * @default undefined + * @defaultValue undefined */ referrerPolicy?: ReferrerPolicy /** * Whether the script is a module script * - * @default undefined + * @defaultValue undefined */ noModule?: boolean /** * Whether the script is deferred * - * @default false + * @defaultValue false */ defer?: boolean /** * Additional attributes to be set on the script element * - * @default {} + * @defaultValue {} */ attrs?: Record } diff --git a/src/use-script-tag/index.ts b/src/use-script-tag/index.ts index 872a0acc..58327c46 100644 --- a/src/use-script-tag/index.ts +++ b/src/use-script-tag/index.ts @@ -21,55 +21,55 @@ export interface UseScriptTagOptions { /** * Whether load script immediately at mount * - * @default true + * @defaultValue true */ immediate?: boolean /** * Whether the script is loaded asynchronously * - * @default true + * @defaultValue true */ async?: boolean /** * The type of the script * - * @default 'text/javascript' + * @defaultValue 'text/javascript' */ type?: string /** * Whether to control the loading of the script manually * - * @default false + * @defaultValue false */ manual?: boolean /** * The value of the `crossorigin` attribute * - * @default undefined + * @defaultValue undefined */ crossOrigin?: 'anonymous' | 'use-credentials' /** * The value of the `referrerpolicy` attribute * - * @default undefined + * @defaultValue undefined */ referrerPolicy?: ReferrerPolicy /** * Whether the script is a module script * - * @default undefined + * @defaultValue undefined */ noModule?: boolean /** * Whether the script is deferred * - * @default false + * @defaultValue false */ defer?: boolean /** * Additional attributes to be set on the script element * - * @default {} + * @defaultValue {} */ attrs?: Record } diff --git a/src/use-style-tag/index.mdx b/src/use-style-tag/index.mdx index 6d80bf3d..35de3083 100644 --- a/src/use-style-tag/index.mdx +++ b/src/use-style-tag/index.mdx @@ -44,19 +44,19 @@ export interface UseStyleTagOptions { /** * Whether to load the style tag immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * Whether to manually load the style tag * - * @default false + * @defaultValue false */ manual?: boolean /** * Unique identifier of the style tag * - * @default auto-incremented + * @defaultValue auto-incremented */ id?: string } diff --git a/src/use-style-tag/index.ts b/src/use-style-tag/index.ts index f5d03763..2f194622 100644 --- a/src/use-style-tag/index.ts +++ b/src/use-style-tag/index.ts @@ -52,19 +52,19 @@ export interface UseStyleTagOptions { /** * Whether to load the style tag immediately * - * @default true + * @defaultValue true */ immediate?: boolean /** * Whether to manually load the style tag * - * @default false + * @defaultValue false */ manual?: boolean /** * Unique identifier of the style tag * - * @default auto-incremented + * @defaultValue auto-incremented */ id?: string } diff --git a/src/use-text-direction/index.mdx b/src/use-text-direction/index.mdx index 3735fda2..7397afc3 100644 --- a/src/use-text-direction/index.mdx +++ b/src/use-text-direction/index.mdx @@ -38,13 +38,13 @@ export interface UseTextDirectionOptions { /** * The target element to set the text direction attribute. * - * @default 'html' + * @defaultValue 'html' */ target?: ElementTarget /** * The initial text direction value. * - * @default 'ltr' + * @defaultValue 'ltr' */ initialValue?: UseTextDirectionValue } diff --git a/src/use-text-direction/index.ts b/src/use-text-direction/index.ts index 3d272445..19fb7ab6 100644 --- a/src/use-text-direction/index.ts +++ b/src/use-text-direction/index.ts @@ -12,13 +12,13 @@ export interface UseTextDirectionOptions { /** * The target element to set the text direction attribute. * - * @default 'html' + * @defaultValue 'html' */ target?: ElementTarget /** * The initial text direction value. * - * @default 'ltr' + * @defaultValue 'ltr' */ initialValue?: UseTextDirectionValue } diff --git a/src/use-throttled-fn/index.mdx b/src/use-throttled-fn/index.mdx index 50216176..ade3476e 100644 --- a/src/use-throttled-fn/index.mdx +++ b/src/use-throttled-fn/index.mdx @@ -42,19 +42,19 @@ export type ThrottleOptions = { /** * time frame for throttle * - * @default 0 + * @defaultValue 0 */ wait?: number /** * whether to invoke the function at the start of each period * - * @default true + * @defaultValue true */ leading?: boolean /** * whether to invoke the function at the end of each period * - * @default true + * @defaultValue true */ trailing?: boolean } diff --git a/src/use-time-ago/format-time-ago.ts b/src/use-time-ago/format-time-ago.ts index b5c8bf7f..0e3e5a3e 100644 --- a/src/use-time-ago/format-time-ago.ts +++ b/src/use-time-ago/format-time-ago.ts @@ -17,7 +17,7 @@ export type FormatTimeAgoOptions { /** * Remove nullish values from the URLSearchParams * - * @default true + * @defaultValue true */ removeNullishValues?: boolean /** * Remove falsy values from the URLSearchParams * - * @default false + * @defaultValue false */ removeFalsyValues?: boolean /** * Initial value for the URLSearchParams * - * @default {} + * @defaultValue {} */ initialValue?: T & Record /** * Write back to `window.history` automatically * - * @default true + * @defaultValue true */ write?: boolean } diff --git a/src/use-url-search-params/index.ts b/src/use-url-search-params/index.ts index 499ecebd..2b57a91f 100644 --- a/src/use-url-search-params/index.ts +++ b/src/use-url-search-params/index.ts @@ -16,25 +16,25 @@ export interface UseUrlSearchParamsOptions { /** * Remove nullish values from the URLSearchParams * - * @default true + * @defaultValue true */ removeNullishValues?: boolean /** * Remove falsy values from the URLSearchParams * - * @default false + * @defaultValue false */ removeFalsyValues?: boolean /** * Initial value for the URLSearchParams * - * @default {} + * @defaultValue {} */ initialValue?: T & Record> /** * Write back to `window.history` automatically * - * @default true + * @defaultValue true */ write?: boolean } diff --git a/src/use-user-idle/index.mdx b/src/use-user-idle/index.mdx index ac2410cf..7518153c 100644 --- a/src/use-user-idle/index.mdx +++ b/src/use-user-idle/index.mdx @@ -42,25 +42,25 @@ export interface UseUserIdleOptions { /** * Event names that listen to for detected user activity * - * @default ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel'] + * @defaultValue ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel'] */ events?: WindowEventName[] /** * Listen for document visibility change * - * @default true + * @defaultValue true */ watchVisibility?: boolean /** * Initial state of the ref idle * - * @default false + * @defaultValue false */ initialState?: boolean /** * Reset the idle state immediately * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-user-idle/index.ts b/src/use-user-idle/index.ts index 39eeb045..e7b2834c 100644 --- a/src/use-user-idle/index.ts +++ b/src/use-user-idle/index.ts @@ -16,25 +16,25 @@ export interface UseUserIdleOptions { /** * Event names that listen to for detected user activity * - * @default ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel'] + * @defaultValue ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel'] */ events?: WindowEventName[] /** * Listen for document visibility change * - * @default true + * @defaultValue true */ watchVisibility?: boolean /** * Initial state of the ref idle * - * @default false + * @defaultValue false */ initialState?: boolean /** * Reset the idle state immediately * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-user-media/index.mdx b/src/use-user-media/index.mdx index 848c3ef3..c98f1383 100644 --- a/src/use-user-media/index.mdx +++ b/src/use-user-media/index.mdx @@ -46,14 +46,14 @@ export interface UseUserMediaOptions { /** * Recreate stream when deviceIds or constraints changed * - * @default true + * @defaultValue true */ autoSwitch?: boolean /** * MediaStreamConstraints to be applied to the requested MediaStream * If provided, the constraints will override videoDeviceId and audioDeviceId * - * @default {} + * @defaultValue {} */ constraints?: MediaStreamConstraints } diff --git a/src/use-user-media/index.ts b/src/use-user-media/index.ts index 304f33e2..e789bc2b 100644 --- a/src/use-user-media/index.ts +++ b/src/use-user-media/index.ts @@ -20,14 +20,14 @@ export interface UseUserMediaOptions { /** * Recreate stream when deviceIds or constraints changed * - * @default true + * @defaultValue true */ autoSwitch?: boolean /** * MediaStreamConstraints to be applied to the requested MediaStream * If provided, the constraints will override videoDeviceId and audioDeviceId * - * @default {} + * @defaultValue {} */ constraints?: MediaStreamConstraints } diff --git a/src/use-web-observer/index.mdx b/src/use-web-observer/index.mdx index 23dc0610..2a96197e 100644 --- a/src/use-web-observer/index.mdx +++ b/src/use-web-observer/index.mdx @@ -49,7 +49,7 @@ export interface UseWebObserverOptions { /** * Start the observer immediate after calling this function * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-web-observer/index.ts b/src/use-web-observer/index.ts index e23d4c98..2232d81c 100644 --- a/src/use-web-observer/index.ts +++ b/src/use-web-observer/index.ts @@ -14,7 +14,7 @@ export interface UseWebObserverOptions { /** * Start the observer immediate after calling this function * - * @default true + * @defaultValue true */ immediate?: boolean } diff --git a/src/use-window-scroll/index.mdx b/src/use-window-scroll/index.mdx index 3a12eb45..dda41075 100644 --- a/src/use-window-scroll/index.mdx +++ b/src/use-window-scroll/index.mdx @@ -38,7 +38,7 @@ export interface UseWindowScrollOptions { /** * The scroll behavior, set to 'smooth' to enable smooth scrolling. * - * @default 'auto' + * @defaultValue 'auto' */ behavior?: ScrollBehavior } diff --git a/src/use-window-scroll/index.ts b/src/use-window-scroll/index.ts index 0b957863..7cec9a2d 100644 --- a/src/use-window-scroll/index.ts +++ b/src/use-window-scroll/index.ts @@ -11,7 +11,7 @@ export interface UseWindowScrollOptions { /** * The scroll behavior, set to 'smooth' to enable smooth scrolling. * - * @default 'auto' + * @defaultValue 'auto' */ behavior?: ScrollBehavior } diff --git a/src/use-window-size/index.mdx b/src/use-window-size/index.mdx index 6993cdc8..c668d1ca 100644 --- a/src/use-window-size/index.mdx +++ b/src/use-window-size/index.mdx @@ -40,25 +40,25 @@ export interface UseWindowSizeOptions { /** * The initial width of the window. * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ initialWidth?: number /** * The initial height of the window. * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ initialHeight?: number /** * Whether to listen to orientation changes. * - * @default true + * @defaultValue true */ listenOrientation?: boolean /** * Whether to include the scrollbar width. * - * @default true + * @defaultValue true */ includeScrollbar?: boolean } diff --git a/src/use-window-size/index.ts b/src/use-window-size/index.ts index b4f3f6e3..d261abba 100644 --- a/src/use-window-size/index.ts +++ b/src/use-window-size/index.ts @@ -11,25 +11,25 @@ export interface UseWindowSizeOptions { /** * The initial width of the window. * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ initialWidth?: number /** * The initial height of the window. * - * @default Number.POSITIVE_INFINITY + * @defaultValue Number.POSITIVE_INFINITY */ initialHeight?: number /** * Whether to listen to orientation changes. * - * @default true + * @defaultValue true */ listenOrientation?: boolean /** * Whether to include the scrollbar width. * - * @default true + * @defaultValue true */ includeScrollbar?: boolean } diff --git a/src/utils/create-effect/async.ts b/src/utils/create-effect/async.ts index 7fd1bb16..a2475af7 100644 --- a/src/utils/create-effect/async.ts +++ b/src/utils/create-effect/async.ts @@ -4,9 +4,17 @@ import { useLatest } from '../../use-latest' import type { DependencyList } from 'react' import type { ExtendedReactEffect } from '../basic' +/** + * A callback function for async effect + * + * @param {() => boolean} isCancelled - `() => boolean`, a function to check if the effect is cancelled + * @returns {void} `void` + */ export type AsyncEffectCallback = (isCancelled: () => boolean) => void -export function createAsyncEffect(effect: ExtendedReactEffect) { +type UseAsyncEffect = (callback: AsyncEffectCallback, deps?: DependencyList, ...args: T[]) => void + +export function createAsyncEffect(effect: ExtendedReactEffect): UseAsyncEffect { return (callback: AsyncEffectCallback, deps?: DependencyList, ...args: T[]): void => { const [isCancelledRef, isCancelled] = useGetterRef(false) const latest = useLatest({ callback }) diff --git a/src/utils/debounce.ts b/src/utils/debounce.ts index 3219cc0a..106ad0f9 100644 --- a/src/utils/debounce.ts +++ b/src/utils/debounce.ts @@ -4,19 +4,19 @@ export type DebounceOptions = { /** * Time to wait before invoking the function * - * @default 0 + * @defaultValue 0 */ wait?: number /** * immediately invoke before the timeout * - * @default false + * @defaultValue false */ leading?: boolean /** * invoke the function after the timeout * - * @default true + * @defaultValue true */ trailing?: boolean } diff --git a/src/utils/throttle.ts b/src/utils/throttle.ts index 516ffaea..e66c0303 100644 --- a/src/utils/throttle.ts +++ b/src/utils/throttle.ts @@ -4,19 +4,19 @@ export type ThrottleOptions = { /** * time frame for throttle * - * @default 0 + * @defaultValue 0 */ wait?: number /** * whether to invoke the function at the start of each period * - * @default true + * @defaultValue true */ leading?: boolean /** * whether to invoke the function at the end of each period * - * @default true + * @defaultValue true */ trailing?: boolean }