// // Copyright 2026 DXOS.org // // @import-as-namespace import { ark } from '@ark-ui/react/factory'; import { Popover as PopoverPrimitive, usePopoverContext } from '@ark-ui/react/popover'; import { Portal } from '@ark-ui/react/portal'; import React, { type ComponentPropsWithoutRef, type ReactNode, type RefObject, createContext, forwardRef, useCallback, useContext, useEffect, useRef, useState, } from 'react'; import { mx } from '@dxos/ui-theme'; import { DX_POPOVER_CONTENT_ATTR, type ThemedClassName } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { Button } from '../Button/Button.tsx'; import { Container, DefaultGutterProvider } from '../Container/Container.tsx'; import { popupPositioning, usePopupSize } from '../ScrollArea/PopupScroll.tsx'; import * as ScrollArea from '../ScrollArea/ScrollArea.tsx'; /** Gap between trigger and popup, in px (positioning takes a number, not a CSS variable). */ const POPUP_GUTTER = 2; type LabelPart = 'title' | 'description'; /** Lets Title and Description announce themselves to the Content they are rendered in (DESIGN.md follow-up 45). */ const LabelsContext = createContext<((part: LabelPart, present: boolean) => void) | undefined>(undefined); const useLabelPart = (part: LabelPart) => { const register = useContext(LabelsContext); useEffect(() => { register?.(part, true); return () => register?.(part, false); }, [register, part]); }; // // Root // type PopoverRootProps = PopoverPrimitive.RootProps; /** * Ark popover; content mounts on open and unmounts on close unless the caller opts out. `modal` traps focus and hides * the rest of the page from assistive tech; a virtual trigger is `positioning.getAnchorRect` (or an `Anchor`). */ const PopoverRoot = ({ lazyMount = true, unmountOnExit = true, positioning, ...props }: PopoverRootProps) => ( ); PopoverRoot.displayName = 'Popover.Root'; // // Trigger // type PopoverTriggerProps = PopoverPrimitive.TriggerProps; /** Use `asChild` to open the popover from a `Button`. */ const PopoverTrigger = forwardRef((props, forwardedRef) => ( )); PopoverTrigger.displayName = 'Popover.Trigger'; // // Anchor // type PopoverAnchorProps = PopoverPrimitive.AnchorProps; /** Positions the content against this element instead of the trigger. */ const PopoverAnchor = forwardRef((props, forwardedRef) => ( )); PopoverAnchor.displayName = 'Popover.Anchor'; // // Content // type PopoverContentProps = ThemedClassName & { /** Overrides the size inherited from the anchor's or trigger's nearest sized ancestor (Phase 4 decision 2); `md` without one. */ size?: Size; /** Point at the trigger with an arrow in the popup's surface colour. */ arrow?: boolean; /** Portals into this element instead of the body (e.g. a sized scope, AUDIT 2.2). */ container?: RefObject; }; /** Portalled panel at `level='popup'`, padded by the size's gap, with an arrow unless `arrow={false}`. */ const PopoverContent = forwardRef( ({ classNames, size, arrow = true, container, children, ...props }, forwardedRef) => { const popover = usePopoverContext(); const popupSize = usePopupSize( size, popover.open, [popover.getAnchorProps().id, popover.getTriggerProps().id], 'md', ); // zag checks for a title once, when the machine starts, which is before lazily mounted content exists. const [labels, setLabels] = useState>({ title: false, description: false }); const [register] = useState( () => (part: LabelPart, present: boolean) => setLabels((labels) => ({ ...labels, [part]: present })), ); // zag measures on opening, so a positioner that mounts later (keyed or conditional content) would stay at 0,0. const repositionRef = useRef(popover.reposition); repositionRef.current = popover.reposition; const handlePositioner = useCallback((element: HTMLDivElement | null) => { if (element) { repositionRef.current(); } }, []); return ( {children} {arrow && ( )} ); }, ); PopoverContent.displayName = 'Popover.Content'; // // Header // type PopoverHeaderProps = ThemedClassName>; /** A block row holding the Title and an optional trailing CloseTrigger. */ const PopoverHeader = forwardRef(({ classNames, ...props }, forwardedRef) => (
)); PopoverHeader.displayName = 'Popover.Header'; // // Title // type PopoverTitleProps = ThemedClassName; const PopoverTitle = forwardRef(({ classNames, ...props }, forwardedRef) => { useLabelPart('title'); return ; }); PopoverTitle.displayName = 'Popover.Title'; // // Description // type PopoverDescriptionProps = ThemedClassName; const PopoverDescription = forwardRef( ({ classNames, ...props }, forwardedRef) => { useLabelPart('description'); return ( ); }, ); PopoverDescription.displayName = 'Popover.Description'; // // Body // type PopoverBodyProps = ThemedClassName> & { children?: ReactNode; }; /** * Scrolling content, composed like `Dialog.Body` (decision 5): the popover is capped at the space the positioner * reports, and the body takes the rest after Header and Footer; its inset gutter holds the thumb. */ const PopoverBody = forwardRef(({ classNames, children, ...props }, forwardedRef) => ( {/* Its direct content (a form's Viewport) joins these rails rather than nesting a second inset. */} {children} )); PopoverBody.displayName = 'Popover.Body'; // // CloseTrigger // type PopoverCloseTriggerProps = Omit & { /** With `asChild`, the child (e.g. a `Button`) closes the popover instead of the default icon button. */ children?: ReactNode; icon?: string; label?: string; }; const PopoverCloseTrigger = forwardRef( ({ asChild, children, icon = 'ph--x--regular', label = 'Close', onClick, ...props }, forwardedRef) => { const popover = usePopoverContext(); // zag's close trigger sets `aria-label="close"`, which would rename a child with its own text (e.g. "Done"). return asChild ? ( { onClick?.(event); if (!event.defaultPrevented) { popover.setOpen(false); } }} ref={forwardedRef} > {children} ) : (