Skip to content

Commit 2efd674

Browse files
authored
fix: resolve incorrect link activation for first and last sections (#869)
1 parent 4f819ed commit 2efd674

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

apps/svelte.dev/src/routes/docs/[...path]/OnThisPage.svelte

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,33 @@
1010
afterNavigate(() => {
1111
current = location.hash.slice(1);
1212
headings = content.querySelectorAll('h2');
13+
update(); // Ensure active link is set correctly on navigation
1314
});
1415
16+
// Update function to activate the correct section link
1517
function update() {
16-
// a section is 'active' when it crosses the threshold
1718
const threshold = (innerHeight * 1) / 3;
19+
let found = false;
1820
19-
for (let i = 0; i < headings.length; i += 1) {
21+
for (let i = 0; i < headings.length; i++) {
2022
const heading = headings[i];
2123
const next = headings[i + 1];
2224
25+
// If the current heading is above the threshold and the next heading is below it
2326
if (
24-
next &&
2527
heading.getBoundingClientRect().top < threshold &&
26-
next.getBoundingClientRect().top > threshold
28+
(!next || next.getBoundingClientRect().top > threshold)
2729
) {
2830
current = heading.id;
31+
found = true;
2932
break;
3033
}
3134
}
35+
36+
// Handle case when scrolled to the top of the page
37+
if (!found && scrollY === 0) {
38+
current = '';
39+
}
3240
}
3341
</script>
3442

0 commit comments

Comments
 (0)