This commit is contained in:
Vula Builder
2026-07-26 16:40:59 +00:00
parent 34b8930ced
commit cdebc2265d
+87
View File
@@ -0,0 +1,87 @@
'use client'
import { cn } from "@/lib/utils"
import { useEffect, useRef } from "react"
import { X } from 'lucide-react'
interface ModalProps {
open: boolean
onClose: () => void
title?: string
description?: string
className?: string
children: React.ReactNode
}
export default function Modal({ open, onClose, title, description, className, children }: ModalProps) {
const overlayRef = useRef<HTMLDivElement>(null)
const contentRef = useRef<HTMLDivElement>(null)
// Close on Escape key
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose()
}
if (open) {
document.addEventListener("keydown", handleKeyDown)
// Prevent body scroll
document.body.style.overflow = "hidden"
}
return () => {
document.removeEventListener("keydown", handleKeyDown)
document.body.style.overflow = "auto"
}
}, [open, onClose])
// Focus trap (basic) focus content on open
useEffect(() => {
if (open && contentRef.current) {
contentRef.current.focus()
}
}, [open])
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
ref={contentRef}
role="dialog"
aria-modal="true"
aria-labelledby={title ? "modal-title" : undefined}
aria-describedby={description ? "modal-description" : undefined}
tabIndex={-1}
className={cn(
"relative w-full max-w-lg mx-4 max-h-[85vh] overflow-y-auto rounded-lg bg-background p-6 shadow-xl focus:outline-none",
className
)}
>
<button
onClick={onClose}
className="absolute top-4 right-4 rounded-sm opacity-70 ring-offset-background 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 id="modal-title" className="text-2xl font-semibold text-foreground mb-2">
{title}
</h2>
)}
{description && (
<p id="modal-description" className="text-sm text-muted-foreground mb-4">
{description}
</p>
)}
{children}
</div>
</div>
)
}