Skip to content

Commit f5ecdd2

Browse files
Fix deep-link anchor offset and improve layout stability in Firefox (#697)
1 parent 9272d59 commit f5ecdd2

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

.vitepress/theme/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ export default {
4848
};
4949
onMounted(() => {
5050
initZoom();
51+
// Re-scroll to hash after fonts/layout settle. Firefox does not re-scroll
52+
// after late layout shifts on huge pages (e.g. common.html), so the
53+
// initial anchor jump lands far off. Run only on initial load.
54+
if (inBrowser && location.hash) {
55+
const id = decodeURIComponent(location.hash.slice(1));
56+
const fontsReady = document.fonts?.ready ?? Promise.resolve();
57+
const loadReady =
58+
document.readyState === "complete"
59+
? Promise.resolve()
60+
: new Promise((r) =>
61+
window.addEventListener("load", r, { once: true })
62+
);
63+
Promise.all([fontsReady, loadReady]).then(() => {
64+
requestAnimationFrame(() =>
65+
requestAnimationFrame(() => {
66+
const el = document.getElementById(id);
67+
if (el) el.scrollIntoView();
68+
})
69+
);
70+
});
71+
}
5172
});
5273
watch(
5374
() => route.path,

.vitepress/theme/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@
157157
display: inline; /* block by default set by vitepress */
158158
}
159159

160+
/* Fix deep-link anchor offset under fixed navbar.
161+
Also guards against Firefox layout-shift drift on huge pages (e.g. common.html). */
162+
html {
163+
scroll-padding-top: calc(var(--vp-nav-height, 64px) + 16px);
164+
}
165+
:target {
166+
scroll-margin-top: calc(var(--vp-nav-height, 64px) + 16px);
167+
}
168+
160169
/* Make content area a little wider to improve tables */
161170
.content-container {
162171
max-width: 100% !important;

0 commit comments

Comments
 (0)