Skip to content

Commit 5865251

Browse files
committed
automatically derive current page in RootNav
1 parent 49fed51 commit 5865251

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/components/navigation/sidebars/nav/RootNav.astro

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export interface Props {
55
currentClass?: string;
66
}
77
8-
const { currentRoute, currentModule, currentClass } = Astro.props;
9-
108
import { getModulesData } from "@config/io/generateTypeData";
119
import type { TreeEntry } from "./Tree.astro";
1210
import Tree from "./Tree.astro";
@@ -18,50 +16,64 @@ const modules = await getModulesData();
1816
import { getCollection } from "astro:content";
1917
const guidePages = await getCollection("guide");
2018
21-
function genGuideNav(base: string): TreeEntry[] | undefined {
19+
interface NavTree {
20+
title: string,
21+
slug: string,
22+
entries?: NavTree[],
23+
}
24+
25+
const currentPath = Astro.url.pathname.slice(1).split('/');
26+
27+
function mkTree(mount: string, pathIdx: number, { title, slug, entries }: NavTree): TreeEntry {
28+
const link = `${mount}/${slug}`;
29+
30+
return {
31+
title,
32+
link,
33+
current: currentPath[pathIdx] === slug,
34+
entries: entries?.map(entry => mkTree(link, pathIdx + 1, entry)),
35+
};
36+
}
37+
38+
function genGuideNav(base: string): NavTree[] | undefined {
2239
const pages = guidePages
2340
.filter(page => page.id.match(`^${base}[^/]*$`) !== null && page.id !== "index")
2441
.sort((a, b) => a.data.index - b.data.index)
2542
.map(page => ({
2643
title: page.data.title,
27-
link: `/docs/guide/${page.id}`,
28-
current: currentModule === page.id,
44+
slug: page.id,
2945
entries: genGuideNav(page.id + "/"),
3046
}));
3147
3248
return pages.length === 0 ? undefined : pages;
3349
}
3450
35-
const guide = {
51+
const guide = mkTree("/docs", 1, {
3652
title: "Usage Guide",
37-
link: "/docs/guide",
38-
current: currentRoute?.startsWith("guide") ?? false,
53+
slug: "guide",
3954
entries: genGuideNav(""),
40-
}
55+
});
4156
42-
const types = {
57+
const types = mkTree("/docs", 1, {
4358
title: "Quickshell Types",
44-
link: "/docs/types",
45-
current: currentRoute?.startsWith("types") ?? false,
59+
slug: "types",
4660
entries: modules.map(module => ({
4761
title: module.name,
48-
link: `/docs/types/${module.name}`,
49-
current: currentModule === module.name,
62+
slug: module.name,
5063
entries: module.types.map(type => ({
5164
title: type.name,
52-
link: `/docs/types/${module.name}/${type.name}`,
53-
current: currentClass === type.name,
65+
slug: type.name,
5466
}))
5567
}))
56-
};
68+
});
5769
5870
---
5971
<nav class="navtree">
6072
<VersionSelector title="Versions" link=`${Astro.currentLocale}` current/>
6173
<Link
6274
title="About Quickshell"
6375
link="/about"
64-
current={currentRoute === "about"}
76+
current={Astro.url.pathname === "/about"}
6577
/>
6678
<Tree {...guide}/>
6779
<Tree {...types}/>

src/components/navigation/sidebars/nav/index.astro

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
import SidebarWrapper from "./SidebarWrapper.tsx";
33
import RootNav from "./RootNav.astro";
44
5-
const url = Astro.url.pathname.split("/");
6-
const currentRoute = url[2];
7-
const currentModule = url[3];
8-
const currentClass = url[4];
9-
105
export interface Props {
116
mobile: boolean;
127
}
@@ -16,9 +11,9 @@ const { mobile } = Astro.props;
1611
<aside class=`nav-wrapper${mobile ? "-mobile" : ""} id="nav"`>
1712
{ mobile ? (
1813
<SidebarWrapper client:load>
19-
<RootNav currentRoute={currentRoute} currentModule={currentModule} currentClass={currentClass}/>
14+
<RootNav/>
2015
</SidebarWrapper>
2116
) : (
22-
<RootNav currentRoute={currentRoute} currentModule={currentModule} currentClass={currentClass}/>
17+
<RootNav/>
2318
)}
2419
</aside>

0 commit comments

Comments
 (0)