JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6940264
  • Score
    100M100P100Q228617F
  • License MIT

A polyfill for the Resize Observer API

Package Exports

  • resize-observer-polyfill
  • resize-observer-polyfill/dist/ResizeObserver
  • resize-observer-polyfill/dist/ResizeObserver.es.js
  • resize-observer-polyfill/dist/ResizeObserver.global
  • resize-observer-polyfill/dist/ResizeObserver.global.js
  • resize-observer-polyfill/index.global

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resize-observer-polyfill) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ResizeObserver Polyfill

Build Status

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 non-delayed CSS transitions/animations and can optionally observe resizing of a <textarea> element along with changes caused by the :hover pseudo-class.

Compliant with the spec and the native implementation. The size is 3.0kb 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 the 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 cr = entry.contentRect;

        console.log('Element:', entry.target);
        console.log(`Element's size: ${cr.width}px x ${cr.height}px`);
        console.log(`Element's paddings: ${cr.top}px ; ${cr.left}px`);
    }
});

ro.observe(document.body);

Though you always can extend the global object manually 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 or module fields, e.g. for Rollup or Webpack 2.

Note: global versions (index.global and dist/ResizeObserver.global) are deprecated and will be removed in the next major release.

Configuration

ResizeObserver class additionally implements following static accessor property:

continuousUpdates

By default things like resizing of a <textarea> element, changes caused by the CSS :hover pseudo-class and delayed CSS transitions are not tracked. To handle them you can set ResizeObserver.continuousUpdates = true which in turn will start a continuous update cycle which runs every 100 milliseconds (as a RAF callback). Keep in mind that this is going to affect the performance.

NOTE: changes made to this property affect all existing and future instances of ResizeObserver.

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 karmas' 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.