watch-resize
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

👀 watchResize

code style: airbnb code style: prettier

Watch any DOM element for size changes without polyfills.

💁🏼‍♂️ Introduction

A zero-dependency, cross-compatible ResizeObserver alternative that doesn't require polyfills. You can watch any element for size changes based on its bounding box.

🔗 Installation

Install via yarn or npm:

yarn add watch-resize
npm install watch-resize

🛠️ Usage

import { watchResize } from 'watch-resize';

const target = document.getElementById('my-element');

watchResize(target, ({ element, event, prevBoundingClientRect, destroy }) => {
  // Do stuff here for each "resize"
})).then(() => {
  // Once the promise resolves, the resize watcher has successfully mounted!
});

An object implementing ResizePayload is passed to subscribe handler:

export interface ResizePayload<T extends HTMLElement> {
  element: T;
  event: UIEvent;
  prevBoundingClientRect: ClientRect | DOMRect; // The previous result of "element.getBoundingClientRect()".
  destroy: () => void; // Unobserves the element and cleans up event listeners
}

Readme

Keywords

none

Package Sidebar

Install

npm i watch-resize

Weekly Downloads

55

Version

3.0.2

License

MIT

Unpacked Size

28.7 kB

Total Files

13

Last publish

Collaborators

  • smithki