What is resize-observer-polyfill?
The resize-observer-polyfill package provides a polyfill for the ResizeObserver API, which allows you to be notified when an element's content rectangle has changed its size, and thus react accordingly. This is particularly useful for responsive designs and element-specific layout updates.
What are resize-observer-polyfill's main functionalities?
Observing element size changes
This feature allows you to observe changes in the size of an element. The callback function is executed whenever the size of the observed element changes, providing you with the new dimensions and allowing you to perform any necessary adjustments.
var ro = new ResizeObserver(entries => {
for (let entry of entries) {
const cr = entry.contentRect;
console.log('Element:', entry.target);
console.log(`Element size: ${cr.width}px x ${cr.height}px`);
console.log(`Element padding: ${cr.top}px ; ${cr.left}px`);
}
});
ro.observe(document.getElementById('myElement'));
Other packages similar to resize-observer-polyfill
element-resize-detector
element-resize-detector is an npm package that offers a way to detect when an element has been resized. It differs from resize-observer-polyfill in that it uses scroll events and other tricks to detect size changes instead of the native ResizeObserver API, which can be useful for broader browser support.
css-element-queries
css-element-queries provides a way to attach event listeners to elements for various purposes, including element resize detection. It includes a ResizeSensor class that can be used to detect when an element's size changes. This package offers additional functionalities like element query conditions, which go beyond the scope of resize-observer-polyfill.
ResizeObserver Polyfill
A polyfill for the Resize Observer API.
Implementation is based on the MutationObserver (no polling unless DOM changes) with a fall back to a continuous dirty checking cycle if the first one is not supported. Doesn't modify observed elements. Handles CSS transitions/animations, <textarea>
resizes and can possibly observe changes caused by dynamic CSS pseudo-classes, e.g. by :hover
.
Compliant with the spec and the native implementation. The size is 2.6kb when minified and gzipped.
Live demo (has style problems in IE10 and lower).
Installation
From NPM:
npm install resize-observer-polyfill --save-dev
From Bower:
bower install resize-observer-polyfill --save-dev
Or just grab one of the pre-built versions from dist
.
Browser Support
Polyfill has been tested and known to work in the following browsers:
- Chrome 40+ , native since v54
- Firefox 37+
- Safari 9+, including mobile
- Opera 30+
- Edge 13+
- Internet Explorer 9+
NOTE: Internet Explorer 8 and its earlier versions are not supported.
Usage Examples
It's recommended to use this library in the form of a ponyfill, which doesn't inflict modifications of the global object.
import ResizeObserver from 'resize-observer-polyfill';
const ro = new ResizeObserver((entries, observer) => {
for (const entry of entries) {
const {left, top, width, height} = entry.contentRect;
console.log('Element:', entry.target);
console.log(`Element's size: ${ width }px x ${ height }px`);
console.log(`Element's paddings: ${ top }px ; ${ left }px`);
}
});
ro.observe(document.body);
Though you always can extend the global object if you need it.
import ResizeObserver from 'resize-observer-polyfill';
window.ResizeObserver = ResizeObserver;
Package's main file is a ES5 UMD module and it will be dynamically substituted by the ES6 version for those bundlers that are aware of the jnext:main field, e.g. for Rollup.
Note: global versions (index.global
and dist/ResizeObserver.global
) are deprecated and will be removed in the next major release.
Limitations
- CSS changes caused by dynamic pseudo-classes, e.g.
:hover
and :focus
, are not tracked. As a workaround you can add a short transition which would trigger the transitionend
event when an element receives one of the former classes (example). - Delayed transitions will receive only one notification with the latest dimensions of an element.
Building and testing
To build polyfill. Creates UMD bundle in the dist
folder:
npm run build
To run a code style test:
npm run test:lint
Running unit tests:
npm run test:spec
To test in a browser that is not present in karma's config file:
npm run test:spec:custom
Testing against a native implementation:
npm run test:spec:native
NOTE: after you invoke spec:native
and spec:custom
commands head to the http://localhost:9876/debug.html
page.