shadcn-ahooks

useSize

A hook that observes size change of an element.

Overview

A hook that observes size change of an element.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSize.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSize.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSize.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSize.json

A hook that observes size change of an element.

Examples

Default usage

Pass in the DOM element

API

const size = useSize(target);

Params

PropertyDescriptionTypeDefault
targetDOM element or ref objectElement | (() => Element) | MutableRefObject<Element>-

Result

| Property | Description | Type | Default | | -- | - | | - | | size | Size of the element | { width: number, height: number } \| undefined | { width: target.clientWidth, height: target.clientHeight } \| undefined |

On this page