import { useEffect, useRef, createPortal } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; type ModalProps = { isOpen: boolean; onClose: () => void; title: string; children: React.ReactNode; footer?: React.ReactNode; className?: string; bodyClassName?: string; maxWidth?: string; }; const Modal = ( { isOpen, onClose, title, children, footer, className = '', bodyClassName = '', maxWidth = 'max-w-2xl', }: ModalProps ) => { const modalRef = useRef( null ); useEffect( () => { const handleEscape = ( event: KeyboardEvent ) => { if ( event.key === 'Escape' ) { onClose(); } }; if ( isOpen ) { document.addEventListener( 'keydown', handleEscape ); document.body.style.overflow = 'hidden'; } return () => { document.removeEventListener( 'keydown', handleEscape ); document.body.style.overflow = 'unset'; }; }, [ isOpen, onClose ] ); if ( ! isOpen ) { return null; } return createPortal(
, document.body, ); }; export default Modal;