1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
5 | const _excluded = ["actions", "anchor", "children", "onItemsChange", "slotProps", "slots"];
|
6 | import * as React from 'react';
|
7 | import PropTypes from 'prop-types';
|
8 | import { HTMLElementType, refType } from '@mui/utils';
|
9 | import { getMenuUtilityClass } from './menuClasses';
|
10 | import { useMenu } from '../useMenu';
|
11 | import { MenuProvider } from '../useMenu/MenuProvider';
|
12 | import { unstable_composeClasses as composeClasses } from '../composeClasses';
|
13 | import { Unstable_Popup as Popup } from '../Unstable_Popup';
|
14 | import { useSlotProps } from '../utils/useSlotProps';
|
15 | import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
|
16 | import { ListActionTypes } from '../useList';
|
17 | import { jsx as _jsx } from "react/jsx-runtime";
|
18 | function 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 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const Menu = 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 _jsx(Root, _extends({}, rootProps, {
|
93 | children: _jsx(Listbox, _extends({}, listboxProps, {
|
94 | children: _jsx(MenuProvider, {
|
95 | value: contextValue,
|
96 | children: children
|
97 | })
|
98 | }))
|
99 | }));
|
100 | }
|
101 | return _jsx(Popup, _extends({
|
102 | keepMounted: true
|
103 | }, rootProps, {
|
104 | open: open,
|
105 | anchor: anchor,
|
106 | slots: {
|
107 | root: Root
|
108 | },
|
109 | children: _jsx(Listbox, _extends({}, listboxProps, {
|
110 | children: _jsx(MenuProvider, {
|
111 | value: contextValue,
|
112 | children: children
|
113 | })
|
114 | }))
|
115 | }));
|
116 | });
|
117 | process.env.NODE_ENV !== "production" ? Menu.propTypes = {
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | |
123 |
|
124 |
|
125 | actions: refType,
|
126 | |
127 |
|
128 |
|
129 | anchor: PropTypes .oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
|
130 | |
131 |
|
132 |
|
133 | children: PropTypes.node,
|
134 | |
135 |
|
136 |
|
137 | className: PropTypes.string,
|
138 | |
139 |
|
140 |
|
141 | onItemsChange: PropTypes.func,
|
142 | |
143 |
|
144 |
|
145 |
|
146 | slotProps: PropTypes.shape({
|
147 | listbox: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
|
148 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
149 | }),
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 | slots: PropTypes.shape({
|
156 | listbox: PropTypes.elementType,
|
157 | root: PropTypes.elementType
|
158 | })
|
159 | } : void 0;
|
160 | export { Menu }; |
\ | No newline at end of file |