UNPKG

5.02 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 const {
41 actions,
42 anchor: anchorProp,
43 children,
44 onItemsChange,
45 slotProps = {},
46 slots = {}
47 } = props,
48 other = _objectWithoutPropertiesLoose(props, _excluded);
49 const {
50 contextValue,
51 getListboxProps,
52 dispatch,
53 open,
54 triggerElement
55 } = useMenu({
56 onItemsChange,
57 componentName: 'Menu'
58 });
59 const anchor = anchorProp ?? triggerElement;
60 React.useImperativeHandle(actions, () => ({
61 dispatch,
62 resetHighlight: () => dispatch({
63 type: ListActionTypes.resetHighlight,
64 event: null
65 })
66 }), [dispatch]);
67 const ownerState = _extends({}, props, {
68 open
69 });
70 const classes = useUtilityClasses(ownerState);
71 const Root = slots.root ?? 'div';
72 const rootProps = useSlotProps({
73 elementType: Root,
74 externalSlotProps: slotProps.root,
75 externalForwardedProps: other,
76 additionalProps: {
77 ref: forwardedRef,
78 role: undefined
79 },
80 className: classes.root,
81 ownerState
82 });
83 const Listbox = slots.listbox ?? 'ul';
84 const listboxProps = useSlotProps({
85 elementType: Listbox,
86 getSlotProps: getListboxProps,
87 externalSlotProps: slotProps.listbox,
88 className: classes.listbox,
89 ownerState
90 });
91 if (open === true && anchor == null) {
92 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
93 children: /*#__PURE__*/_jsx(Listbox, _extends({}, listboxProps, {
94 children: /*#__PURE__*/_jsx(MenuProvider, {
95 value: contextValue,
96 children: children
97 })
98 }))
99 }));
100 }
101 return /*#__PURE__*/_jsx(Popup, _extends({
102 keepMounted: true
103 }, rootProps, {
104 open: open,
105 anchor: anchor,
106 slots: {
107 root: Root
108 },
109 children: /*#__PURE__*/_jsx(Listbox, _extends({}, listboxProps, {
110 children: /*#__PURE__*/_jsx(MenuProvider, {
111 value: contextValue,
112 children: children
113 })
114 }))
115 }));
116});
117process.env.NODE_ENV !== "production" ? Menu.propTypes /* remove-proptypes */ = {
118 // ┌────────────────────────────── Warning ──────────────────────────────┐
119 // │ These PropTypes are generated from the TypeScript type definitions. │
120 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
121 // └─────────────────────────────────────────────────────────────────────┘
122 /**
123 * A ref with imperative actions that can be performed on the menu.
124 */
125 actions: refType,
126 /**
127 * The element based on which the menu is positioned.
128 */
129 anchor: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
130 /**
131 * @ignore
132 */
133 children: PropTypes.node,
134 /**
135 * @ignore
136 */
137 className: PropTypes.string,
138 /**
139 * Function called when the items displayed in the menu change.
140 */
141 onItemsChange: PropTypes.func,
142 /**
143 * The props used for each slot inside the Menu.
144 * @default {}
145 */
146 slotProps: PropTypes.shape({
147 listbox: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
148 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
149 }),
150 /**
151 * The components used for each slot inside the Menu.
152 * Either a string to use a HTML element or a component.
153 * @default {}
154 */
155 slots: PropTypes.shape({
156 listbox: PropTypes.elementType,
157 root: PropTypes.elementType
158 })
159} : void 0;
160export { Menu };
\No newline at end of file