@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.
184 lines (182 loc) • 6.32 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { usePreviewCardRootContext } from '../root/PreviewCardContext.js';
import { usePreviewCardPositioner } from './usePreviewCardPositioner.js';
import { PreviewCardPositionerContext } from './PreviewCardPositionerContext.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { HTMLElementType } from '../../utils/proptypes.js';
import { popupStateMapping } from '../../utils/popupStateMapping.js';
/**
* Positions the popup against the trigger.
* Renders a `<div>` element.
*
* Documentation: [Base UI Preview Card](https://base-ui.com/react/components/preview-card)
*/
import { jsx as _jsx } from "react/jsx-runtime";
const PreviewCardPositioner = /*#__PURE__*/React.forwardRef(function PreviewCardPositioner(props, forwardedRef) {
const {
render,
className,
anchor,
positionMethod = 'absolute',
side = 'bottom',
align = 'center',
sideOffset = 0,
alignOffset = 0,
collisionBoundary = 'clipping-ancestors',
collisionPadding = 5,
arrowPadding = 5,
sticky = false,
keepMounted = false,
...otherProps
} = props;
const {
open,
mounted,
floatingRootContext,
setPositionerElement
} = usePreviewCardRootContext();
const positioner = usePreviewCardPositioner({
anchor,
floatingRootContext,
positionMethod,
open,
mounted,
keepMounted,
side,
sideOffset,
align,
alignOffset,
arrowPadding,
collisionBoundary,
collisionPadding,
sticky
});
const state = React.useMemo(() => ({
open,
side: positioner.side,
align: positioner.align,
anchorHidden: positioner.anchorHidden
}), [open, positioner.side, positioner.align, positioner.anchorHidden]);
const contextValue = React.useMemo(() => ({
side: positioner.side,
align: positioner.align,
arrowRef: positioner.arrowRef,
arrowUncentered: positioner.arrowUncentered,
arrowStyles: positioner.arrowStyles
}), [positioner.side, positioner.align, positioner.arrowRef, positioner.arrowUncentered, positioner.arrowStyles]);
const mergedRef = useForkRef(setPositionerElement, forwardedRef);
const {
renderElement
} = useComponentRenderer({
propGetter: positioner.getPositionerProps,
render: render ?? 'div',
className,
state,
ref: mergedRef,
extraProps: otherProps,
customStyleHookMapping: popupStateMapping
});
const shouldRender = keepMounted || mounted;
if (!shouldRender) {
return null;
}
return /*#__PURE__*/_jsx(PreviewCardPositionerContext.Provider, {
value: contextValue,
children: renderElement()
});
});
process.env.NODE_ENV !== "production" ? PreviewCardPositioner.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 'center'
*/
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 preview card 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.
* @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 popup in the viewport after
* the anchor element is scrolled out of view.
* @default false
*/
sticky: PropTypes.bool
} : void 0;
export { PreviewCardPositioner };