UNPKG

5.15 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["actions", "anchor", "children", "onItemsChange", "slotProps", "slots"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { HTMLElementType, refType } from '@mui/utils';
9import { getMenuUtilityClass } from './menuClasses';
10import { useMenu } from '../useMenu';
11import { MenuProvider } from '../useMenu/MenuProvider';
12import { unstable_composeClasses as composeClasses } from '../composeClasses';
13import { Unstable_Popup as Popup } from '../Unstable_Popup';
14import { useSlotProps } from '../utils/useSlotProps';
15import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
16import { ListActionTypes } from '../useList';
17import { jsx as _jsx } from "react/jsx-runtime";
18function useUtilityClasses(ownerState) {
19 const {
20 open
21 } = ownerState;
22 const slots = {
23 root: ['root', open && 'expanded'],
24 listbox: ['listbox', open && 'expanded']
25 };
26 return composeClasses(slots, useClassNamesOverride(getMenuUtilityClass));
27}
28
29/**
30 *
31 * Demos:
32 *
33 * - [Menu](https://mui.com/base-ui/react-menu/)
34 *
35 * API:
36 *
37 * - [Menu API](https://mui.com/base-ui/react-menu/components-api/#menu)
38 */
39const Menu = /*#__PURE__*/React.forwardRef(function Menu(props, forwardedRef) {
40 var _slots$root, _slots$listbox;
41 const {
42 actions,
43 anchor: anchorProp,
44 children,
45 onItemsChange,
46 slotProps = {},
47 slots = {}
48 } = props,
49 other = _objectWithoutPropertiesLoose(props, _excluded);
50 const {
51 contextValue,
52 getListboxProps,
53 dispatch,
54 open,
55 triggerElement
56 } = useMenu({
57 onItemsChange,
58 componentName: 'Menu'
59 });
60 const anchor = anchorProp != null ? anchorProp : triggerElement;
61 React.useImperativeHandle(actions, () => ({
62 dispatch,
63 resetHighlight: () => dispatch({
64 type: ListActionTypes.resetHighlight,
65 event: null
66 })
67 }), [dispatch]);
68 const ownerState = _extends({}, props, {
69 open
70 });
71 const classes = useUtilityClasses(ownerState);
72 const Root = (_slots$root = slots.root) != null ? _slots$root : 'div';
73 const rootProps = useSlotProps({
74 elementType: Root,
75 externalSlotProps: slotProps.root,
76 externalForwardedProps: other,
77 additionalProps: {
78 ref: forwardedRef,
79 role: undefined
80 },
81 className: classes.root,
82 ownerState
83 });
84 const Listbox = (_slots$listbox = slots.listbox) != null ? _slots$listbox : 'ul';
85 const listboxProps = useSlotProps({
86 elementType: Listbox,
87 getSlotProps: getListboxProps,
88 externalSlotProps: slotProps.listbox,
89 className: classes.listbox,
90 ownerState
91 });
92 if (open === true && anchor == null) {
93 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
94 children: /*#__PURE__*/_jsx(Listbox, _extends({}, listboxProps, {
95 children: /*#__PURE__*/_jsx(MenuProvider, {
96 value: contextValue,
97 children: children
98 })
99 }))
100 }));
101 }
102 return /*#__PURE__*/_jsx(Popup, _extends({
103 keepMounted: true
104 }, rootProps, {
105 open: open,
106 anchor: anchor,
107 slots: {
108 root: Root
109 },
110 children: /*#__PURE__*/_jsx(Listbox, _extends({}, listboxProps, {
111 children: /*#__PURE__*/_jsx(MenuProvider, {
112 value: contextValue,
113 children: children
114 })
115 }))
116 }));
117});
118process.env.NODE_ENV !== "production" ? Menu.propTypes /* remove-proptypes */ = {
119 // ┌────────────────────────────── Warning ──────────────────────────────┐
120 // │ These PropTypes are generated from the TypeScript type definitions. │
121 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
122 // └─────────────────────────────────────────────────────────────────────┘
123 /**
124 * A ref with imperative actions that can be performed on the menu.
125 */
126 actions: refType,
127 /**
128 * The element based on which the menu is positioned.
129 */
130 anchor: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
131 /**
132 * @ignore
133 */
134 children: PropTypes.node,
135 /**
136 * @ignore
137 */
138 className: PropTypes.string,
139 /**
140 * Function called when the items displayed in the menu change.
141 */
142 onItemsChange: PropTypes.func,
143 /**
144 * The props used for each slot inside the Menu.
145 * @default {}
146 */
147 slotProps: PropTypes.shape({
148 listbox: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
149 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
150 }),
151 /**
152 * The components used for each slot inside the Menu.
153 * Either a string to use a HTML element or a component.
154 * @default {}
155 */
156 slots: PropTypes.shape({
157 listbox: PropTypes.elementType,
158 root: PropTypes.elementType
159 })
160} : void 0;
161export { Menu };
\No newline at end of file