import * as React from "react"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { modalOverlayClassName, modalSurfaceClassName, } from "@/components/ui/floating-surface"; import { cn } from "@/lib/utils"; import { FloatingPortalContext } from "@/components/ui/floating-portal"; const Dialog = DialogPrimitive.Root; const DialogTrigger = DialogPrimitive.Trigger; const DialogClose = DialogPrimitive.Close; const DialogPortal = DialogPrimitive.Portal; export const DialogLayoutContext = React.createContext(null); const DialogOverlay = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; // The portal's presence ref must reach the animated content, not the plain // positioning wrapper; otherwise the wrapper unmounts before the exit finishes. const DialogPositionedContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { positionerStyle?: React.CSSProperties; } >(({ positionerStyle, ...props }, ref) => (
)); DialogPositionedContent.displayName = "DialogPositionedContent"; interface DialogContentProps extends React.ComponentPropsWithoutRef { showCloseButton?: boolean; overlayClassName?: string; } const DialogContent = React.forwardRef< React.ElementRef, DialogContentProps >(({ className, children, showCloseButton = true, overlayClassName, onOpenAutoFocus, ...props }, ref) => { const { t } = useTranslation(); const [container, setContainer] = React.useState(null); const contentNode = React.useRef(null); const layoutAnchor = React.useContext(DialogLayoutContext); const [layout, setLayout] = React.useState(); React.useLayoutEffect(() => { if (!layoutAnchor) { setLayout(undefined); return; } const update = () => { const rect = layoutAnchor.getBoundingClientRect(); const style = getComputedStyle(layoutAnchor); const start = parseFloat(style.paddingLeft) || 0; const end = parseFloat(style.paddingRight) || 0; if (rect.width > 0) setLayout({ left: rect.left + start, right: "auto", width: rect.width - start - end, paddingInline: 0 }); }; update(); const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update); observer?.observe(layoutAnchor); window.addEventListener("resize", update); return () => { observer?.disconnect(); window.removeEventListener("resize", update); }; }, [layoutAnchor]); const contentRef = React.useCallback((node: HTMLDivElement | null) => { contentNode.current = node; setContainer(node); if (typeof ref === "function") ref(node); else if (ref) ref.current = node; }, [ref]); return ( { if (onOpenAutoFocus) onOpenAutoFocus(event); else { event.preventDefault(); contentNode.current?.focus({ preventScroll: true }); } }} className={cn( modalSurfaceClassName, "relative grid w-full max-w-lg origin-center gap-4 rounded-modal p-6 duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 motion-reduce:animate-none", className, )} {...props} > {children} {showCloseButton ? ( {t("common.close")} ) : null} ); }); DialogContent.displayName = DialogPrimitive.Content.displayName; const DialogHeader = ({ className, ...props }: React.HTMLAttributes) => (
); DialogHeader.displayName = "DialogHeader"; const DialogFooter = ({ className, ...props }: React.HTMLAttributes) => (
); DialogFooter.displayName = "DialogFooter"; const DialogTitle = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogTitle.displayName = DialogPrimitive.Title.displayName; const DialogDescription = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogDescription.displayName = DialogPrimitive.Description.displayName; export { Dialog, DialogTrigger, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, };