forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductCollapsibleSection.tsx
122 lines (113 loc) · 4.12 KB
/
ProductCollapsibleSection.tsx
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import cx from 'classnames'
import { useState, SyntheticEvent } from 'react'
import { ChevronDownIcon } from '@primer/octicons-react'
import { ActionList } from '@primer/react'
import { Link } from 'components/Link'
import { ProductTreeNode } from 'components/context/MainContext'
import { EventType, sendEvent } from 'src/events/browser'
import styles from './SidebarProduct.module.scss'
type SectionProps = {
routePath: string
page: ProductTreeNode
title: string
defaultOpen: boolean
}
export const ProductCollapsibleSection = (props: SectionProps) => {
const { routePath, defaultOpen, title, page } = props
const [isOpen, setIsOpen] = useState(defaultOpen)
const onToggle = (e: SyntheticEvent) => {
const newIsOpen = (e.target as HTMLDetailsElement).open
setIsOpen(newIsOpen)
sendEvent({
type: EventType.navigate,
navigate_label: `details ${newIsOpen ? 'open' : 'close'}: ${title}`,
})
}
// The lowest level page link displayed in the tree
const renderTerminalPageLink = (page: ProductTreeNode) => {
const title = page.shortTitle || page.title
const isCurrent = routePath === page.href
return (
<ActionList.Item
key={page.href}
data-testid="sidebar-article"
data-is-current-page={isCurrent}
className={cx(
'width-full position-relative',
styles.sidebarArticle,
isCurrent && ['text-bold', styles.sidebarArticleActive]
)}
sx={{
padding: '2px 0',
':hover': {
borderRadius: 0,
},
}}
>
<Link
href={page.href}
className={cx(
'd-block pl-6 pr-5 py-1 no-underline width-full',
isCurrent ? 'color-fg-accent' : 'color-fg-default'
)}
>
{title}
</Link>
</ActionList.Item>
)
}
return (
<details open={defaultOpen} onToggle={onToggle} className="details-reset">
<summary className="outline-none">
<div className="d-flex flex-justify-between">
<div className="pl-4 pr-1 py-2 f5 d-block flex-auto mr-3 color-fg-default no-underline text-bold">
{title}
</div>
<span style={{ marginTop: 7 }} className="flex-shrink-0 pr-3">
<ChevronDownIcon className={cx('opacity-60', isOpen && 'rotate-180')} />
</span>
</div>
</summary>
{
<>
{/* <!-- some pages have nested child pages (formerly known as a mapTopic) --> */}
{page.childPages[0]?.documentType === 'mapTopic' ? (
<ul className="list-style-none position-relative">
{page.childPages.map((childPage, i) => {
const childTitle = childPage.shortTitle || childPage.title
const isActive = routePath.includes(childPage.href)
const isCurrent = routePath === childPage.href
return (
<li key={childPage.href + i} data-is-current-page={isCurrent}>
<details
open={isActive}
onToggle={(e) => e.stopPropagation()}
className="details-reset"
>
<summary>
<div className={cx('pl-4 pr-5 py-2 no-underline')}>{childTitle}</div>
</summary>
<div data-testid="sidebar-article-group" className="pb-0">
<ActionList variant="full" className="my-2">
{childPage.childPages.map((cp) => {
return renderTerminalPageLink(cp)
})}
</ActionList>
</div>
</details>
</li>
)
})}
</ul>
) : page.childPages[0]?.documentType === 'article' ? (
<div data-testid="sidebar-article-group" className="pb-0">
<ActionList variant="full" className="my-2">
{page.childPages.map(renderTerminalPageLink)}
</ActionList>
</div>
) : null}
</>
}
</details>
)
}