Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2176,17 +2176,16 @@ export class App extends Protocol<BaseContext> {
* ```
*/
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.
Expand All @@ -2201,7 +2200,40 @@ export class App extends Protocol<BaseContext> {
// 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);
Expand All @@ -2222,7 +2254,20 @@ export class App extends Protocol<BaseContext> {
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);
};
}

/**
Expand Down