Skip to content

Commit 5c6d2cf

Browse files
jdxclaude
andcommitted
docs: keep --vp-layout-top-height in sync via ResizeObserver
The height CSS variable was set once in a single rAF and never updated afterward. On window resize, orientation change, or text reflow the banner height could change, leaving the VitePress nav offset stale and causing overlap or a visible gap. Observe the banner element and resync the variable whenever its size changes; disconnect the observer when the banner is dismissed. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 519d421 commit 5c6d2cf

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

docs/.vitepress/theme/banner.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,32 @@ function render(b) {
4343
el.appendChild(a);
4444
}
4545

46+
const syncHeight = () => {
47+
document.documentElement.style.setProperty(
48+
"--vp-layout-top-height",
49+
`${el.offsetHeight}px`,
50+
);
51+
};
52+
53+
const observer =
54+
typeof ResizeObserver !== "undefined"
55+
? new ResizeObserver(syncHeight)
56+
: null;
57+
4658
const btn = document.createElement("button");
4759
btn.type = "button";
4860
btn.setAttribute("aria-label", "Dismiss");
4961
btn.textContent = "\u00d7";
5062
btn.addEventListener("click", () => {
5163
localStorage.setItem(STORAGE_KEY, b.id);
64+
observer?.disconnect();
5265
el.remove();
5366
document.documentElement.style.removeProperty("--vp-layout-top-height");
5467
});
5568
el.appendChild(btn);
5669

5770
document.body.prepend(el);
5871

59-
requestAnimationFrame(() => {
60-
document.documentElement.style.setProperty(
61-
"--vp-layout-top-height",
62-
`${el.offsetHeight}px`,
63-
);
64-
});
72+
requestAnimationFrame(syncHeight);
73+
observer?.observe(el);
6574
}

0 commit comments

Comments
 (0)