UI & Layout
useMeasure
About
Measures both the inner and outer dimensions of any DOM element in a performant way and updates when dimensions change. Uses ResizeObserver for efficient size change detection, providing comprehensive measurement data including inner dimensions (clientWidth/clientHeight), outer dimensions (offsetWidth/offsetHeight), and scroll dimensions for both inner and outer measurements.
Examples
Basic usage
With callback and debouncing
Responsive component layout with inner/outer awareness
Comparing inner vs outer measurements
Disabled measurements
Arguments
| Argument | Type | Description | Default value |
|---|---|---|---|
| options | UseMeasureOptions | Configuration options object | {} |
UseMeasureOptions
| Property | Type | Description | Default value |
|---|---|---|---|
| debounce | number | Number of milliseconds to debounce measurements | 0 |
| disabled | boolean | Whether measurements are disabled | false |
| onMeasure | (measurements: UseMeasureMeasurements) => void | Callback function called when dimensions change | undefined |
Return
Returns a tuple [ref, measurements] with the following structure:
| Index | Property | Type | Description |
|---|---|---|---|
| 0 | ref | CallbackRef | Callback ref to attach to the DOM element you want to measure |
| 1 | measurements | UseMeasureMeasurements | Object containing all measurement data |
UseMeasureMeasurements
| Property | Type | Description |
|---|---|---|
| innerWidth | number | Inner width (clientWidth) - content area width excluding scrollbars |
| innerHeight | number | Inner height (clientHeight) - content area height excluding scrollbars |
| innerScrollWidth | number | Total scrollable width (scrollWidth) - width of the entire content area |
| innerScrollHeight | number | Total scrollable height (scrollHeight) - height of the entire content area |
| outerWidth | number | Outer width (offsetWidth) - total element width including borders, padding, and scrollbars |
| outerHeight | number | Outer height (offsetHeight) - total element height including borders, padding, and scrollbars |
| outerScrollWidth | number | Total scrollable outer width - outer width of the entire scrollable content |
| outerScrollHeight | number | Total scrollable outer height - outer height of the entire scrollable content |
Notes
Browser Compatibility
- Requires ResizeObserver support (available in all modern browsers)
- Falls back gracefully in SSR environments
- Warns when ResizeObserver is not available
Performance Considerations
- Uses ResizeObserver for efficient resize detection instead of polling
- Supports debouncing to limit measurement frequency during rapid changes
- Automatically cleans up observers when components unmount
- Observes with multiple box types to track both inner and outer changes
Usage Tips
- The hook returns
0for all dimensions until a DOM element is attached via theref - Use the
disabledoption to temporarily stop measurements without losing the ref - The
onMeasurecallback is called after state updates, useful for logging or external state management - Debouncing is especially useful for expensive operations triggered by dimension changes
- Inner measurements exclude borders and padding, while outer measurements include them
- Use outer measurements when you need to know the total space an element occupies
- Use inner measurements when you need to know the available content area
Inner vs Outer Measurements
- Inner measurements (
innerWidth,innerHeight): Content area only, excludes borders, padding, and scrollbars - Outer measurements (
outerWidth,outerHeight): Complete element size including borders, padding, and scrollbars - Scroll measurements: Available for both inner and outer, showing total scrollable content dimensions
- Practical use: Outer measurements are useful for layout calculations, inner measurements for content positioning
SSR Compatibility
- Safe to use in server-side rendering environments
- Provides appropriate warnings when ResizeObserver or window is unavailable
- Returns default values (0) for all measurements during SSR