source: imaps-frontend/node_modules/resize-observer-polyfill/README.md@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.4 KB
Line 
1ResizeObserver Polyfill
2=============
3
4[![Build Status][travis-image]][travis-url]
5
6
7A polyfill for the Resize Observer API.
8
9Implementation is based on the MutationObserver and uses Mutation Events as a fall back if the first one is not supported, so there will be no polling unless DOM changes. Doesn't modify observed elements. Handles CSS transitions/animations and can possibly observe changes caused by dynamic CSS pseudo-classes, e.g. by `:hover`.
10
11Follows the [spec](http://rawgit.com/WICG/ResizeObserver/master/index.html) and the native implementation. The size is _2.44 KiB_ when minified and gzipped.
12
13[Live demo](http://que-etc.github.io/resize-observer-polyfill) (has style problems in IE10 and lower).
14
15## Installation
16
17From NPM:
18
19```sh
20npm install resize-observer-polyfill --save-dev
21```
22
23~~From Bower:~~ (will be removed with the next major release)
24
25```sh
26bower install resize-observer-polyfill --save-dev
27```
28
29
30## Browser Support
31
32Polyfill has been tested in the following browsers:
33
34[![Build Status](https://saucelabs.com/browser-matrix/que-etc.svg)](https://saucelabs.com/beta/builds/303f5344a7214ba5b62bc7079a15d376)
35
36**NOTE:** Internet Explorer 8 and its earlier versions are not supported.
37
38## Usage Example
39
40It's recommended to use this library in the form of a [ponyfill](https://github.com/sindresorhus/ponyfill), which doesn't inflict modifications of the global object.
41
42```javascript
43import ResizeObserver from 'resize-observer-polyfill';
44
45const ro = new ResizeObserver((entries, observer) => {
46 for (const entry of entries) {
47 const {left, top, width, height} = entry.contentRect;
48
49 console.log('Element:', entry.target);
50 console.log(`Element's size: ${ width }px x ${ height }px`);
51 console.log(`Element's paddings: ${ top }px ; ${ left }px`);
52 }
53});
54
55ro.observe(document.body);
56```
57
58Package's main file is a ES5 [UMD](https://github.com/umdjs/umd) bundle that will be swapped with the ES6 modules version for those bundlers that are aware of the [module](https://github.com/rollup/rollup/wiki/pkg.module) field, e.g. for [Rollup](https://github.com/rollup/rollup) or webpack 2+.
59
60**Note**: global version of the polyfill (`dist/ResizeObserver.global`) is deprecated and will be removed in the next major release.
61
62## Observation Strategy
63
64As mentioned above, this implementation primarily (but not solely) relies on Mutation Observer with a fallback to Mutation Events for IE 9 and IE 10.
65
66Speaking of Mutation Events as a fallback approach: they might not be as ugly as they are being rendered, particularly when their calls are batched, throttled and there is no need to analyze changes. Given that, they won't interrupt browser's reflow/repaint cycles (same for MutationObserver) and may even outperform Internet Explorer's implementation of MO causing little to no performance degradation. In contemporary browsers (Chrome, Firefox, etc.) Mutation Observer slows down [the suite](https://jsfiddle.net/que_etc/gaqLe8rn/) that includes 200 iterations of adding/removing elements, changing attributes and modifying text data by less than 1%. Internet Explorer gives different results with MO slowing down the same suite by 2-3% while Mutation Events show the difference of ~0.6%.
67
68As for the reasons why other approaches, namely the iframe/object and `scroll` strategies, were ruled out:
69* They require the observed element to be non-statically positioned.
70* You can't apply them directly to quite a number of elements: `<img>`, `<input>`, `<textarea>`, `<canvas>`, `<tr>`, `<tbody>`, `<thead>`, `<table>`, etc. For most of them you would need to keep an extra `<div>` wrapper and almost all instances of the SVGGraphicsElement will be out of scope.
71* The ResizeObserver spec requires to deliver notifications when a non-empty visible element becomes hidden, i.e. when either this element directly or one of its parent nodes receive the `display: none` state. Same goes for when it's being removed from or added to the DOM. It's not possible to handle these cases merely by using former approaches, so you'd still need to either subscribe for DOM mutations or to continuously check the element's state.
72
73And though every approach has its own limitations, I reckon that it'd be too much of a trade-off to have those constraints when building a polyfill.
74
75## Limitations
76
77* Notifications are delivered ~20ms after actual changes happen.
78* Changes caused by dynamic pseudo-classes, e.g. `:hover` and `:focus`, are not tracked. As a workaround you could add a short transition which would trigger the `transitionend` event when an element receives one of the former classes ([example](https://jsfiddle.net/que_etc/7fudzqng/)).
79* Delayed transitions will receive only one notification with the latest dimensions of an element.
80
81## Building and Testing
82
83To build polyfill. Creates UMD bundle in the `dist` folder:
84
85```sh
86npm run build
87```
88
89To run a code style test:
90```sh
91npm run test:lint
92```
93
94Running unit tests:
95```sh
96npm run test:spec
97```
98
99To test in a browser that is not present in karma's config file:
100```sh
101npm run test:spec:custom
102```
103
104Testing against a native implementation:
105```sh
106npm run test:spec:native
107```
108
109**NOTE:** after you invoke `spec:native` and `spec:custom` commands head to the `http://localhost:9876/debug.html` page.
110
111[travis-image]: https://travis-ci.org/que-etc/resize-observer-polyfill.svg?branch=master
112[travis-url]: https://travis-ci.org/que-etc/resize-observer-polyfill
Note: See TracBrowser for help on using the repository browser.