import React, { useRef, useLayoutEffect, MouseEvent } from 'react' import classnames from 'classnames' import { createPortal } from 'react-dom' import { BaseComponentProps } from '@models' import { Button } from '@components' import { noop } from '@lib/helper' import { useI18n } from '@stores' import './style.scss' interface ModalProps extends BaseComponentProps { // show modal show?: boolean // modal title title: string // size size?: 'small' | 'big' // body className bodyClassName?: string // body style bodyStyle?: React.CSSProperties // show footer footer?: boolean // on click ok onOk?: typeof noop // on click close onClose?: typeof noop } export function Modal (props: ModalProps) { const { show = true, title = 'Modal', size = 'small', footer = true, onOk = noop, onClose = noop, bodyClassName, bodyStyle, className, style, children } = props const { translation } = useI18n() const { t } = translation('Modal') const portalRef = useRef(document.createElement('div')) const maskRef = useRef(null) useLayoutEffect(() => { const current = portalRef.current document.body.appendChild(current) return () => { document.body.removeChild(current) } }, []) function handleMaskClick (e: MouseEvent) { if (e.target === maskRef.current) { onClose() } } const modal = (
{title}
{children}
{ footer && (
) }
) return createPortal(modal, portalRef.current) }