Skip to Content
ohooks

useMeasure

Measures the dimensions and box insets of a DOM element via a callback ref and ResizeObserver.

Loading preview...

The useMeasure hook returns a [ref, rect] tuple that observes a DOM element with ResizeObserver and provides its width, height, and box insets (top, left, right, bottom, x, y), automatically re-subscribing when conditionally rendered elements mount or unmount.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useMeasure.json

Usage

API Reference

UseMeasureOptions

Configuration options for the observed CSS box model.

Property
Specification
box?
uniondefault:'content-box'
CSS box model to observe
enabled?
uniondefault:true
Whether measurement observation is active

MeasureRect

Measured dimensions and box coordinates returned as the second tuple element.

Property
Specification
xrequired
number
Horizontal offset of the measured box in pixels
yrequired
number
Vertical offset of the measured box in pixels
widthrequired
number
Width of the measured box in pixels
heightrequired
number
Height of the measured box in pixels
toprequired
number
Top inset of the measured box in pixels
rightrequired
number
Right edge coordinate (left + width) in pixels
bottomrequired
number
Bottom edge coordinate (top + height) in pixels
leftrequired
number
Left inset of the measured box in pixels