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 99 SvgChevronDownSingle ,
1010 SvgChevronUpSingle ,
1111} from '@guardian/source/react-components' ;
12- import { useState } from 'react' ;
12+ import { useEffect , useState } from 'react' ;
1313import { ArticleDisplay , type ArticleFormat } from '../lib/articleFormat' ;
1414import { getZIndex } from '../lib/getZIndex' ;
1515import type { TableOfContentsItem } from '../model/enhanceTableOfContents' ;
@@ -75,7 +75,7 @@ const detailsStyles = css`
7575` ;
7676const stickyStyles = css `
7777 position : sticky;
78- top : 0 ;
78+ top : -1 px ;
7979 background : ${ palette ( '--article-background' ) } ;
8080 z-index : ${ getZIndex ( 'tableOfContents' ) } ;
8181 summary {
@@ -138,6 +138,23 @@ const verticalStyle = css`
138138export const TableOfContents = ( { tableOfContents, format } : Props ) => {
139139 const [ open , setOpen ] = useState ( tableOfContents . length < 5 ) ;
140140
141+ 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+ } , [ ] ) ;
157+
141158 return (
142159 < details
143160 open = { open }
You can’t perform that action at this time.
0 commit comments