This commit is contained in:
Vula Builder
2026-07-27 18:59:59 +00:00
parent d42cc66a78
commit 19bd5e4281
+77
View File
@@ -0,0 +1,77 @@
'use client'
import { cn } from "@/lib/utils"
import { useEffect, useRef, useCallback } 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 closeButtonRef = useRef<HTMLButtonElement>(null)
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") {
onClose()
}
},
[onClose]
)
useEffect(() => {
if (open) {
document.addEventListener("keydown", handleKeyDown)
// Focus trap: focus close button when modal opens
setTimeout(() => closeButtonRef.current?.focus(), 100)
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-background/80 backdrop-blur-sm"
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-lg rounded-lg bg-card border border-border shadow-xl p-6",
className
)}
>
{title && (
<h2 className="text-lg font-semibold mb-4">{title}</h2>
)}
<button
ref={closeButtonRef}
onClick={onClose}
className="absolute top-4 right-4 rounded-md p-1 text-muted-foreground hover:text-foreground focus:outline-none focus:ring-1 focus:ring-primary"
aria-label="Close modal"
>
<X className="h-5 w-5" />
</button>
{children}
</div>
</div>
)
}