112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
import React, { useEffect } from 'react';
|
|
import { X } from 'lucide-react';
|
|
|
|
export interface ModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
title: React.ReactNode;
|
|
icon?: React.ComponentType<{ className?: string }>;
|
|
children: React.ReactNode;
|
|
footer?: React.ReactNode;
|
|
maxWidth?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | 'full';
|
|
className?: string;
|
|
bodyClassName?: string;
|
|
}
|
|
|
|
const maxWidthClasses: Record<NonNullable<ModalProps['maxWidth']>, string> = {
|
|
sm: 'max-w-sm',
|
|
md: 'max-w-md',
|
|
lg: 'max-w-lg',
|
|
xl: 'max-w-xl',
|
|
'2xl': 'max-w-2xl',
|
|
'3xl': 'max-w-3xl',
|
|
'4xl': 'max-w-4xl',
|
|
'5xl': 'max-w-5xl',
|
|
full: 'max-w-[95vw]',
|
|
};
|
|
|
|
export default function Modal({
|
|
isOpen,
|
|
onClose,
|
|
title,
|
|
icon: Icon,
|
|
children,
|
|
footer,
|
|
maxWidth = '2xl',
|
|
className = '',
|
|
bodyClassName = '',
|
|
}: ModalProps) {
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
document.body.style.overflow = 'hidden';
|
|
} else {
|
|
document.body.style.overflow = '';
|
|
}
|
|
return () => {
|
|
document.body.style.overflow = '';
|
|
};
|
|
}, [isOpen]);
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape' && isOpen) {
|
|
onClose();
|
|
}
|
|
};
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
}, [isOpen, onClose]);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
return (
|
|
<div
|
|
className="fixed inset-0 z-50 flex items-center justify-center p-3 sm:p-4 md:p-6 bg-black/60 backdrop-blur-xs font-vazir animate-in fade-in duration-200"
|
|
dir="rtl"
|
|
onClick={onClose}
|
|
>
|
|
<div
|
|
className={`bg-white rounded-3xl w-full ${maxWidthClasses[maxWidth]} max-h-[92dvh] sm:max-h-[88vh] flex flex-col border border-gray-100 shadow-2xl overflow-hidden animate-in zoom-in-95 duration-200 ${className}`}
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
{/* Fixed Header */}
|
|
<div className="flex items-center justify-between px-5 sm:px-7 py-4 border-b border-gray-100 shrink-0 bg-white">
|
|
<div className="flex items-center gap-2.5 min-w-0">
|
|
{Icon && (
|
|
<div className="w-8 h-8 rounded-xl bg-purple-50 text-purple-600 flex items-center justify-center shrink-0">
|
|
<Icon className="w-4 h-4" />
|
|
</div>
|
|
)}
|
|
<h3 className="text-base sm:text-lg font-black text-gray-900 truncate">
|
|
{title}
|
|
</h3>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="w-8 h-8 rounded-xl bg-gray-50 text-gray-400 hover:text-gray-700 hover:bg-gray-100 flex items-center justify-center transition-colors shrink-0 cursor-pointer"
|
|
aria-label="بستن"
|
|
>
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Scrollable Body */}
|
|
<div
|
|
className={`p-5 sm:p-7 overflow-y-auto flex-1 overscroll-contain text-xs sm:text-sm text-gray-700 space-y-4 ${bodyClassName}`}
|
|
>
|
|
{children}
|
|
</div>
|
|
|
|
{/* Fixed Footer */}
|
|
{footer && (
|
|
<div className="px-5 sm:px-7 py-4 bg-gray-50/80 border-t border-gray-100 flex flex-wrap items-center justify-end gap-3 shrink-0">
|
|
{footer}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|