// Modal.
js
import React, { useEffect } from "react";
import ReactDOM from "react-dom";
export function Modal({ isOpen, onClose, children }) {
useEffect(() => {
const handleEsc = (e) => {
if (e.key === "Escape") onClose();
};
window.addEventListener("keydown", handleEsc);
return () => window.removeEventListener("keydown", handleEsc);
}, [onClose]);
if (!isOpen) return null;
return ReactDOM.createPortal(
<div
onClick={onClose}
style={{
position: "fixed",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: "rgba(0,0,0,0.5)",
zIndex: 9999,
}}
>
<div
onClick={(e) => e.stopPropagation()}
style={{
background: "#fff",
margin: "10% auto",
padding: "20px",
width: "300px",
borderRadius: "8px",
}}
>
{children}
</div>
</div>,
document.getElementById("modal-root")
);