-
Notifications
You must be signed in to change notification settings - Fork 352
Highlight correct TOC entry on page load #2185
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
Merged
drammock
merged 9 commits into
pydata:main
from
gabalafou:highlight-correct-toc-entry-on-page-load
Jul 28, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ae2fbbc
On page load, highlight TOC entry if there's a hash in the URL
gabalafou acc6f63
fix Safari
gabalafou 681db94
use the same function for link click and page load
gabalafou fb17d30
comment
gabalafou fe06298
comment
gabalafou 8fd3f56
comment
gabalafou b5c3dc4
comment
gabalafou ae49b46
No need to wait for window.location.hash to update
gabalafou 07c69ba
sync even when user modifies hash directly in the browser address bar
gabalafou 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 |
---|---|---|
|
@@ -1013,20 +1013,79 @@ function setupArticleTocSyncing() { | |
return; | ||
} | ||
|
||
// When the website visitor clicks a link in the TOC, we want that link to be | ||
// highlighted/activated, NOT whichever TOC link the intersection observer | ||
// callback would otherwise highlight, so we turn off the observer and turn it | ||
// back on later. | ||
// Create a boolean variable that allows us to turn off the intersection | ||
// observer (and then later back on). When the website visitor clicks an | ||
// in-page link, we want that entry in the TOC to be highlighted/activated, | ||
// NOT whichever TOC link the intersection observer callback would otherwise | ||
// highlight. | ||
let disableObserver = false; | ||
pageToc.addEventListener("click", (event) => { | ||
|
||
function temporarilyDisableObserver(time) { | ||
disableObserver = true; | ||
const clickedTocLink = tocLinks.find((el) => el.contains(event.target)); | ||
activate(clickedTocLink); | ||
setTimeout(() => { | ||
// Give the page ample time to finish scrolling, then re-enable the | ||
// intersection observer. | ||
disableObserver = false; | ||
}, 1000); | ||
}, time); | ||
} | ||
|
||
/** | ||
* If the provided URL hash fragment (beginning with "#") matches an entry in | ||
* the page table of contents, highlight that entry and temporarily disable | ||
* the intersection observer while the page scrolls to the corresponding | ||
* heading. | ||
*/ | ||
function syncTocHash(hash) { | ||
if (hash.length > 1) { | ||
const matchingTocLink = tocLinks.find((tocLink) => tocLink.hash === hash); | ||
if (matchingTocLink) { | ||
// It's important to disable the intersection observer before | ||
// highlighting the TOC link and its corresponding article heading. This | ||
// is because the browser takes a little time to scroll to the article | ||
// heading, and while scrolling, it could trigger intersection events | ||
// that cause some other link in the table of contents to be | ||
// highlighted. | ||
temporarilyDisableObserver(1000); | ||
activate(matchingTocLink); | ||
} | ||
} | ||
} | ||
|
||
// On page load... | ||
// | ||
// When the page loads, sync the page's table of contents. | ||
syncTocHash(window.location.hash); | ||
|
||
// On navigation to another part of the page... | ||
// | ||
// When the user navigates to another part of the page, sync the page's table | ||
// of contents. | ||
window.addEventListener("hashchange", () => { | ||
// By the time this event is fired, window.location.hash has already been | ||
// updated with the new hash | ||
syncTocHash(window.location.hash); | ||
}); | ||
|
||
// On return to the same part of the page... | ||
// | ||
// The hashchange event will handle most cases where we need to sync the table | ||
// of contents with a hash link click. But there is one edge case it doesn't | ||
// handle, which is when the user clicks an internal page link whose hash is | ||
// already in the browser address bar. For example, the user loads the page at | ||
// #first-heading, scrolls to the bottom of the page, then clicks in the | ||
// sidebar table of contents to go back up to the first heading in the | ||
// article. In this case the "hashchange" event will not fire. Nonetheless, we | ||
// want to guarantee that the TOC entry for the first heading gets | ||
// highlighted. Note we cannot rely exclusively on the "click" event for all | ||
// internal page navigations because it will not fire in the edge case where | ||
// the user modifies the hash directly in the browser address bar. | ||
window.addEventListener("click", (event) => { | ||
const link = event.target.closest("a"); | ||
if ( | ||
link && | ||
link.hash === window.location.hash && | ||
link.origin === window.location.origin | ||
) { | ||
syncTocHash(link.hash); | ||
} | ||
}); | ||
|
||
/** | ||
|
@@ -1127,7 +1186,7 @@ function setupArticleTocSyncing() { | |
} | ||
|
||
observer = new IntersectionObserver(callback, options); | ||
headingsToTocLinks.keys().forEach((heading) => { | ||
Array.from(headingsToTocLinks.keys()).forEach((heading) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was actually causing a runtime error in Safari for me. Only the latest versions of Safari support forEach on the map iterator. |
||
observer.observe(heading); | ||
}); | ||
} | ||
|
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.
In a previous version of this PR, I used a timeout of 0 ms to call the TOC sync function. In some browsers, this gave
window.location
time to equal the just-clicked URL. But in some other browsers (Ubuntu + Firefox), it was not enough time. I realized that there's no reason to wait. We already have the hash fragment, so just sync it immediately.