|
10 | 10 | */ |
11 | 11 |
|
12 | 12 | import cx from 'classnames'; |
13 | | -import {startTransition, useOptimistic} from 'react'; |
14 | 13 | import {useTocHighlight} from './useTocHighlight'; |
15 | 14 | import {IsInTocContext} from '../MDX/TocContext'; |
16 | 15 | import type {Toc} from '../MDX/TocContext'; |
17 | 16 |
|
18 | | -function waitForScrollEnd(): Promise<void> { |
19 | | - return new Promise((resolve) => { |
20 | | - let settled = false; |
21 | | - const finish = () => { |
22 | | - if (settled) return; |
23 | | - settled = true; |
24 | | - window.removeEventListener('scrollend', finish); |
25 | | - resolve(); |
26 | | - }; |
27 | | - window.addEventListener('scrollend', finish, {once: true}); |
28 | | - setTimeout(finish, 1000); |
29 | | - }); |
30 | | -} |
31 | | - |
32 | 17 | export function Toc({headings}: {headings: Toc}) { |
33 | 18 | const {currentIndex} = useTocHighlight(); |
34 | | - const [optimisticIndex, setOptimisticIndex] = useOptimistic(currentIndex); |
35 | 19 | // TODO: We currently have a mismatch between the headings in the document |
36 | 20 | // and the headings we find in MarkdownPage (i.e. we don't find Recap or Challenges). |
37 | 21 | // Select the max TOC item we have here for now, but remove this after the fix. |
38 | | - const selectedIndex = Math.min(optimisticIndex, headings.length - 1); |
| 22 | + const selectedIndex = Math.min(currentIndex, headings.length - 1); |
39 | 23 | return ( |
40 | 24 | <nav role="navigation" className="pt-20 sticky top-0 end-0"> |
41 | 25 | {headings.length > 0 && ( |
@@ -75,13 +59,7 @@ export function Toc({headings}: {headings: Toc}) { |
75 | 59 | : 'text-secondary dark:text-secondary-dark', |
76 | 60 | 'block hover:text-link dark:hover:text-link-dark leading-normal py-2' |
77 | 61 | )} |
78 | | - href={h.url} |
79 | | - onClick={() => { |
80 | | - startTransition(async () => { |
81 | | - setOptimisticIndex(i); |
82 | | - await waitForScrollEnd(); |
83 | | - }); |
84 | | - }}> |
| 62 | + href={h.url}> |
85 | 63 | {h.text} |
86 | 64 | </a> |
87 | 65 | </li> |
|
0 commit comments