Skip to content

Commit 8082970

Browse files
Kyle McLarenclaude
andcommitted
Highlight current anchor link in sidebar and rename nav items
- Add data-current attribute to sidebar links matching current URL hash - Show green accent border for current endpoint link - Update on hashchange for smooth navigation - Rename "Policy" to "Network Policy" and "HTTP Proxy" to "Proxy" Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 18343ae commit 8082970

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/components/Head.astro

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ const breadcrumbSchema = {
189189
<script>
190190
function initSidebarNavigation() {
191191
const currentPath = window.location.pathname;
192+
const currentHash = window.location.hash;
193+
const currentUrl = currentPath + currentHash;
192194

193195
// Find all nested sidebar groups within API Reference
194196
const sidebarGroups = document.querySelectorAll('nav[aria-label="Main"] details details');
@@ -211,6 +213,20 @@ const breadcrumbSchema = {
211213
details.setAttribute('open', '');
212214
}
213215

216+
// Mark anchor links as current if they match the current URL hash
217+
const allLinks = details.querySelectorAll(':scope > ul a[href]');
218+
allLinks.forEach((link) => {
219+
const linkHref = link.getAttribute('href') || '';
220+
// Check if link matches current path+hash or just current path (for base page)
221+
if (linkHref === currentUrl ||
222+
(linkHref === currentPath) ||
223+
(linkHref === currentPath + '/')) {
224+
link.setAttribute('data-current', 'true');
225+
} else {
226+
link.removeAttribute('data-current');
227+
}
228+
});
229+
214230
// Skip click handler setup if already initialized
215231
if (summary.dataset.navInit) return;
216232
summary.dataset.navInit = 'true';
@@ -242,4 +258,7 @@ const breadcrumbSchema = {
242258
initSidebarNavigation();
243259
}
244260
document.addEventListener('astro:page-load', initSidebarNavigation);
261+
262+
// Also update on hash change (when clicking anchor links)
263+
window.addEventListener('hashchange', initSidebarNavigation);
245264
</script>

src/lib/sidebar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const sidebarConfig: SidebarGroup[] = [
200200
],
201201
},
202202
{
203-
label: 'Policy',
203+
label: 'Network Policy',
204204
collapsed: true,
205205
items: [
206206
{
@@ -216,7 +216,7 @@ export const sidebarConfig: SidebarGroup[] = [
216216
],
217217
},
218218
{
219-
label: 'HTTP Proxy',
219+
label: 'Proxy',
220220
collapsed: true,
221221
items: [
222222
{

src/styles/custom.css

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,13 @@ h1, h2, h3, h4, h5, h6 {
780780
border-radius: 0 0.5rem 0.5rem 0;
781781
}
782782

783-
[data-theme='light'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]) {
783+
[data-theme='light'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]),
784+
[data-theme='light'] nav[aria-label="Main"] ul ul li:has(> a[data-current="true"]) {
784785
border-inline-start: 2px solid oklch(0.5 0.2 285);
785786
}
786787

787-
[data-theme='light'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]) > a {
788+
[data-theme='light'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]) > a,
789+
[data-theme='light'] nav[aria-label="Main"] ul ul li:has(> a[data-current="true"]) > a {
788790
margin-left: -1px;
789791
}
790792

@@ -808,11 +810,13 @@ h1, h2, h3, h4, h5, h6 {
808810
border-radius: 0 0.5rem 0.5rem 0;
809811
}
810812

811-
[data-theme='dark'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]) {
813+
[data-theme='dark'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]),
814+
[data-theme='dark'] nav[aria-label="Main"] ul ul li:has(> a[data-current="true"]) {
812815
border-inline-start: 2px solid var(--sl-color-accent);
813816
}
814817

815-
[data-theme='dark'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]) > a {
818+
[data-theme='dark'] nav[aria-label="Main"] ul ul li:has(> a[aria-current="page"]) > a,
819+
[data-theme='dark'] nav[aria-label="Main"] ul ul li:has(> a[data-current="true"]) > a {
816820
margin-left: -1px;
817821
}
818822

0 commit comments

Comments
 (0)