@mui/base
Version:
MUI Base is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
148 lines (144 loc) • 4.25 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { getMenuItemUtilityClass } from "./menuItemClasses.js";
import { useMenuItem, useMenuItemContextStabilizer } from "../useMenuItem/index.js";
import { unstable_composeClasses as composeClasses } from "../composeClasses/index.js";
import { useSlotProps } from "../utils/useSlotProps.js";
import { useClassNamesOverride } from "../utils/ClassNameConfigurator.js";
import { ListContext } from "../useList/index.js";
import { jsx as _jsx } from "react/jsx-runtime";
function useUtilityClasses(ownerState) {
const {
disabled,
focusVisible
} = ownerState;
const slots = {
root: ['root', disabled && 'disabled', focusVisible && 'focusVisible']
};
return composeClasses(slots, useClassNamesOverride(getMenuItemUtilityClass));
}
const InnerMenuItem = /*#__PURE__*/React.memo(/*#__PURE__*/React.forwardRef(function MenuItem(props, forwardedRef) {
const {
children,
disabled: disabledProp = false,
label,
id,
slotProps = {},
slots = {},
...other
} = props;
const {
getRootProps,
disabled,
focusVisible,
highlighted
} = useMenuItem({
id,
disabled: disabledProp,
rootRef: forwardedRef,
label
});
const ownerState = {
...props,
disabled,
focusVisible,
highlighted
};
const classes = useUtilityClasses(ownerState);
const Root = slots.root ?? 'li';
const rootProps = useSlotProps({
elementType: Root,
getSlotProps: getRootProps,
externalSlotProps: slotProps.root,
externalForwardedProps: other,
className: classes.root,
ownerState
});
return /*#__PURE__*/_jsx(Root, {
...rootProps,
children: children
});
}));
/**
* An unstyled menu item to be used within a Menu.
*
* Demos:
*
* - [Menu](https://mui.com/base-ui/react-menu/)
*
* API:
*
* - [MenuItem API](https://mui.com/base-ui/react-menu/components-api/#menu-item)
*/
const MenuItem = /*#__PURE__*/React.forwardRef(function MenuItem(props, ref) {
const {
id: idProp
} = props;
// This wrapper component is used as a performance optimization.
// `useMenuItemContextStabilizer` ensures that the context value
// is stable across renders, so that the actual MenuItem re-renders
// only when it needs to.
const {
contextValue,
id
} = useMenuItemContextStabilizer(idProp);
return /*#__PURE__*/_jsx(ListContext.Provider, {
value: contextValue,
children: /*#__PURE__*/_jsx(InnerMenuItem, {
...props,
id: id,
ref: ref
})
});
});
process.env.NODE_ENV !== "production" ? MenuItem.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
children: PropTypes.node,
/**
* @ignore
*/
className: PropTypes.string,
/**
* If `true`, the menu item will be disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, the menu item won't receive focus when the mouse moves over it.
*
* @default false
*/
disableFocusOnHover: PropTypes.bool,
/**
* A text representation of the menu item's content.
* Used for keyboard text navigation matching.
*/
label: PropTypes.string,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* The props used for each slot inside the MenuItem.
* @default {}
*/
slotProps: PropTypes.shape({
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}),
/**
* The components used for each slot inside the MenuItem.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots: PropTypes.shape({
root: PropTypes.elementType
})
} : void 0;
export { MenuItem };