UNPKG

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