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.

228 lines (226 loc) 7.67 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useForkRef } from '../../utils/useForkRef.js'; import { useSelectRootContext } from '../root/SelectRootContext.js'; import { CompositeList } from '../../composite/list/CompositeList.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { popupStateMapping } from '../../utils/popupStateMapping.js'; import { useSelectPositioner } from './useSelectPositioner.js'; import { SelectPositionerContext } from './SelectPositionerContext.js'; /** * Positions the select menu popup against the trigger. * Renders a `<div>` element. * * Documentation: [Base UI Select](https://base-ui.com/react/components/select) */ import { jsx as _jsx } from "react/jsx-runtime"; const SelectPositioner = /*#__PURE__*/React.forwardRef(function SelectPositioner(props, ref) { const { anchor, positionMethod = 'absolute', className, render, side = 'bottom', align = 'start', sideOffset = 0, alignOffset = 0, collisionBoundary = 'clipping-ancestors', collisionPadding, arrowPadding = 5, sticky = false, trackAnchor = true, ...otherProps } = props; const { open, mounted, setPositionerElement, listRef, labelsRef, floatingRootContext } = useSelectRootContext(); const { getPositionerProps, positioner } = useSelectPositioner({ anchor, floatingRootContext, positionMethod, mounted, side, sideOffset, align, alignOffset, arrowPadding, collisionBoundary, collisionPadding, sticky, trackAnchor, allowAxisFlip: false }); const mergedRef = useForkRef(ref, setPositionerElement); const state = React.useMemo(() => ({ open, side: positioner.side, align: positioner.align, anchorHidden: positioner.anchorHidden }), [open, positioner.side, positioner.align, positioner.anchorHidden]); const { renderElement } = useComponentRenderer({ propGetter: getPositionerProps, render: render ?? 'div', ref: mergedRef, className, state, customStyleHookMapping: popupStateMapping, extraProps: otherProps }); return /*#__PURE__*/_jsx(CompositeList, { elementsRef: listRef, labelsRef: labelsRef, children: /*#__PURE__*/_jsx(SelectPositionerContext.Provider, { value: positioner, children: renderElement() }) }); }); process.env.NODE_ENV !== "production" ? SelectPositioner.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. * @default 'start' */ 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.oneOfType([(props, propName) => { if (props[propName] == null) { return new Error(`Prop '${propName}' is required but wasn't specified`); } if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) { return new Error(`Expected prop '${propName}' to be of type Element`); } return null; }, PropTypes.func, PropTypes.shape({ contextElement: (props, propName) => { if (props[propName] == null) { return null; } if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) { return new Error(`Expected prop '${propName}' to be of type Element`); } return null; }, getBoundingClientRect: PropTypes.func.isRequired, getClientRects: PropTypes.func }), PropTypes.shape({ current: (props, propName) => { if (props[propName] == null) { return null; } if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) { return new Error(`Expected prop '${propName}' to be of type Element`); } return null; } })]), /** * 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.oneOfType([PropTypes.oneOf(['clipping-ancestors']), PropTypes.arrayOf((props, propName) => { if (props[propName] == null) { return new Error(`Prop '${propName}' is required but wasn't specified`); } if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) { return new Error(`Expected prop '${propName}' to be of type Element`); } return null; }), (props, propName) => { if (props[propName] == null) { return new Error(`Prop '${propName}' is required but wasn't specified`); } if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) { return new Error(`Expected prop '${propName}' to be of type Element`); } return null; }, PropTypes.shape({ height: PropTypes.number.isRequired, width: PropTypes.number.isRequired, x: PropTypes.number.isRequired, y: PropTypes.number.isRequired })]), /** * 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 })]), /** * The CSS position method for positioning the Select popup element. * @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. * @default 'bottom' */ 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 select menu in the viewport after * the anchor element is scrolled out of view. * @default false */ sticky: PropTypes.bool, /** * Whether the select popup continuously tracks its anchor after the initial positioning upon mount. * @default true */ trackAnchor: PropTypes.bool } : void 0; export { SelectPositioner };