Skip to content

Commit

Permalink
feat(curriculum): highlight current page in sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Jan 31, 2024
1 parent a24a380 commit 3e46674
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
12 changes: 12 additions & 0 deletions client/src/curriculum/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@
}
}
}

.sidebar {
li > em {
background-color: var(--background-toc-active);
border-left: 2px solid var(--category-color);
display: inline-block;
font-style: normal;
font-weight: 600;
padding: 0.25rem 0.25rem 0.25rem 0.5rem;
width: 100%;
}
}
}
4 changes: 3 additions & 1 deletion client/src/curriculum/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export function CurriculumLanding(props: HydrationData<any, CurriculumDoc>) {
</aside>
{PLACEMENT_ENABLED && <SidePlacement />}
</div>
{doc.sidebar && <Sidebar sidebar={doc.sidebar} />}
{doc.sidebar && (
<Sidebar current={doc.mdn_url} sidebar={doc.sidebar} />
)}
</div>
<article className="curriculum-content" lang={doc?.locale}>
<header>
Expand Down
4 changes: 3 additions & 1 deletion client/src/curriculum/module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export function CurriculumModule(props: HydrationData<any, CurriculumDoc>) {
</aside>
{PLACEMENT_ENABLED && <SidePlacement />}
</div>
{doc.sidebar && <Sidebar sidebar={doc.sidebar} />}
{doc.sidebar && (
<Sidebar current={doc.mdn_url} sidebar={doc.sidebar} />
)}
</div>
<article className="curriculum-content" lang={doc?.locale}>
<header>
Expand Down
4 changes: 3 additions & 1 deletion client/src/curriculum/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export function CurriculumModuleOverview(
</aside>
{PLACEMENT_ENABLED && <SidePlacement />}
</div>
{doc.sidebar && <Sidebar sidebar={doc.sidebar} />}
{doc.sidebar && (
<Sidebar current={doc.mdn_url} sidebar={doc.sidebar} />
)}
</div>
<article className="curriculum-content" lang={doc?.locale}>
<header>
Expand Down
41 changes: 37 additions & 4 deletions client/src/curriculum/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { ModuleIndexEntry } from "../../../libs/types/curriculum";

import "./module.scss";
export function Sidebar({ sidebar = [] }: { sidebar: ModuleIndexEntry[] }) {
export function Sidebar({
current = "",
sidebar = [],
}: {
current: string;
sidebar: ModuleIndexEntry[];
}) {
return (
<aside className="sidebar">
<aside className="sidebar" data-current={current}>
<ol>
{sidebar.map((o, i) => (
<li key={i}>
<a href={o.url}>{o.title}</a>
<SidebarLink current={current} url={o.url} title={o.title} />
{o.children && (
<ol>
{o.children.map((c, j) => {
return (
<li key={`${i}-${j}`}>
<a href={c.url}>{c.title}</a>
<SidebarLink
current={current}
url={c.url}
title={c.title}
/>
</li>
);
})}
Expand All @@ -25,3 +35,26 @@ export function Sidebar({ sidebar = [] }: { sidebar: ModuleIndexEntry[] }) {
</aside>
);
}

function SidebarLink({
current,
url,
title,
}: {
current: string;
url: string;
title: string;
}) {
const isCurrent = url === current;
if (isCurrent) {
return (
<em>
<a href={url} aria-current="page">
{title}
</a>
</em>
);
} else {
return <a href={url}>{title}</a>;
}
}

0 comments on commit 3e46674

Please sign in to comment.