import { arrow as arrowMiddleware, autoUpdate, flip as flipMiddleware, offset, shift, size as sizeMiddleware, useFloating, } from '@floating-ui/react'; import { clsx } from 'clsx'; import { CSSProperties, HTMLAttributes, PropsWithChildren, RefObject, SyntheticEvent, forwardRef, useContext, useRef, } from 'react'; import { OverlayIdContext } from '../../Provider/overlay/OverlayIdProvider'; import Dimmer from '../../Dimmer'; import useDirection from '../hooks/useDirection'; import { Position } from '..'; import { type FloatingPlacement, POPOVER_OFFSET, fallbackPlacements, mirrorPositionForRtl, positionToPlacement, } from './Panel.positionUtils'; export type PanelProps = PropsWithChildren<{ arrow?: boolean; flip?: boolean; altAxis?: boolean; open?: boolean; onClose?: (event: Event | SyntheticEvent) => void; position?: FloatingPlacement; anchorRef: RefObject; anchorWidth?: boolean; considerHeight?: boolean; }> & HTMLAttributes; const Panel = forwardRef(function Panel( { arrow = false, flip = true, altAxis = false, children, open = false, onClose, position = Position.BOTTOM, anchorRef, anchorWidth = false, considerHeight = false, ...rest }: PanelProps, reference, ) { const arrowRef = useRef(null); const { isRTL } = useDirection(); const resolvedPosition = isRTL ? mirrorPositionForRtl(position) : position; const floatingPlacement = positionToPlacement(resolvedPosition); const middleware = []; if (arrow) { middleware.push(offset(POPOVER_OFFSET)); } if (flip && fallbackPlacements[floatingPlacement]) { middleware.push( flipMiddleware({ fallbackPlacements: fallbackPlacements[floatingPlacement], }), ); } if (altAxis) { middleware.push(shift()); } if (arrow) { middleware.push(arrowMiddleware({ element: arrowRef, padding: 8 })); } if (considerHeight) { middleware.push( sizeMiddleware({ padding: POPOVER_OFFSET, apply: ({ elements, availableHeight }) => { elements.floating.style.setProperty( '--np-panel-available-height', `${availableHeight}px`, ); }, }), ); } const { refs, floatingStyles, placement, middlewareData } = useFloating({ placement: floatingPlacement, middleware, elements: { reference: anchorRef.current, }, whileElementsMounted: open ? autoUpdate : undefined, }); const arrowStyle: CSSProperties = { ...(middlewareData.arrow?.x != null ? { left: middlewareData.arrow.x } : {}), ...(middlewareData.arrow?.y != null ? { top: middlewareData.arrow.y } : {}), }; const contentStyle: CSSProperties = { ...(anchorWidth ? { width: anchorRef.current?.clientWidth } : undefined), }; const overlayId = useContext(OverlayIdContext); return (