Files
redash/viz-lib/src/services/resizeObserver.ts
Elad Ossadon c290864ccd Convert viz-lib to TypeScript (#5310)
Co-authored-by: ts-migrate <>
2020-12-15 18:21:37 -08:00

33 lines
857 B
TypeScript

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: any, callback: any) {
if (node && !items.has(node)) {
const shouldTrigger = items.size === 0;
items.set(node, { callback });
if (shouldTrigger) {
checkItems();
}
return () => items.delete(node);
}
return () => {};
}