fn

useElementSize

v0.0.14testeddemo

Reactive 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

ParameterTypeDescription
targetMaybeComputedElementRefElement to measure (ref, getter, or component instance)
initialSize?ElementSizeInitial size, restored when the element detaches
options?UseElementSizeOptionsOptions forwarded to ResizeObserver (box, window)

Returns

UseElementSizeReturnReactive width, height, and a stop handle
PropertyTypeDescription
widthShallowRef<number>
heightShallowRef<number>
stop() => void