UNPKG

4.59 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["children", "disabled", "label", "id", "slotProps", "slots"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { getMenuItemUtilityClass } from './menuItemClasses';
9import { useMenuItem, useMenuItemContextStabilizer } from '../useMenuItem';
10import { unstable_composeClasses as composeClasses } from '../composeClasses';
11import { useSlotProps } from '../utils/useSlotProps';
12import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
13import { ListContext } from '../useList';
14import { jsx as _jsx } from "react/jsx-runtime";
15function useUtilityClasses(ownerState) {
16 const {
17 disabled,
18 focusVisible
19 } = ownerState;
20 const slots = {
21 root: ['root', disabled && 'disabled', focusVisible && 'focusVisible']
22 };
23 return composeClasses(slots, useClassNamesOverride(getMenuItemUtilityClass));
24}
25const InnerMenuItem = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(function MenuItem(props, forwardedRef) {
26 var _slots$root;
27 const {
28 children,
29 disabled: disabledProp = false,
30 label,
31 id,
32 slotProps = {},
33 slots = {}
34 } = props,
35 other = _objectWithoutPropertiesLoose(props, _excluded);
36 const {
37 getRootProps,
38 disabled,
39 focusVisible,
40 highlighted
41 } = useMenuItem({
42 id,
43 disabled: disabledProp,
44 rootRef: forwardedRef,
45 label
46 });
47 const ownerState = _extends({}, props, {
48 disabled,
49 focusVisible,
50 highlighted
51 });
52 const classes = useUtilityClasses(ownerState);
53 const Root = (_slots$root = slots.root) != null ? _slots$root : 'li';
54 const rootProps = useSlotProps({
55 elementType: Root,
56 getSlotProps: getRootProps,
57 externalSlotProps: slotProps.root,
58 externalForwardedProps: other,
59 className: classes.root,
60 ownerState
61 });
62 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
63 children: children
64 }));
65}));
66
67/**
68 * An unstyled menu item to be used within a Menu.
69 *
70 * Demos:
71 *
72 * - [Menu](https://mui.com/base-ui/react-menu/)
73 *
74 * API:
75 *
76 * - [MenuItem API](https://mui.com/base-ui/react-menu/components-api/#menu-item)
77 */
78const MenuItem = /*#__PURE__*/React.forwardRef(function MenuItem(props, ref) {
79 const {
80 id: idProp
81 } = props;
82
83 // This wrapper component is used as a performance optimization.
84 // `useMenuItemContextStabilizer` ensures that the context value
85 // is stable across renders, so that the actual MenuItem re-renders
86 // only when it needs to.
87 const {
88 contextValue,
89 id
90 } = useMenuItemContextStabilizer(idProp);
91 return /*#__PURE__*/_jsx(ListContext.Provider, {
92 value: contextValue,
93 children: /*#__PURE__*/_jsx(InnerMenuItem, _extends({}, props, {
94 id: id,
95 ref: ref
96 }))
97 });
98});
99process.env.NODE_ENV !== "production" ? MenuItem.propTypes /* remove-proptypes */ = {
100 // ┌────────────────────────────── Warning ──────────────────────────────┐
101 // │ These PropTypes are generated from the TypeScript type definitions. │
102 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
103 // └─────────────────────────────────────────────────────────────────────┘
104 /**
105 * @ignore
106 */
107 children: PropTypes.node,
108 /**
109 * @ignore
110 */
111 className: PropTypes.string,
112 /**
113 * If `true`, the menu item will be disabled.
114 * @default false
115 */
116 disabled: PropTypes.bool,
117 /**
118 * If `true`, the menu item won't receive focus when the mouse moves over it.
119 *
120 * @default false
121 */
122 disableFocusOnHover: PropTypes.bool,
123 /**
124 * A text representation of the menu item's content.
125 * Used for keyboard text navigation matching.
126 */
127 label: PropTypes.string,
128 /**
129 * @ignore
130 */
131 onClick: PropTypes.func,
132 /**
133 * The props used for each slot inside the MenuItem.
134 * @default {}
135 */
136 slotProps: PropTypes.shape({
137 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
138 }),
139 /**
140 * The components used for each slot inside the MenuItem.
141 * Either a string to use a HTML element or a component.
142 * @default {}
143 */
144 slots: PropTypes.shape({
145 root: PropTypes.elementType
146 })
147} : void 0;
148export { MenuItem };
\No newline at end of file