@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.
120 lines (118 loc) • 4.2 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { FloatingFocusManager } from '@floating-ui/react';
import { usePopoverRootContext } from '../root/PopoverRootContext.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { usePopoverPositionerContext } from '../positioner/PopoverPositionerContext.js';
import { usePopoverPopup } from './usePopoverPopup.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { popupStateMapping as baseMapping } from '../../utils/popupStateMapping.js';
import { refType } from '../../utils/proptypes.js';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
import { transitionStatusMapping } from '../../utils/styleHookMapping.js';
import { jsx as _jsx } from "react/jsx-runtime";
const customStyleHookMapping = {
...baseMapping,
...transitionStatusMapping
};
/**
* A container for the popover contents.
* Renders a `<div>` element.
*
* Documentation: [Base UI Popover](https://base-ui.com/react/components/popover)
*/
const PopoverPopup = /*#__PURE__*/React.forwardRef(function PopoverPopup(props, forwardedRef) {
const {
className,
render,
initialFocus,
finalFocus,
...otherProps
} = props;
const {
open,
instantType,
transitionStatus,
getRootPopupProps,
titleId,
descriptionId,
popupRef,
mounted,
openReason
} = usePopoverRootContext();
const positioner = usePopoverPositionerContext();
const {
getPopupProps,
resolvedInitialFocus
} = usePopoverPopup({
getProps: getRootPopupProps,
titleId,
descriptionId,
initialFocus
});
const state = React.useMemo(() => ({
open,
side: positioner.side,
align: positioner.align,
instant: instantType,
transitionStatus
}), [open, positioner.side, positioner.align, instantType, transitionStatus]);
const mergedRef = useForkRef(popupRef, forwardedRef);
const {
renderElement
} = useComponentRenderer({
propGetter: getPopupProps,
ref: mergedRef,
render: render ?? 'div',
className,
state,
extraProps: transitionStatus === 'starting' ? mergeReactProps(otherProps, {
style: {
transition: 'none'
}
}) : otherProps,
customStyleHookMapping
});
return /*#__PURE__*/_jsx(FloatingFocusManager, {
context: positioner.positionerContext,
modal: false,
disabled: !mounted || openReason === 'hover',
initialFocus: resolvedInitialFocus,
returnFocus: finalFocus,
children: renderElement()
});
});
process.env.NODE_ENV !== "production" ? PopoverPopup.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,
/**
* 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]),
/**
* Determines the element to focus when the popover is closed.
* By default, focus returns to the trigger.
*/
finalFocus: refType,
/**
* Determines the element to focus when the popover is opened.
* By default, the first focusable element is focused.
*/
initialFocus: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.func, refType]),
/**
* 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])
} : void 0;
export { PopoverPopup };