"use client"; import React, { useEffect, useState } from "react"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { paperMixin } from "../styles"; import { cls } from "../util"; import { usePortalContainer } from "../hooks/PortalContainerContext"; export type DialogProps = { open?: boolean; onOpenChange?: (open: boolean) => void; children: React.ReactNode; className?: string; containerClassName?: string; fullWidth?: boolean; fullHeight?: boolean; fullScreen?: boolean; scrollable?: boolean; maxWidth?: keyof typeof widthClasses; modal?: boolean; onOpenAutoFocus?: (e: Event) => void; onEscapeKeyDown?: (e: KeyboardEvent) => void; onPointerDownOutside?: (e: Event) => void; onInteractOutside?: (e: Event) => void; /** * If `true`, the dialog will not focus the first focusable element when opened. */ disableInitialFocus?: boolean; portalContainer?: HTMLElement | null; }; const widthClasses = { xs: "max-w-xs w-xs", sm: "max-w-sm w-sm", md: "max-w-md w-md", lg: "max-w-lg w-lg", xl: "max-w-xl w-xl", "2xl": "max-w-2xl w-2xl", "3xl": "max-w-3xl w-3xl", "4xl": "max-w-4xl w-4xl", "5xl": "max-w-5xl w-5xl", "6xl": "max-w-6xl w-6xl", "7xl": "max-w-7xl w-7xl", full: "max-w-full w-full" }; export const Dialog = ({ open, onOpenChange, children, className, containerClassName, fullWidth = true, fullHeight, fullScreen, scrollable = true, maxWidth = "lg", modal = true, onOpenAutoFocus, onEscapeKeyDown, onPointerDownOutside, onInteractOutside, disableInitialFocus = true, portalContainer }: DialogProps) => { const [displayed, setDisplayed] = useState(false); // Get the portal container from context const contextContainer = usePortalContainer(); // Prioritize manual prop, fallback to context container const finalContainer = (portalContainer ?? contextContainer ?? undefined) as HTMLElement | undefined; useEffect(() => { if (!open) { const timeout = setTimeout(() => { setDisplayed(false); }, 100); return () => clearTimeout(timeout); } else { setDisplayed(true); return () => { }; } }, [open]); return (
{ if (disableInitialFocus) { e.preventDefault(); } onOpenAutoFocus?.(e); }} onPointerDownOutside={onPointerDownOutside} onInteractOutside={onInteractOutside} className={cls("h-full outline-none flex justify-center items-center z-40 opacity-100 transition-all duration-200 ease-in-out")} >
{children}
); };