docs: use builtin active link detection (#1850)

This commit is contained in:
Marvin Hagemeister
2023-10-04 14:34:42 +02:00
committed by GitHub
parent 3e1b78736d
commit 9fdb0d5b01
3 changed files with 19 additions and 27 deletions
+12 -16
View File
@@ -9,7 +9,6 @@ import { type VersionLink } from "../routes/docs/[...slug].tsx";
export default function DocsSidebar( export default function DocsSidebar(
props: { props: {
path: string;
mobile?: boolean; mobile?: boolean;
versionLinks: VersionLink[]; versionLinks: VersionLink[];
selectedVersion: string; selectedVersion: string;
@@ -63,33 +62,30 @@ export default function DocsSidebar(
<ul class="list-inside font-semibold nested ml-2.5"> <ul class="list-inside font-semibold nested ml-2.5">
{CATEGORIES[props.selectedVersion].map((category) => ( {CATEGORIES[props.selectedVersion].map((category) => (
<SidebarCategory path={props.path} category={category} /> <SidebarCategory key={category.href} category={category} />
))} ))}
</ul> </ul>
</> </>
); );
} }
const link = "text(gray-900 hover:gray-600)";
const linkActive = "text-green-700 hover:underline font-bold";
export function SidebarCategory(props: { export function SidebarCategory(props: {
path: string;
category: TableOfContentsCategory; category: TableOfContentsCategory;
}) { }) {
const { title, href, entries } = props.category; const { title, href, entries } = props.category;
const outerLink = `${href == props.path ? linkActive : link} font-bold`;
return ( return (
<li class="my-2 block"> <li class="my-2 block">
<a href={href} class={outerLink}> <a
href={href}
class="text(gray-900 hover:gray-600) [data-current]:text-green-700 [data-current]:hover:underline font-bold"
>
{title} {title}
</a> </a>
{entries.length > 0 && ( {entries.length > 0 && (
<ul class="py-2 pl-4 nested list-outside"> <ul class="py-2 pl-4 nested list-outside">
{entries.map((entry) => ( {entries.map((entry) => (
<SidebarEntry path={props.path} entry={entry} /> <SidebarEntry key={entry.href} entry={entry} />
))} ))}
</ul> </ul>
)} )}
@@ -98,18 +94,18 @@ export function SidebarCategory(props: {
} }
export function SidebarEntry(props: { export function SidebarEntry(props: {
path: string;
entry: TableOfContentsCategoryEntry; entry: TableOfContentsCategoryEntry;
}) { }) {
const { title, href } = props.entry; const { title, href } = props.entry;
const innerLink = `${
href == props.path ? linkActive : link
} transition-colors hover:text-green-500 font-normal`;
return ( return (
<li class="py-0.5"> <li class="py-0.5">
<a href={href} class={innerLink}>{title}</a> <a
href={href}
class="[data-current]:text-green-700 transition-colors hover:text-green-500 font-normal"
>
{title}
</a>
</li> </li>
); );
} }
+1 -3
View File
@@ -31,9 +31,7 @@ export default function NavigationBar(
href={item.href} href={item.href}
class={`p-2 ${ class={`p-2 ${
isHome ? "text-green-900" : "text-gray-600" isHome ? "text-green-900" : "text-gray-600"
} hover:underline ${ } hover:underline [data-current]:font-bold [data-ancestor]:font-bold`}
props.active == item.href ? "font-bold" : ""
}`}
> >
{item.name} {item.name}
</a> </a>
+6 -8
View File
@@ -157,17 +157,17 @@ export default function DocsPage(props: PageProps<Data>) {
</Head> </Head>
<div class="flex flex-col min-h-screen"> <div class="flex flex-col min-h-screen">
<Header title="docs" active="/docs" /> <Header title="docs" active="/docs" />
<Main path={props.url.pathname} page={props.data.page} /> <Main page={props.data.page} />
<Footer /> <Footer />
</div> </div>
</> </>
); );
} }
function Main(props: { path: string; page: Page }) { function Main(props: { page: Page }) {
return ( return (
<div class="flex-1"> <div class="flex-1">
<MobileSidebar path={props.path} page={props.page} /> <MobileSidebar page={props.page} />
<div class="flex mx-auto max-w-screen-xl px-4 md:px-0 py-5 md:py-0 justify-end"> <div class="flex mx-auto max-w-screen-xl px-4 md:px-0 py-5 md:py-0 justify-end">
<label <label
for="docs_sidebar" for="docs_sidebar"
@@ -191,14 +191,14 @@ function Main(props: { path: string; page: Page }) {
</label> </label>
</div> </div>
<div class="mx-auto max-w-screen-xl flex gap-6 md:gap-8"> <div class="mx-auto max-w-screen-xl flex gap-6 md:gap-8">
<DesktopSidebar path={props.path} page={props.page} /> <DesktopSidebar page={props.page} />
<Content page={props.page} /> <Content page={props.page} />
</div> </div>
</div> </div>
); );
} }
function MobileSidebar(props: { path: string; page: Page }) { function MobileSidebar(props: { page: Page }) {
return ( return (
<> <>
<input <input
@@ -220,7 +220,6 @@ function MobileSidebar(props: { path: string; page: Page }) {
<nav class="pt-6 pb-16 px-4 overflow-x-auto"> <nav class="pt-6 pb-16 px-4 overflow-x-auto">
<DocsSidebar <DocsSidebar
mobile mobile
path={props.path}
versionLinks={props.page.versionLinks} versionLinks={props.page.versionLinks}
selectedVersion={props.page.version} selectedVersion={props.page.version}
/> />
@@ -231,11 +230,10 @@ function MobileSidebar(props: { path: string; page: Page }) {
); );
} }
function DesktopSidebar(props: { path: string; page: Page }) { function DesktopSidebar(props: { page: Page }) {
return ( return (
<nav class="w-[18rem] flex-shrink-0 hidden md:block py-7 px-4"> <nav class="w-[18rem] flex-shrink-0 hidden md:block py-7 px-4">
<DocsSidebar <DocsSidebar
path={props.path}
versionLinks={props.page.versionLinks} versionLinks={props.page.versionLinks}
selectedVersion={props.page.version} selectedVersion={props.page.version}
/> />