Skip to content

Commit 25240c9

Browse files
Layout: sidebar scroll anchor (#391)
Fixes bug where the sidebar wouldn't scroll to the user's current page location. * Layout: WIP. Jumping to location works, but only first matching title * Layout: Scroll references href now and works completely * Layout: Cleared logging and lint errors * Layout: Fixed biome issues * Layout: Changed scroll to be at the top * Layout: Change currentpage to use path over HREF --------- Co-authored-by: Lam Nguyen <[email protected]>
1 parent 0bacccd commit 25240c9

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

assets/js/sidebar-v2.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ document.addEventListener('click', (e) => {
3737
}
3838
});
3939

40+
document.addEventListener('DOMContentLoaded', () => {
41+
const currentPage = window.location.pathname;
42+
const match = Array.from(
43+
document.querySelectorAll('.sidebar__link--current')
44+
).find((el) => el?.id?.includes(currentPage));
45+
46+
if (match) match.scrollIntoView({ behavior: 'instant', block: 'start' });
47+
});
48+
4049
const debounce = (callback, wait) => {
4150
let timeoutId = null;
4251
return (...args) => {
@@ -51,7 +60,6 @@ window.addEventListener(
5160
'resize',
5261
debounce(() => {
5362
const sidebar = document.getElementById('sidebar-v2');
54-
5563
if (
5664
window.innerWidth > 88 * 16 &&
5765
sidebar.classList.contains('sidebar__mobile-open')

0 commit comments

Comments
 (0)