UNPKG

6.57 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _utils = require("@mui/utils");
13var _menuClasses = require("./menuClasses");
14var _useMenu = _interopRequireDefault(require("../useMenu"));
15var _composeClasses = _interopRequireDefault(require("../composeClasses"));
16var _Popper = _interopRequireDefault(require("../Popper"));
17var _useSlotProps = _interopRequireDefault(require("../utils/useSlotProps"));
18var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
19var _MenuProvider = _interopRequireDefault(require("../useMenu/MenuProvider"));
20var _jsxRuntime = require("react/jsx-runtime");
21const _excluded = ["actions", "anchorEl", "children", "defaultOpen", "listboxId", "onOpenChange", "open", "slotProps", "slots"];
22function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
24function useUtilityClasses(ownerState) {
25 const {
26 open
27 } = ownerState;
28 const slots = {
29 root: ['root', open && 'expanded'],
30 listbox: ['listbox', open && 'expanded']
31 };
32 return (0, _composeClasses.default)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_menuClasses.getMenuUtilityClass));
33}
34/**
35 *
36 * Demos:
37 *
38 * - [Menu](https://mui.com/base/react-menu/)
39 *
40 * API:
41 *
42 * - [Menu API](https://mui.com/base/react-menu/components-api/#menu)
43 */
44const Menu = /*#__PURE__*/React.forwardRef(function Menu(props, forwardedRef) {
45 var _slots$root, _slots$listbox;
46 const {
47 actions,
48 anchorEl,
49 children,
50 defaultOpen,
51 listboxId,
52 onOpenChange,
53 open: openProp,
54 slotProps = {},
55 slots = {}
56 } = props,
57 other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
58 const {
59 contextValue,
60 getListboxProps,
61 dispatch,
62 open
63 } = (0, _useMenu.default)({
64 defaultOpen,
65 open: openProp,
66 onOpenChange,
67 listboxId
68 });
69 React.useImperativeHandle(actions, () => ({
70 dispatch
71 }), [dispatch]);
72 const ownerState = (0, _extends2.default)({}, props, {
73 open
74 });
75 const classes = useUtilityClasses(ownerState);
76 const Root = (_slots$root = slots.root) != null ? _slots$root : _Popper.default;
77 const rootProps = (0, _useSlotProps.default)({
78 elementType: Root,
79 externalForwardedProps: other,
80 externalSlotProps: slotProps.root,
81 additionalProps: {
82 anchorEl,
83 open,
84 keepMounted: true,
85 role: undefined,
86 ref: forwardedRef
87 },
88 className: classes.root,
89 ownerState
90 });
91 const Listbox = (_slots$listbox = slots.listbox) != null ? _slots$listbox : 'ul';
92 const listboxProps = (0, _useSlotProps.default)({
93 elementType: Listbox,
94 getSlotProps: getListboxProps,
95 externalSlotProps: slotProps.listbox,
96 ownerState,
97 className: classes.listbox
98 });
99 return /*#__PURE__*/(0, _jsxRuntime.jsx)(Root, (0, _extends2.default)({}, rootProps, {
100 children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Listbox, (0, _extends2.default)({}, listboxProps, {
101 children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_MenuProvider.default, {
102 value: contextValue,
103 children: children
104 })
105 }))
106 }));
107});
108process.env.NODE_ENV !== "production" ? Menu.propTypes /* remove-proptypes */ = {
109 // ----------------------------- Warning --------------------------------
110 // | These PropTypes are generated from the TypeScript type definitions |
111 // | To update them edit TypeScript types and run "yarn proptypes" |
112 // ----------------------------------------------------------------------
113 /**
114 * A ref with imperative actions.
115 * It allows to select the first or last menu item.
116 */
117 actions: _utils.refType,
118 /**
119 * An HTML element, [virtualElement](https://popper.js.org/docs/v2/virtual-elements/),
120 * or a function that returns either.
121 * It's used to set the position of the popper.
122 */
123 anchorEl: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_utils.HTMLElementType, _propTypes.default.object, _propTypes.default.func]),
124 /**
125 * @ignore
126 */
127 children: _propTypes.default.node,
128 /**
129 * @ignore
130 */
131 defaultOpen: _propTypes.default.bool,
132 /**
133 * @ignore
134 */
135 listboxId: _propTypes.default.string,
136 /**
137 * Triggered when focus leaves the menu and the menu should close.
138 */
139 onOpenChange: _propTypes.default.func,
140 /**
141 * Controls whether the menu is displayed.
142 * @default false
143 */
144 open: _propTypes.default.bool,
145 /**
146 * The props used for each slot inside the Menu.
147 * @default {}
148 */
149 slotProps: _propTypes.default /* @typescript-to-proptypes-ignore */.shape({
150 listbox: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
151 root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
152 }),
153 /**
154 * The components used for each slot inside the Menu.
155 * Either a string to use a HTML element or a component.
156 * @default {}
157 */
158 slots: _propTypes.default.shape({
159 listbox: _propTypes.default.elementType,
160 root: _propTypes.default.elementType
161 })
162} : void 0;
163var _default = Menu;
164exports.default = _default;
\No newline at end of file