import { JSX } from "preact"; import { useEffect, useState } from "preact/hooks"; interface ModalAction { text: string; onClick?: (text: string) => void | Promise; } interface ModalGlobalHook { show: ( title: string, content: string | JSX.Element, actions: ModalAction[], ) => void; hide: () => void; } declare global { interface Window { $modal?: ModalGlobalHook; } } export default function Modal() { const [visible, setVisible] = useState(false); const [title, setTitle] = useState(""); const [content, setContent] = useState(""); const [actions, setActions] = useState([]); const showModal = ( newTitle: string, newContent: string | JSX.Element, newActions: ModalAction[], ) => { setTitle(newTitle || ""); setContent(newContent || ""); setActions(newActions || []); setVisible(true); }; const hideModal = () => { setVisible(false); }; useEffect(() => { window.$modal = { show: ( title: string, content: string | JSX.Element, actions: ModalAction[], ) => showModal(title, content, actions), hide: () => hideModal(), }; return () => { delete window.$modal; }; }, []); return ( <>
{ hideModal(); }} /> {title ?
{title}
: null}
{content}
{actions.length > 0 ? (
{actions.map((action, index) => ( ))}
) : null}
); }