Files
redash/client/app/services/resizeObserver.js
Arik Fraimovich 56d3be2248 Prettier all the Javascript code & GitHub Action (#4433)
* Prettier all the JS files

* Add GitHub Action to autoformat code pushed to master

* Fix eslint violation due to formatting.

* Remove GitHub actions for styling

* Add restyled.io config
2019-12-11 17:05:38 +02:00

33 lines
847 B
JavaScript

const items = new Map();
function checkItems() {
if (items.size > 0) {
items.forEach((item, node) => {
const bounds = node.getBoundingClientRect();
// convert to int (because these numbers needed for comparisons), but preserve 1 decimal point
const width = Math.round(bounds.width * 10);
const height = Math.round(bounds.height * 10);
if (item.width !== width || item.height !== height) {
item.width = width;
item.height = height;
item.callback(node);
}
});
setTimeout(checkItems, 100);
}
}
export default function observe(node, callback) {
if (node && !items.has(node)) {
const shouldTrigger = items.size === 0;
items.set(node, { callback });
if (shouldTrigger) {
checkItems();
}
return () => items.delete(node);
}
return () => {};
}