import React, { DetailedHTMLProps, HTMLAttributes } from "react"; import { twMerge } from "tailwind-merge"; import { createRoot } from "react-dom/client"; import Paper from "./Paper"; type Props = DetailedHTMLProps< HTMLAttributes, HTMLDivElement > & { target: React.ReactNode; }; /** * # Modal Component * @className_wrapper twui-modal-root * @className_wrapper twui-modal */ export default function Modal({ target, ...props }: Props) { const [wrapper, setWrapper] = React.useState(null); React.useEffect(() => { const wrapperEl = document.createElement("div"); wrapperEl.className = twMerge( "fixed z-[200000] top-0 left-0 w-screen h-screen", "flex flex-col items-center justify-center", "twui-modal-root" ); setWrapper(wrapperEl); }, []); const modalEl = (
{ closeModal({ wrapperEl: wrapper }); }} >
{props.children}
); const targetEl = (
{ if (!wrapper) return; document.body.appendChild(wrapper); const root = createRoot(wrapper); root.render(modalEl); }} > {target}
); return targetEl; } function closeModal({ wrapperEl }: { wrapperEl: HTMLDivElement | null }) { if (!wrapperEl) return; wrapperEl.parentElement?.removeChild(wrapperEl); }