import { useEffect, useRef } from "react"; import { createPortal } from "react-dom"; import { Button } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; interface CustomModalProps { title: string; onRequestClose: () => void; className?: string; isDismissible?: boolean; shouldCloseOnClickOutside?: boolean; children: React.ReactNode; } export default function CustomModal({ title, onRequestClose, className = "", isDismissible = true, shouldCloseOnClickOutside = true, children, }: CustomModalProps) { const modalRef = useRef(null); const previousActiveElement = useRef(null); useEffect(() => { // Store the previously focused element previousActiveElement.current = document.activeElement as HTMLElement; // Focus the modal if (modalRef.current) { modalRef.current.focus(); } // Prevent body scrolling document.body.style.overflow = "hidden"; // Handle ESC key const handleEscape = (event: KeyboardEvent) => { if (event.key === "Escape" && isDismissible) { onRequestClose(); } }; document.addEventListener("keydown", handleEscape); return () => { // Restore body scrolling document.body.style.overflow = ""; // Restore focus to previous element if (previousActiveElement.current) { previousActiveElement.current.focus(); } document.removeEventListener("keydown", handleEscape); }; }, [isDismissible, onRequestClose]); const handleBackdropClick = (event: React.MouseEvent) => { if ( shouldCloseOnClickOutside && isDismissible && event.target === event.currentTarget ) { onRequestClose(); } }; const modalContent = (
e.stopPropagation()} >
{isDismissible && ( )}
{children}
); return createPortal(modalContent, document.body); }