Skip to content

Commit e4bf005

Browse files
committed
different sidebar
1 parent aa8b0f8 commit e4bf005

3 files changed

Lines changed: 56 additions & 52 deletions

File tree

astro.config.mjs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@ export default defineConfig({
99
integrations: [
1010
starlight({
1111
title: "Ecency",
12-
head: [
13-
{
14-
tag: "script",
15-
attrs: {
16-
src: "/sidebar-scroll.js",
17-
type: "module",
18-
},
19-
},
20-
],
12+
components: {
13+
Sidebar: "./src/components/Sidebar.astro",
14+
},
2115
social: {
2216
github: "https://github.com/ecency",
2317
},

public/sidebar-scroll.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/components/Sidebar.astro

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
import Default from "@astrojs/starlight/components/Sidebar.astro";
3+
import type { Props } from "@astrojs/starlight/props";
4+
5+
const props = Astro.props as Props;
6+
---
7+
8+
<div data-ecency-sidebar>
9+
<Default {...props}>
10+
<slot />
11+
</Default>
12+
</div>
13+
14+
<script client:load>
15+
const ACTIVE_SELECTOR = "[data-ecency-sidebar] a[aria-current='page']";
16+
17+
const prefersReducedMotion = window.matchMedia?.(
18+
"(prefers-reduced-motion: reduce)"
19+
).matches;
20+
21+
const scrollActiveLink = (behavior = "auto") => {
22+
const activeLink = document.querySelector(ACTIVE_SELECTOR);
23+
if (!activeLink || typeof activeLink.scrollIntoView !== "function") return;
24+
25+
// Scroll the active link into the center of the sidebar
26+
activeLink.scrollIntoView({
27+
block: "center",
28+
inline: "nearest",
29+
behavior: prefersReducedMotion ? "auto" : behavior,
30+
});
31+
};
32+
33+
const run = () => {
34+
// initial attempt
35+
scrollActiveLink("smooth");
36+
37+
// if you want to be extra-safe with late-rendered content, you could
38+
// wrap in a small timeout, but often not needed:
39+
// setTimeout(() => scrollActiveLink("smooth"), 0);
40+
};
41+
42+
if (document.readyState === "loading") {
43+
document.addEventListener("DOMContentLoaded", run, { once: true });
44+
} else {
45+
run();
46+
}
47+
48+
// Handle Astro/Starlight client-side navigation
49+
window.addEventListener("astro:after-swap", () => {
50+
// no animation on fast internal nav if you prefer:
51+
scrollActiveLink("auto");
52+
});
53+
</script>

0 commit comments

Comments
 (0)