Skip to content

Commit c5ac2eb

Browse files
committed
custom sidebar
1 parent e4bf005 commit c5ac2eb

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/components/Sidebar.astro

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const props = Astro.props as Props;
1212
</div>
1313

1414
<script client:load>
15-
const ACTIVE_SELECTOR = "[data-ecency-sidebar] a[aria-current='page']";
15+
const SIDEBAR_SELECTOR = "[data-ecency-sidebar]";
16+
const ACTIVE_SELECTOR = `${SIDEBAR_SELECTOR} a[aria-current='page']`;
17+
const SECTION_SELECTOR = `${SIDEBAR_SELECTOR} details`;
1618

1719
const prefersReducedMotion = window.matchMedia?.(
1820
"(prefers-reduced-motion: reduce)"
@@ -30,8 +32,22 @@ const props = Astro.props as Props;
3032
});
3133
};
3234

35+
const toggleSectionsForActivePage = () => {
36+
const activeLink = document.querySelector(ACTIVE_SELECTOR);
37+
if (!activeLink) return;
38+
39+
const sections = Array.from(
40+
document.querySelectorAll<HTMLDetailsElement>(SECTION_SELECTOR)
41+
);
42+
43+
sections.forEach((section) => {
44+
section.open = section.contains(activeLink);
45+
});
46+
};
47+
3348
const run = () => {
3449
// initial attempt
50+
toggleSectionsForActivePage();
3551
scrollActiveLink("smooth");
3652

3753
// if you want to be extra-safe with late-rendered content, you could
@@ -47,7 +63,17 @@ const props = Astro.props as Props;
4763

4864
// Handle Astro/Starlight client-side navigation
4965
window.addEventListener("astro:after-swap", () => {
66+
toggleSectionsForActivePage();
5067
// no animation on fast internal nav if you prefer:
5168
scrollActiveLink("auto");
5269
});
5370
</script>
71+
72+
<style>
73+
[data-ecency-sidebar] {
74+
/* Account for the fixed header so the final sidebar items stay visible */
75+
max-height: calc(100vh - var(--sl-nav-height));
76+
overflow-y: auto;
77+
padding-block-end: 1rem;
78+
}
79+
</style>

0 commit comments

Comments
 (0)