File tree Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -157,16 +157,17 @@ export function SidebarTableOfContents() {
157157
158158 // Mark the active item based on the scroll position
159159 useEffect ( ( ) => {
160- if ( ! tocItems . length ) {
160+ const innerHeight = window . innerHeight ;
161+ if ( ! tocItems . length || ! innerHeight ) {
161162 return ( ) => { } ;
162163 }
163164 // account for the header height
164165 const rootMarginTop = 100 ;
165166 // element is consiered in view if it is in the top 1/3 of the screen
166- const rootMarginBottomRaw = ( 2 / 3 ) * window . innerHeight - rootMarginTop ;
167- const rootMarginBottom = Math . floor ( rootMarginBottomRaw ) ;
167+ const rootMarginBottomRaw = ( 2 / 3 ) * innerHeight - rootMarginTop ;
168+ const rootMarginBottom = Math . floor ( rootMarginBottomRaw ) * - 1 ;
168169 const observerOptions = {
169- rootMargin : `${ rootMarginTop } px 0px - ${ rootMarginBottom } px 0px` ,
170+ rootMargin : `${ rootMarginTop } px 0px ${ rootMarginBottom } px 0px` ,
170171 threshold : 1 ,
171172 } ;
172173 const observer = new IntersectionObserver ( entries => {
@@ -183,7 +184,7 @@ export function SidebarTableOfContents() {
183184 } , observerOptions ) ;
184185 const headings = tocItems . map ( item => item . element ) ;
185186 headings . forEach ( heading => observer . observe ( heading ) ) ;
186- return ( ) => headings . forEach ( heading => observer . unobserve ( heading ) ) ;
187+ return ( ) => observer . disconnect ( ) ;
187188 } , [ tocItems ] ) ;
188189
189190 return (
You can’t perform that action at this time.
0 commit comments