"use client"; import { useEffect, useRef } from "react"; import { createPortal } from "react-dom"; import { X } from "lucide-react"; interface ModalProps { /** Control visibility */ open: boolean; /** Callback for both explicit and implicit close actions */ onClose: () => void; /** Optional heading */ title?: string; children: React.ReactNode; /** Center horizontally and vertically? (default true) */ center?: boolean; } export default function Modal({ open, onClose, title, children, center = true, }: ModalProps) { const dialogRef = useRef(null); // Open / close imperatively to keep in sync with prop useEffect(() => { const dialog = dialogRef.current; if (!dialog) return; if (open && !dialog.open) dialog.showModal(); if (!open && dialog.open) dialog.close(); }, [open]); // Close on native close event useEffect(() => { const dialog = dialogRef.current; if (!dialog) return; const handleClose = () => onClose(); dialog.addEventListener("close", handleClose); return () => dialog.removeEventListener("close", handleClose); }, [onClose]); // ESC -> close (for browsers without built‑in handling) useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [open, onClose]); if (typeof window === "undefined") return null; // SSR guard return createPortal( {/* Card */}
{title && (

{title}

)}
{children}
, document.body ); }