-
Notifications
You must be signed in to change notification settings - Fork 9
Keep active sidebar item visible across navigation #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+50
−0
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| (function () { | ||
| if (typeof window === "undefined") return; | ||
|
|
||
| // Mintlify's markup is not a stable contract; take the first scrollable match. | ||
| function findSidebar() { | ||
| var candidates = document.querySelectorAll( | ||
| "aside, nav[aria-label='Sidebar'], aside[aria-label='Sidebar'], #sidebar" | ||
| ); | ||
| for (var i = 0; i < candidates.length; i++) { | ||
| if (candidates[i].scrollHeight > candidates[i].clientHeight + 1) { | ||
| return candidates[i]; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function showActive() { | ||
| var sb = findSidebar(); | ||
| if (!sb) return; | ||
| var link = sb.querySelector( | ||
| "a[aria-current='page'], a[aria-current='true'], a[data-active='true']" | ||
| ); | ||
| // Guard against acting on the previous page's link before the DOM updates. | ||
| if (!link || link.pathname !== location.pathname) return; | ||
|
|
||
| var sbRect = sb.getBoundingClientRect(); | ||
| var linkRect = link.getBoundingClientRect(); | ||
| var margin = 24; | ||
|
|
||
| if (linkRect.top < sbRect.top + margin) { | ||
| sb.scrollTop -= sbRect.top + margin - linkRect.top; | ||
| } else if (linkRect.bottom > sbRect.bottom - margin) { | ||
| sb.scrollTop += linkRect.bottom - (sbRect.bottom - margin); | ||
| } | ||
| } | ||
|
|
||
| // Mintlify has no navigation event, but it rewrites data-current-path on | ||
| // <html> on every client-side route change. That mutation is our signal. | ||
| var html = document.documentElement; | ||
| var last = html.getAttribute("data-current-path"); | ||
| new MutationObserver(function () { | ||
| var cur = html.getAttribute("data-current-path"); | ||
| if (cur === last) return; | ||
| last = cur; | ||
| requestAnimationFrame(showActive); | ||
| }).observe(html, { attributes: true, attributeFilter: ["data-current-path"] }); | ||
|
|
||
| if (document.readyState === "complete") showActive(); | ||
| else window.addEventListener("load", showActive); | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No retry after race condition guard causes silent failure
Medium Severity
On SPA navigation,
data-current-pathon<html>triggers theMutationObserver, which schedules a singlerequestAnimationFrame(showActive). If Mintlify's React render cycle hasn't yet updatedaria-currenton sidebar links by the time that frame fires, the guard on line 24 (link.pathname !== location.pathname) correctly bails out to avoid acting on the old link — but there is no retry. The feature silently becomes a no-op for that navigation. The PR description mentions "polling until the sidebar mounts plus one 150ms re-apply" but neither mechanism is actually implemented.Additional Locations (1)
sidebar-scroll.js#L40-L46Reviewed by Cursor Bugbot for commit e2ef311. Configure here.