%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/ui/ |
| Current File : //home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/ui/Modal.tsx |
import { useEffect, type ReactNode } from "react";
import { createPortal } from "react-dom";
interface ModalProps {
open: boolean;
onClose: () => void;
title: string;
subtitle?: string;
children: ReactNode;
width?: string;
}
export default function Modal({
open,
onClose,
title,
subtitle,
children,
width = "max-w-md",
}: ModalProps) {
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
document.addEventListener("keydown", onKey);
document.body.style.overflow = "hidden";
return () => {
document.removeEventListener("keydown", onKey);
document.body.style.overflow = "";
};
}, [open, onClose]);
if (!open) return null;
return createPortal(
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
{/* Overlay */}
<div
className="absolute inset-0 animate-fade-in bg-ink-950/85"
onClick={onClose}
/>
{/* Panel solid — scrollable jika konten lebih tinggi dari layar */}
<div
className={`relative flex max-h-[calc(100dvh-2rem)] w-full flex-col overflow-y-auto ${width} animate-scale-in panel-strong`}
>
<div className="flex items-start justify-between p-5 pb-0">
<div>
<h2 className="text-base font-semibold text-white">{title}</h2>
{subtitle && (
<p className="mt-0.5 text-xs text-slate-500">{subtitle}</p>
)}
</div>
<button
onClick={onClose}
aria-label="Tutup"
className="rounded-lg p-1.5 text-slate-500 transition hover:bg-white/[0.06] hover:text-slate-200"
>
<svg
className="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
<div className="p-5">{children}</div>
</div>
</div>,
document.body,
);
}