@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.
213 lines (211 loc) • 7.1 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { FloatingNode, useFloatingNodeId, useFloatingParentNodeId, useFloatingTree } from '@floating-ui/react';
import { MenuPositionerContext } from './MenuPositionerContext.js';
import { useMenuRootContext } from '../root/MenuRootContext.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useMenuPositioner } from './useMenuPositioner.js';
import { HTMLElementType } from '../../utils/proptypes.js';
import { popupStateMapping } from '../../utils/popupStateMapping.js';
import { CompositeList } from '../../composite/list/CompositeList.js';
/**
* Positions the menu popup against the trigger.
* Renders a `<div>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
import { jsx as _jsx } from "react/jsx-runtime";
const MenuPositioner = /*#__PURE__*/React.forwardRef(function MenuPositioner(props, forwardedRef) {
const {
anchor,
positionMethod = 'absolute',
className,
render,
keepMounted = false,
side,
align,
sideOffset = 0,
alignOffset = 0,
collisionBoundary = 'clipping-ancestors',
collisionPadding = 5,
arrowPadding = 5,
sticky = false,
...otherProps
} = props;
const {
open,
floatingRootContext,
setPositionerElement,
itemDomElements,
itemLabels,
mounted,
nested,
setOpen
} = useMenuRootContext();
const {
events: menuEvents
} = useFloatingTree();
const nodeId = useFloatingNodeId();
const parentNodeId = useFloatingParentNodeId();
let computedSide = side;
let computedAlign = align;
if (!side) {
computedSide = nested ? 'inline-end' : 'bottom';
}
if (!align) {
computedAlign = nested ? 'start' : 'center';
}
const positioner = useMenuPositioner({
anchor,
floatingRootContext,
positionMethod,
open,
mounted,
side: computedSide,
sideOffset,
align: computedAlign,
alignOffset,
arrowPadding,
collisionBoundary,
collisionPadding,
sticky,
nodeId,
parentNodeId,
menuEvents,
setOpen
});
const state = React.useMemo(() => ({
open,
side: positioner.side,
align: positioner.align,
anchorHidden: positioner.anchorHidden,
nested
}), [open, positioner.side, positioner.align, positioner.anchorHidden, nested]);
const contextValue = React.useMemo(() => ({
side: positioner.side,
align: positioner.align,
arrowRef: positioner.arrowRef,
arrowUncentered: positioner.arrowUncentered,
arrowStyles: positioner.arrowStyles,
floatingContext: positioner.floatingContext
}), [positioner.side, positioner.align, positioner.arrowRef, positioner.arrowUncentered, positioner.arrowStyles, positioner.floatingContext]);
const mergedRef = useForkRef(forwardedRef, setPositionerElement);
const {
renderElement
} = useComponentRenderer({
propGetter: positioner.getPositionerProps,
render: render ?? 'div',
className,
state,
customStyleHookMapping: popupStateMapping,
ref: mergedRef,
extraProps: otherProps
});
const shouldRender = keepMounted || mounted;
if (!shouldRender) {
return null;
}
return /*#__PURE__*/_jsx(MenuPositionerContext.Provider, {
value: contextValue,
children: /*#__PURE__*/_jsx(FloatingNode, {
id: nodeId,
children: /*#__PURE__*/_jsx(CompositeList, {
elementsRef: itemDomElements,
labelsRef: itemLabels,
children: renderElement()
})
})
});
});
process.env.NODE_ENV !== "production" ? MenuPositioner.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* How to align the popup relative to the specified side.
*/
align: PropTypes.oneOf(['center', 'end', 'start']),
/**
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset: PropTypes.number,
/**
* An element to position the popup against.
* By default, the popup will be positioned against the trigger.
*/
anchor: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
/**
* Minimum distance to maintain between the arrow and the edges of the popup.
*
* Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
* @default 5
*/
arrowPadding: PropTypes.number,
/**
* @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]),
/**
* An element or a rectangle that delimits the area that the popup is confined to.
* @default 'clipping-ancestors'
*/
collisionBoundary: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([HTMLElementType, PropTypes.arrayOf(HTMLElementType), PropTypes.string, PropTypes.shape({
height: PropTypes.number,
width: PropTypes.number,
x: PropTypes.number,
y: PropTypes.number
})]),
/**
* Additional space to maintain from the edge of the collision boundary.
* @default 5
*/
collisionPadding: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
bottom: PropTypes.number,
left: PropTypes.number,
right: PropTypes.number,
top: PropTypes.number
})]),
/**
* Whether to keep the HTML element in the DOM while the menu is hidden.
* @default false
*/
keepMounted: PropTypes.bool,
/**
* Determines which CSS `position` property to use.
* @default 'absolute'
*/
positionMethod: PropTypes.oneOf(['absolute', 'fixed']),
/**
* 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]),
/**
* Which side of the anchor element to align the popup against.
* May automatically change to avoid collisions.
*/
side: PropTypes.oneOf(['bottom', 'inline-end', 'inline-start', 'left', 'right', 'top']),
/**
* Distance between the anchor and the popup.
* @default 0
*/
sideOffset: PropTypes.number,
/**
* Whether to maintain the menu in the viewport after
* the anchor element is scrolled out of view.
* @default false
*/
sticky: PropTypes.bool
} : void 0;
export { MenuPositioner };