fn

useResizeObserver

v0.0.14testeddemo

Reports changes to the dimensions of an element via ResizeObserver. Accepts a single target or an array of (reactive) targets. The observer is recreated only when the resolved elements change, and can be paused/resumed.

Examples

ts
useResizeObserver(el, ([entry]) => {
  console.log(entry.contentRect.width);
});
ts
const { pause, resume } = useResizeObserver([el1, el2], (entries) => {
  // react to multiple targets
}, { box: 'border-box' });

Demo

Loading demo…

Signature

ts
export function useResizeObserver(
  target: MaybeComputedElementRef | MaybeComputedElementRef[],
  callback: ResizeObserverCallback,
  options: UseResizeObserverOptions ={ ... }

Parameters

ParameterTypeDescription
targetMaybeComputedElementRef | MaybeComputedElementRef[]Element(s) to observe
callbackResizeObserverCallbackInvoked with the observer entries
options?UseResizeObserverOptionsOptions

Returns

UseResizeObserverReturnisSupported, isActive, pause, resume, and stop
PropertyTypeDescription
isSupportedReadonly<Ref<boolean>>Whether ResizeObserver is supported in the current environment
isActiveReadonly<Ref<boolean>>Whether the observer is currently active
pause() => voidTemporarily stop observing (disconnects the observer) while keeping the target watcher alive, so observing can be resumed later
resume() => voidResume observing after a pause
stop() => voidPermanently stop observing and tear down the target watcher