JSPM

watch-size

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 43954
  • Score
    100M100P100Q145761F
  • License CC0-1.0

Watch the size changes of a DOM element

Package Exports

  • watch-size

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 (watch-size) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Watch Size PostCSS

NPM Version Build Status Support Chat

Watch Size lets you watch the size changes of a DOM element using the watchSize function.

Usage

Add Watch Size to your project:

npm install watch-size

Use watchSize() to rewrite your queries:

import watchSize from 'watch-size';

const stop = watchSize(
  document.querySelector('.box:nth-child(2)')
  ({ width, height }) => { /* callback */ }
);

/* use stop() to end listening */

How does it work?

The watchSize function attaches hidden, over-sized DOM elements to your target with scroll listeners that are triggered whenever the element resizes. This allows the listener to avoid polling, and to truly respond to size changes. This technique can be used in all browsers going back to Internet Explorer 9.

The entire script is about 400 bytes when minified and gzipped.