UNPKG

11.5 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _reactIs = require("react-is");
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _clsx = _interopRequireDefault(require("clsx"));
23
24var _utils = require("@material-ui/utils");
25
26var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
27
28var _Popover = _interopRequireDefault(require("../Popover"));
29
30var _MenuList = _interopRequireDefault(require("../MenuList"));
31
32var ReactDOM = _interopRequireWildcard(require("react-dom"));
33
34var _setRef = _interopRequireDefault(require("../utils/setRef"));
35
36var _useTheme = _interopRequireDefault(require("../styles/useTheme"));
37
38var _deprecatedPropType = _interopRequireDefault(require("../utils/deprecatedPropType"));
39
40var RTL_ORIGIN = {
41 vertical: 'top',
42 horizontal: 'right'
43};
44var LTR_ORIGIN = {
45 vertical: 'top',
46 horizontal: 'left'
47};
48var styles = {
49 /* Styles applied to the `Paper` component. */
50 paper: {
51 // specZ: The maximum height of a simple menu should be one or more rows less than the view
52 // height. This ensures a tapable area outside of the simple menu with which to dismiss
53 // the menu.
54 maxHeight: 'calc(100% - 96px)',
55 // Add iOS momentum scrolling.
56 WebkitOverflowScrolling: 'touch'
57 },
58
59 /* Styles applied to the `List` component via `MenuList`. */
60 list: {
61 // We disable the focus ring for mouse, touch and keyboard users.
62 outline: 0
63 }
64};
65exports.styles = styles;
66var Menu = /*#__PURE__*/React.forwardRef(function Menu(props, ref) {
67 var _props$autoFocus = props.autoFocus,
68 autoFocus = _props$autoFocus === void 0 ? true : _props$autoFocus,
69 children = props.children,
70 classes = props.classes,
71 _props$disableAutoFoc = props.disableAutoFocusItem,
72 disableAutoFocusItem = _props$disableAutoFoc === void 0 ? false : _props$disableAutoFoc,
73 _props$MenuListProps = props.MenuListProps,
74 MenuListProps = _props$MenuListProps === void 0 ? {} : _props$MenuListProps,
75 onClose = props.onClose,
76 onEnteringProp = props.onEntering,
77 open = props.open,
78 _props$PaperProps = props.PaperProps,
79 PaperProps = _props$PaperProps === void 0 ? {} : _props$PaperProps,
80 PopoverClasses = props.PopoverClasses,
81 _props$transitionDura = props.transitionDuration,
82 transitionDuration = _props$transitionDura === void 0 ? 'auto' : _props$transitionDura,
83 _props$TransitionProp = props.TransitionProps;
84 _props$TransitionProp = _props$TransitionProp === void 0 ? {} : _props$TransitionProp;
85 var onEntering = _props$TransitionProp.onEntering,
86 TransitionProps = (0, _objectWithoutProperties2.default)(_props$TransitionProp, ["onEntering"]),
87 _props$variant = props.variant,
88 variant = _props$variant === void 0 ? 'selectedMenu' : _props$variant,
89 other = (0, _objectWithoutProperties2.default)(props, ["autoFocus", "children", "classes", "disableAutoFocusItem", "MenuListProps", "onClose", "onEntering", "open", "PaperProps", "PopoverClasses", "transitionDuration", "TransitionProps", "variant"]);
90 var theme = (0, _useTheme.default)();
91 var autoFocusItem = autoFocus && !disableAutoFocusItem && open;
92 var menuListActionsRef = React.useRef(null);
93 var contentAnchorRef = React.useRef(null);
94
95 var getContentAnchorEl = function getContentAnchorEl() {
96 return contentAnchorRef.current;
97 };
98
99 var handleEntering = function handleEntering(element, isAppearing) {
100 if (menuListActionsRef.current) {
101 menuListActionsRef.current.adjustStyleForScrollbar(element, theme);
102 }
103
104 if (onEnteringProp) {
105 onEnteringProp(element, isAppearing);
106 }
107
108 if (onEntering) {
109 onEntering(element, isAppearing);
110 }
111 };
112
113 var handleListKeyDown = function handleListKeyDown(event) {
114 if (event.key === 'Tab') {
115 event.preventDefault();
116
117 if (onClose) {
118 onClose(event, 'tabKeyDown');
119 }
120 }
121 };
122 /**
123 * the index of the item should receive focus
124 * in a `variant="selectedMenu"` it's the first `selected` item
125 * otherwise it's the very first item.
126 */
127
128
129 var activeItemIndex = -1; // since we inject focus related props into children we have to do a lookahead
130 // to check if there is a `selected` item. We're looking for the last `selected`
131 // item and use the first valid item as a fallback
132
133 React.Children.map(children, function (child, index) {
134 if (! /*#__PURE__*/React.isValidElement(child)) {
135 return;
136 }
137
138 if (process.env.NODE_ENV !== 'production') {
139 if ((0, _reactIs.isFragment)(child)) {
140 console.error(["Material-UI: The Menu component doesn't accept a Fragment as a child.", 'Consider providing an array instead.'].join('\n'));
141 }
142 }
143
144 if (!child.props.disabled) {
145 if (variant !== "menu" && child.props.selected) {
146 activeItemIndex = index;
147 } else if (activeItemIndex === -1) {
148 activeItemIndex = index;
149 }
150 }
151 });
152 var items = React.Children.map(children, function (child, index) {
153 if (index === activeItemIndex) {
154 return /*#__PURE__*/React.cloneElement(child, {
155 ref: function ref(instance) {
156 // #StrictMode ready
157 contentAnchorRef.current = ReactDOM.findDOMNode(instance);
158 (0, _setRef.default)(child.ref, instance);
159 }
160 });
161 }
162
163 return child;
164 });
165 return /*#__PURE__*/React.createElement(_Popover.default, (0, _extends2.default)({
166 getContentAnchorEl: getContentAnchorEl,
167 classes: PopoverClasses,
168 onClose: onClose,
169 TransitionProps: (0, _extends2.default)({
170 onEntering: handleEntering
171 }, TransitionProps),
172 anchorOrigin: theme.direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN,
173 transformOrigin: theme.direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN,
174 PaperProps: (0, _extends2.default)({}, PaperProps, {
175 classes: (0, _extends2.default)({}, PaperProps.classes, {
176 root: classes.paper
177 })
178 }),
179 open: open,
180 ref: ref,
181 transitionDuration: transitionDuration
182 }, other), /*#__PURE__*/React.createElement(_MenuList.default, (0, _extends2.default)({
183 onKeyDown: handleListKeyDown,
184 actions: menuListActionsRef,
185 autoFocus: autoFocus && (activeItemIndex === -1 || disableAutoFocusItem),
186 autoFocusItem: autoFocusItem,
187 variant: variant
188 }, MenuListProps, {
189 className: (0, _clsx.default)(classes.list, MenuListProps.className)
190 }), items));
191});
192process.env.NODE_ENV !== "production" ? Menu.propTypes = {
193 // ----------------------------- Warning --------------------------------
194 // | These PropTypes are generated from the TypeScript type definitions |
195 // | To update them edit the d.ts file and run "yarn proptypes" |
196 // ----------------------------------------------------------------------
197
198 /**
199 * A HTML element, or a function that returns it.
200 * It's used to set the position of the menu.
201 */
202 anchorEl: _propTypes.default
203 /* @typescript-to-proptypes-ignore */
204 .oneOfType([_utils.HTMLElementType, _propTypes.default.func]),
205
206 /**
207 * If `true` (Default) will focus the `[role="menu"]` if no focusable child is found. Disabled
208 * children are not focusable. If you set this prop to `false` focus will be placed
209 * on the parent modal container. This has severe accessibility implications
210 * and should only be considered if you manage focus otherwise.
211 */
212 autoFocus: _propTypes.default.bool,
213
214 /**
215 * Menu contents, normally `MenuItem`s.
216 */
217 children: _propTypes.default.node,
218
219 /**
220 * Override or extend the styles applied to the component.
221 * See [CSS API](#css) below for more details.
222 */
223 classes: _propTypes.default.object,
224
225 /**
226 * When opening the menu will not focus the active item but the `[role="menu"]`
227 * unless `autoFocus` is also set to `false`. Not using the default means not
228 * following WAI-ARIA authoring practices. Please be considerate about possible
229 * accessibility implications.
230 */
231 disableAutoFocusItem: _propTypes.default.bool,
232
233 /**
234 * Props applied to the [`MenuList`](/api/menu-list/) element.
235 */
236 MenuListProps: _propTypes.default.object,
237
238 /**
239 * Callback fired when the component requests to be closed.
240 *
241 * @param {object} event The event source of the callback.
242 * @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`, `"tabKeyDown"`.
243 */
244 onClose: _propTypes.default.func,
245
246 /**
247 * Callback fired before the Menu enters.
248 * @deprecated Use the `TransitionProps` prop instead.
249 */
250 onEnter: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `TransitionProps` prop instead.'),
251
252 /**
253 * Callback fired when the Menu has entered.
254 * @deprecated Use the `TransitionProps` prop instead.
255 */
256 onEntered: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `TransitionProps` prop instead.'),
257
258 /**
259 * Callback fired when the Menu is entering.
260 * @deprecated Use the `TransitionProps` prop instead.
261 */
262 onEntering: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `TransitionProps` prop instead.'),
263
264 /**
265 * Callback fired before the Menu exits.
266 * @deprecated Use the `TransitionProps` prop instead.
267 */
268 onExit: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `TransitionProps` prop instead.'),
269
270 /**
271 * Callback fired when the Menu has exited.
272 * @deprecated Use the `TransitionProps` prop instead.
273 */
274 onExited: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `TransitionProps` prop instead.'),
275
276 /**
277 * Callback fired when the Menu is exiting.
278 * @deprecated Use the `TransitionProps` prop instead.
279 */
280 onExiting: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `TransitionProps` prop instead.'),
281
282 /**
283 * If `true`, the menu is visible.
284 */
285 open: _propTypes.default.bool.isRequired,
286
287 /**
288 * @ignore
289 */
290 PaperProps: _propTypes.default.object,
291
292 /**
293 * `classes` prop applied to the [`Popover`](/api/popover/) element.
294 */
295 PopoverClasses: _propTypes.default.object,
296
297 /**
298 * The length of the transition in `ms`, or 'auto'
299 */
300 transitionDuration: _propTypes.default.oneOfType([_propTypes.default.oneOf(['auto']), _propTypes.default.number, _propTypes.default.shape({
301 appear: _propTypes.default.number,
302 enter: _propTypes.default.number,
303 exit: _propTypes.default.number
304 })]),
305
306 /**
307 * Props applied to the transition element.
308 * By default, the element is based on this [`Transition`](http://reactcommunity.org/react-transition-group/transition) component.
309 */
310 TransitionProps: _propTypes.default.object,
311
312 /**
313 * The variant to use. Use `menu` to prevent selected items from impacting the initial focus
314 * and the vertical alignment relative to the anchor element.
315 */
316 variant: _propTypes.default.oneOf(['menu', 'selectedMenu'])
317} : void 0;
318
319var _default = (0, _withStyles.default)(styles, {
320 name: 'MuiMenu'
321})(Menu);
322
323exports.default = _default;
\No newline at end of file