forked from mui/material-ui
/
MenuIconSideNavExample.js
185 lines (180 loc) · 5.26 KB
/
MenuIconSideNavExample.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
import * as React from 'react';
import Menu, { menuClasses } from '@mui/joy/Menu';
import MenuItem from '@mui/joy/MenuItem';
import IconButton from '@mui/joy/IconButton';
import List from '@mui/joy/List';
import ListItem from '@mui/joy/ListItem';
import Sheet from '@mui/joy/Sheet';
import Apps from '@mui/icons-material/Apps';
import Settings from '@mui/icons-material/Settings';
import Person from '@mui/icons-material/Person';
// The Menu is built on top of Popper v2, so it accepts `modifiers` prop that will be passed to the Popper.
// https://popper.js.org/docs/v2/modifiers/offset/
const modifiers = [
{
name: 'offset',
options: {
offset: ({ placement }) => {
if (placement.includes('end')) {
return [8, 20];
}
return [-8, 20];
},
},
},
];
function MenuButton({ children, menu, open, onOpen, onLeaveMenu, label, ...props }) {
const buttonRef = React.useRef(null);
const isOnButton = React.useRef(false);
const menuActions = React.useRef(null);
const internalOpen = React.useRef(open);
const handleButtonKeyDown = (event) => {
internalOpen.current = open;
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
onOpen(event);
if (event.key === 'ArrowUp') {
menuActions.current?.highlightLastItem();
}
}
};
return (
<React.Fragment>
<IconButton
{...props}
ref={buttonRef}
variant="plain"
color="neutral"
aria-haspopup="menu"
aria-expanded={open ? 'true' : undefined}
aria-controls={open ? `nav-example-menu-${label}` : undefined}
onMouseDown={() => {
internalOpen.current = open;
}}
onClick={() => {
if (!internalOpen.current) {
onOpen();
}
}}
onMouseEnter={() => {
onOpen();
isOnButton.current = true;
}}
onMouseLeave={() => {
isOnButton.current = false;
}}
onKeyDown={handleButtonKeyDown}
sx={{
bgcolor: open ? 'neutral.plainHoverBg' : undefined,
'&.Joy-focusVisible': {
bgcolor: 'neutral.plainHoverBg',
},
}}
>
{children}
</IconButton>
{React.cloneElement(menu, {
open,
onClose: () => {
menu.props.onClose?.();
buttonRef.current?.focus();
},
onMouseLeave: () => {
onLeaveMenu(() => isOnButton.current);
},
actions: menuActions,
anchorEl: buttonRef.current,
modifiers,
slotProps: {
listbox: {
id: `nav-example-menu-${label}`,
'aria-label': label,
},
},
placement: 'right-start',
sx: {
width: 288,
[`& .${menuClasses.listbox}`]: {
'--List-padding': 'var(--List-divider-gap)',
},
},
})}
</React.Fragment>
);
}
export default function MenuIconSideNavExample() {
const [menuIndex, setMenuIndex] = React.useState(null);
const itemProps = {
onClick: () => setMenuIndex(null),
};
const createHandleLeaveMenu = (index) => (getIsOnButton) => {
setTimeout(() => {
const isOnButton = getIsOnButton();
if (!isOnButton) {
setMenuIndex((latestIndex) => {
if (index === latestIndex) {
return null;
}
return latestIndex;
});
}
}, 200);
};
return (
<Sheet sx={{ borderRadius: 'sm', py: 1, mr: 20, bgcolor: 'background.body' }}>
<List>
<ListItem>
<MenuButton
label="Apps"
open={menuIndex === 0}
onOpen={() => setMenuIndex(0)}
onLeaveMenu={createHandleLeaveMenu(0)}
menu={
<Menu onClose={() => setMenuIndex(null)}>
<MenuItem {...itemProps}>Application 1</MenuItem>
<MenuItem {...itemProps}>Application 2</MenuItem>
<MenuItem {...itemProps}>Application 3</MenuItem>
</Menu>
}
>
<Apps />
</MenuButton>
</ListItem>
<ListItem>
<MenuButton
label="Settings"
open={menuIndex === 1}
onOpen={() => setMenuIndex(1)}
onLeaveMenu={createHandleLeaveMenu(1)}
menu={
<Menu onClose={() => setMenuIndex(null)}>
<MenuItem {...itemProps}>Setting 1</MenuItem>
<MenuItem {...itemProps}>Setting 2</MenuItem>
<MenuItem {...itemProps}>Setting 3</MenuItem>
</Menu>
}
>
<Settings />
</MenuButton>
</ListItem>
<ListItem>
<MenuButton
label="Personal"
open={menuIndex === 2}
onOpen={() => setMenuIndex(2)}
onLeaveMenu={createHandleLeaveMenu(2)}
menu={
<Menu onClose={() => setMenuIndex(null)}>
<MenuItem {...itemProps}>Personal 1</MenuItem>
<MenuItem {...itemProps}>Personal 2</MenuItem>
<MenuItem {...itemProps}>Personal 3</MenuItem>
</Menu>
}
>
<Person />
</MenuButton>
</ListItem>
</List>
</Sheet>
);
}