UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

214 lines (208 loc) 6.37 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useFloatingTree } from '@floating-ui/react'; import { useMenuItem } from './useMenuItem.js'; import { useMenuRootContext } from '../root/MenuRootContext.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useBaseUiId } from '../../utils/useBaseUiId.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { useCompositeListItem } from '../../composite/list/useCompositeListItem.js'; import { jsx as _jsx } from "react/jsx-runtime"; const InnerMenuItem = /*#__PURE__*/React.forwardRef(function InnerMenuItem(props, forwardedRef) { const { className, closeOnClick = true, disabled = false, highlighted, id, menuEvents, propGetter, render, allowMouseUpTriggerRef, typingRef, ...other } = props; const { getRootProps } = useMenuItem({ closeOnClick, disabled, highlighted, id, menuEvents, ref: forwardedRef, allowMouseUpTriggerRef, typingRef }); const state = React.useMemo(() => ({ disabled, highlighted }), [disabled, highlighted]); const { renderElement } = useComponentRenderer({ render: render || 'div', className, state, propGetter: externalProps => propGetter(getRootProps(externalProps)), extraProps: other }); return renderElement(); }); const MemoizedInnerMenuItem = /*#__PURE__*/React.memo(InnerMenuItem); /** * An individual interactive item in the menu. * Renders a `<div>` element. * * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu) */ process.env.NODE_ENV !== "production" ? InnerMenuItem.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 */ allowMouseUpTriggerRef: PropTypes.shape({ current: PropTypes.bool.isRequired }).isRequired, /** * @ignore */ children: PropTypes.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * Whether to close the menu when the item is clicked. * * @default true */ closeOnClick: PropTypes.bool, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * @ignore */ highlighted: PropTypes.bool.isRequired, /** * @ignore */ id: PropTypes.string, /** * Overrides the text label to use when the item is matched during keyboard text navigation. */ label: PropTypes.string, /** * @ignore */ menuEvents: PropTypes.shape({ emit: PropTypes.func.isRequired, off: PropTypes.func.isRequired, on: PropTypes.func.isRequired }).isRequired, /** * The click handler for the menu item. */ onClick: PropTypes.func, /** * @ignore */ propGetter: PropTypes.func.isRequired, /** * Allows you to replace the component’s HTML element * with a different tag, or compose it with another component. * * Accepts a `ReactElement` or a function that returns the element to render. */ render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]), /** * @ignore */ typingRef: PropTypes.shape({ current: PropTypes.bool.isRequired }).isRequired } : void 0; /** * An individual interactive item in the menu. * Renders a `<div>` element. * * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu) */ const MenuItem = /*#__PURE__*/React.forwardRef(function MenuItem(props, forwardedRef) { const { id: idProp, label, ...other } = props; const itemRef = React.useRef(null); const listItem = useCompositeListItem({ label }); const mergedRef = useForkRef(forwardedRef, listItem.ref, itemRef); const { getItemProps, activeIndex, allowMouseUpTriggerRef, typingRef } = useMenuRootContext(); const id = useBaseUiId(idProp); const highlighted = listItem.index === activeIndex; const { events: menuEvents } = useFloatingTree(); // This wrapper component is used as a performance optimization. // MenuItem reads the context and re-renders the actual MenuItem // only when it needs to. return /*#__PURE__*/_jsx(MemoizedInnerMenuItem, { ...other, id: id, ref: mergedRef, highlighted: highlighted, menuEvents: menuEvents, propGetter: getItemProps, allowMouseUpTriggerRef: allowMouseUpTriggerRef, typingRef: typingRef }); }); 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, /** * Whether to close the menu when the item is clicked. * * @default true */ closeOnClick: PropTypes.bool, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * @ignore */ id: PropTypes.string, /** * Overrides the text label to use when the item is matched during keyboard text navigation. */ label: PropTypes.string, /** * The click handler for the menu item. */ onClick: PropTypes.func } : void 0; export { MenuItem };