Skip to content

Commit da67192

Browse files
authored
Merge pull request #10963 from marmelab/doc-headless-improve-sidebar-scroll
[chore] [Doc] Improve sidebar scrolling on headless docs
2 parents f667a89 + 66a3020 commit da67192

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

docs_headless/src/components/CustomSidebar.astro

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@ const { sidebar } = Astro.locals.starlightRoute;
1414
"a[aria-current='page']"
1515
);
1616
if (currentItem) {
17-
sidebarContainer.scrollTo({
18-
top: currentItem.offsetTop - sidebarContainer.offsetTop,
19-
behavior: 'smooth',
20-
});
17+
// Check if the current item is already visible in the sidebar viewport
18+
const containerRect = sidebarContainer.getBoundingClientRect();
19+
const itemRect = currentItem.getBoundingClientRect();
20+
21+
const isItemVisible =
22+
itemRect.top >= containerRect.top &&
23+
itemRect.bottom <= containerRect.bottom;
24+
25+
// Only scroll if the item is not already visible
26+
if (!isItemVisible) {
27+
sidebarContainer.scrollTo({
28+
top: currentItem.offsetTop - sidebarContainer.offsetTop,
29+
behavior: 'auto',
30+
});
31+
}
2132
}
2233
});
2334
</script>

0 commit comments

Comments
 (0)