-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathMenu.js
289 lines (253 loc) · 6.27 KB
/
Menu.js
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import React, { useMemo, useState, useRef, useEffect } from 'react'
import { Drawer } from '@mui/material'
import clsx from 'clsx'
import PropTypes from 'prop-types'
import MenuContext from './MenuContext'
import SEOLinks from './SEOLinks'
import MenuBody from './MenuBody'
const PREFIX = 'RSFMenu'
const defaultClasses = {
drawer: `${PREFIX}-drawer`,
list: `${PREFIX}-list`,
listPadding: `${PREFIX}-listPadding`,
header: `${PREFIX}-header`,
icon: `${PREFIX}-icon`,
headerText: `${PREFIX}-headerText`,
bodyWrap: `${PREFIX}-bodyWrap`,
hidden: `${PREFIX}-hidden`,
visible: `${PREFIX}-visible`,
link: `${PREFIX}-link`,
leaf: `${PREFIX}-leaf`,
drawerFixed: `${PREFIX}-drawerFixed`,
}
const Menu = React.memo(props => {
const {
classes: c = {},
className,
anchor,
drawerWidth,
persistent,
root,
open,
onClose,
renderFooter,
renderHeader,
renderBack,
renderItem,
renderItemContent,
renderDrawer,
...others
} = props
const classes = { ...defaultClasses, ...c }
const [state, setState] = useState(() => {
return {
card: 0,
cards: [{ ...root, root: true }],
}
})
// this is needed so we can always update the *current* state, not the snapshot that
// was present when the callbacks were memoized
const stateRef = useRef(state)
useEffect(() => {
stateRef.current = state
}, [state])
useEffect(() => {
setState({
card: 0,
cards: [{ ...root, root: true }],
})
}, [root])
// this ensures that the expanded state is reset when showing a new card
const nextKey = useRef(0)
const onItemClick = (item, depth) => {
const cards = [...stateRef.current.cards]
const card = depth + 1
item.key = nextKey.current++ // this ensures that the expanded state is reset when showing a new card
if (card >= cards.length) {
cards.push(item)
} else {
cards[card] = item
}
setState({
card,
cards: cards.slice(0, card + 1),
})
}
const goBack = card => {
setState({
card,
cards: stateRef.current.cards,
})
}
// it is important to memoize the context, otherwise it will cause all consumers to rerender
// every time Menu rerenders
const context = useMemo(
() => ({
classes,
onItemClick,
goBack,
renderFooter,
renderHeader,
renderBack,
renderItem,
renderItemContent,
close: onClose,
drawerWidth,
}),
[classes],
)
return (
<MenuContext.Provider value={context}>
{renderDrawer ? (
renderDrawer()
) : (
<Drawer
variant={persistent ? 'persistent' : 'temporary'}
open={open || persistent}
onClose={onClose}
anchor={anchor}
ModalProps={{
keepMounted: true,
}}
PaperProps={{
style: { width: `${drawerWidth}px` },
}}
classes={{
root: className,
paper: clsx(classes.drawer, {
[classes.drawerFixed]: persistent,
}),
modal: classes.modal,
}}
>
<MenuBody
card={state.card}
cards={state.cards}
root={root}
drawerWidth={drawerWidth}
{...others}
/>
</Drawer>
)}
<SEOLinks root={root} />
</MenuContext.Provider>
)
})
Menu.propTypes = {
root: PropTypes.object,
/**
* The width of the drawer in pixels
*/
drawerWidth: PropTypes.number,
/**
* An element to display at the top of the root of the menu
*/
rootHeader: PropTypes.element,
/**
* An element to display at the bottom of the root of the menu
*/
rootFooter: PropTypes.element,
/**
* A function to render a custom header in menu cards. It is passed an object
* with:
*
* - item: The menu item record being rendered
*
* The function should return a React element or fragment.
*/
renderHeader: PropTypes.func,
/**
* A function to render a custom footer menu cards. It is passed an object
* with:
*
* - item: The menu item record being rendered
*
* The function should return a React element or fragment.
*/
renderFooter: PropTypes.func,
/**
* A function to render a custom back navigation for menu cards. It is passed
* an object with:
*
* - item: The menu item record being rendered
*
* The function should return a React element or fragment.
*/
renderBack: PropTypes.func,
/**
* Set to true to display the menu
*/
open: PropTypes.bool,
/**
* Set to true to dock the menu so that it's always open and not modal
*/
persistent: PropTypes.bool,
/**
* CSS classes for this component
*/
classes: PropTypes.objectOf(PropTypes.string),
/**
* Called when the menu is closed
*/
onClose: PropTypes.func,
/**
* The icon to use for collapsed groups
*/
ExpandIcon: PropTypes.elementType,
/**
* The icon to use for expanded groups
*/
CollapseIcon: PropTypes.elementType,
/**
* Sets the side of the screen from which the menu appears.
*/
anchor: PropTypes.oneOf(['left', 'right']),
/**
* Overrides the default rendering of a menu item. It is passed the following arguments:
*
* - item - the menu item record being rendered.
*
* Return undefined to render the default contents
*
* Example:
*
* ```js
* renderItem={item => {
* return item.text === 'My Special Item ? <MySpecialItem/> : null
* }}
* ```
*/
renderItem: PropTypes.func,
/**
* Overrides the content of a menu item. It is passed the following arguments:
*
* - item - the menu item record being rendered.
*
* Return null to render the default contents
*
* Example:
*
* ```js
* renderItemContent={item => {
* return leaf ? <ListItemText primary={item.text}/> : null
* }}
* ```
*/
renderItemContent: PropTypes.func,
/**
* Set to `true` to show the item corresponding to the current URL as selected.
*/
trackSelected: PropTypes.bool,
/**
* A function to override the rendering the drawer
*/
renderDrawer: PropTypes.func,
className: PropTypes.string,
}
Menu.defaultProps = {
drawerWidth: 330,
anchor: 'left',
trackSelected: false,
DrawerComponent: Drawer,
}
export default Menu