Deploy
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
'use client'
|
||||
|
||||
import { ReactNode, useEffect, useId, useRef, useState } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { X } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface ModalProps {
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
title?: string
|
||||
description?: string
|
||||
children?: ReactNode
|
||||
className?: string
|
||||
}
|
||||
|
||||
export default function Modal({
|
||||
open,
|
||||
onClose,
|
||||
title,
|
||||
description,
|
||||
children,
|
||||
className
|
||||
}: ModalProps) {
|
||||
const [mounted, setMounted] = useState(false)
|
||||
const closeButtonRef = useRef<HTMLButtonElement>(null)
|
||||
const titleId = useId()
|
||||
const descriptionId = useId()
|
||||
|
||||
useEffect(() => {
|
||||
setMounted(true)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
|
||||
const previousOverflow = document.body.style.overflow
|
||||
document.body.style.overflow = 'hidden'
|
||||
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {
|
||||
onClose()
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', handleKeyDown)
|
||||
return () => {
|
||||
document.body.style.overflow = previousOverflow
|
||||
document.removeEventListener('keydown', handleKeyDown)
|
||||
}
|
||||
}, [open, onClose])
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
closeButtonRef.current?.focus()
|
||||
}
|
||||
}, [open])
|
||||
|
||||
if (!mounted || !open) {
|
||||
return null
|
||||
}
|
||||
|
||||
return createPortal(
|
||||
<div
|
||||
className='fixed inset-0 z-50 flex items-center justify-center bg-background/80 p-4 backdrop-blur-sm'
|
||||
onMouseDown={(event) => {
|
||||
if (event.target === event.currentTarget) {
|
||||
onClose()
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div
|
||||
role='dialog'
|
||||
aria-modal={true}
|
||||
aria-label={title ? undefined : 'Modal'}
|
||||
aria-labelledby={title ? titleId : undefined}
|
||||
aria-describedby={description ? descriptionId : undefined}
|
||||
className={cn('bg-card border border-border rounded-lg shadow-xl w-full max-w-lg', className)}
|
||||
>
|
||||
<div className='flex items-start justify-between gap-4 p-6 pb-4'>
|
||||
{title ? (
|
||||
<div>
|
||||
<h2 id={titleId} className='text-xl font-semibold text-foreground'>
|
||||
{title}
|
||||
</h2>
|
||||
{description ? (
|
||||
<p id={descriptionId} className='mt-1 text-sm text-muted-foreground'>{description}</p>
|
||||
) : null}
|
||||
</div>
|
||||
) : (
|
||||
<span />
|
||||
)}
|
||||
<button
|
||||
ref={closeButtonRef}
|
||||
type='button'
|
||||
onClick={onClose}
|
||||
aria-label='Close modal'
|
||||
className='ml-auto inline-flex h-8 w-8 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2'
|
||||
>
|
||||
<X className='h-4 w-4' />
|
||||
</button>
|
||||
</div>
|
||||
<div className='px-6 pb-6'>{children}</div>
|
||||
</div>
|
||||
</div>,
|
||||
document.body
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user