This commit is contained in:
Vula Builder
2026-07-28 12:39:43 +00:00
parent 2d82da9c03
commit 591240722c
+90
View File
@@ -0,0 +1,90 @@
'use client'
import { cn } from "@/lib/utils"
import { X } from 'lucide-react'
import React, { useEffect, useRef } from "react"
interface ModalProps {
isOpen: boolean
onClose: () => void
title?: string
children: React.ReactNode
className?: string
}
export default function Modal({
isOpen,
onClose,
title,
children,
className
}: ModalProps) {
const overlayRef = useRef<HTMLDivElement>(null)
const contentRef = useRef<HTMLDivElement>(null)
// Close on escape
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
onClose()
}
}
if (isOpen) {
document.addEventListener("keydown", handleKeyDown)
document.body.style.overflow = "hidden"
}
return () => {
document.removeEventListener("keydown", handleKeyDown)
document.body.style.overflow = ""
}
}, [isOpen, onClose])
// Focus trap on open
useEffect(() => {
if (isOpen && contentRef.current) {
contentRef.current.focus()
}
}, [isOpen])
if (!isOpen) return null
const handleOverlayClick = (e: React.MouseEvent) => {
if (e.target === overlayRef.current) {
onClose()
}
}
return (
<div
ref={overlayRef}
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
onClick={handleOverlayClick}
>
<div
ref={contentRef}
tabIndex={-1}
role="dialog"
aria-modal="true"
aria-label={title || "Modal"}
className={cn(
"relative w-full max-w-lg rounded-lg bg-background shadow-lg focus:outline-none",
className
)}
>
<div className="flex items-center justify-between border-b border-border p-4">
{title && (
<h2 className="text-lg font-semibold text-foreground">{title}</h2>
)}
<button
onClick={onClose}
className="ml-auto rounded-full p-1 text-muted-foreground hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-primary"
aria-label="Close modal"
>
<X size={20} />
</button>
</div>
<div className="p-4">{children}</div>
</div>
</div>
)
}