65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
'use client'
|
|
|
|
import { cn } from "@/lib/utils"
|
|
import { X } from 'lucide-react'
|
|
import { useEffect, useRef } from "react"
|
|
|
|
interface ModalProps {
|
|
isOpen: boolean
|
|
onClose: () => void
|
|
children: React.ReactNode
|
|
className?: string
|
|
}
|
|
|
|
export default function Modal({ isOpen, onClose, children, className }: ModalProps) {
|
|
const overlayRef = useRef<HTMLDivElement>(null)
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
document.body.style.overflow = "hidden"
|
|
} else {
|
|
document.body.style.overflow = ""
|
|
}
|
|
return () => {
|
|
document.body.style.overflow = ""
|
|
}
|
|
}, [isOpen])
|
|
|
|
useEffect(() => {
|
|
const handleEscape = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape") onClose()
|
|
}
|
|
document.addEventListener("keydown", handleEscape)
|
|
return () => document.removeEventListener("keydown", handleEscape)
|
|
}, [onClose])
|
|
|
|
if (!isOpen) return null
|
|
|
|
return (
|
|
<div
|
|
ref={overlayRef}
|
|
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm"
|
|
onClick={(e) => {
|
|
if (e.target === overlayRef.current) onClose()
|
|
}}
|
|
role="dialog"
|
|
aria-modal="true"
|
|
>
|
|
<div
|
|
className={cn(
|
|
"relative w-full max-w-lg rounded-lg border border-border bg-background p-6 shadow-lg",
|
|
className
|
|
)}
|
|
>
|
|
<button
|
|
onClick={onClose}
|
|
className="absolute right-4 top-4 rounded-sm opacity-70 hover:opacity-100 focus:outline-none"
|
|
aria-label="Close"
|
|
>
|
|
<X className="h-4 w-4" />
|
|
</button>
|
|
{children}
|
|
</div>
|
|
</div>
|
|
)
|
|
} |