This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathSidebarNav.js
More file actions
267 lines (253 loc) Β· 7.16 KB
/
SidebarNav.js
File metadata and controls
267 lines (253 loc) Β· 7.16 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import NavItems, {GA_EVENT_CATEGORY_SIDEBAR, NavContext} from './NavItems';
import PropTypes from 'prop-types';
import React, {forwardRef, useImperativeHandle, useMemo, useRef} from 'react';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import {BiCollapseVertical, BiExpandVertical} from 'react-icons/bi';
import {
Button,
Flex,
HStack,
IconButton,
Menu,
MenuButton,
MenuItemOption,
MenuList,
MenuOptionGroup,
Tooltip,
chakra
} from '@chakra-ui/react';
import {
FiChevronDown,
FiChevronLeft,
FiChevronsLeft,
FiLock,
FiUnlock
} from 'react-icons/fi';
import {PAGE_FOOTER_HEIGHT} from '../PageLayout';
import {flattenNavItems} from '../../utils';
const SidebarButton = props => (
<IconButton size="sm" fontSize="lg" variant="ghost" {...props} />
);
const SidebarNav = forwardRef(
(
{
docset,
navItems,
versions,
currentVersion,
onVersionChange,
onGoBack,
hideSidebar,
isLocked,
onLockToggle
},
ref
) => {
const navRef = useRef();
useImperativeHandle(ref, () => ({
focusFirstLink: () => {
navRef.current.querySelector('a')?.focus();
}
}));
const navGroups = useMemo(
() => flattenNavItems(navItems).filter(item => item.children),
[navItems]
);
// set all nav items to open by default
const initialNavState = useMemo(
() =>
navGroups.reduce(
(acc, group) => ({
...acc,
[group.id]: !group.isDefaultCollapsed
}),
{}
),
[navGroups]
);
// save nav state in storage
const [localNavState, setLocalNavState] = useLocalStorage('nav');
// combine initial and local nav states
const nav = useMemo(
() => ({
...initialNavState,
...localNavState
}),
[localNavState, initialNavState]
);
// compute expand/collapse all state from nav state
const isAllExpanded = useMemo(
() =>
// get an array of the state of all nav items that also exist in the list of
// valid nav group ids (above)
navGroups.every(group => nav[group.id]),
[navGroups, nav]
);
const navContextValue = useMemo(
() => ({
nav,
setNav: setLocalNavState
}),
[nav, setLocalNavState]
);
return (
<Flex
direction="column"
h="full"
overflow="auto"
overscrollBehavior="none"
>
<Flex align="center" p="4" pos="sticky" top="0" zIndex="1" bg="bg">
{onGoBack && (
<IconButton
ml="-2.5"
mr="1"
variant="ghost"
size="sm"
fontSize="xl"
icon={<FiChevronLeft />}
onClick={onGoBack}
/>
)}
<chakra.h2
mr="auto"
fontSize="xl"
fontWeight="semibold"
lineHeight={1.6}
>
{docset}
</chakra.h2>
{versions?.length > 1 && (
<Menu>
<MenuButton
alignSelf="flex-start"
size="sm"
ml="2"
variant="outline"
rightIcon={<FiChevronDown />}
as={Button}
>
{currentVersion}
</MenuButton>
<MenuList>
<MenuOptionGroup value={currentVersion}>
{versions.map((version, index) => (
<MenuItemOption
key={index}
value={version.label}
onClick={event => {
event.stopPropagation();
onVersionChange?.(version);
}}
>
{version.label}
</MenuItemOption>
))}
</MenuOptionGroup>
</MenuList>
</Menu>
)}
</Flex>
<NavContext.Provider value={navContextValue}>
<chakra.nav px="4" pb="3" ref={navRef}>
<NavItems items={navItems} />
</chakra.nav>
</NavContext.Provider>
<Flex
align="center"
mt="auto"
flexShrink={0}
pos="sticky"
bottom="0"
pl="4"
pr="2"
bg="bg"
borderTopWidth={1}
css={{
height: PAGE_FOOTER_HEIGHT
}}
>
<chakra.span fontWeight="semibold" fontSize="sm">
Navigation controls
</chakra.span>
<HStack spacing="1" ml="auto">
{navGroups.length > 0 && (
<Tooltip
label={`${
isAllExpanded ? 'Collapse' : 'Expand'
} all categories`}
>
<div>
<SidebarButton
aria-label={`${
isAllExpanded ? 'Collapse' : 'Expand'
} all categories`}
icon={
isAllExpanded ? (
<BiCollapseVertical />
) : (
<BiExpandVertical />
)
}
onClick={event => {
event.stopPropagation();
const expanded = !isAllExpanded;
setLocalNavState(
navGroups.reduce(
(acc, group) => ({
...acc,
[group.id]: expanded
}),
{}
)
);
window.gtag?.('event', 'Toggle all', {
event_category: GA_EVENT_CATEGORY_SIDEBAR,
event_label: expanded ? 'expand' : 'collapse'
});
}}
/>
</div>
</Tooltip>
)}
{hideSidebar && (
<Tooltip label="Hide navigation">
<div>
<SidebarButton
aria-label="Hide navigation"
onClick={hideSidebar}
icon={<FiChevronsLeft />}
/>
</div>
</Tooltip>
)}
{onLockToggle && (
<Tooltip label={`${isLocked ? 'Unlock' : 'Lock'} sidebar`}>
<div>
<SidebarButton
aria-label={`${isLocked ? 'Unlock' : 'Lock'} sidebar`}
icon={isLocked ? <FiLock /> : <FiUnlock />}
onClick={onLockToggle}
/>
</div>
</Tooltip>
)}
</HStack>
</Flex>
</Flex>
);
}
);
SidebarNav.propTypes = {
docset: PropTypes.string.isRequired,
navItems: PropTypes.array.isRequired,
versions: PropTypes.array,
currentVersion: PropTypes.string,
onVersionChange: PropTypes.func,
onGoBack: PropTypes.func,
hideSidebar: PropTypes.func,
isLocked: PropTypes.bool,
onLockToggle: PropTypes.func
};
SidebarNav.displayName = 'SidebarNav';
export default SidebarNav;