You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR implements a new scrollspy mechanism to highlight entries in the
secondary sidebar TOC; fixes#1965, and builds on top of #2107.
## Approach
Every link in the secondary sidebar TOC is a reference to a section in
the main article. This PR works by adding an `IntersectionObserver` that
observes the _heading_ at the top of every one of these sections. As the
user scrolls down, the `IntersectionObserver` triggers a callback when
new section headings come into view; an object containing the visibility
of the various sections is kept up to date by these events. The first
visible heading is the one that needs to have its respective TOC entry
highlighted.
## Other Notes
- Unrelated, but I kept getting errors coming from a place in
`setupAnnouncementBanner` where a `const` was being assigned to. I fixed
this just to make it stop complaining. If you'd rather have this in a
separate PR, I'm happy to do so.
- The `IntersectionObserver` is set to observe element crossings with
the viewport, but because there's a sticky menu bar at the top of the
page I added a `rootMargin` to pad down the top edge so that element
crossings happen below that menu bar. Testing by hand this seems to work
pretty well.
- I also tested clicking on entries in the TOC, and as long as you click
something high enough up on the page (not close to the bottom), the
correct element gets highlighted as expected. Of course, if you have a
bunch of sections right at the bottom, the one you click on won't be
highlighted:
```
-------------
| Section A |
| |
--|-----------|---
⁞ | | ⁞
⁞ | | ⁞
⁞ | | ⁞
⁞ | Section B | ⁞ <-- Viewport is still considered to be focused on Section A,
⁞ | Section C | ⁞ which extends all the way to the start of Section B
⁞ | end | ⁞
------------------
```
---------
Co-authored-by: gabalafou <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
rootMargin: `-${headerHeight}px 0px -70% 0px`,// Use -70% for the bottom margin so that intersection events happen in only the top third of the viewport
1107
+
threshold: 0,// Trigger as soon as the heading goes into (or out of) the top 30% of the viewport
0 commit comments