|
14 | 14 | return null; |
15 | 15 | } |
16 | 16 |
|
17 | | - function findActiveLink(sb) { |
18 | | - return sb.querySelector( |
19 | | - "a[aria-current='page'], a[aria-current='true'], a[data-active='true']" |
20 | | - ); |
21 | | - } |
22 | | - |
23 | 17 | function showActive() { |
24 | 18 | var sb = findSidebar(); |
25 | | - if (!sb) return false; |
26 | | - var link = findActiveLink(sb); |
27 | | - if (!link) return false; |
| 19 | + if (!sb) return; |
| 20 | + var link = sb.querySelector( |
| 21 | + "a[aria-current='page'], a[aria-current='true'], a[data-active='true']" |
| 22 | + ); |
| 23 | + // Guard against acting on the previous page's link before the DOM updates. |
| 24 | + if (!link || link.pathname !== location.pathname) return; |
28 | 25 |
|
29 | 26 | var sbRect = sb.getBoundingClientRect(); |
30 | 27 | var linkRect = link.getBoundingClientRect(); |
|
35 | 32 | } else if (linkRect.bottom > sbRect.bottom - margin) { |
36 | 33 | sb.scrollTop += linkRect.bottom - (sbRect.bottom - margin); |
37 | 34 | } |
38 | | - return true; |
39 | 35 | } |
40 | 36 |
|
41 | | - // Sidebar may not be mounted yet after a route change; retry until it is, |
42 | | - // then re-apply once to survive a late re-render that resets scroll. |
43 | | - function pollShow() { |
44 | | - var attempts = 0; |
45 | | - var iv = setInterval(function () { |
46 | | - attempts++; |
47 | | - if (showActive()) { |
48 | | - clearInterval(iv); |
49 | | - setTimeout(showActive, 150); |
50 | | - } else if (attempts > 30) { |
51 | | - clearInterval(iv); |
52 | | - } |
53 | | - }, 50); |
54 | | - } |
55 | | - |
56 | | - var origPush = history.pushState; |
57 | | - history.pushState = function () { |
58 | | - var r = origPush.apply(this, arguments); |
59 | | - pollShow(); |
60 | | - return r; |
61 | | - }; |
62 | | - var origReplace = history.replaceState; |
63 | | - history.replaceState = function () { |
64 | | - var r = origReplace.apply(this, arguments); |
65 | | - pollShow(); |
66 | | - return r; |
67 | | - }; |
68 | | - window.addEventListener("popstate", pollShow); |
69 | | - |
70 | | - if (document.readyState === "complete") { |
71 | | - pollShow(); |
72 | | - } else { |
73 | | - window.addEventListener("load", pollShow); |
74 | | - } |
| 37 | + // Mintlify has no navigation event, but it rewrites data-current-path on |
| 38 | + // <html> on every client-side route change. That mutation is our signal. |
| 39 | + var html = document.documentElement; |
| 40 | + var last = html.getAttribute("data-current-path"); |
| 41 | + new MutationObserver(function () { |
| 42 | + var cur = html.getAttribute("data-current-path"); |
| 43 | + if (cur === last) return; |
| 44 | + last = cur; |
| 45 | + requestAnimationFrame(showActive); |
| 46 | + }).observe(html, { attributes: true, attributeFilter: ["data-current-path"] }); |
| 47 | + |
| 48 | + if (document.readyState === "complete") showActive(); |
| 49 | + else window.addEventListener("load", showActive); |
75 | 50 | })(); |
0 commit comments