/** @jsxImportSource @emotion/react */ import { css } from '@emotion/react'; import { useRef, useEffect } from 'react'; import { RcIconClose } from '../icons'; import { useLockBodyScroll } from '../helpers'; export interface RcModalProps { onClose?: () => void; title?: string; titleFontSize?: string; iconSize?: string; marginVert?: string; verticalGap?: string; paddingHoriz?: string; iconPadding?: string; color?: string; bgColor?: string; bgOpacity?: number; titleBgColor?: string; titleBorderColor?: string; width?: string; height?: string; minWidth?: string; maxWidth?: string; minHeight?: string; maxHeight?: string; borderRadius?: string; zIndex?: number; allowClickOutsideToClose?: boolean; noCloseButton?: boolean; noHightlightCloseButton?: boolean; cssTitle?: string; } export const RcModal: React.FC = ({ onClose, title, titleFontSize = '16px', iconSize = '20px', marginVert = '10px', verticalGap = '10px', paddingHoriz = '20px', iconPadding = '25px', color = '#484848', bgColor = '#ffffff', bgOpacity = 0.4, titleBgColor, titleBorderColor, width, height, minWidth, maxWidth, minHeight, maxHeight, borderRadius = '8px', zIndex = 1, allowClickOutsideToClose = true, noCloseButton, noHightlightCloseButton = true, cssTitle = `font-weight: bold; font-size: 1.2em;`, children, }) => { const refRoot = useRef(null); useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (allowClickOutsideToClose) { if (refRoot.current && e.target) { if (!refRoot.current.contains(e.target as Node)) { if (onClose) { onClose(); } } } } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [onClose, allowClickOutsideToClose]); useLockBodyScroll(); return (
{title && (
{cssTitle ? {title} : {title}}
)}
{children}
{!noCloseButton && onClose && (
)}
); };