Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

resize-observer

pelotoncycle646.5kApache-2.01.0.4TypeScript support: included

An implementation and polyfill of the Resize Observer draft.

ResizeObserver, polyfill, ponyfill, element-resize

readme

resize-observer

Build Status NPM Version

npm bundle size (minified + gzip)

This library aims to be a faithful implementation and ponyfill of the Resize Observer draft. An optional polyfill option exists as well.

Installation

resize-observer is available on NPM and Yarn:

> npm install resize-observer
> yarn add resize-observer

Setup

As a ponyfill/module

resize-observer does not install itself by default. As such, you can import it like any other module:

import { ResizeObserver } from 'resize-observer';

const ro = new ResizeObserver(() => console.log('resize observed!'));
ro.observe(document.body);

As a polyfill

resize-observer provides a file that can be referenced from your browser that automatically installs ResizeObserver on the global window object. Both minified and non-minified versions exist, and are found in the package under the dist/ directory:

<script src="/node_modules/resize-observer/dist/resize-observer.js"></script>
<script type="text/javascript">
  const ro = new window.ResizeObserver(() => alert('Observing things is super cool!'));
  /* use your ResizeObserver! */
</script>

A install method is also provided to do the same within your own code:

import { install } from 'resize-observer';

install();

const ro = new window.ResizeObserver(() => alert('Observe all the things!'));
/* ... */

Note: Calling install will always overwrite window.ResizeObserver. If you'd like to only install resize-observer when it doesn't already exist, you can add a simple check before calling install:

import { install } from 'resize-observer';

if (!window.ResizeObserver) install();

/* ... */

TypeScript definitions

resize-observer is written in TypeScript. The definition files are included in the package and should be picked up automatically.

changelog

Change Log

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning. All minor versions less than 1.0.0 are breaking changes.

1.0.4 - 2021-11-29

  • Fix: Allow elements created in one window context to be observed when mounted to the DOM of another window context (#100)

1.0.3 - 2021-11-15

  • Fix: Allow elements to be observed across window contexts (#99)

1.0.2 - 2021-05-24

Fixed

  • Fix: Enable reuse of ResizeObserver (#96)
  • Fix allowing double tracking of the first element (#94; thgreasi)

1.0.1 - 2021-05-24

Fixed

  • Fix memory & cpu leak after disconnect() (#93; thgreasi)

Chores

  • Update various dev dependencies (#25, #27, #29, #31; dependabot)

1.0.0 - 2018-11-28

Added

  • Added unpkg field in package.json. You can now load dist/resize-observer.min.js via <script src="https://unpkg.com/resize-observer"></script>. (#14; renaatdemuynck)

Changed

  • Include src/ files in the npm package (#12)
  • resize-observer is now a ponyfill
  • Rewritten in TypeScript
  • Type declaration files are now generated
  • Migrate from Travis to CircleCI for PR tests (#13)
  • Removed defective coverage badge (f65192b)

1.0.0-alpha.1 - 2018-08-21

Changed

  • Include src/ files in the npm package (#12)

1.0.0-alpha.0 - 2018-08-13

Changed

  • resize-observer is now a ponyfill
  • Rewritten in TypeScript
  • Type declaration files are now generated

0.1.1 - 2018-08-10

Fixed

  • Added typings to package.json

0.1.0 - 2018-03-25

Fixed

  • new ResizeObserver, ResizeObserver.prototype.observe, and , ResizeObserver.prototype.unobserve should throw a TypeError when called with no arguments or the wrong argument type.

0.0.4 - 2016-10-26

Fixed

  • Use setTimeout instead of setInterval for fallback when requestAnimationFrame doesn't exist

0.0.3 - 2016-06-10

Changed

  • Updated CHANGELOG with links to diffs

Fixed

  • Removed erroneous [ from Typescript definition file
  • Improved Typescript definition file to more closely describe the interface

0.0.2 - 2016-06-08

Added

  • tests using mocha, chai, mock-browser, mock-raf

Fixed

  • get basic tests running
  • several critical typos and bugs that kept anything from working
  • CHANGELOG is now CHANGELOG.md for proper viewing in Github

0.0.1 - 2016-06-07

Added

  • base untested resize-observer
  • resize-observer typescript definition file
  • readme
  • license
  • npm package