Skip to main content

useResizeObserver

Purpose

The useResizeObserver hook allows you to observe when elements change size. It is a wrapper around the ResizeObserver API, making it easier to use in Stimulus components and automating registering and cleaning up the observer on connect and disconnect.

Side Effects

The useResizeObserver hook will create an instance of ResizeObserver when it is called, and registers the observer with the DOM, observing the element passed in.

When the component is unmounted, the observer will be disconnected and cleaned up.

Usage

useResizeObserver

ParametersTypePurposeDefault
controllerControllerThe instance of the controller to install the behaviour on. This allows the mixin to automatically register cleanup on the controller's disconnect.this
elementHTMLElementThe element to observe.
handler (Optional)FunctionThe callback to invoke when the changes occur. Accepts a single parameter which is an array of ResizeObserverEntry Reference.
options (Optional)ResizeObserverOptions ReferenceThe raw options to pass to to the observe call when initializing the ResizeObserver