Skip to content

fix(docs): drive the TOC scroll spy with an interval poll instead of events - #308

Merged
vshengbro merged 1 commit into
masterfrom
fix/toc-spy-interval-poll
Oct 10, 2026
Merged

vshengbro merged 1 commit into
masterfrom
fix/toc-spy-interval-poll

Conversation

@vshengbro

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #307. The scroll spy shipped there activates the first TOC entry on
load, but scroll-driven activation is unreliable: delegated scroll listeners on
document can stay silent while the container scrolls, and requestAnimationFrame is
paused in hidden tabs. Plain-load activation also raced the TOC's first render, so
the marker sometimes never appeared.

The spy now polls the scroll container's scrollTop on a 100ms setInterval — one
property read per tick, early-out when nothing changed — with hashchange and a
MutationObserver kept as immediate accelerators. A tick also re-marks when the
tracked node was detached by a re-render, so the active state self-heals instead
of vanishing.

Verification

Deterministic across repeated fresh loads and scrolls on a local docs build:

  • load: first entry active at font-weight 700
  • scrolled to bottom: last entry active at 700
  • back to top: first entry active again
  • hover on an inactive entry: stays 400
  • repeated trials produce identical results

cargo fmt --check clean, clippy 0 warnings, full test suite passing.
Version 0.28.22, workspace pins realigned.

…events

The event-driven spy could not be relied on: delegated scroll listeners on
document can stay silent while the container scrolls, and rAF is paused in
hidden tabs, so scroll-driven activation worked only intermittently and
plain-load activation raced the TOC's first render.

The spy now polls the scroll container's scrollTop on a 100ms setInterval
(one property read per tick, early-out when unchanged), with hashchange and
a MutationObserver as immediate accelerators. A tick also re-marks when the
tracked node was detached by a re-render, so activation self-heals instead
of disappearing. Verified deterministic across repeated loads and scrolls:
first entry active on load, last entry at the bottom, current section in
between, hover on inactive entries stays at weight 400.
@ghfind-review ghfind-review Bot added the review: high ghfind author score; see https://ghfind.com label Oct 10, 2026
@vshengbro
vshengbro merged commit 3be8f57 into master Oct 10, 2026
8 checks passed
@vshengbro
vshengbro deleted the fix/toc-spy-interval-poll branch October 10, 2026 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: high ghfind author score; see https://ghfind.com

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant