# installation: Installation URL: /docs/installation Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/content/docs/installation.mdx How to install dependencies and structure your app. *** title: Installation description: How to install dependencies and structure your app. ---------------------------------------------------------------- ## Install Hooks ### CLI Use the shadcn CLI to add hooks to your project. Dependencies are resolved automatically. ```bash npx shadcn@latest add @shadcnhooks/use-boolean ``` ```bash pnpm dlx shadcn@latest add @shadcnhooks/use-boolean ``` ```bash yarn dlx shadcn@latest add @shadcnhooks/use-boolean ``` ```bash bun x shadcn@latest add @shadcnhooks/use-boolean ``` [shadcn/installation](https://ui.shadcn.com/docs/installation) ### npm Package Or install all hooks at once via npm: ```bash npm install shadcn-hooks ``` ```bash pnpm add shadcn-hooks ``` ```bash yarn add shadcn-hooks ``` ```bash bun add shadcn-hooks ``` Then import directly: ```tsx import { useBoolean, useCounter, useClipboard } from "shadcn-hooks"; ``` # introduction: Introduction URL: /docs/introduction Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/content/docs/introduction.mdx A comprehensive React Hooks Collection built with Shadcn *** title: Introduction description: A comprehensive React Hooks Collection built with Shadcn --------------------------------------------------------------------- ## What is Shadcn Hooks? Shadcn Hooks is a carefully curated collection of modern React hooks designed to enhance your development experience. Built with the same philosophy as Shadcn UI, this collection provides high-quality, TypeScript-first hooks that are production-ready and follow React best practices. ## Key Features * **TypeScript First**: All hooks are written in TypeScript with full type safety * **SSR Compatible**: Hooks work seamlessly with server-side rendering * **Modern React**: Built for React 19+ with latest patterns and optimizations * **Zero Dependencies**: Most hooks have minimal or no external dependencies * **Tree Shakeable**: Import only what you need * **Well Tested**: Each hook is thoroughly tested and documented ## Hook Categories ### State Management * [`useBoolean`](/docs/hooks/use-boolean) - Toggle boolean values with semantic methods * [`useControllableValue`](/docs/hooks/use-controllable-value) - Manage a controllable value * [`useCounter`](/docs/hooks/use-counter) - Create and manage counter state with increment, decrement, and reset * [`useDebounce`](/docs/hooks/use-debounce) - A hook to debounce a value * [`useResetState`](/docs/hooks/use-reset-state) - Reset a state to its initial state * [`useThrottle`](/docs/hooks/use-throttle) - A hook to throttle a value * [`useToggle`](/docs/hooks/use-toggle) - Simple boolean toggle functionality ### Advanced * [`useCreation`](/docs/hooks/use-creation) - Create a value only once * [`useCustomCompareEffect`](/docs/hooks/use-custom-compare-effect) - Effect with custom compare function * [`useLatest`](/docs/hooks/use-latest) - Always get the latest value in closures * [`useLockFn`](/docs/hooks/use-lock-fn) - Prevent concurrent function calls * [`useMemoizedFn`](/docs/hooks/use-memoized-fn) - Memoize function references * [`usePrevious`](/docs/hooks/use-previous) - Get the previous value of a state ### Lifecycle & Effects * [`useDebounceEffect`](/docs/hooks/use-debounce-effect) - A hook to debounce an effect * [`useDebounceFn`](/docs/hooks/use-debounce-fn) - A hook to debounce a function * [`useDeepCompareEffect`](/docs/hooks/use-deep-compare-effect) - Effect with deep comparison of dependencies * [`useDeepCompareLayoutEffect`](/docs/hooks/use-deep-compare-layout-effect) - Layout effect with deep comparison * [`useEffectEvent`](/docs/hooks/use-effect-event) - Stable event handlers for effects * [`useEffectWithTarget`](/docs/hooks/use-effect-with-target) - A hook to create an effect with a target element that re-runs when the target or dependencies change * [`useInterval`](/docs/hooks/use-interval) - A hook to create an interval * [`useIsHydrated`](/docs/hooks/use-is-hydrated) - Detect client-side hydration * [`useIsomorphicLayoutEffect`](/docs/hooks/use-isomorphic-layout-effect) - SSR-safe layout effects * [`useMount`](/docs/hooks/use-mount) - Execute code on component mount * [`useThrottleEffect`](/docs/hooks/use-throttle-effect) - A hook to throttle an effect * [`useThrottleFn`](/docs/hooks/use-throttle-fn) - A hook to throttle a function * [`useTimeout`](/docs/hooks/use-timeout) - A hook to create a timeout * [`useUnmount`](/docs/hooks/use-unmount) - Execute cleanup on component unmount * [`useUpdate`](/docs/hooks/use-update) - Force a component to re-render * [`useUpdateEffect`](/docs/hooks/use-update-effect) - Run an effect only when the component updates ### Browser * [`useActiveElement`](/docs/hooks/use-active-element) - Track the currently focused element in the document * [`useBattery`](/docs/hooks/use-battery) - Monitor battery level and charging state * [`useClickAnyWhere`](/docs/hooks/use-click-any-where) - Attach a click event listener to the document * [`useClickAway`](/docs/hooks/use-click-away) - A hook to click away from an element * [`useClipboard`](/docs/hooks/use-clipboard) - A hook to copy text to clipboard * [`useDocumentVisibility`](/docs/hooks/use-document-visibility) - Track whether the current document is visible or hidden * [`useElementSize`](/docs/hooks/use-element-size) - Track an element's width and height with ResizeObserver * [`useEventListener`](/docs/hooks/use-event-listener) - Attach event listeners to DOM elements, the window, or media query lists. * [`useFps`](/docs/hooks/use-fps) - Calculate the FPS of the browser * [`useFullscreen`](/docs/hooks/use-fullscreen) - Manage fullscreen state * [`useHash`](/docs/hooks/use-hash) - Track URL hash changes * [`useHover`](/docs/hooks/use-hover) - Check if the mouse is hovering over an element * [`useInViewport`](/docs/hooks/use-in-viewport) - Check if an element is in the viewport * [`useIsMatchMedia`](/docs/hooks/use-is-match-media) - React to media query changes * [`useIsOnline`](/docs/hooks/use-is-online) - Monitor online/offline status * [`useMouse`](/docs/hooks/use-mouse) - Track pointer coordinates with optional touch support * [`useNetwork`](/docs/hooks/use-network) - Monitor network state * [`useScrollLock`](/docs/hooks/use-scroll-lock) - Lock the scroll of the body * [`useTextSelection`](/docs/hooks/use-text-selection) - Get the text selection from an element * [`useTitle`](/docs/hooks/use-title) - Manage and observe document title changes ### Dev * [`useWhyDidYouUpdate`](/docs/hooks/use-why-did-you-update) - A hook to log the props that changed in a component ### External Libraries * [`useMcp`](/docs/hooks/use-mcp) - Model Context Protocol integration * [`useQuery`](/docs/hooks/use-query) - TanStack Query integration * [`useStickToBottom`](/docs/hooks/use-stick-to-bottom) - A lightweight React Hook intended mainly for AI chat applications, for smoothly sticking to bottom of messages * [`useSWR`](/docs/hooks/use-swr) - SWR data fetching ## Getting Started Each hook comes with: * **CLI Installation**: Use the shadcn CLI for easy setup * **Manual Installation**: Copy-paste the source code * **TypeScript Support**: Full type definitions included * **Documentation**: Comprehensive examples and usage patterns ## Design Philosophy Shadcn Hooks follows the same principles as Shadcn UI: * **Copy, don't fork**: Own your code * **Minimal dependencies**: Keep bundles small * **Modern patterns**: Use latest React features * **Developer experience**: Great TypeScript support and documentation ## Credits This collection is inspired by and builds upon the excellent work of: * [ahooks](https://ahooks.js.org/) - Comprehensive React hooks library * [usehooks-ts](https://usehooks-ts.com/) - TypeScript-first hooks collection # skills: AI Skills URL: /docs/skills Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/content/docs/skills.mdx Install AI Skills to help coding assistants understand and use hooks correctly. *** title: AI Skills description: Install AI Skills to help coding assistants understand and use hooks correctly. -------------------------------------------------------------------------------------------- Shadcn Hooks provides an [AI Skills](https://github.com/Debbl/shadcn-hooks/tree/main/skills/shadcn-hooks) directory that helps AI coding assistants (Cursor, Codex, etc.) understand and correctly apply every hook. Once installed, your AI assistant can automatically suggest the right hook for any requirement, with accurate type signatures and usage patterns. Run via [skills CLI](https://skills.sh/docs/cli): ```bash npx skills add Debbl/shadcn-hooks ``` This downloads the skill and configures it for use with your AI agent (Cursor, Codex, etc.). ## What's Included The skill contains: * **`SKILL.md`** — A decision-and-implementation guide with a categorized table of all hooks, their descriptions, and invocation rules (`AUTO` / `EXTERNAL`). * **`references/`** — One reference file per hook with: * Description and usage examples * Full TypeScript type declarations * Parameters and return value tables This allows AI assistants to: 1. **Auto-suggest** the right hook when it matches your requirement 2. **Generate correct code** with proper type signatures and options 3. **Follow best practices** for each hook's usage pattern # lib: createEffectWithTarget URL: /docs/lib/create-effect-with-target Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/lib/create-effect-with-target/index.mdx A hook to create an effect with a target. *** title: createEffectWithTarget description: A hook to create an effect with a target. ------------------------------------------------------ ## Installation Copy and paste the following code into your project.
# lib: isBrowser URL: /docs/lib/is-browser Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/lib/is-browser/index.mdx A library that checks if the code is running in a browser. *** title: isBrowser description: A library that checks if the code is running in a browser. ----------------------------------------------------------------------- ## Installation Copy and paste the following code into your project. # lib: createContext URL: /docs/lib/create-context Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/lib/create-context/index.mdx Create a context object. *** title: createContext description: Create a context object. ------------------------------------- ## Installation Copy and paste the following code into your project. ## Credits * [create-context](https://github.com/radix-ui/primitives/tree/main/packages/react/context) # hooks: useActiveElement URL: /docs/hooks/use-active-element Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-active-element/index.mdx A hook to track the currently focused element in the document *** title: useActiveElement description: A hook to track the currently focused element in the document -------------------------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts export interface UseActiveElementOptions { /** * Search active element deeply inside shadow dom. * @default true */ deep?: boolean /** * Track active element when it's removed from the DOM * Using a MutationObserver under the hood * @default false */ triggerOnRemoval?: boolean } export function useActiveElement( options?: UseActiveElementOptions, ): T | null ``` ## Credits * [useActiveElement](https://vueuse.org/core/useActiveElement/) # hooks: useBattery URL: /docs/hooks/use-battery Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-battery/index.mdx A hook to monitor battery information *** title: useBattery description: A hook to monitor battery information -------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts export interface UseBatteryState { isSupported: boolean charging: boolean chargingTime: number dischargingTime: number level: number } /** * Monitor the current battery information using the Battery Status API. * * @returns Current battery state and support flag. */ export function useBattery(): UseBatteryState ``` ## Credits * [useBattery](https://vueuse.org/core/useBattery/) # hooks: useBoolean URL: /docs/hooks/use-boolean Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-boolean/index.mdx A hook to toggle a boolean value *** title: useBoolean description: A hook to toggle a boolean value --------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to toggle a boolean value * @param defaultValue - The default value of the boolean */ export function useBoolean(defaultValue?: boolean): readonly [ boolean, { /** * Set the value of the boolean * @param value - The value to set the boolean to */ set: (value: boolean) => void /** * Set the value of the boolean to true */ setTrue: () => void /** * Set the value of the boolean to false */ setFalse: () => void /** * Toggle the value of the boolean */ toggle: () => void }, ] ``` ## Credits * [use-boolean](https://ahooks.js.org/hooks/use-boolean) # hooks: useClickAnyWhere URL: /docs/hooks/use-click-any-where Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-click-any-where/index.mdx A hook to click anywhere *** title: useClickAnyWhere description: A hook to click anywhere ------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to click anywhere * @param handler - The handler to call when the user clicks anywhere */ export function useClickAnyWhere(handler: (event: MouseEvent) => void): void ``` ## Credits * [use-click-any-where](https://usehooks-ts.com/react-hook/use-click-any-where) # hooks: useClickAway URL: /docs/hooks/use-click-away Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-click-away/index.mdx A hook to click away from an element *** title: useClickAway description: A hook to click away from an element ------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.


