diff --git a/docs/.vitepress/theme/banner.ts b/docs/.vitepress/theme/banner.ts index 140ec53bd8..b4b8666283 100644 --- a/docs/.vitepress/theme/banner.ts +++ b/docs/.vitepress/theme/banner.ts @@ -51,12 +51,25 @@ function render(b: BannerData): void { el.appendChild(a); } + const syncHeight = () => { + document.documentElement.style.setProperty( + "--vp-layout-top-height", + `${el.offsetHeight}px`, + ); + }; + + const observer = + typeof ResizeObserver !== "undefined" + ? new ResizeObserver(syncHeight) + : null; + const btn = document.createElement("button"); btn.type = "button"; btn.setAttribute("aria-label", "Dismiss"); btn.textContent = "\u00d7"; btn.addEventListener("click", () => { localStorage.setItem(STORAGE_KEY, b.id); + observer?.disconnect(); el.remove(); document.documentElement.style.removeProperty("--vp-layout-top-height"); }); @@ -64,10 +77,6 @@ function render(b: BannerData): void { document.body.prepend(el); - requestAnimationFrame(() => { - document.documentElement.style.setProperty( - "--vp-layout-top-height", - `${el.offsetHeight}px`, - ); - }); + requestAnimationFrame(syncHeight); + observer?.observe(el); }