Skip to content

Commit 793d22f

Browse files
committed
Highlight the selected menu link on the docs sidebar. Closes #76.
1 parent 7aeacc9 commit 793d22f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

docs/static/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ document.addEventListener('DOMContentLoaded', () => {
102102
}
103103
}
104104
});
105+
106+
// Highlight the current sidebar link.
107+
const sb = document.querySelector('aside[data-sidebar]');
108+
if (sb) {
109+
const p = location.pathname.replace(/\/$/, '');
110+
111+
// First, try selecting the URL with the hash ID, then the direct URL, then the /components section.
112+
const a = (location.hash && sb.querySelector(`a[href="${p}/${location.hash}"], a[href="${p}${location.hash}"]`))
113+
|| sb.querySelector(`a[href="${p}/"], a[href="${p}"]`)
114+
|| sb.querySelector(`a[href="/components/#${p.split('/').filter(Boolean).pop()}"]`);
115+
116+
if (a) {
117+
a.setAttribute('aria-current', 'page');
118+
}
119+
}
105120
});
106121

107122

0 commit comments

Comments
 (0)