fn
useElementSize
v0.0.14testeddemoReactive size of an element, backed by ResizeObserver.
Measures synchronously on mount, handles SVG elements via getBoundingClientRect,
and sums multiple box fragments (e.g. multi-column layouts).
Examples
ts
const el = useTemplateRef('el');
const { width, height } = useElementSize(el);ts
const { width, height, stop } = useElementSize(el, { width: 100, height: 100 }, { box: 'border-box' });Demo
Loading demo…
Signature
ts
export function useElementSize(
target: MaybeComputedElementRef,
initialSize: ElementSize ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
target | MaybeComputedElementRef | Element to measure (ref, getter, or component instance) |
initialSize? | ElementSize | Initial size, restored when the element detaches |
options? | UseElementSizeOptions | Options forwarded to ResizeObserver (box, window) |
Returns
UseElementSizeReturnReactive width, height, and a stop handle| Property | Type | Description |
|---|---|---|
width | ShallowRef<number> | — |
height | ShallowRef<number> | — |
stop | () => void | — |