import React, { useEffect, Fragment } from 'react'; import ReactDOM from 'react-dom'; import { Transition } from '@headlessui/react'; export default function Modal({ children, show = false, maxWidth = '2xl', closeable = true, onClose = () => {} }) { useEffect(() => { document.body.style.overflow = show ? 'hidden' : null; }, [show]); const close = () => { if (closeable) { onClose(); } }; const closeOnEscape = (e) => { if (e.key === 'Escape' && props.show) { close(); } }; useEffect(() => { document.addEventListener('keydown', closeOnEscape); return () => { document.removeEventListener('keydown', closeOnEscape); document.body.style.overflow = null; }; }, []); const maxWidthClass = { sm: 'sm:max-w-sm', md: 'sm:max-w-md', lg: 'sm:max-w-lg', xl: 'sm:max-w-xl', '2xl': 'sm:max-w-2xl', }[maxWidth]; const modalRoot = document.getElementById('modal-root'); return ReactDOM.createPortal(
{children}
, modalRoot ); }