This package provides a robust polyfill for the W3C Resize Observer API, enabling consistent functionality across web browsers, including those without native support. Currently at stable version 1.5.1, the library maintains an active development pace, primarily focusing on bug fixes and minor enhancements, as evidenced by recent releases like 1.5.1 and 1.5.0. Its core strength lies in its non-polling observation strategy, which leverages MutationObserver with Mutation Events as a fallback, ensuring efficient performance without constant DOM checks. It differentiates itself by accurately following the Resize Observer specification, handling CSS transitions/animations, and observing changes from dynamic CSS pseudo-classes without modifying observed elements. The polyfill is lightweight, weighing only 2.44 KiB minified and gzipped, and is designed to be used as a ponyfill to prevent global object pollution.
npm install resize-observer-polyfillVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to import and instantiate ResizeObserver, then observe the document body for dimension changes, logging the new size and padding information.
Use `import ResizeObserver from 'resize-observer-polyfill';` in your build process, leveraging module bundlers.
Migrate to npm for package management: `npm install resize-observer-polyfill --save-dev`.
Ensure your code and TypeScript types are compatible with `DOMRectReadOnly` for `entry.contentRect` properties (e.g., `width`, `height`, `x`, `y`, `top`, `left`, `right`, `bottom`).
For projects requiring IE8 support, an alternative solution for element resize detection would be necessary, as this polyfill does not cover it.
Remove `continuousUpdates` option usage and instead listen for `transitionend` events if continuous updates during transitions are specifically required, though the polyfill generally handles transitions implicitly.
Upgrade to `resize-observer-polyfill@1.3.1` or newer. Ensure Webpack is configured to correctly handle UMD or ES module bundles without relying on the deprecated `module` field behavior.
For ES Modules: `import ResizeObserver from 'resize-observer-polyfill';`. For CommonJS: `const { default: ResizeObserver } = require('resize-observer-polyfill');` or ensure the polyfill is globally available if that's the intended usage pattern (though not recommended).Update your TypeScript types or code to reflect the `DOMRectReadOnly` interface. Ensure properties like `left`, `top`, `right`, `bottom`, `x`, `y` are accessed correctly as defined by `DOMRectReadOnly`.
Implement protective measures in your callback to prevent infinite loops. For example, by setting an 'expected size' and not triggering further resizes if the element is already at that size, or by throttling/debouncing the actions within the observer callback. Ensure that style changes within the callback do not immediately cause another dimension change.
No dependency data recorded yet.