Skip to content

Commit 0cbc725

Browse files
committed
WIP
1 parent 48e29ed commit 0cbc725

1 file changed

Lines changed: 7 additions & 15 deletions

File tree

dotcom-rendering/src/components/TableOfContents.importable.tsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { useEffect, useState } from 'react';
1313
import { ArticleDisplay, type ArticleFormat } from '../lib/articleFormat';
1414
import { getZIndex } from '../lib/getZIndex';
15+
import { useIsInView } from '../lib/useIsInView';
1516
import type { TableOfContentsItem } from '../model/enhanceTableOfContents';
1617
import { palette } from '../palette';
1718

@@ -136,30 +137,21 @@ const verticalStyle = css`
136137
*/
137138

138139
export 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 => {

0 commit comments

Comments
 (0)