This commit is contained in:
Vula Builder
2026-07-26 16:56:18 +00:00
parent 6bd8ca5aa0
commit d009009d4c
+68
View File
@@ -0,0 +1,68 @@
'use client'
import { useEffect, useRef, useCallback } 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 handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") onClose()
},
[onClose]
)
useEffect(() => {
if (open) {
document.addEventListener("keydown", handleKeyDown)
document.body.style.overflow = "hidden"
} else {
document.body.style.overflow = ""
}
return () => {
document.removeEventListener("keydown", handleKeyDown)
document.body.style.overflow = ""
}
}, [open, handleKeyDown])
if (!open) return null
return (
<div
ref={overlayRef}
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50"
onClick={(e) => {
if (e.target === overlayRef.current) onClose()
}}
>
<div
className={cn(
"relative w-full max-w-lg mx-4 rounded-lg bg-[#f4f9f5] p-6 shadow-xl",
className
)}
>
<div className="flex items-center justify-between mb-4">
{title && <h2 className="text-xl font-semibold text-[#0d2114]">{title}</h2>}
<button
onClick={onClose}
className="p-1 rounded-md hover:bg-[#8b5a3c]/10 transition-colors"
aria-label="Close modal"
>
<X className="h-5 w-5 text-[#0d2114]" />
</button>
</div>
{children}
</div>
</div>
)
}