@clayui/shared
Version:
ClayShared component
30 lines (29 loc) • 627 B
JavaScript
const rectAttrs = [
"bottom",
"height",
"left",
"right",
"top",
"width"
];
function rectChanged(a = {}, b = {}) {
return rectAttrs.some((prop) => a[prop] !== b[prop]);
}
let rafId;
function run(node, state) {
const newRect = node.getBoundingClientRect();
if (rectChanged(newRect, state.rect)) {
state.rect = newRect;
state.callback(state.rect);
}
rafId = window.requestAnimationFrame(() => run(node, state));
}
function observeRect(node, callback) {
run(node, { callback, hasRectChanged: false, rect: void 0 });
return () => {
cancelAnimationFrame(rafId);
};
}
export {
observeRect
};