-
-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathDashboardSidebarPageItem.tsx
More file actions
372 lines (355 loc) · 10.7 KB
/
DashboardSidebarPageItem.tsx
File metadata and controls
372 lines (355 loc) · 10.7 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled, type Theme, SxProps } from '@mui/material';
import Avatar from '@mui/material/Avatar';
import Box from '@mui/material/Box';
import Collapse from '@mui/material/Collapse';
import Grow from '@mui/material/Grow';
import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import type {} from '@mui/material/themeCssVarsAugmentation';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import invariant from 'invariant';
import { Link } from '../shared/Link';
import { DashboardSidebarPageItemContext, NavigationContext } from '../shared/context';
import { getItemPath, getItemTitle } from '../shared/navigation';
import { MINI_DRAWER_WIDTH } from './shared';
import type { Navigation, NavigationPageItem } from '../AppProvider';
const NavigationListItemButton = styled(ListItemButton)(({ theme }) => ({
borderRadius: 8,
'&.Mui-selected': {
'& .MuiListItemIcon-root': {
color: (theme.vars ?? theme).palette.primary.dark,
},
'& .MuiTypography-root': {
color: (theme.vars ?? theme).palette.primary.dark,
},
'& .MuiSvgIcon-root': {
color: (theme.vars ?? theme).palette.primary.dark,
},
'& .MuiAvatar-root': {
backgroundColor: (theme.vars ?? theme).palette.primary.dark,
},
'& .MuiTouchRipple-child': {
backgroundColor: (theme.vars ?? theme).palette.primary.dark,
},
},
'& .MuiSvgIcon-root': {
color: (theme.vars ?? theme).palette.action.active,
},
'& .MuiAvatar-root': {
backgroundColor: (theme.vars ?? theme).palette.action.active,
},
}));
export interface DashboardSidebarPageItemProps {
/**
* Navigation page item definition.
*/
item: NavigationPageItem;
/**
* Link `href` for when the item is rendered as a link.
* @default getItemPath(navigationContext, item)
*/
href?: string;
/**
* The component used to render the item as a link.
* @default Link
*/
LinkComponent?: React.ElementType;
/**
* If `true`, expands any nested navigation in the item, otherwise collapse it.
* @default false
*/
expanded?: boolean;
/**
* Use to apply selected styling.
* @default false
*/
selected?: boolean;
/**
* If `true`, the item is disabled.
* @default false
*/
disabled?: boolean;
}
export interface DashboardSidebarPageItemContextProps
extends Partial<DashboardSidebarPageItemProps> {
id: string;
onClick: (itemId: string, item: NavigationPageItem) => void;
isMini?: boolean;
isSidebarFullyExpanded?: boolean;
isSidebarFullyCollapsed?: boolean;
renderNestedNavigation: (subNavigation: Navigation) => React.ReactNode;
}
const LIST_ITEM_ICON_SIZE = 34;
/**
*
* Demos:
*
* - [Dashboard Layout](https://mui.com/toolpad/core/react-dashboard-layout/)
*
* API:
*
* - [DashboardSidebarPageItem API](https://mui.com/toolpad/core/api/dashboard-sidebar-page-item)
*/
function DashboardSidebarPageItem(props: DashboardSidebarPageItemProps) {
const navigationContext = React.useContext(NavigationContext);
const pageItemContextProps = React.useContext(DashboardSidebarPageItemContext);
invariant(pageItemContextProps, 'No navigation page item context provided.');
const contextAwareProps = {
...pageItemContextProps,
...props,
};
const {
item,
href = getItemPath(navigationContext, item),
LinkComponent: LinkComponentProp,
expanded = false,
selected = false,
disabled = false,
id,
onClick,
isMini = false,
isSidebarFullyExpanded = true,
isSidebarFullyCollapsed = false,
renderNestedNavigation,
} = contextAwareProps;
const [hoveredMiniSidebarItemId, setHoveredMiniSidebarItemId] = React.useState<string | null>(
null,
);
const handleClick = React.useCallback(() => {
onClick(id, item);
}, [id, item, onClick]);
let nestedNavigationCollapseSx: SxProps<Theme> = { display: 'none' };
if (isMini && isSidebarFullyCollapsed) {
nestedNavigationCollapseSx = {
fontSize: 18,
position: 'absolute',
top: '41.5%',
right: '2px',
transform: 'translateY(-50%) rotate(-90deg)',
};
} else if (!isMini && isSidebarFullyExpanded) {
nestedNavigationCollapseSx = {
ml: 0.5,
transform: `rotate(${expanded ? 0 : -90}deg)`,
transition: (theme: Theme) =>
theme.transitions.create('transform', {
easing: theme.transitions.easing.sharp,
duration: 100,
}),
};
}
const hasExternalHref = href.startsWith('http://') || href.startsWith('https://');
const LinkComponent = LinkComponentProp ?? (hasExternalHref ? 'a' : Link);
const title = getItemTitle(item);
const listItem = (
<ListItem
{...(item.children && isMini
? {
onMouseEnter: () => {
setHoveredMiniSidebarItemId(id);
},
onMouseLeave: () => {
setHoveredMiniSidebarItemId(null);
},
}
: {})}
sx={{
py: 0,
px: 1,
overflowX: 'hidden',
}}
>
<NavigationListItemButton
selected={selected}
disabled={disabled}
sx={{
px: 1.4,
height: isMini ? 60 : undefined,
}}
{...(item.children && !isMini
? {
onClick: handleClick,
}
: {})}
{...(!item.children
? {
LinkComponent,
...(hasExternalHref
? {
target: '_blank',
rel: 'noopener noreferrer',
}
: {}),
href,
onClick: handleClick,
}
: {})}
>
{item.icon || isMini ? (
<Box
sx={
isMini
? {
position: 'absolute',
left: '50%',
top: 'calc(50% - 6px)',
transform: 'translate(-50%, -50%)',
}
: {}
}
>
<ListItemIcon
sx={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
minWidth: LIST_ITEM_ICON_SIZE,
}}
>
{item.icon ?? null}
{!item.icon && isMini ? (
<Avatar
sx={{
width: LIST_ITEM_ICON_SIZE - 7,
height: LIST_ITEM_ICON_SIZE - 7,
fontSize: 12,
}}
>
{title
.split(' ')
.slice(0, 2)
.map((titleWord) => titleWord.charAt(0).toUpperCase())}
</Avatar>
) : null}
</ListItemIcon>
{isMini ? (
<Typography
variant="caption"
sx={{
position: 'absolute',
bottom: -18,
left: '50%',
transform: 'translateX(-50%)',
fontSize: 10,
fontWeight: 500,
textAlign: 'center',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
maxWidth: MINI_DRAWER_WIDTH - 28,
}}
>
{title}
</Typography>
) : null}
</Box>
) : null}
{!isMini ? (
<ListItemText
primary={title}
sx={{
ml: 1.2,
whiteSpace: 'nowrap',
zIndex: 1,
}}
/>
) : null}
{item.action && !isMini && isSidebarFullyExpanded ? item.action : null}
{item.children ? <ExpandMoreIcon sx={nestedNavigationCollapseSx} /> : null}
</NavigationListItemButton>
{item.children && isMini ? (
<Grow in={id === hoveredMiniSidebarItemId}>
<Box
sx={{
position: 'fixed',
left: MINI_DRAWER_WIDTH - 2,
pl: '6px',
}}
>
<Paper
sx={{
pt: 0.5,
pb: 0.5,
transform: 'translateY(calc(50% - 30px))',
}}
>
{renderNestedNavigation(item.children)}
</Paper>
</Box>
</Grow>
) : null}
</ListItem>
);
return (
<React.Fragment key={id}>
{listItem}
{item.children && !isMini ? (
<Collapse in={expanded} timeout="auto" unmountOnExit>
{renderNestedNavigation(item.children)}
</Collapse>
) : null}
</React.Fragment>
);
}
DashboardSidebarPageItem.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* If `true`, the item is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, expands any nested navigation in the item, otherwise collapse it.
* @default false
*/
expanded: PropTypes.bool,
/**
* Link `href` for when the item is rendered as a link.
* @default getItemPath(navigationContext, item)
*/
href: PropTypes.string,
/**
* Navigation page item definition.
*/
item: PropTypes.shape({
action: PropTypes.node,
children: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.object,
PropTypes.shape({
kind: PropTypes.oneOf(['header']).isRequired,
title: PropTypes.string.isRequired,
}),
PropTypes.shape({
kind: PropTypes.oneOf(['divider']).isRequired,
}),
]).isRequired,
),
icon: PropTypes.node,
kind: PropTypes.oneOf(['page']),
pattern: PropTypes.string,
segment: PropTypes.string,
title: PropTypes.string,
}).isRequired,
/**
* The component used to render the item as a link.
* @default Link
*/
LinkComponent: PropTypes.elementType,
/**
* Use to apply selected styling.
* @default false
*/
selected: PropTypes.bool,
} as any;
export { DashboardSidebarPageItem };