-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathTableOfContents.tsx
More file actions
81 lines (76 loc) · 2.64 KB
/
TableOfContents.tsx
File metadata and controls
81 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import cx from 'classnames'
import React from 'react'
import { Link } from '@/frame/components/Link'
import type { TocItem } from '@/landings/types'
import { ActionList } from '@primer/react'
import styles from './TableOfContents.module.css'
type Props = {
items: Array<TocItem>
variant?: 'compact' | 'expanded'
}
export const TableOfContents = (props: Props) => {
const { items, variant = 'expanded' } = props
return (
<div
data-testid="table-of-contents"
className={cx(variant === 'compact' ? 'list-style-outside pl-2' : '')}
>
{variant === 'expanded' &&
items.map((item) => {
const { fullPath: href, title, intro } = item
return (
<div
key={href}
data-testid="expanded-item"
className="pt-4 pb-3 f4 d-list-item width-full list-style-none border-bottom"
>
<h2 className="py-1 h4">
<Link href={href} className="color-fg-accent">
{title}
</Link>
</h2>
{intro && (
<div className="f4 color-fg-muted" dangerouslySetInnerHTML={{ __html: intro }} />
)}
</div>
)
})}
{variant === 'compact' && (
<ActionList>
{items.map((item) => {
const { fullPath, title, childTocItems } = item
return (
<React.Fragment key={fullPath}>
<ActionList.LinkItem href={fullPath} as="a" className={styles.linkItem}>
{title}
</ActionList.LinkItem>
{(childTocItems || []).length > 0 && (
<li className="f4 color-fg-accent d-list-item d-block width-full text-underline">
<ActionList
className={cx(
variant === 'compact' ? 'list-style-circle pl-5' : 'list-style-none',
)}
>
{(childTocItems || []).filter(Boolean).map((childItem) => {
return (
<ActionList.LinkItem
key={childItem.fullPath}
href={childItem.fullPath}
as="a"
className={styles.linkItem}
>
{childItem.title}
</ActionList.LinkItem>
)
})}
</ActionList>
</li>
)}
</React.Fragment>
)
})}
</ActionList>
)}
</div>
)
}