1
1
import cn from 'clsx'
2
2
import type { Heading } from 'nextra'
3
3
import type { ReactElement } from 'react'
4
- import { useEffect , useMemo , useRef } from 'react'
4
+ import { useEffect , useRef } from 'react'
5
5
import scrollIntoView from 'scroll-into-view-if-needed'
6
6
import { useActiveAnchor , useConfig } from '../contexts'
7
7
import { renderComponent } from '../utils'
8
8
import { Anchor } from './anchor'
9
9
import { BackToTop } from './back-to-top'
10
10
11
11
export type TOCProps = {
12
- headings : Heading [ ]
12
+ toc : Heading [ ]
13
13
filePath : string
14
14
}
15
15
@@ -18,17 +18,12 @@ const linkClassName = cn(
18
18
'contrast-more:nx-text-gray-800 contrast-more:dark:nx-text-gray-50'
19
19
)
20
20
21
- export function TOC ( { headings , filePath } : TOCProps ) : ReactElement {
21
+ export function TOC ( { toc , filePath } : TOCProps ) : ReactElement {
22
22
const activeAnchor = useActiveAnchor ( )
23
23
const config = useConfig ( )
24
24
const tocRef = useRef < HTMLDivElement > ( null )
25
25
26
- const items = useMemo (
27
- ( ) => headings . filter ( heading => heading . depth > 1 ) ,
28
- [ headings ]
29
- )
30
-
31
- const hasHeadings = items . length > 0
26
+ const hasHeadings = toc . length > 0
32
27
const hasMetaInfo = Boolean (
33
28
config . feedback . content ||
34
29
config . editLink . component ||
@@ -70,7 +65,7 @@ export function TOC({ headings, filePath }: TOCProps): ReactElement {
70
65
{ renderComponent ( config . toc . title ) }
71
66
</ p >
72
67
< ul >
73
- { items . map ( ( { id, value, depth } ) => (
68
+ { toc . map ( ( { id, value, depth } ) => (
74
69
< li className = "nx-my-2 nx-scroll-my-6 nx-scroll-py-6" key = { id } >
75
70
< a
76
71
href = { `#${ id } ` }
@@ -81,18 +76,15 @@ export function TOC({ headings, filePath }: TOCProps): ReactElement {
81
76
4 : 'ltr:nx-pl-8 rtl:nx-pr-8' ,
82
77
5 : 'ltr:nx-pl-12 rtl:nx-pr-12' ,
83
78
6 : 'ltr:nx-pl-16 rtl:nx-pr-16'
84
- } [ depth as Exclude < typeof depth , 1 > ] ,
85
- 'nx-inline-block' ,
79
+ } [ depth ] ,
80
+ 'nx-inline-block nx-transition-colors nx-subpixel-antialiased ' ,
86
81
activeAnchor [ id ] ?. isActive
87
- ? 'nx-text-primary-600 nx-subpixel-antialiased contrast-more:!nx-text-primary-600'
82
+ ? 'nx-text-primary-600 contrast-more:!nx-text-primary-600'
88
83
: 'nx-text-gray-500 hover:nx-text-gray-900 dark:nx-text-gray-400 dark:hover:nx-text-gray-300' ,
89
84
'contrast-more:nx-text-gray-900 contrast-more:nx-underline contrast-more:dark:nx-text-gray-50 nx-w-full nx-break-words'
90
85
) }
91
86
>
92
- { config . toc . headingComponent ?.( {
93
- id,
94
- children : value
95
- } ) ?? value }
87
+ { value }
96
88
</ a >
97
89
</ li >
98
90
) ) }
0 commit comments