-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Menu.js
294 lines (269 loc) · 8.22 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
290
291
292
293
294
import * as React from 'react';
import { isFragment } from 'react-is';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/core';
import { HTMLElementType } from '@mui/utils';
import MenuList from '../MenuList';
import Paper from '../Paper';
import Popover from '../Popover';
import styled, { rootShouldForwardProp } from '../styles/styled';
import useTheme from '../styles/useTheme';
import useThemeProps from '../styles/useThemeProps';
import { getMenuUtilityClass } from './menuClasses';
const RTL_ORIGIN = {
vertical: 'top',
horizontal: 'right',
};
const LTR_ORIGIN = {
vertical: 'top',
horizontal: 'left',
};
const useUtilityClasses = (ownerState) => {
const { classes } = ownerState;
const slots = {
root: ['root'],
paper: ['paper'],
list: ['list'],
};
return composeClasses(slots, getMenuUtilityClass, classes);
};
const MenuRoot = styled(Popover, {
shouldForwardProp: (prop) => rootShouldForwardProp(prop) || prop === 'classes',
name: 'MuiMenu',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})({});
const MenuPaper = styled(Paper, {
name: 'MuiMenu',
slot: 'Paper',
overridesResolver: (props, styles) => styles.paper,
})({
// specZ: The maximum height of a simple menu should be one or more rows less than the view
// height. This ensures a tapable area outside of the simple menu with which to dismiss
// the menu.
maxHeight: 'calc(100% - 96px)',
// Add iOS momentum scrolling for iOS < 13.0
WebkitOverflowScrolling: 'touch',
});
const MenuMenuList = styled(MenuList, {
name: 'MuiMenu',
slot: 'List',
overridesResolver: (props, styles) => styles.list,
})({
// We disable the focus ring for mouse, touch and keyboard users.
outline: 0,
});
const Menu = React.forwardRef(function Menu(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiMenu' });
const {
autoFocus = true,
children,
disableAutoFocusItem = false,
MenuListProps = {},
onClose,
open,
PaperProps = {},
PopoverClasses,
transitionDuration = 'auto',
TransitionProps: { onEntering, ...TransitionProps } = {},
variant = 'selectedMenu',
...other
} = props;
const theme = useTheme();
const isRtl = theme.direction === 'rtl';
const ownerState = {
...props,
autoFocus,
disableAutoFocusItem,
MenuListProps,
onEntering,
PaperProps,
transitionDuration,
TransitionProps,
variant,
};
const classes = useUtilityClasses(ownerState);
const autoFocusItem = autoFocus && !disableAutoFocusItem && open;
const menuListActionsRef = React.useRef(null);
const handleEntering = (element, isAppearing) => {
if (menuListActionsRef.current) {
menuListActionsRef.current.adjustStyleForScrollbar(element, theme);
}
if (onEntering) {
onEntering(element, isAppearing);
}
};
const handleListKeyDown = (event) => {
if (event.key === 'Tab') {
event.preventDefault();
if (onClose) {
onClose(event, 'tabKeyDown');
}
}
};
/**
* the index of the item should receive focus
* in a `variant="selectedMenu"` it's the first `selected` item
* otherwise it's the very first item.
*/
let activeItemIndex = -1;
// since we inject focus related props into children we have to do a lookahead
// to check if there is a `selected` item. We're looking for the last `selected`
// item and use the first valid item as a fallback
React.Children.map(children, (child, index) => {
if (!React.isValidElement(child)) {
return;
}
if (process.env.NODE_ENV !== 'production') {
if (isFragment(child)) {
console.error(
[
"MUI: The Menu component doesn't accept a Fragment as a child.",
'Consider providing an array instead.',
].join('\n'),
);
}
}
if (!child.props.disabled) {
if (variant === 'selectedMenu' && child.props.selected) {
activeItemIndex = index;
} else if (activeItemIndex === -1) {
activeItemIndex = index;
}
}
});
return (
<MenuRoot
classes={PopoverClasses}
onClose={onClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: isRtl ? 'right' : 'left',
}}
transformOrigin={isRtl ? RTL_ORIGIN : LTR_ORIGIN}
PaperProps={{
component: MenuPaper,
...PaperProps,
classes: {
...PaperProps.classes,
root: classes.paper,
},
}}
className={classes.root}
open={open}
ref={ref}
transitionDuration={transitionDuration}
TransitionProps={{ onEntering: handleEntering, ...TransitionProps }}
ownerState={ownerState}
{...other}
>
<MenuMenuList
onKeyDown={handleListKeyDown}
actions={menuListActionsRef}
autoFocus={autoFocus && (activeItemIndex === -1 || disableAutoFocusItem)}
autoFocusItem={autoFocusItem}
variant={variant}
{...MenuListProps}
className={clsx(classes.list, MenuListProps.className)}
>
{children}
</MenuMenuList>
</MenuRoot>
);
});
Menu.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* An HTML element, or a function that returns one.
* It's used to set the position of the menu.
*/
anchorEl: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
HTMLElementType,
PropTypes.func,
]),
/**
* If `true` (Default) will focus the `[role="menu"]` if no focusable child is found. Disabled
* children are not focusable. If you set this prop to `false` focus will be placed
* on the parent modal container. This has severe accessibility implications
* and should only be considered if you manage focus otherwise.
* @default true
*/
autoFocus: PropTypes.bool,
/**
* Menu contents, normally `MenuItem`s.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* When opening the menu will not focus the active item but the `[role="menu"]`
* unless `autoFocus` is also set to `false`. Not using the default means not
* following WAI-ARIA authoring practices. Please be considerate about possible
* accessibility implications.
* @default false
*/
disableAutoFocusItem: PropTypes.bool,
/**
* Props applied to the [`MenuList`](/api/menu-list/) element.
* @default {}
*/
MenuListProps: PropTypes.object,
/**
* Callback fired when the component requests to be closed.
*
* @param {object} event The event source of the callback.
* @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`, `"tabKeyDown"`.
*/
onClose: PropTypes.func,
/**
* If `true`, the component is shown.
*/
open: PropTypes.bool.isRequired,
/**
* @ignore
*/
PaperProps: PropTypes.object,
/**
* `classes` prop applied to the [`Popover`](/api/popover/) element.
*/
PopoverClasses: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object])),
PropTypes.func,
PropTypes.object,
]),
/**
* The length of the transition in `ms`, or 'auto'
* @default 'auto'
*/
transitionDuration: PropTypes.oneOfType([
PropTypes.oneOf(['auto']),
PropTypes.number,
PropTypes.shape({
appear: PropTypes.number,
enter: PropTypes.number,
exit: PropTypes.number,
}),
]),
/**
* Props applied to the transition element.
* By default, the element is based on this [`Transition`](https://reactcommunity.org/react-transition-group/transition) component.
* @default {}
*/
TransitionProps: PropTypes.object,
/**
* The variant to use. Use `menu` to prevent selected items from impacting the initial focus.
* @default 'selectedMenu'
*/
variant: PropTypes.oneOf(['menu', 'selectedMenu']),
};
export default Menu;