import React, { useEffect, ReactNode } from 'react';
import { createPortal } from 'react-dom';
import './modal.scss';
import Icon from '../Icon';
import { colors } from '../../colors/colorConstants';
interface ModalProps {
isOpen: boolean;
onClose: () => void;
/*** label value for aria-label */
contentLabel?: string;
/*** default header will be provided with title and close icon. */
defaultHeader?: boolean;
/*** Title to be displayed in the header when defaultHeader is true*/
headerTitle?: string;
/*** Optional content to be displayed on the right side of the header when defaultHeader is true */
headerRightContent?: ReactNode;
/*** Custom styles for the overlay and content */
style?: {
overlay?: React.CSSProperties;
content?: React.CSSProperties;
};
/*** Custom class names for the modal content */
contentClassName?: string;
/*** Custom class name for the overlay */
overlayClassName?: string;
/*** Whether the modal should close when the 'Escape' key is pressed */
shouldCloseOnEsc?: boolean;
/*** Whether to hide the app from screen readers when the modal is open */
ariaHideApp?: boolean; //for screen readers
/*** Whether the modal should close when clicking outside of it (on the overlay) */
shouldCloseOnOverlayClick?: boolean;
// shouldFocusAfterRender?: boolean;
// shouldReturnFocusAfterClose?: boolean;
/***Content to be displayed inside the modal */
children: ReactNode;
}
interface CloseIconCompProps {
onClose: () => void;
}
const CloseIconComp = ({ onClose }: CloseIconCompProps) => (
);
const Modal: React.FC = ({
isOpen,
onClose,
contentLabel,
defaultHeader = false,
headerTitle = 'Title',
headerRightContent = ,
style = {},
contentClassName,
overlayClassName,
shouldCloseOnEsc = true,
ariaHideApp = true,
shouldCloseOnOverlayClick = true,
// shouldFocusAfterRender = true,
// shouldReturnFocusAfterClose = true,
children,
}) => {
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape' && shouldCloseOnEsc) {
onClose();
}
};
if (isOpen) {
document.addEventListener('keydown', handleKeyDown);
if (ariaHideApp) {
document.body.style.overflow = 'hidden';
}
}
return () => {
document.removeEventListener('keydown', handleKeyDown);
if (ariaHideApp) {
document.body.style.overflow = '';
}
};
}, [isOpen, onClose, ariaHideApp, shouldCloseOnEsc]);
if (!isOpen) return null;
return createPortal(