## API ```ts type TargetValue = T | undefined | null type TargetType = HTMLElement | Element | Window | Document type BasicTarget = | (() => TargetValue) | TargetValue | RefObject> type DocumentEventKey = keyof DocumentEventMap /** * A hook to click away from an element * @param onClickAway - The function to call when the user clicks away from the target * @param target - The target to click away from * @param eventName - The event to listen for /** * A hook to click away from an element * @param onClickAway - The function to call when the user clicks away from the target * @param target - The target to click away from * @param eventName - The event to listen for */ */ export function useClickAway( onClickAway: (event: T) => void, target: BasicTarget | BasicTarget[], eventName?: DocumentEventKey | DocumentEventKey[], ): void ``` ## Credits * [use-click-away](https://ahooks.js.org/hooks/use-click-away) # hooks: useClipboard URL: /docs/hooks/use-clipboard Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-clipboard/index.mdx A hook to copy text to clipboard *** title: useClipboard description: A hook to copy text to clipboard --------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.



## API ```ts export interface UseClipboardOptions { /** * Enabled reading for clipboard * * @default false */ read?: boolean /** * Copy source */ source?: string /** * Milliseconds to reset state of `copied` ref * * @default 1500 */ copiedDuring?: number /** * Whether fallback to document.execCommand('copy') if clipboard is undefined. * * @default false */ legacy?: boolean } export interface UseClipboardReturn { isSupported: boolean text: string copied: boolean copy: (text?: string) => Promise } type PermissionState = 'granted' | 'denied' | 'prompt' | undefined /** * A hook to copy text to clipboard * @param options - The options for the clipboard * @returns The clipboard state and methods */ export function useClipboard(options?: UseClipboardOptions): UseClipboardReturn ``` ## Credits * [use-clipboard](https://vueuse.org/core/useClipboard/) # hooks: useControllableValue URL: /docs/hooks/use-controllable-value Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-controllable-value/index.mdx A hook to manage a controllable value *** title: useControllableValue description: A hook to manage a controllable value -------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts export interface Options { defaultValue?: T defaultValuePropName?: string valuePropName?: string trigger?: string } export type Props = Record export interface StandardProps { value: T defaultValue?: T onChange: (val: T) => void } /** * A hook to manage a controllable value * @param props - The props object containing value, defaultValue, and onChange */ function useControllableValue( props: StandardProps, ): [T, (v: SetStateAction) => void] function useControllableValue( props?: Props, options?: Options, ): [T, (v: SetStateAction, ...args: any[]) => void] ``` ## Credits * [use-controllable-value](https://ahooks.js.org/hooks/use-controllable-value) # hooks: useCounter URL: /docs/hooks/use-counter Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-counter/index.mdx A hook that creates a counter. *** title: useCounter description: A hook that creates a counter. ------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook that creates a counter. * @param initialValue - The initial value of the counter * @returns A tuple containing the counter value and an object with the actions to increment, decrement, and reset the counter */ export function useCounter(initialValue: number = 0): readonly [ number, { /** * Set the value of the counter * @param value - The value to set the counter to */ set: (value: number | ((value: number) => number)) => void /** * Increment the value of the counter */ inc: () => void /** * Decrement the value of the counter */ dec: () => void /** * Reset the value of the counter to the initial value */ reset: () => void }, ] ``` # hooks: useCreation URL: /docs/hooks/use-creation Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-creation/index.mdx A hook to create a value only once *** title: useCreation description: A hook to create a value only once ----------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to create a value only once * @param factory - The factory function to create the value * @param deps - The dependencies to create the value * @returns The created value */ export function useCreation(factory: () => T, deps: DependencyList): T ``` ## Credits * [use-creation](https://ahooks.js.org/hooks/use-creation) # hooks: useCustomCompareEffect URL: /docs/hooks/use-custom-compare-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-custom-compare-effect/index.mdx A hook like useEffect but with a custom compare function *** title: useCustomCompareEffect description: A hook like useEffect but with a custom compare function --------------------------------------------------------------------- import { Demo01 } from './demo/demo-01' > Only when the user's ID changes will the effect be triggered ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook like useEffect but with a custom compare function * @param effect - The effect to use * @param deps - The dependencies to use * @param customCompare - The custom compare function to use */ function useCustomCompareEffect( effect: EffectCallback, deps: T, customCompare: (a: T, b: T) => boolean, ): void ``` ## Notes > **Important:** Do not call `setState` methods inside the `customCompare` function. The compare function should be a pure function that only performs comparison logic. ```ts // ❌ Don't do this const compareById = (a: User[], b: User[]) => { const result = a[0]?.id === b[0]?.id setCount((prev) => prev + 1) // ❌ This will cause infinite re-renders return result } // ✅ Do this instead const compareById = (a: User[], b: User[]) => { const result = a[0]?.id === b[0]?.id return result } ``` ## Credits * [createDeepCompareEffect](https://github.com/alibaba/hooks/blob/master/packages/hooks/src/createDeepCompareEffect/index.ts) * [useCustomCompareEffect](https://github.com/streamich/react-use/blob/master/src/useCustomCompareEffect.ts) # hooks: useDebounce URL: /docs/hooks/use-debounce Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-debounce/index.mdx A hook to debounce a value *** title: useDebounce description: A hook to debounce a value --------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts interface DebounceOptions { /** * An optional AbortSignal to cancel the debounced function. */ signal?: AbortSignal /** * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both. * If `edges` includes "leading", the function will be invoked at the start of the delay period. * If `edges` includes "trailing", the function will be invoked at the end of the delay period. * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period. * @default ["trailing"] */ edges?: Array<'leading' | 'trailing'> } /** * A hook to debounce a value * @param value - The value to debounce * @param debounceMs - The debounce time in milliseconds default to 1000 * @param options - The options for the debounce value * @returns The debounced value */ export function useDebounce( value: T, debounceMs?: number, options?: DebounceOptions, ): T ``` ## Credits * [use-debounce](https://ahooks.js.org/hooks/use-debounce) # hooks: useDebounceEffect URL: /docs/hooks/use-debounce-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-debounce-effect/index.mdx A hook to debounce an effect *** title: useDebounceEffect description: A hook to debounce an effect ----------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts interface DebounceOptions { /** * An optional AbortSignal to cancel the debounced function. */ signal?: AbortSignal /** * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both. * If `edges` includes "leading", the function will be invoked at the start of the delay period. * If `edges` includes "trailing", the function will be invoked at the end of the delay period. * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period. * @default ["trailing"] */ edges?: Array<'leading' | 'trailing'> } /** * A hook to debounce an effect * @param effect - The effect to debounce * @param deps - The dependencies to debounce the effect on * @param debounceMs - The debounce time in milliseconds default to 1000 * @param options - The options for the debounce effect * @returns The debounced effect */ export function useDebounceEffect( effect: EffectCallback, deps: DependencyList, debounceMs?: number, options?: DebounceOptions, ): void ``` ## Credits * [use-debounce-effect](https://ahooks.js.org/hooks/use-debounce-effect) # hooks: useDebounceFn URL: /docs/hooks/use-debounce-fn Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-debounce-fn/index.mdx A hook to debounce a function *** title: useDebounceFn description: A hook to debounce a function ------------------------------------------ import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts interface DebounceOptions { /** * An optional AbortSignal to cancel the debounced function. */ signal?: AbortSignal /** * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both. * If `edges` includes "leading", the function will be invoked at the start of the delay period. * If `edges` includes "trailing", the function will be invoked at the end of the delay period. * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period. * @default ["trailing"] */ edges?: Array<'leading' | 'trailing'> } /** * A hook to debounce a function * @param fn - The function to debounce * @param debounceMs - The debounce time in milliseconds default to 1000 * @param options - The options for the debounce function * @returns The debounced function */ export function useDebounceFn any>( fn: Fn, debounceMs?: number, options?: DebounceOptions, ): { run: DebouncedFunction cancel: () => void flush: () => void } ``` ## Credits * [use-debounce-fn](https://ahooks.js.org/hooks/use-debounce-fn) # hooks: useDeepCompareEffect URL: /docs/hooks/use-deep-compare-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-deep-compare-effect/index.mdx A hook to use a deep compare effect *** title: useDeepCompareEffect description: A hook to use a deep compare effect ------------------------------------------------ ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to use a deep compare effect * @param effect - The effect to use * @param deps - The dependencies to use */ export function useDeepCompareEffect( effect: EffectCallback, deps: DependencyList, ): void ``` ## Credits * [createDeepCompareEffect](https://github.com/alibaba/hooks/blob/master/packages/hooks/src/createDeepCompareEffect/index.ts) * [useCustomCompareEffect](https://github.com/streamich/react-use/blob/master/src/useCustomCompareEffect.ts) # hooks: useDeepCompareLayoutEffect URL: /docs/hooks/use-deep-compare-layout-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-deep-compare-layout-effect/index.mdx A hook to use a deep compare layout effect *** title: useDeepCompareLayoutEffect description: A hook to use a deep compare layout effect ------------------------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to use a deep compare layout effect * @param effect - The effect to use * @param deps - The dependencies to use */ export function useDeepCompareLayoutEffect( effect: EffectCallback, deps: DependencyList, ): void ``` ## Credits * [createDeepCompareEffect](https://github.com/alibaba/hooks/blob/master/packages/hooks/src/createDeepCompareEffect/index.ts) * [useCustomCompareEffect](https://github.com/streamich/react-use/blob/master/src/useCustomCompareEffect.ts) # hooks: useDocumentVisibility URL: /docs/hooks/use-document-visibility Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-document-visibility/index.mdx A hook to track the document visibility state *** title: useDocumentVisibility description: A hook to track the document visibility state ---------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * Reactively track `document.visibilityState`. * * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState * @returns The current document visibility state. */ export function useDocumentVisibility(): DocumentVisibilityState ``` ## Credits * [useDocumentVisibility](https://github.com/vueuse/vueuse/blob/main/packages/core/useDocumentVisibility/index.ts) # hooks: useEffectEvent URL: /docs/hooks/use-effect-event Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-effect-event/index.mdx A hook to declare an effect event *** title: useEffectEvent description: A hook to declare an effect event ---------------------------------------------- ## Installation > If you are using React 19.2, please use the built-in [useEffectEvent](https://react.dev/reference/react/useEffectEvent) instead. Copy and paste the following code into your project. ## API ```ts /** * A hook to declare an effect event * @param fn - The function to declare as an effect event * @returns The function declared as an effect event */ export function useEffectEvent void>( fn: T, ): T ``` ## Credits * [use-effect-event](https://github.com/sanity-io/use-effect-event) # hooks: useEffectWithTarget URL: /docs/hooks/use-effect-with-target Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-effect-with-target/index.mdx A hook to create an effect with a target element that re-runs when the target or dependencies change *** title: useEffectWithTarget description: A hook to create an effect with a target element that re-runs when the target or dependencies change ----------------------------------------------------------------------------------------------------------------- ## Installation Copy and paste the following code into your project.
## Usage `useEffectWithTarget` is similar to `useEffect`, but it accepts a `target` parameter. The effect will re-run when either the dependencies or the target element changes. ### Basic Usage ```tsx import { useEffectWithTarget } from '@/hooks/use-effect-with-target' import { useRef } from 'react' function MyComponent() { const divRef = useRef(null) useEffectWithTarget( () => { const element = divRef.current if (element) { // Do something with the element console.log('Element mounted:', element) } return () => { // Cleanup console.log('Element unmounted') } }, [], divRef, ) return
Hello
} ``` ### With DOM Element ```tsx import { useEffectWithTarget } from '@/hooks/use-effect-with-target' function MyComponent() { const element = document.getElementById('my-element') useEffectWithTarget( () => { if (element) { // Do something with the element } }, [], element, ) return
Hello
} ``` ### With Function Target ```tsx import { useEffectWithTarget } from '@/hooks/use-effect-with-target' function MyComponent() { useEffectWithTarget( () => { const element = document.querySelector('.my-class') if (element) { // Do something with the element } }, [], () => document.querySelector('.my-class'), ) return
Hello
} ``` ### With Multiple Targets ```tsx import { useEffectWithTarget } from '@/hooks/use-effect-with-target' import { useRef } from 'react' function MyComponent() { const ref1 = useRef(null) const ref2 = useRef(null) useEffectWithTarget( () => { // Effect runs when either ref1 or ref2 changes console.log('Targets changed') }, [], [ref1, ref2], ) return ( <>
First
Second
) } ``` ### With Dependencies ```tsx import { useEffectWithTarget } from '@/hooks/use-effect-with-target' import { useRef, useState } from 'react' function MyComponent() { const [count, setCount] = useState(0) const divRef = useRef(null) useEffectWithTarget( () => { // Effect runs when count changes or when divRef.current changes console.log('Count:', count) }, [count], divRef, ) return (

Count: {count}

) } ``` ## API ```ts type TargetValue = T | undefined | null type TargetType = HTMLElement | Element | Window | Document type BasicTarget = | (() => TargetValue) | TargetValue | RefObject> /** * A hook to create an effect with a target element * @param effect - The effect callback function * @param deps - The dependency array (same as useEffect) * @param target - The target element(s). Can be a DOM element, ref object, function that returns an element, or an array of these * @returns void */ export const useEffectWithTarget: ( effect: EffectCallback, deps: DependencyList, target: BasicTarget | BasicTarget[], ) => void ``` ## Credits * [create-effect-with-target](https://github.com/alibaba/hooks/blob/master/packages/hooks/src/utils/createEffectWithTarget.ts) # hooks: useElementSize URL: /docs/hooks/use-element-size Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-element-size/index.mdx A hook to track an element's width and height *** title: useElementSize description: A hook to track an element's width and height ---------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts export interface ElementSize { width: number height: number } export interface UseElementSizeOptions { /** * ResizeObserver box model to use for measuring. * * @default 'content-box' */ box?: ResizeObserverBoxOptions } /** * A hook to track an element's width and height. * * @param target - The target element to observe * @param initialSize - Initial width and height * @param options - Measurement options * @returns Current element size */ export function useElementSize( target: BasicTarget, initialSize?: ElementSize, options?: UseElementSizeOptions, ): ElementSize ``` ## Credits * [useElementSize](https://vueuse.org/core/useElementSize/) # hooks: useFps URL: /docs/hooks/use-fps Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-fps/index.mdx A hook to calculate the FPS *** title: useFps description: A hook to calculate the FPS ---------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts interface UseFpsProps { /** * Calculate the FPS on every x frames. * @default 10 */ every?: number } /** * A hook to calculate the FPS * @param options - The options for the FPS * @param options.every - The number of frames to calculate the FPS on * @default { every: 10 } * @returns The FPS */ export function useFps(options?: UseFpsProps): number ``` ## Credits * [use-fps](https://vueuse.org/core/useFps/) # hooks: useEventListener URL: /docs/hooks/use-event-listener Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-event-listener/index.mdx A hook to attach event listeners to DOM elements, the window, or media query lists. *** title: useEventListener description: A hook to attach event listeners to DOM elements, the window, or media query lists. ------------------------------------------------------------------------------------------------ import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.


## API ```ts type noop = (...p: any) => void export type Target = BasicTarget interface Options { target?: T capture?: boolean once?: boolean passive?: boolean enable?: boolean } function useEventListener( eventName: K, handler: (ev: HTMLElementEventMap[K]) => void, options?: Options, ): void function useEventListener( eventName: K, handler: (ev: ElementEventMap[K]) => void, options?: Options, ): void function useEventListener( eventName: K, handler: (ev: DocumentEventMap[K]) => void, options?: Options, ): void function useEventListener( eventName: K, handler: (ev: WindowEventMap[K]) => void, options?: Options, ): void function useEventListener( eventName: string | string[], handler: (event: Event) => void, options?: Options, ): void function useEventListener( eventName: string | string[], handler: noop, options: Options, ): void export { useEventListener } ``` ## Credits * [use-event-listener](https://ahooks.js.org/hooks/use-event-listener) # hooks: useFullscreen URL: /docs/hooks/use-fullscreen Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-fullscreen/index.mdx A hook to manage fullscreen state *** title: useFullscreen description: A hook to manage fullscreen state ---------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.




## API ```ts export interface UseFullscreenOptions { /** * Automatically exit fullscreen when component is unmounted * * @default false */ autoExit?: boolean } /** * A hook to manage fullscreen state * @param target - The target element to make fullscreen. If not provided, uses document.documentElement * @param options - Configuration options */ export function useFullscreen( target?: BasicTarget, options?: UseFullscreenOptions, ): { isSupported: boolean isFullscreen: boolean enter: PickFunction<() => Promise> exit: PickFunction<() => Promise> toggle: PickFunction<() => Promise> } ``` ## Credits * [use-fullscreen](https://vueuse.org/core/useFullscreen/) # hooks: useHash URL: /docs/hooks/use-hash Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-hash/index.mdx A hook to get current hash *** title: useHash description: A hook to get current hash --------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to get current hash * @returns The current hash */ export function useHash(): string ``` ## Credits * [use-hash](https://github.com/shuding/next-view-transitions/blob/main/src/use-hash.ts) # hooks: useHover URL: /docs/hooks/use-hover Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-hover/index.mdx A hook to check if the mouse is hovering over an element *** title: useHover description: A hook to check if the mouse is hovering over an element --------------------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.


## API ```ts /** * A hook to check if the mouse is hovering over an element * @param target - The target to check if the mouse is hovering over * @param options - The options for the hook * @param options.onEnter - The function to call when the mouse enters the target * @param options.onLeave - The function to call when the mouse leaves the target * @param options.onChange - The function to call when the mouse enters or leaves the target * @returns The boolean value indicating if the mouse is hovering over the target */ export function useHover(target: BasicTarget, options?: Options): boolean ``` ## Credits * [use-hover](https://ahooks.js.org/hooks/use-hover) # hooks: useInViewport URL: /docs/hooks/use-in-viewport Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-in-viewport/index.mdx A hook to check if an element is in the viewport *** title: useInViewport description: A hook to check if an element is in the viewport ------------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts /** * A hook to check if an element is in the viewport * @param target - The target to check if it is in the viewport * @param options - The options for the hook * @param options.rootMargin - The root margin for the intersection observer * @param options.threshold - The threshold for the intersection observer * @param options.root - The root element for the intersection observer * @param options.callback - The callback function to call when the target is in the viewport * @returns The boolean value indicating if the target is in the viewport and the ratio of the intersection */ export function useInViewport( target: BasicTarget | BasicTarget[], options?: Options, ): readonly [boolean | undefined, number | undefined] ``` ## Credits * [use-in-viewport](https://ahooks.js.org/hooks/use-in-viewport) # hooks: useInterval URL: /docs/hooks/use-interval Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-interval/index.mdx A hook that creates an interval. *** title: useInterval description: A hook that creates an interval. --------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook that creates an interval. * @param fn - The function to execute repeatedly * @param delay - The delay in milliseconds between each execution. Set to null to pause the interval * @param options - The options for the interval * @param options.immediate - Whether to execute the function immediately * @default { immediate: false } if not provided * @returns A function to clear the interval */ export function useInterval( fn: () => void, delay: number | null = null, options?: { immediate?: boolean }, ): () => void ``` ## Credits * [use-interval](https://ahooks.js.org/hooks/use-interval) # hooks: useIsHydrated URL: /docs/hooks/use-is-hydrated Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-is-hydrated/index.mdx A hook to check if the component is hydrated *** title: useIsHydrated description: A hook to check if the component is hydrated --------------------------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to check if the component is hydrated * @returns A boolean indicating if the component is hydrated */ export function useIsHydrated(): boolean ``` ## Credits * [use-is-hydrated](https://github.com/sergiodxa/remix-utils/blob/main/src/react/use-hydrated.ts) # hooks: useIsOnline URL: /docs/hooks/use-is-online Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-is-online/index.mdx A hook to check if the user is online *** title: useIsOnline description: A hook to check if the user is online -------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to check if the network is online * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event * @returns A boolean indicating if the network is online */ export function useIsOnline(): boolean ``` # hooks: useIsMatchMedia URL: /docs/hooks/use-is-match-media Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-is-match-media/index.mdx A hook to check if the media query matches *** title: useIsMatchMedia description: A hook to check if the media query matches ------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to check if the media query matches * @param mediaQueryString - The media query string to check * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia#mediaquerystring * @returns A boolean indicating if the media query matches */ export function useIsMatchMedia(mediaQueryString: string): boolean ``` ## Credits * [use-is-match-media](https://github.com/Debbl/use-is-match-media) # hooks: useIsomorphicLayoutEffect URL: /docs/hooks/use-isomorphic-layout-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-isomorphic-layout-effect/index.mdx A hook to use layout effect on the client side *** title: useIsomorphicLayoutEffect description: A hook to use layout effect on the client side ----------------------------------------------------------- ## Installation Copy and paste the following code into your project.
## API ````ts /** * Custom hook that uses either `useLayoutEffect` or `useEffect` based on the environment (client-side or server-side). * @param {Function} effect - The effect function to be executed. * @param {Array} [dependencies] - An array of dependencies for the effect (optional). * @public * @see [Documentation](https://usehooks-ts.com/react-hook/use-isomorphic-layout-effect) * @example * ```tsx * useIsomorphicLayoutEffect(() => { * // Code to be executed during the layout phase on the client side * }, [dependency1, dependency2]); * ``` */ export const useIsomorphicLayoutEffect: ( effect: EffectCallback, deps?: DependencyList, ) => void ```` * [use-isomorphic-layout-effect](https://usehooks-ts.com/react-hook/use-isomorphic-layout-effect) # hooks: useLatest URL: /docs/hooks/use-latest Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-latest/index.mdx A hook to get the latest value of a variable *** title: useLatest description: A hook to get the latest value of a variable --------------------------------------------------------- ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to get the latest value of a variable * @param value - The value to get the latest value of * @returns The latest value of the variable */ export function useLatest(value: T): RefObject ``` ## Credits * [use-latest](https://github.com/Andarist/use-latest) # hooks: useLockFn URL: /docs/hooks/use-lock-fn Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-lock-fn/index.mdx A hook to lock a function until the previous promise is resolved *** title: useLockFn description: A hook to lock a function until the previous promise is resolved ----------------------------------------------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to lock a function until the previous promise is resolved * @param fn - The function to lock * @returns The locked function */ export function useLockFn

( fn: (...args: P) => Promise, ): (...args: P) => Promise ``` # hooks: useMcp URL: /docs/hooks/use-mcp Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-mcp/index.mdx A hook to get the latest value of a variable *** title: useMcp description: A hook to get the latest value of a variable --------------------------------------------------------- * [use-mcp](https://github.com/modelcontextprotocol/use-mcp) Copy and paste the following code into your project. # hooks: useMemoizedFn URL: /docs/hooks/use-memoized-fn Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-memoized-fn/index.mdx A hook to declare a memoized function *** title: useMemoizedFn description: A hook to declare a memoized function -------------------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts type noop = (this: any, ...args: any[]) => any type PickFunction = ( this: ThisParameterType, ...args: Parameters ) => ReturnType /** * A hook to declare a memoized function * @param fn - The function to declare as a memoized function * @returns The memoized function */ export function useMemoizedFn(fn: T): PickFunction ``` ## Credits * [use-memoized-fn](https://ahooks.js.org/hooks/use-memoized-fn) # hooks: useMount URL: /docs/hooks/use-mount Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-mount/index.mdx A hook to run a function when the component mounts *** title: useMount description: A hook to run a function when the component mounts --------------------------------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts type MountCallback = EffectCallback | (() => Promise void)>) /** * A hook to run a function when the component mounts * @param fn - The function to run when the component mounts * @returns The function to run when the component mounts */ export function useMount(fn: MountCallback): void ``` ## Credits * [use-mount](https://ahooks.js.org/hooks/use-mount) # hooks: useMouse URL: /docs/hooks/use-mouse Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-mouse/index.mdx A hook to track mouse position with optional touch support *** title: useMouse description: A hook to track mouse position with optional touch support ----------------------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts export type UseMouseCoordType = 'page' | 'client' | 'screen' | 'movement' export type UseMouseSourceType = 'mouse' | 'touch' | null export interface UseMouseInitialValue { x: number y: number } export interface UseMouseState extends UseMouseInitialValue { sourceType: UseMouseSourceType } export interface UseMouseOptions { type?: UseMouseCoordType touch?: boolean resetOnTouchEnds?: boolean initialValue?: UseMouseInitialValue window?: Window } /** * Reactive mouse position with optional touch support. */ export function useMouse(options?: UseMouseOptions): UseMouseState ``` ## Credits * [useMouse](https://vueuse.org/core/useMouse/) # hooks: useNetwork URL: /docs/hooks/use-network Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-network/index.mdx A hook to get the network state *** title: useNetwork description: A hook to get the network state -------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation * The network state */ export interface NetworkState { /** * The date when the network state was last updated */ since?: Date /** * Whether the network is online */ online?: boolean /** * The round-trip time in milliseconds */ rtt?: number /** * The type of the network */ type?: string /** * The downlink speed in Mbps */ downlink?: number /** * Whether the user has enabled data saving */ saveData?: boolean /** * The maximum downlink speed in Mbps */ downlinkMax?: number /** * The effective type of the network */ effectiveType?: string } /** * A hook to get the network state * @returns The network state */ export function useNetwork(): NetworkState ``` ## Credits * [use-network](https://ahooks.js.org/hooks/use-network) # hooks: useOs URL: /docs/hooks/use-os Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-os/index.mdx A hook to detect the current operating system *** title: useOs description: A hook to detect the current operating system ---------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts export type UseOSReturnValue = | 'undetermined' | 'macos' | 'ios' | 'windows' | 'android' | 'linux' | 'chromeos' export interface UseOsOptions { /** * Defer browser OS detection until after mount to avoid server/client mismatches. * @default true */ getValueInEffect?: boolean } /** * Detect the current operating system from the browser environment. * * @param options Hook options. * @returns The detected operating system or `undetermined`. */ export function getOS(options?: UseOsOptions): UseOSReturnValue /** * Reactively detect the current operating system from `navigator.userAgent`. * * @param options Hook options. * @returns The detected operating system or `undetermined`. */ export function useOs(options?: UseOsOptions): UseOSReturnValue ``` ## Credits * [use-os](https://github.com/mantinedev/mantine/blob/master/packages/%40mantine/hooks/src/use-os/use-os.ts) # hooks: usePrevious URL: /docs/hooks/use-previous Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-previous/index.mdx A hook to get the previous value of a variable *** title: usePrevious description: A hook to get the previous value of a variable ----------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts export type ShouldUpdateFunc = (prev?: T, next?: T) => boolean const defaultShouldUpdate = (a?: T, b?: T) => !Object.is(a, b) /** * A hook to get the previous value of a state * @param state - The state to get the previous value of * @param shouldUpdate - The function to determine if the state should be updated * @returns The previous value of the state */ export function usePrevious( state: T, shouldUpdate?: ShouldUpdateFunc, ): T | undefined ``` ## Credits * [use-previous](https://ahooks.js.org/hooks/use-previous) # hooks: useResetState URL: /docs/hooks/use-reset-state Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-reset-state/index.mdx A hook to reset a state *** title: useResetState description: A hook to reset a state ------------------------------------ import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts type ResetState = () => void /** * A hook to reset a state * @param initialState - The initial state */ export function useResetState( initialState: S | (() => S), ): readonly [S, Dispatch>, ResetState] ``` ## Credits * [use-reset-state](https://ahooks.js.org/hooks/use-reset-state) # hooks: useQuery URL: /docs/hooks/use-query Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-query/index.mdx useQuery is a hook that allows you to fetch data from an API and cache the results. *** title: useQuery description: useQuery is a hook that allows you to fetch data from an API and cache the results. ------------------------------------------------------------------------------------------------ ## Installation * [use-query](https://github.com/TanStack/query) Copy and paste the following code into your project. # hooks: useScrollLock URL: /docs/hooks/use-scroll-lock Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-scroll-lock/index.mdx A hook to lock the scroll of the body *** title: useScrollLock description: A hook to lock the scroll of the body -------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts interface UseScrollLockOptions { autoLock?: boolean lockTarget?: HTMLElement | string widthReflow?: boolean } interface UseScrollLockReturn { isLocked: boolean lock: () => void unlock: () => void } /** * A hook to lock the scroll of the body * @param options - The options for the hook * @param options.autoLock - Whether to automatically lock the scroll when the component is mounted * @param options.lockTarget - The target element to lock the scroll on * @param options.widthReflow - Whether to reflow the width of the body when the scroll is locked * @returns The scroll lock state and the functions to lock and unlock the scroll */ function useScrollLock(options?: UseScrollLockOptions): UseScrollLockReturn ``` ## Credits * [use-scroll-lock](https://usehooks-ts.com/react-hook/use-scroll-lock) # hooks: useStickToBottom URL: /docs/hooks/use-stick-to-bottom Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-stick-to-bottom/index.mdx A lightweight React Hook intended mainly for AI chat applications, for smoothly sticking to bottom of messages *** title: useStickToBottom description: A lightweight React Hook intended mainly for AI chat applications, for smoothly sticking to bottom of messages --------------------------------------------------------------------------------------------------------------------------- ## Installation * [use-stick-to-bottom](https://github.com/stackblitz-labs/use-stick-to-bottom) Copy and paste the following code into your project. # hooks: useSWR URL: /docs/hooks/use-swr Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-swr/index.mdx A hook to get the latest value of a variable *** title: useSWR description: A hook to get the latest value of a variable --------------------------------------------------------- ## Installation * [use-swr](https://github.com/vercel/swr) Copy and paste the following code into your project. # hooks: useTextSelection URL: /docs/hooks/use-text-selection Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-text-selection/index.mdx A hook to get the text selection and its bounding rect from an element *** title: useTextSelection description: A hook to get the text selection and its bounding rect from an element ----------------------------------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts interface Rect { top: number left: number bottom: number right: number height: number width: number } export interface State extends Rect { text: string } /** * A hook to get the text selection from an element * @param target - The target to get the text selection from */ export function useTextSelection( target?: BasicTarget, ): State ``` ## Credits * [use-text-selection](https://ahooks.js.org/hooks/use-text-selection) # hooks: useThrottle URL: /docs/hooks/use-throttle Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-throttle/index.mdx A hook to throttle a value *** title: useThrottle description: A hook to throttle a value --------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts interface ThrottleOptions { /** * An optional AbortSignal to cancel the throttled function. */ signal?: AbortSignal /** * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both. * If `edges` includes "leading", the function will be invoked at the start of the delay period. * If `edges` includes "trailing", the function will be invoked at the end of the delay period. * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period. * @default ["leading", "trailing"] */ edges?: Array<'leading' | 'trailing'> } /** * A hook to throttle a value * @param value - The value to throttle * @param throttleMs - The throttle time in milliseconds default to 1000 * @param options - The options for the throttle value * @returns The throttled value */ function useThrottle( value: T, throttleMs?: number, options?: ThrottleOptions, ): T ``` ## Credits * [use-throttle](https://ahooks.js.org/hooks/use-throttle) # hooks: useThrottleEffect URL: /docs/hooks/use-throttle-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-throttle-effect/index.mdx A hook to throttle an effect *** title: useThrottleEffect description: A hook to throttle an effect ----------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts interface ThrottleOptions { /** * An optional AbortSignal to cancel the throttled function. */ signal?: AbortSignal /** * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both. * If `edges` includes "leading", the function will be invoked at the start of the delay period. * If `edges` includes "trailing", the function will be invoked at the end of the delay period. * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period. * @default ["leading", "trailing"] */ edges?: Array<'leading' | 'trailing'> } /** /** * A hook to throttle an effect * @param effect - The effect to throttle * @param deps - The dependencies for the effect * @param throttleMs - The throttle time in milliseconds default to 1000 * @param options - The options for the throttle value * @returns The throttled effect */ function useThrottleEffect( effect: EffectCallback, deps: DependencyList, throttleMs?: number, options?: ThrottleOptions, ): void ``` ## Credits * [use-throttle-effect](https://ahooks.js.org/hooks/use-throttle-effect) # hooks: useThrottleFn URL: /docs/hooks/use-throttle-fn Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-throttle-fn/index.mdx A hook to throttle a function *** title: useThrottleFn description: A hook to throttle a function ------------------------------------------ import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.

## API ```ts interface ThrottleOptions { /** * An optional AbortSignal to cancel the throttled function. */ signal?: AbortSignal /** * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both. * If `edges` includes "leading", the function will be invoked at the start of the delay period. * If `edges` includes "trailing", the function will be invoked at the end of the delay period. * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period. * @default ["leading", "trailing"] */ edges?: Array<'leading' | 'trailing'> } /** * A hook to throttle a function * @param fn - The function to throttle * @param throttleMs - The throttle time in milliseconds default to 1000 * @param options - The options for the throttle function * @returns The throttled function */ function useThrottleFn any>( fn: Fn, throttleMs?: number, options?: ThrottleOptions, ): { run: ThrottledFunction<(...args: Parameters) => any> cancel: () => void flush: () => void } ``` ## Credits * [use-throttle-fn](https://ahooks.js.org/hooks/use-throttle-fn) # hooks: useTimeout URL: /docs/hooks/use-timeout Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-timeout/index.mdx A hook that creates a timeout. *** title: useTimeout description: A hook that creates a timeout. ------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook that creates a timeout. * @param fn - The function to execute after the timeout * @param delay - The delay in milliseconds before the function is executed * @returns A function to clear the timeout */ export function useTimeout(fn: () => void, delay: number = 0): () => void ``` ## Credits * [use-timeout](https://ahooks.js.org/hooks/use-timeout) # hooks: useTitle URL: /docs/hooks/use-title Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-title/index.mdx A hook to reactively manage document.title *** title: useTitle description: A hook to reactively manage document.title ------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project.
## API ```ts export interface UseTitleOptions { /** * Observe external changes to `document.title`. * @default false */ observe?: boolean /** * Optional template for the final browser title. * Use `%s` as the placeholder when providing a string template. */ titleTemplate?: string | ((title: string) => string) } /** * Reactively read and update `document.title`. * * @param newTitle Optional initial/controlled title value. * @param options Hook options. */ export function useTitle( newTitle?: string | null, options?: UseTitleOptions, ): readonly [string, Dispatch>] ``` ## Credits * [useTitle](https://github.com/vueuse/vueuse/blob/main/packages/core/useTitle/index.ts) # hooks: useToggle URL: /docs/hooks/use-toggle Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-toggle/index.mdx A hook to toggle a value *** title: useToggle description: A hook to toggle a value ------------------------------------- ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to toggle a value * @param defaultValue - The default value of the value * @param reverseValue - The reverse value of the value * @returns The value and the actions to toggle the value */ function useToggle(): [boolean, Actions] function useToggle(defaultValue: T): [T, Actions] function useToggle( defaultValue: T, reverseValue: U, ): [T | U, Actions] ``` ## Credits * [use-toggle](https://github.com/Andarist/use-toggle) # hooks: useUnmount URL: /docs/hooks/use-unmount Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-unmount/index.mdx A hook to run a function when the component unmounts *** title: useUnmount description: A hook to run a function when the component unmounts ----------------------------------------------------------------- ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to run a function when the component unmounts * @param fn - The function to run when the component unmounts * @returns The function to run when the component unmounts */ export function useUnmount(fn: () => void): void ``` ## Credits * [use-unmount](https://ahooks.js.org/hooks/use-unmount) # hooks: useUpdateEffect URL: /docs/hooks/use-update-effect Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-update-effect/index.mdx A hook to run an effect only when the component updates *** title: useUpdateEffect description: A hook to run an effect only when the component updates -------------------------------------------------------------------- ## Installation Copy and paste the following code into your project.
## API ```ts /** * A hook to run an effect only when the component updates * @param effect - The effect to run * @param deps - The dependencies to run the effect on */ export function useUpdateEffect( effect: EffectCallback, deps: DependencyList, ): void ``` ## Credits * [use-update-effect](https://ahooks.js.org/hooks/use-update-effect) # hooks: useUpdate URL: /docs/hooks/use-update Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-update/index.mdx A hook to force a component to re-render *** title: useUpdate description: A hook to force a component to re-render ----------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to force a component to re-render * @returns A function to force a component to re-render */ export function useUpdate(): () => void ``` ## Credits * [use-update](https://ahooks.js.org/hooks/use-update) # hooks: useWhyDidYouUpdate URL: /docs/hooks/use-why-did-you-update Source: https://raw.githubusercontent.com/Debbl/shadcn-hooks/refs/heads/main/src/registry/hooks/use-why-did-you-update/index.mdx A hook to log the props that changed in a component *** title: useWhyDidYouUpdate description: A hook to log the props that changed in a component ---------------------------------------------------------------- import { Demo01 } from './demo/demo-01' ## Installation Copy and paste the following code into your project. ## API ```ts /** * A hook to log the props that changed in a component * @param componentName - The name of the component * @param props - The props that changed in the component */ export function useWhyDidYouUpdate(componentName: string, props: IProps): void ``` ## Credits * [use-why-did-you-update](https://ahooks.js.org/hooks/use-why-did-you-update)