This commit is contained in:
Vula Builder
2026-08-01 16:26:35 +00:00
parent d8627c24f6
commit 4797adeb8a
+74
View File
@@ -0,0 +1,74 @@
'use client'
import { useEffect, useRef, useState } from "react"
import { X } from 'lucide-react'
import { cn } from "@/lib/utils"
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 [mounted, setMounted] = useState(false)
useEffect(() => {
if (open) {
setMounted(true)
document.body.style.overflow = "hidden"
} else {
document.body.style.overflow = ""
// delay unmount for exit animation
const timer = setTimeout(() => setMounted(false), 200)
return () => clearTimeout(timer)
}
}, [open])
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape" && open) onClose()
}
document.addEventListener("keydown", handleKeyDown)
return () => document.removeEventListener("keydown", handleKeyDown)
}, [open, onClose])
if (!mounted) return null
return (
<div
ref={overlayRef}
className={cn(
"fixed inset-0 z-50 flex items-center justify-center bg-background/80 backdrop-blur-sm transition-opacity duration-200",
open ? "opacity-100" : "opacity-0 pointer-events-none"
)}
onClick={(e) => { if (e.target === overlayRef.current) onClose() }}
role="dialog"
aria-modal="true"
aria-label={title || "Modal"}
>
<div
className={cn(
"relative w-full max-w-md rounded-lg border border-border bg-card p-6 shadow-lg transition-transform duration-200",
open ? "scale-100" : "scale-95",
className
)}
>
<button
onClick={onClose}
className="absolute right-4 top-4 rounded-sm opacity-70 hover:opacity-100 focus:outline-none focus:bg-accent"
aria-label="Close"
>
<X className="h-4 w-4" />
</button>
{title && (
<h2 className="text-lg font-semibold text-foreground mb-4">{title}</h2>
)}
{children}
</div>
</div>
)
}