2021-05-12 17:19:02 +02:00
|
|
|
import { h, Fragment } from 'preact';
|
|
|
|
import { createPortal } from 'preact/compat';
|
|
|
|
import { useState, useEffect } from 'preact/hooks';
|
|
|
|
|
2022-02-27 15:04:12 +01:00
|
|
|
export default function Dialog({ children, portalRootID = 'dialogs' }) {
|
2021-05-12 17:19:02 +02:00
|
|
|
const portalRoot = portalRootID && document.getElementById(portalRootID);
|
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
setShow(true);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const dialog = (
|
|
|
|
<Fragment>
|
|
|
|
<div
|
|
|
|
data-testid="scrim"
|
|
|
|
key="scrim"
|
2021-08-26 13:54:36 +02:00
|
|
|
className="fixed bg-fixed inset-0 z-10 flex justify-center items-center bg-black bg-opacity-40"
|
2021-05-12 17:19:02 +02:00
|
|
|
>
|
|
|
|
<div
|
|
|
|
role="modal"
|
2023-02-26 14:05:27 +01:00
|
|
|
className={`absolute rounded shadow-2xl bg-white dark:bg-gray-700 sm:max-w-sm md:max-w-md lg:max-w-lg text-gray-900 dark:text-white transition-transform transition-opacity duration-75 transform scale-90 opacity-0 ${
|
2021-05-12 17:19:02 +02:00
|
|
|
show ? 'scale-100 opacity-100' : ''
|
|
|
|
}`}
|
|
|
|
>
|
2022-02-27 15:04:12 +01:00
|
|
|
{children}
|
2021-05-12 17:19:02 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
return portalRoot ? createPortal(dialog, portalRoot) : dialog;
|
|
|
|
}
|