This commit is contained in:
Vula Builder
2026-06-03 16:38:54 +00:00
parent c4adce966c
commit f74bd24e6a
+74
View File
@@ -0,0 +1,74 @@
'use client'
import React, { 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 contentRef = useRef<HTMLDivElement>(null)
const handleEsc = useCallback(
(e: KeyboardEvent) => {
if (e.key === 'Escape') onClose()
},
[onClose]
)
useEffect(() => {
if (open) {
document.addEventListener('keydown', handleEsc)
document.body.style.overflow = 'hidden'
// Focus trap: focus content on open
contentRef.current?.focus()
}
return () => {
document.removeEventListener('keydown', handleEsc)
document.body.style.overflow = ''
}
}, [open, handleEsc])
if (!open) return null
return (
<div
ref={overlayRef}
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm"
onClick={(e) => {
if (e.target === overlayRef.current) onClose()
}}
role="dialog"
aria-modal="true"
aria-label={title || 'Modal dialog'}
>
<div
ref={contentRef}
tabIndex={-1}
className={cn(
"relative w-full max-w-lg rounded-lg bg-background p-6 shadow-lg",
className
)}
>
<button
onClick={onClose}
className="absolute right-4 top-4 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-primary"
aria-label="Close modal"
>
<X className="h-4 w-4" />
</button>
{title && (
<h2 className="mb-4 text-2xl font-semibold text-foreground">{title}</h2>
)}
{children}
</div>
</div>
)
}