import React, { ReactNode, useEffect, useRef } from "react"; import { ModalHeader } from "./modal-header"; import { ModalFooter } from "./modal-footer"; type Size = "sm" | "md" | "lg" | "full"; const sizeClass: Record = { sm: "max-w-xl", md: "max-w-3xl", lg: "max-w-5xl", full: "max-w-full h-full", }; type Props = { open: boolean; onClose: () => void; children: ReactNode; title?: string; footer?: ReactNode; headerRight?: ReactNode; size?: Size; closeOnBackdrop?: boolean; className?: string; }; export function Modal({ open, onClose, children, title, footer, headerRight, size = "md", closeOnBackdrop = true, className, }: Props) { const containerRef = useRef(null); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; if (open) window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [open, onClose]); useEffect(() => { if (open) { containerRef.current?.focus(); } }, [open]); if (!open) return null; return (
closeOnBackdrop && onClose()} />
{title ?

{title}

: null}
{children}
{footer ?? ( )}
); }