Skip to content

Commit 9c1d9c4

Browse files
Fix sidebar not scrolling to active chapter on page load (#8)
The sidebar stayed at the top regardless of which chapter was active, forcing users to manually scroll to find their current position. Add data-active attribute to the active link and scroll it into view on page load. Co-authored-by: Ona <no-reply@ona.com>
1 parent aab0360 commit 9c1d9c4

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/components/Sidebar.astro

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const base = import.meta.env.BASE_URL.replace(/\/$/, "");
5151
? "text-[var(--text-primary)] font-medium bg-[var(--bg-tertiary)]"
5252
: "text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-hover)]",
5353
]}
54+
{...(isActive ? { "data-active": "", "aria-current": "page" } : {})}
5455
>
5556
{ch.chapter && (
5657
<span class="font-[var(--font-mono)] text-[11px] text-[var(--text-tertiary)] mt-0.5 shrink-0 w-5">
@@ -69,6 +70,15 @@ const base = import.meta.env.BASE_URL.replace(/\/$/, "");
6970
</aside>
7071

7172
<script>
73+
// Scroll the active sidebar link into view on page load
74+
const activeLink = document.querySelector('#sidebar a[data-active]');
75+
if (activeLink) {
76+
// Use requestAnimationFrame to ensure layout is complete before scrolling
77+
requestAnimationFrame(() => {
78+
activeLink.scrollIntoView({ block: 'center', behavior: 'instant' });
79+
});
80+
}
81+
7282
document.querySelectorAll('#sidebar a').forEach(link => {
7383
link.addEventListener('click', () => {
7484
if (window.innerWidth < 1024) {

0 commit comments

Comments
 (0)