import React, { ReactNode, useEffect, useRef } from 'react'; import './Modal.css'; export interface ModalProps { /** * Title displayed at the top of the modal */ title?: string; /** * Content of the modal */ children: ReactNode; /** * Whether the modal is visible or not */ isOpen: boolean; /** * Function to call when the modal close button is clicked */ onClose: () => void; /** * Optional CSS class name to add to the modal for custom styling */ className?: string; /** * Whether to show the close button in the header * @default true */ showCloseButton?: boolean; /** * Whether to close the modal when clicking outside of it * @default true */ closeOnOutsideClick?: boolean; } /** * A reusable modal component that can be used for various purposes */ const Modal: React.FC = ({ title, children, isOpen, onClose, className = '', showCloseButton = true, closeOnOutsideClick = true }) => { const modalRef = useRef(null); // Close modal when escape key is pressed useEffect(() => { const handleEscKey = (event: KeyboardEvent) => { if (isOpen && event.key === 'Escape') { onClose(); } }; document.addEventListener('keydown', handleEscKey); return () => { document.removeEventListener('keydown', handleEscKey); }; }, [isOpen, onClose]); // Handle outside click const handleBackdropClick = (event: React.MouseEvent) => { if (closeOnOutsideClick && modalRef.current && !modalRef.current.contains(event.target as Node)) { onClose(); } }; if (!isOpen) { return null; } return (
{(title || showCloseButton) && (
{title && } {showCloseButton && ( )}
)}
); }; export default Modal;