This commit is contained in:
Vula Builder
2026-07-14 19:09:04 +00:00
parent 064f23a97d
commit 05ff2b8b83
+81
View File
@@ -0,0 +1,81 @@
'use client'
import { cn } from "@/lib/utils"
import { useEffect, useRef, useState } from "react"
import { X } from 'lucide-react'
interface ModalProps {
open: boolean
onClose: () => void
title?: string
children: React.ReactNode
className?: string
}
export default function Modal({ open, onClose, title, children, className }: ModalProps) {
const overlayRef = useRef<HTMLDivElement>(null)
const [visible, setVisible] = useState(false)
useEffect(() => {
if (open) {
setVisible(true)
document.body.style.overflow = "hidden"
} else {
document.body.style.overflow = ""
// delay removal for animation
const timer = setTimeout(() => setVisible(false), 200)
return () => clearTimeout(timer)
}
return () => {
document.body.style.overflow = ""
}
}, [open])
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose()
}
document.addEventListener("keydown", handleEscape)
return () => document.removeEventListener("keydown", handleEscape)
}, [onClose])
if (!visible) return null
return (
<div
ref={overlayRef}
className={cn(
"fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm transition-opacity duration-200",
open ? "opacity-100" : "opacity-0"
)}
onClick={(e) => {
if (e.target === overlayRef.current) onClose()
}}
role="dialog"
aria-modal="true"
aria-label={title || "Dialog"}
>
<div
className={cn(
"relative w-full max-w-lg rounded-lg bg-[#f2f4f5] p-6 shadow-xl border border-[#d4dadd] transition-all duration-200",
open ? "scale-100" : "scale-95",
className
)}
>
<div className="flex items-center justify-between mb-4">
{title && (
<h2 className="text-xl font-semibold text-[#1f2b33]">{title}</h2>
)}
<button
onClick={onClose}
className="ml-auto p-1 rounded-md hover:bg-[#e8eaeb] transition-colors"
aria-label="Close dialog"
>
<X className="h-5 w-5 text-[#36454F]" />
</button>
</div>
{children}
</div>
</div>
)
}