R
fn

useResizeObserver

v0.0.15testeddemo

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
targetanyElement(s) to observe
callbackResizeObserverCallbackInvoked with the observer entries
options?UseResizeObserverOptionsOptions

Returns

UseResizeObserverReturn`isSupported`, `isActive`, `pause`, `resume`, and `stop`