File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -106,6 +106,22 @@ document.addEventListener('DOMContentLoaded', () => {
106106 // Highlight sidebar items.
107107 highlightNav ( ) ;
108108 window . addEventListener ( 'hashchange' , highlightNav ) ;
109+
110+ // Highlight sidebar link based on scroll position in demo sections.
111+ const sections = document . querySelectorAll ( '.demo-section' ) ;
112+ const side = document . querySelector ( 'aside[data-sidebar]' ) ;
113+ if ( sections . length && side ) {
114+ const ob = new IntersectionObserver ( items => {
115+ const ok = items . filter ( e => e . isIntersecting ) . sort ( ( a , b ) => a . boundingClientRect . top - b . boundingClientRect . top ) [ 0 ] ;
116+ if ( ! ok ) return ;
117+
118+ side . querySelector ( '[aria-current]' ) ?. removeAttribute ( 'aria-current' ) ;
119+ side . querySelector ( `a[href$="#${ ok . target . id } "]` ) ?. setAttribute ( 'aria-current' , 'page' ) ;
120+ } , { rootMargin : '0px 0px -60% 0px' } ) ;
121+
122+ // Attach a scroll observer to all sections.
123+ sections . forEach ( s => ob . observe ( s ) ) ;
124+ }
109125} ) ;
110126
111127
You can’t perform that action at this time.
0 commit comments