Skip to content

Commit 02b32cf

Browse files
committed
Highlight the section you are AT in the left rail, not the last one visible
The rail's current-section pick walked every heading visible in the viewport and kept the last h2 — so clicking a section landed correctly but highlighted whichever later section peeked in from below. Current is now the last h2 at or above the top reading line (matching the 72px scroll-padding a clicked anchor settles at), falling back to the first visible section when everything is below the line.
1 parent ed46882 commit 02b32cf

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

‎apps/sim/lib/workspace-files/artifact-stylesheet.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,19 @@ export const SIM_ARTIFACT_SHELL = `<script>
823823
for (let i = first; i <= last; i++) tocLinks[i].classList.add('is-active')
824824
thumb.style.setProperty('--track-top', positions[first][0] + 'px')
825825
thumb.style.setProperty('--track-bottom', positions[last][1] + 'px')
826+
// The left rail highlights the section CONTAINING the reading position
827+
// (the last h2 at or above the top line, matching the 72px
828+
// scroll-padding a clicked anchor lands at) — not the furthest section
829+
// merely visible below, which mis-highlighted after every click.
826830
let section = null
827-
for (let i = 0; i <= last; i++) if (headings[i].tagName === 'H2') section = headings[i]
831+
for (let i = 0; i < headings.length; i++) {
832+
if (headings[i].tagName === 'H2' && rects[i] <= viewTop + 1) section = headings[i]
833+
}
834+
if (!section) {
835+
for (let i = first; i <= last; i++) {
836+
if (headings[i].tagName === 'H2') { section = headings[i]; break }
837+
}
838+
}
828839
if (section) {
829840
const link = byId.get(section.id)
830841
if (link) link.classList.add('is-active')

0 commit comments

Comments
 (0)