-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (59 loc) · 2.5 KB
/
Copy pathscript.js
File metadata and controls
65 lines (59 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Mintlify positions the fixed #sidebar with an inline `top` computed in a
window "scroll" listener (handleFooterAndSidebarScrollTop): when the footer
enters the viewport it shifts the sidebar up (negative top) to clear it, and
nothing but another scroll event ever recomputes it. If the page height
changes after a measurement (the /introduction body is client-rendered, so
the pre-hydration page is short with the footer in view), the sidebar is
left stuck above the viewport. style.css reserves min-height:100vh on
#content so that short state can't be painted or measured in the first
place; this watcher is a backstop for residual cases (e.g. the browser
clamping a restored scroll position against the not-yet-grown page). Heal by
re-running Mintlify's own listener with a synthetic scroll event: no actual
scrolling, so nothing visibly moves - and unlike the old 1px scroll nudge it
also works when the page is too short to scroll at all, which is exactly the
state the bug is born in. */
function sidebarLooksBroken() {
const sidebar = document.getElementById("sidebar");
if (!sidebar) return false;
const top = parseFloat(sidebar.style.top);
if (!(top < 0)) return false;
// Negative top is legitimate while the footer is in (or near) view; broken
// is negative top with the footer nowhere near the viewport.
const footer = document.getElementById("footer");
if (!footer) return true;
return footer.getBoundingClientRect().top > window.innerHeight + 200;
}
function watchSidebar() {
let watchedSidebar = null;
// cap repairs per page as a guard against fighting a future Mintlify version
let repairs = 0;
const repair = () => {
if (repairs++ < 10) window.dispatchEvent(new Event("scroll"));
};
const styleObserver = new MutationObserver(() => {
if (sidebarLooksBroken()) repair();
});
const attach = () => {
// React can recreate #sidebar across SPA navs; re-attach when it changes
const sidebar = document.getElementById("sidebar");
if (sidebar && sidebar !== watchedSidebar) {
watchedSidebar = sidebar;
styleObserver.disconnect();
styleObserver.observe(sidebar, {
attributes: true,
attributeFilter: ["style"],
});
}
if (sidebarLooksBroken()) repair();
};
new MutationObserver(() => {
repairs = 0;
attach();
}).observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-current-path"],
});
window.addEventListener("load", attach);
attach();
}
watchSidebar();