Skip to content

Commit 58a7304

Browse files
committed
scroll state
1 parent 1a1f7e6 commit 58a7304

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

public/sidebar-scroll.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
(() => {
2-
const storageKey = 'sl-sidebar-state';
32
const desktopQuery = '(min-width: 50em)';
43

54
const isDesktopSidebar = () => matchMedia(desktopQuery).matches;
65

7-
const shouldRestoreFromStorage = (sidebar) => {
8-
const persistTarget = sidebar.querySelector('sl-sidebar-state-persist');
9-
const hash = persistTarget?.dataset.hash;
6+
const isActiveLinkVisible = (sidebar, activeLink) => {
7+
const sidebarRect = sidebar.getBoundingClientRect();
8+
const linkRect = activeLink.getBoundingClientRect();
109

11-
try {
12-
const storedState = JSON.parse(sessionStorage.getItem(storageKey) || 'null');
13-
return Boolean(hash && storedState?.hash === hash && typeof storedState.scroll === 'number');
14-
} catch {
15-
return false;
16-
}
10+
return linkRect.top >= sidebarRect.top && linkRect.bottom <= sidebarRect.bottom;
1711
};
1812

1913
const centerLinkInSidebar = () => {
2014
const sidebar = document.getElementById('starlight__sidebar');
21-
if (!sidebar || !isDesktopSidebar() || shouldRestoreFromStorage(sidebar)) return true;
15+
if (!sidebar || !isDesktopSidebar()) return true;
2216

2317
const activeLink = sidebar.querySelector("a[aria-current='page']");
2418
if (!activeLink) return false;
2519

20+
if (isActiveLinkVisible(sidebar, activeLink)) return true;
21+
2622
const sidebarRect = sidebar.getBoundingClientRect();
2723
const linkRect = activeLink.getBoundingClientRect();
2824
const linkOffset = linkRect.top - sidebarRect.top + sidebar.scrollTop;

0 commit comments

Comments
 (0)