This commit is contained in:
Vula Builder
2026-08-03 11:42:13 +00:00
parent afc2e0d974
commit df027886ce
+85
View File
@@ -0,0 +1,85 @@
'use client'
import { useEffect, useRef, useId } from 'react'
import * as React from 'react'
import { X } from 'lucide-react'
import { cn } from '@/lib/utils'
interface ModalProps {
open: boolean
onClose: () => void
title?: string
description?: string
children: React.ReactNode
className?: string
showCloseButton?: boolean
}
export default function Modal({ open, onClose, title, description, children, className, showCloseButton = true }: ModalProps) {
const titleId = React.useId()
const panelRef = React.useRef<HTMLDivElement>(null)
React.useEffect(() => {
if (!open) return
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') onClose()
}
const previousFocus = document.activeElement as HTMLElement | null
const originalOverflow = document.body.style.overflow
panelRef.current?.focus()
document.addEventListener('keydown', handleKeyDown)
document.body.style.overflow = 'hidden'
return () => {
document.removeEventListener('keydown', handleKeyDown)
document.body.style.overflow = originalOverflow
previousFocus?.focus()
}
}, [open, onClose])
if (!open) return null
return (
<div className='fixed inset-0 z-50 flex items-center justify-center p-4'>
<div
aria-hidden='true'
className='absolute inset-0 bg-background/80 backdrop-blur-sm'
onClick={onClose}
/>
<div
ref={panelRef}
role='dialog'
aria-modal='true'
aria-labelledby={title ? titleId : undefined}
tabIndex={-1}
className={cn(
'relative w-full max-w-lg rounded-xl border border-border bg-card text-foreground shadow-lg focus:outline-none',
className
)}
>
{showCloseButton && (
<button
type='button'
onClick={onClose}
aria-label='Close dialog'
className='absolute right-4 top-4 rounded-md p-1 text-muted-foreground hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-primary'
>
<X className='h-5 w-5' />
</button>
)}
{title && (
<div className='p-6 pb-2'>
<h2 id={titleId} className='text-2xl font-semibold'>
{title}
</h2>
{description && <p className='mt-1 text-sm text-muted-foreground'>{description}</p>}
</div>
)}
<div className='p-6'>{children}</div>
</div>
</div>
)
}