File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments