diff --git a/src/app.ts b/src/app.ts index be5cbb2cb..52687eed3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2176,17 +2176,16 @@ export class App extends Protocol { * ``` */ setupSizeChangedNotifications() { - let scheduled = false; + let frameId: number | undefined; let lastWidth = 0; let lastHeight = 0; const sendBodySizeChanged = () => { - if (scheduled) { + if (frameId !== undefined) { return; } - scheduled = true; - requestAnimationFrame(() => { - scheduled = false; + frameId = requestAnimationFrame(() => { + frameId = undefined; const html = document.documentElement; // Measure actual content height by temporarily overriding html sizing. @@ -2201,7 +2200,40 @@ export class App extends Protocol { // destroying their scroll positions. const originalHeight = html.style.height; html.style.height = "max-content"; - const height = Math.ceil(html.getBoundingClientRect().height); + const body = document.body; + const rootRect = html.getBoundingClientRect(); + // Body overflow includes positioned descendants that do not grow either + // observed box. Avoid using the root scroll height, which can include + // the host's existing viewport height and prevent the view shrinking. + let overflowHeight = + body.scrollHeight > body.clientHeight + ? body.getBoundingClientRect().top - + rootRect.top + + body.scrollHeight + : 0; + // A portal positioned against the root may also be outside the body's + // scroll area: either an absolute child of the body, or an absolute + // element inside a zero-height portal container (as Mantine renders + // popovers). Fixed overlays and the root scroll height, which includes + // the viewport minimum, are not counted. + const includeBottom = (element: Element) => { + overflowHeight = Math.max( + overflowHeight, + element.getBoundingClientRect().bottom - rootRect.top, + ); + }; + for (const child of Array.from(body.children)) { + if (getComputedStyle(child).position === "absolute") { + includeBottom(child); + } else if (child.getBoundingClientRect().height === 0) { + for (const element of Array.from(child.querySelectorAll("*"))) { + if (getComputedStyle(element).position === "absolute") { + includeBottom(element); + } + } + } + } + const height = Math.ceil(Math.max(rootRect.height, overflowHeight)); html.style.height = originalHeight; const width = Math.ceil(window.innerWidth); @@ -2222,7 +2254,20 @@ export class App extends Protocol { resizeObserver.observe(document.documentElement); resizeObserver.observe(document.body); - return () => resizeObserver.disconnect(); + // Portals can be inserted, removed, or toggled without a box resize. + // Observe only the body so the temporary html height does not reschedule us. + const mutationObserver = new MutationObserver(sendBodySizeChanged); + mutationObserver.observe(document.body, { + attributes: true, + childList: true, + subtree: true, + }); + + return () => { + resizeObserver.disconnect(); + mutationObserver.disconnect(); + if (frameId !== undefined) cancelAnimationFrame(frameId); + }; } /**