ScrollSpy: deterministic active-section detection#42557
Open
mdo wants to merge 1 commit into
Open
Conversation
Replace the scroll-direction + sequential-offsetTop heuristic (which depended on IntersectionObserver entry order and mis-highlighted when several sections shared the viewport) with a deterministic geometric model: the active section is the deepest one whose top has scrolled to or above an activation line near the top of the scroll root, read fresh from getBoundingClientRect on each (rAF-throttled) scroll. At the bottom the last section wins; above the first nothing is active. Also: settle on scrollend (with a scroll-idle fallback) to restore the URL hash via replaceState and move focus to the target after a smooth-scroll navigation; escape section ids via parseSelector so dotted /special-character ids no longer throw; add a topMargin option for the activation line. Supersedes #41016. Fixes #37858, #39198, #39248, #36387, #40526.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
ScrollSpy chose the active section with a scroll-direction flag + a sequential
offsetToptiebreaker that mutated state while walking IntersectionObserver entries. IO delivers entries in no guaranteed order, so when several sections shared the viewport (e.g. the docs right-rail TOC) the highlight was wrong. Coarse defaults also left stale gaps, smooth scroll suppressed the URL hash, focus wasn't managed, and ids with dots/special characters threw.Change
Deterministic, geometry-based detection:
getBoundingClientRect()on each rAF-throttled scroll — order-independent, immune to section height, no ties. At the bottom the last section wins; above the first, nothing is active.scrollend(with a scroll-idle fallback) settles the final state, restores the URL hash viahistory.replaceState, and moves focus to the target section after a smooth-scroll click.parseSelector, so dotted/special-character ids no longer throw.topMarginoption positions the activation line (default12%);rootMargin/threshold/target/smoothScrollremain.Supersedes #41016 (its ratio-based selection mis-handles tall sections). Fixes #37858, #39198, #39248, #36387, #40526.
Verification
The docs right-rail TOC uses Bootstrap ScrollSpy directly, so it's fixed transitively.