33 * it even exists for users without client-side js
44 * and that means it loads with the page so it avoids
55 * all potential reshuffling or layout recalculations.
6- *
6+ *
77 * So, all this function needs to do is make the links
88 * perform smooth animation, and adjust the "active"
99 * entry as the user scrolls.
1010 */
1111export default function setupToC ( ) {
12+ const container = document . getElementById ( "right-pane" ) ;
1213 const toc = document . getElementById ( "toc" ) ;
13- if ( ! toc ) return ;
14+ if ( ! toc || ! container ) return ;
1415
1516 // Get all relevant elements
1617 const sections = document . getElementById ( "content" ) ! . querySelectorAll ( "h2, h3, h4, h5, h6" ) ;
@@ -23,7 +24,7 @@ export default function setupToC() {
2324 if ( ! target ) return ;
2425 e . preventDefault ( ) ;
2526 e . stopPropagation ( ) ;
26-
27+
2728 target . scrollIntoView ( { behavior : "smooth" } ) ;
2829 } ) ;
2930 }
@@ -32,15 +33,15 @@ export default function setupToC() {
3233 function changeLinkState ( ) {
3334 let index = sections . length ;
3435
35- // Work backkwards to find the first matching section
36- while ( -- index && window . scrollY + 50 < ( sections [ index ] as HTMLElement ) . offsetTop ) { } // eslint-disable-line no-empty
36+ // Work backwards to find the first matching section
37+ while ( -- index && container ! . scrollTop + 50 < ( sections [ index ] as HTMLElement ) . offsetTop ) { } // eslint-disable-line no-empty
3738
3839 // Update the "active" item in ToC
3940 links . forEach ( ( link ) => link . classList . remove ( "active" ) ) ;
4041 links [ index ] . classList . add ( "active" ) ;
4142 }
42-
43+
4344 // Initial render
4445 changeLinkState ( ) ;
45- window . addEventListener ( "scroll" , changeLinkState ) ;
46- }
46+ container . addEventListener ( "scroll" , changeLinkState ) ;
47+ }
0 commit comments