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.

159 lines (157 loc) 4.88 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { FloatingTree } from '@floating-ui/react'; import { useDirection } from '../../direction-provider/DirectionContext.js'; import { MenuRootContext, useMenuRootContext } from './MenuRootContext.js'; import { useMenuRoot } from './useMenuRoot.js'; import { PortalContext } from '../../portal/PortalContext.js'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const inertStyle = ` [data-floating-ui-inert] { pointer-events: none !important; } `; /** * Groups all parts of the menu. * Doesn’t render its own HTML element. * * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu) */ const MenuRoot = function MenuRoot(props) { const { children, defaultOpen = false, disabled = false, closeParentOnEsc = true, loop = true, modal = true, onOpenChange, open, orientation = 'vertical', delay = 100, openOnHover: openOnHoverProp } = props; const direction = useDirection(); const parentContext = useMenuRootContext(true); const nested = parentContext != null; const openOnHover = openOnHoverProp ?? nested; const typingRef = React.useRef(false); const onTypingChange = React.useCallback(nextTyping => { typingRef.current = nextTyping; }, []); const menuRoot = useMenuRoot({ direction, disabled, closeParentOnEsc, onOpenChange, loop, defaultOpen, open, orientation, nested, openOnHover, delay, onTypingChange, modal }); const context = React.useMemo(() => ({ ...menuRoot, nested, parentContext, disabled, allowMouseUpTriggerRef: parentContext?.allowMouseUpTriggerRef ?? menuRoot.allowMouseUpTriggerRef, typingRef, modal }), [menuRoot, nested, parentContext, disabled, modal]); if (!nested) { // set up a FloatingTree to provide the context to nested menus return /*#__PURE__*/_jsxs(FloatingTree, { children: [menuRoot.open && modal && /*#__PURE__*/_jsx("style", { dangerouslySetInnerHTML: { __html: inertStyle } }), /*#__PURE__*/_jsx(MenuRootContext.Provider, { value: context, children: /*#__PURE__*/_jsx(PortalContext.Provider, { value: context.mounted, children: children }) })] }); } return /*#__PURE__*/_jsx(MenuRootContext.Provider, { value: context, children: /*#__PURE__*/_jsx(PortalContext.Provider, { value: context.mounted, children: children }) }); }; process.env.NODE_ENV !== "production" ? MenuRoot.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, /** * When in a submenu, determines whether pressing the Escape key * closes the entire menu, or only the current child menu. * @default true */ closeParentOnEsc: PropTypes.bool, /** * Whether the menu is initially open. * * To render a controlled menu, use the `open` prop instead. * @default false */ defaultOpen: PropTypes.bool, /** * How long to wait before the menu may be opened on hover. Specified in milliseconds. * * Requires the `openOnHover` prop. * @default 100 */ delay: PropTypes.number, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * Whether to loop keyboard focus back to the first item * when the end of the list is reached while using the arrow keys. * @default true */ loop: PropTypes.bool, /** * Whether the menu should prevent outside clicks and lock page scroll when open. * @default true */ modal: PropTypes.bool, /** * Event handler called when the menu is opened or closed. */ onOpenChange: PropTypes.func, /** * Whether the menu is currently open. */ open: PropTypes.bool, /** * Whether the menu should also open when the trigger is hovered. * * Defaults to `true` for nested menus. */ openOnHover: PropTypes.bool, /** * The visual orientation of the menu. * Controls whether roving focus uses up/down or left/right arrow keys. * @default 'vertical' */ orientation: PropTypes.oneOf(['horizontal', 'vertical']) } : void 0; export { MenuRoot };