67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { ReactNode, useEffect } from 'react'
|
|
import { X } from 'lucide-react'
|
|
|
|
interface ModalProps {
|
|
isOpen: boolean
|
|
onClose: () => void
|
|
title?: string
|
|
children: ReactNode
|
|
maxWidth?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '4xl'
|
|
}
|
|
|
|
export function Modal({ isOpen, onClose, title, children, maxWidth = 'lg' }: ModalProps) {
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape') onClose()
|
|
}
|
|
if (isOpen) {
|
|
document.body.style.overflow = 'hidden'
|
|
window.addEventListener('keydown', handleKeyDown)
|
|
}
|
|
return () => {
|
|
document.body.style.overflow = 'unset'
|
|
window.removeEventListener('keydown', handleKeyDown)
|
|
}
|
|
}, [isOpen, onClose])
|
|
|
|
if (!isOpen) return null
|
|
|
|
const maxWidening = {
|
|
sm: 'max-w-sm',
|
|
md: 'max-w-md',
|
|
lg: 'max-w-lg',
|
|
xl: 'max-w-xl',
|
|
'2xl': 'max-w-2xl',
|
|
'4xl': 'max-w-4xl',
|
|
}
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 overflow-y-auto">
|
|
{/* Glass Backdrop */}
|
|
<div
|
|
className="fixed inset-0 bg-slate-950/70 backdrop-blur-md transition-opacity duration-300 animate-in fade-in"
|
|
onClick={onClose}
|
|
/>
|
|
|
|
{/* Modal Dialog */}
|
|
<div className={`relative w-full ${maxWidening[maxWidth]} bg-white dark:bg-slate-900 rounded-2xl border border-slate-200 dark:border-slate-800 shadow-2xl shadow-black/40 overflow-hidden z-10 animate-in zoom-in-95 duration-200`}>
|
|
{title && (
|
|
<div className="flex items-center justify-between px-6 py-4 border-b border-slate-200 dark:border-slate-800 bg-slate-50/50 dark:bg-slate-900/50">
|
|
<h3 className="text-lg font-semibold text-slate-900 dark:text-slate-100">{title}</h3>
|
|
<button
|
|
onClick={onClose}
|
|
className="p-1.5 rounded-lg text-slate-400 hover:text-slate-600 dark:hover:text-slate-200 hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors"
|
|
>
|
|
<X size={18} />
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
<div className="p-6">
|
|
{children}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|