Captain is currently in ALPHA. We are happy to get your feedback
Core API
Hooks Resize Observer

Module: hooks/resize-observer

Table of contents

Interfaces

Functions

Functions

useResizeObserver

useResizeObserver<T>(reference): Size

Custom hook for monitoring and responding to changes in size of a referenced HTML element.

This hook provides a convenient way to get the current width and height of an element when it resizes. The hook observes the element referred to by the passed reference and updates the size state whenever the element's size changes.

Type parameters

NameTypeDescription
Textends HTMLElementThe HTML element type that extends HTMLElement which is targeted by the ref object.

Parameters

NameTypeDescription
referenceRefObject<T>React ref object pointing to the target element to be observed.

Returns

Size

The size of the observed element. The Size object contains two properties: width and height, which represent the current dimensions of the element in pixels. Both properties are initially undefined and get updated upon each resize event.

Example

const ref = useRef<HTMLDivElement>(null);
const size = useResizeObserver(ref);
console.log(size); // { width: 1024, height: 768 }

Defined in

hooks/resize-observer.ts:35 (opens in a new tab)