import type { ReactNode } from 'react'; import { autoUpdate, flip, FloatingFocusManager, FloatingPortal, offset, shift, size, useDismiss, useFloating, useInteractions, useRole, } from '@floating-ui/react'; export function AnchoredDialog({ id, anchor, title, children, onClose, closeLabel = 'Close explanation', }: { id: string; anchor: HTMLElement | null; title: ReactNode; children: ReactNode; onClose: () => void; closeLabel?: string; }) { const { refs, floatingStyles, context } = useFloating({ open: anchor !== null, onOpenChange: (open) => { if (!open) onClose(); }, elements: { reference: anchor }, placement: 'bottom-start', strategy: 'fixed', middleware: [ offset(10), flip({ padding: 12, fallbackPlacements: ['top-start', 'right-start', 'left-start'] }), shift({ padding: 12 }), size({ padding: 12, apply({ availableHeight, elements }) { elements.floating.style.maxHeight = `${Math.max(120, availableHeight)}px`; }, }), ], whileElementsMounted: autoUpdate, }); const dismiss = useDismiss(context); const role = useRole(context, { role: 'dialog' }); const { getFloatingProps } = useInteractions([dismiss, role]); if (!anchor) return null; return (

{title}

{children}
); }