File tree Expand file tree Collapse file tree
dotcom-rendering/src/components Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import {
1212import { useEffect , useState } from 'react' ;
1313import { ArticleDisplay , type ArticleFormat } from '../lib/articleFormat' ;
1414import { getZIndex } from '../lib/getZIndex' ;
15+ import { useIsInView } from '../lib/useIsInView' ;
1516import type { TableOfContentsItem } from '../model/enhanceTableOfContents' ;
1617import { palette } from '../palette' ;
1718
@@ -136,30 +137,21 @@ const verticalStyle = css`
136137 */
137138
138139export const TableOfContents = ( { tableOfContents, format } : Props ) => {
140+ const [ isInView , setNode ] = useIsInView ( { threshold : [ 1 ] } ) ;
139141 const [ open , setOpen ] = useState ( tableOfContents . length < 5 ) ;
140142
141143 useEffect ( ( ) => {
142- const tocElement = document . querySelector (
143- '[data-component="table-of-contents"]' ,
144- ) as HTMLElement ;
145-
146- const observer = new IntersectionObserver (
147- ( [ e ] ) => {
148- if ( e && e . intersectionRatio < 1 ) {
149- setOpen ( false ) ;
150- }
151- } ,
152- { threshold : [ 1 ] } ,
153- ) ;
154-
155- observer . observe ( tocElement ) ;
156- } , [ ] ) ;
144+ if ( ! isInView ) {
145+ setOpen ( false ) ;
146+ }
147+ } , [ isInView ] ) ;
157148
158149 return (
159150 < details
160151 open = { open }
161152 css = { [ detailsStyles , stickyStyles ] }
162153 data-component = "table-of-contents"
154+ ref = { setNode }
163155 >
164156 < summary
165157 onClick = { ( e ) : void => {
You can’t perform that action at this time.
0 commit comments