-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathSidebarRouteTree.tsx
182 lines (175 loc) · 5.4 KB
/
SidebarRouteTree.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import {useRef, useLayoutEffect, Fragment} from 'react';
import cn from 'classnames';
import {useRouter} from 'next/router';
import {SidebarLink} from './SidebarLink';
import {useCollapse} from 'react-collapsed';
import usePendingRoute from 'hooks/usePendingRoute';
import type {RouteItem} from 'components/Layout/getRouteMeta';
import {siteConfig} from 'siteConfig';
interface SidebarRouteTreeProps {
isForceExpanded: boolean;
breadcrumbs: RouteItem[];
routeTree: RouteItem;
level?: number;
}
function CollapseWrapper({
isExpanded,
duration,
children,
}: {
isExpanded: boolean;
duration: number;
children: any;
}) {
const ref = useRef<HTMLDivElement | null>(null);
const timeoutRef = useRef<number | null>(null);
const {getCollapseProps} = useCollapse({
isExpanded,
duration,
});
// Disable pointer events while animating.
const isExpandedRef = useRef(isExpanded);
if (typeof window !== 'undefined') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useLayoutEffect(() => {
const wasExpanded = isExpandedRef.current;
if (wasExpanded === isExpanded) {
return;
}
isExpandedRef.current = isExpanded;
if (ref.current !== null) {
const node: HTMLDivElement = ref.current;
node.style.pointerEvents = 'none';
if (timeoutRef.current !== null) {
window.clearTimeout(timeoutRef.current);
}
timeoutRef.current = window.setTimeout(() => {
node.style.pointerEvents = '';
}, duration + 100);
}
});
}
return (
<div
ref={ref}
className={cn(isExpanded ? 'opacity-100' : 'opacity-50')}
style={{
transition: `opacity ${duration}ms ease-in-out`,
}}>
<div {...getCollapseProps()}>{children}</div>
</div>
);
}
export function SidebarRouteTree({
isForceExpanded,
breadcrumbs,
routeTree,
level = 0,
}: SidebarRouteTreeProps) {
const slug = useRouter().asPath.split(/[\?\#]/)[0];
const pendingRoute = usePendingRoute();
const currentRoutes = routeTree.routes as RouteItem[];
return (
<ul>
{currentRoutes.map(
(
{
path,
title,
routes,
version,
heading,
hasSectionHeader,
sectionHeader,
},
index
) => {
const selected = slug === path;
let listItem = null;
if (!path || heading) {
// if current route item has no path and children treat it as an API sidebar heading
listItem = (
<SidebarRouteTree
level={level + 1}
isForceExpanded={isForceExpanded}
routeTree={{title, routes}}
breadcrumbs={[]}
/>
);
} else if (routes) {
// if route has a path and child routes, treat it as an expandable sidebar item
const isBreadcrumb =
breadcrumbs.length > 1 &&
breadcrumbs[breadcrumbs.length - 1].path === path;
const isExpanded = isForceExpanded || isBreadcrumb || selected;
listItem = (
<li key={`${title}-${path}-${level}-heading`}>
<SidebarLink
key={`${title}-${path}-${level}-link`}
href={path}
isPending={pendingRoute === path}
selected={selected}
level={level}
title={title}
version={version}
isExpanded={isExpanded}
hideArrow={isForceExpanded}
/>
<CollapseWrapper duration={250} isExpanded={isExpanded}>
<SidebarRouteTree
isForceExpanded={isForceExpanded}
routeTree={{title, routes}}
breadcrumbs={breadcrumbs}
level={level + 1}
/>
</CollapseWrapper>
</li>
);
} else {
// if route has a path and no child routes, treat it as a sidebar link
listItem = (
<li key={`${title}-${path}-${level}-link`}>
<SidebarLink
isPending={pendingRoute === path}
href={path}
selected={selected}
level={level}
title={title}
version={version}
/>
</li>
);
}
if (hasSectionHeader) {
let sectionHeaderText =
sectionHeader != null
? sectionHeader.replace('{{version}}', siteConfig.version)
: '';
return (
<Fragment key={`${sectionHeaderText}-${level}-separator`}>
{index !== 0 && (
<li
role="separator"
className="mt-4 mb-2 ms-5 border-b border-border dark:border-border-dark"
/>
)}
<h3
className={cn(
'mb-1 text-sm font-bold ms-5 text-tertiary dark:text-tertiary-dark',
index !== 0 && 'mt-2'
)}>
{sectionHeaderText}
</h3>
</Fragment>
);
} else {
return listItem;
}
}
)}
</ul>
);
}