Files
repo-home/apps/frontend/components/ui/modal.tsx
T

50 lines
1.1 KiB
TypeScript

"use client";
import { useEffect } from "react";
interface Props {
open: boolean;
onClose: () => void;
children: React.ReactNode;
}
export default function Modal({ open, onClose, children }: Props) {
// ESC-Taste schließen
useEffect(() => {
function handleEsc(e: KeyboardEvent) {
if (e.key === "Escape") onClose();
}
if (open) {
window.addEventListener("keydown", handleEsc);
}
return () => window.removeEventListener("keydown", handleEsc);
}, [open, onClose]);
if (!open) return null;
return (
<div style={overlayStyle} onClick={onClose}>
<div style={modalStyle} onClick={(e) => e.stopPropagation()}>
{children}
</div>
</div>
);
}
const overlayStyle = {
position: "fixed" as const,
inset: 0,
backgroundColor: "rgba(0,0,0,0.5)",
display: "flex",
justifyContent: "center",
alignItems: "center",
};
const modalStyle = {
backgroundColor: "white",
padding: "20px",
borderRadius: "8px",
minWidth: "300px",
};