import type { MouseEvent, ReactNode } from 'react'; import React, { useRef } from 'react'; import ReactDOM from 'react-dom'; import { useIntl } from 'react-intl'; import useClickOutside from '../../../hooks/useClickOutside'; import { useLockBodyScroll } from '../../../hooks/useLockBodyScroll'; import globalMessages from '../../../i18n/globalMessages'; import Transition from '../../Transition'; import type { ButtonType } from '../Button'; import Button from '../Button'; import CachedImage from '../CachedImage'; import LoadingSpinner from '../LoadingSpinner'; interface ModalProps { title?: string; onCancel?: (e?: MouseEvent) => void; onOk?: (e?: MouseEvent) => void; onSecondary?: (e?: MouseEvent) => void; onTertiary?: (e?: MouseEvent) => void; cancelText?: string; okText?: string; secondaryText?: string; tertiaryText?: string; okDisabled?: boolean; cancelButtonType?: ButtonType; okButtonType?: ButtonType; secondaryButtonType?: ButtonType; secondaryDisabled?: boolean; tertiaryDisabled?: boolean; tertiaryButtonType?: ButtonType; disableScrollLock?: boolean; backgroundClickable?: boolean; iconSvg?: ReactNode; loading?: boolean; backdrop?: string; } const Modal: React.FC = ({ title, onCancel, onOk, cancelText, okText, okDisabled = false, cancelButtonType = 'default', okButtonType = 'primary', children, disableScrollLock, backgroundClickable = true, iconSvg, loading = false, secondaryButtonType = 'default', secondaryDisabled = false, onSecondary, secondaryText, tertiaryButtonType = 'default', tertiaryDisabled = false, tertiaryText, onTertiary, backdrop, }) => { const intl = useIntl(); const modalRef = useRef(null); useClickOutside(modalRef, () => { typeof onCancel === 'function' && backgroundClickable ? onCancel() : undefined; }); useLockBodyScroll(true, disableScrollLock); return ReactDOM.createPortal( // eslint-disable-next-line jsx-a11y/no-static-element-interactions
{ if (e.key === 'Escape') { typeof onCancel === 'function' && backgroundClickable ? onCancel() : undefined; } }} >
{backdrop && (
)}
{iconSvg &&
{iconSvg}
}
{title && ( {title} )}
{children && (
{children}
)} {(onCancel || onOk || onSecondary || onTertiary) && (
{typeof onOk === 'function' && ( )} {typeof onSecondary === 'function' && secondaryText && ( )} {typeof onTertiary === 'function' && tertiaryText && ( )} {typeof onCancel === 'function' && ( )}
)}
, document.body ); }; export default Modal;