Skip to content

Commit

Permalink
fix(with-resize-observer): prevent layout shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-sanders-cc committed Dec 1, 2022
1 parent e47bab5 commit 07cc728
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/mixins/with-resize-observer/with-resize-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ export function withResizeObserver (ParentClass) {
if (window.ResizeObserver == null) {
return;
}
let first = true;
const ro = new window.ResizeObserver(() => {
window.requestAnimationFrame(() => {
// NOTE: We could use entries[0].borderBoxSize.inlineSize but not supported in Chrome, Safari or polyfill
const { width } = this.getBoundingClientRect();
// NOTE: We could use entries[0].borderBoxSize.inlineSize but not supported in Chrome, Safari or polyfill
const { width } = this.getBoundingClientRect();
if (first) {
first = false;
this._onResize({ width });
});
}
else {
window.requestAnimationFrame(() => {
this._onResize({ width });
});
}
});
ro.observe(this);
this._unobserveResize = () => ro.unobserve(this);
Expand Down

0 comments on commit 07cc728

Please sign in to comment.