This commit is contained in:
Vula Builder
2026-07-27 17:52:34 +00:00
parent 73fb480f67
commit b8c20a998a
+72
View File
@@ -0,0 +1,72 @@
'use client'
import { useEffect, useRef } from "react"
import { X } from 'lucide-react'
import { cn } from "@/lib/utils"
interface ModalProps {
isOpen: boolean
onClose: () => void
title?: string
children: React.ReactNode
className?: string
}
export default function Modal({ isOpen, onClose, title, children, className }: ModalProps) {
const closeButtonRef = useRef<HTMLButtonElement>(null)
useEffect(() => {
if (isOpen) {
document.body.style.overflow = "hidden"
closeButtonRef.current?.focus()
} else {
document.body.style.overflow = "unset"
}
return () => {
document.body.style.overflow = "unset"
}
}, [isOpen])
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") {
onClose()
}
}
if (isOpen) {
document.addEventListener("keydown", handleKeyDown)
}
return () => document.removeEventListener("keydown", handleKeyDown)
}, [isOpen, onClose])
if (!isOpen) return null
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
<div
className={cn(
"relative bg-white rounded-lg shadow-lg w-full max-w-md mx-4 p-6",
className
)}
role="dialog"
aria-modal="true"
aria-labelledby={title ? "modal-title" : undefined}
>
<button
ref={closeButtonRef}
onClick={onClose}
className="absolute top-4 right-4 p-1 rounded-md text-[#6b7280] hover:text-[#101418] hover:bg-[#e7eaef] focus:outline-none focus:ring-2 focus:ring-[#d4af37]"
aria-label="Close modal"
>
<X size={20} />
</button>
{title && (
<h2 id="modal-title" className="text-xl font-semibold text-[#101418] mb-4">
{title}
</h2>
)}
{children}
</div>
</div>
)
}