@@ -39,29 +39,32 @@ export default function SolutionsTableOfContents({
3939
4040 if ( intersectingEntries . length === 0 ) {
4141 // If nothing is intersecting, find the closest section above the viewport
42- const allElements = solutions . map ( ( solution ) => {
43- const solutionId = getSolutionId (
44- solution . slug ?. current || solution . title ,
45- ) ;
46- return document . getElementById ( solutionId ) ;
47- } ) . filter ( Boolean ) as HTMLElement [ ] ;
42+ const allElements = solutions
43+ . map ( ( solution ) => {
44+ const solutionId = getSolutionId (
45+ solution . slug ?. current || solution . title ,
46+ ) ;
47+ return document . getElementById ( solutionId ) ;
48+ } )
49+ . filter ( ( el ) : el is HTMLElement => el !== null ) ;
4850
4951 const viewportTop = window . scrollY + 200 ; // Account for sticky header + TOC
5052 let closestElement : HTMLElement | null = null ;
5153 let closestDistance = Infinity ;
5254
53- allElements . forEach ( ( element ) => {
55+ for ( const element of allElements ) {
5456 const elementTop = element . getBoundingClientRect ( ) . top + window . scrollY ;
5557 const distance = Math . abs ( elementTop - viewportTop ) ;
5658
5759 if ( elementTop <= viewportTop && distance < closestDistance ) {
5860 closestDistance = distance ;
5961 closestElement = element ;
6062 }
61- } ) ;
63+ }
6264
6365 if ( closestElement ) {
64- setActiveId ( closestElement . id ) ;
66+ const elementId = closestElement . id ;
67+ setActiveId ( elementId ) ;
6568 }
6669 return ;
6770 }
@@ -99,18 +102,20 @@ export default function SolutionsTableOfContents({
99102
100103 // Also handle scroll events to update active state when clicking links
101104 const handleScroll = ( ) => {
102- const allElements = solutions . map ( ( solution ) => {
103- const solutionId = getSolutionId (
104- solution . slug ?. current || solution . title ,
105- ) ;
106- return document . getElementById ( solutionId ) ;
107- } ) . filter ( Boolean ) as HTMLElement [ ] ;
105+ const allElements = solutions
106+ . map ( ( solution ) => {
107+ const solutionId = getSolutionId (
108+ solution . slug ?. current || solution . title ,
109+ ) ;
110+ return document . getElementById ( solutionId ) ;
111+ } )
112+ . filter ( ( el ) : el is HTMLElement => el !== null ) ;
108113
109114 const viewportTop = window . scrollY + 200 ;
110115 let activeElement : HTMLElement | null = null ;
111116 let minDistance = Infinity ;
112117
113- allElements . forEach ( ( element ) => {
118+ for ( const element of allElements ) {
114119 const rect = element . getBoundingClientRect ( ) ;
115120 const elementTop = rect . top + window . scrollY ;
116121 const distance = Math . abs ( elementTop - viewportTop ) ;
@@ -120,10 +125,11 @@ export default function SolutionsTableOfContents({
120125 minDistance = distance ;
121126 activeElement = element ;
122127 }
123- } ) ;
128+ }
124129
125130 if ( activeElement ) {
126- setActiveId ( activeElement . id ) ;
131+ const elementId = activeElement . id ;
132+ setActiveId ( elementId ) ;
127133 }
128134 } ;
129135
0 commit comments