useElementSize
Tracks an element's width and height, updating live via ResizeObserver, for layouts CSS alone can't compute.
useElementSize returns an element's { width, height } in pixels and keeps it current as the element is resized. Reach for it when a component has to compute something from its own size: a column count, how many items fit before a "+N", a canvas resolution. Gallery's masonry columns and the pill controls' overflow counter both measure this way.
import { useElementSize } from '@42-central/ui-react';
import { useRef } from 'react';
const ref = useRef<HTMLDivElement>(null);
const size = useElementSize(ref);
<div ref={ref}>{size ? `${size.width} × ${size.height}` : null}</div>;If you only need to restyle something at a size threshold, a CSS container query does it without JavaScript. If you need a boolean for a width threshold, useContainerQuery gives you one directly.
Usage
Drag the box below to resize it. The content box is what's left inside the padding and border; the border box includes them.
Drag the bottom-right corner to resize this box.
- content
- …
- border
- …
Code
const ref = useRef(null);
const content = useElementSize(ref);
const border = useElementSize(ref, { box: 'border' });
<div ref={ref} className="resize overflow-auto border p-4">
content {content?.width} × {content?.height}
border {border?.width} × {border?.height}
</div>Server rendering
The hook returns undefined on the server and until the first observation. A server can't know the element's size, so undefined is the only value both sides agree on during hydration. ResizeObserver reports the current size as soon as it starts observing, so the real value arrives right after mount. Render a fallback for undefined, the layout you'd show at an unknown width.
Content box or border box
The default box: 'content' measures inside the padding and border. It's the box a CSS size container query measures, and the one to use when you lay out children. box: 'border' includes the padding and border: use it when you place the element itself, such as stacking cards by their outer height. The observer watches the box you pick, so with 'content' a padding-only change doesn't update the size.
Sizes are physical. In a vertical writing mode, width is still the horizontal extent.
Don't measure what you set
Don't feed the size back into the measured element's own size: if a padding or a width depends on the measurement, the measurement changes, and the layout can oscillate. Measure a wrapper and size its children instead. CSS container queries follow the same rule: a query can't style its own container.
Measuring conditionally
enabled: false mounts no observer and returns undefined. Use it when only some configurations need a measurement, like Gallery, which observes only for responsive masonry columns.
const size = useElementSize(ref, { enabled: layout === 'masonry' });The ref is read when the hook's effect runs, so the element must be mounted in the same render that mounts the hook or turns enabled on.
API
Prop
Type
Returns { width: number; height: number } | undefined, in CSS pixels. A resize that leaves both numbers unchanged returns the same object, so memos keyed on it don't recompute.