import { ReactNode, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { Button } from './Button'; import { Text } from './Text'; import { defaultLayoutConfig, type LayoutConfig } from '~/lib/layout-utils'; interface ModalProps { isOpen: boolean; onClose: () => void; title: string; children: ReactNode; size?: 'sm' | 'md' | 'lg' | 'xl'; className?: string; config?: Partial; showCloseButton?: boolean; } export function Modal({ isOpen, onClose, title, children, size = 'md', className = '', config = {}, showCloseButton = true, }: ModalProps) { const layoutConfig = { ...defaultLayoutConfig, ...config }; const [isMounted, setIsMounted] = useState(false); useEffect(() => { setIsMounted(true); }, []); useEffect(() => { if (isOpen) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = 'unset'; } return () => { document.body.style.overflow = 'unset'; }; }, [isOpen]); useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); } }; if (isOpen) { document.addEventListener('keydown', handleEscape); } return () => { document.removeEventListener('keydown', handleEscape); }; }, [isOpen, onClose]); if (!isOpen || !isMounted) return null; const sizeClasses = { sm: 'max-w-md', md: 'max-w-lg', lg: 'max-w-2xl', xl: 'max-w-4xl', }; const modalContent = (
{/* Backdrop */}
{/* Modal panel */}
{/* Header */}
{title} {showCloseButton && ( )}
{/* Content */}
{children}
); return createPortal(modalContent, document.body); } interface ConfirmModalProps { isOpen: boolean; onClose: () => void; onConfirm: () => void; title: string; message: string; confirmText?: string; cancelText?: string; variant?: 'danger' | 'warning' | 'info'; config?: Partial; } export function ConfirmModal({ isOpen, onClose, onConfirm, title, message, confirmText = "تأكيد", cancelText = "إلغاء", variant = 'info', config = {}, }: ConfirmModalProps) { const layoutConfig = { ...defaultLayoutConfig, ...config }; const [isMounted, setIsMounted] = useState(false); useEffect(() => { setIsMounted(true); }, []); const handleConfirm = () => { onConfirm(); onClose(); }; const getIcon = () => { switch (variant) { case 'danger': return (
); case 'warning': return (
); default: return (
); } }; return (
{getIcon()}
{title} {message}
); }