diff --git a/src/client/stdlib/resize.ts b/src/client/stdlib/resize.ts index cd5463d20..3e6fec428 100644 --- a/src/client/stdlib/resize.ts +++ b/src/client/stdlib/resize.ts @@ -9,19 +9,21 @@ export function resize( let currentRender = 0; let currentDisplay = 0; let currentWidth: number; - const observer = new ResizeObserver(async ([entry]) => { - const {width, height} = entry.contentRect; - if (render.length === 1 && width === currentWidth) return; // ignore height-only change - currentWidth = width; - const childRender = ++currentRender; - const child = width > 0 ? await render(width, height) : null; // don’t render if detached - if (currentDisplay > childRender) return; // ignore stale renders - currentDisplay = childRender; - while (div.lastChild) div.lastChild.remove(); - if (child == null) return; // clear if nullish - if (render.length !== 1 && isElement(child)) child.style.position = "absolute"; // prevent feedback loop if height is used - div.append(child); - }); + const observer = new ResizeObserver(([entry]) => + requestAnimationFrame(async () => { + const {width, height} = entry.contentRect; + if (render.length === 1 && width === currentWidth) return; // ignore height-only change + currentWidth = width; + const childRender = ++currentRender; + const child = width > 0 ? await render(width, height) : null; // don’t render if detached + if (currentDisplay > childRender) return; // ignore stale renders + currentDisplay = childRender; + while (div.lastChild) div.lastChild.remove(); + if (child == null) return; // clear if nullish + if (render.length !== 1 && isElement(child)) child.style.position = "absolute"; // prevent feedback loop if height is used + div.append(child); + }) + ); observer.observe(div); invalidation?.then(() => observer.disconnect()); return div;