This commit is contained in:
Vula Builder
2026-08-01 16:13:33 +00:00
parent 20673904f0
commit b20fc1d149
+103
View File
@@ -0,0 +1,103 @@
'use client'
import { cn } from "@/lib/utils"
import { X } from 'lucide-react'
import { useCallback, useEffect, useRef } from "react"
interface ModalProps {
isOpen: boolean
onClose: () => void
title?: string
description?: string
children: React.ReactNode
className?: string
showCloseButton?: boolean
}
export default function Modal({
isOpen,
onClose,
title,
description,
children,
className,
showCloseButton = true
}: ModalProps) {
const panelRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (isOpen) {
const originalOverflow = document.body.style.overflow
document.body.style.overflow = "hidden"
return () => {
document.body.style.overflow = originalOverflow
}
}
}, [isOpen])
useEffect(() => {
if (isOpen) {
panelRef.current?.focus()
}
}, [isOpen])
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === "Escape") {
onClose()
}
},
[onClose]
)
useEffect(() => {
if (!isOpen) return
document.addEventListener("keydown", handleKeyDown)
return () => document.removeEventListener("keydown", handleKeyDown)
}, [isOpen, handleKeyDown])
if (!isOpen) return null
return (
<div className="fixed inset-0 z-50 flex items-center justify-center overflow-y-auto p-4 sm:p-6">
<div
className="fixed inset-0 bg-black/50 backdrop-blur-sm"
onClick={onClose}
aria-hidden="true"
/>
<div
ref={panelRef}
role="dialog"
aria-modal="true"
aria-label={title || "Dialog"}
tabIndex={-1}
className={cn(
"relative z-10 w-full max-w-lg max-h-[90vh] overflow-y-auto rounded-lg border border-border bg-background shadow-xl focus:outline-none",
className
)}
>
{showCloseButton && (
<button
type="button"
onClick={onClose}
aria-label="Close dialog"
className="absolute right-4 top-4 z-10 rounded-md p-1 text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground focus:outline-none focus-visible:ring-2 focus-visible:ring-primary"
>
<X className="h-5 w-5" />
</button>
)}
{(title || description) && (
<div className="border-b border-border px-6 py-4 pr-12">
{title && (
<h2 className="text-xl font-semibold text-foreground">{title}</h2>
)}
{description && (
<p className="mt-1 text-sm text-muted-foreground">{description}</p>
)}
</div>
)}
<div className="p-6">{children}</div>
</div>
</div>
)
}