Files
ims/components/ui/modal.tsx

48 lines
1.5 KiB
TypeScript

'use client'
import { useEffect } from 'react'
import { X } from 'lucide-react'
import { Button } from './button'
interface ModalProps {
open: boolean
onClose: () => void
title: string
children: React.ReactNode
footer?: React.ReactNode
width?: 'sm' | 'md' | 'lg'
}
export function Modal({ open, onClose, title, children, footer, width = 'md' }: ModalProps) {
useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() }
document.addEventListener('keydown', onKey)
return () => document.removeEventListener('keydown', onKey)
}, [open, onClose])
if (!open) return null
const maxW = { sm: 'max-w-sm', md: 'max-w-md', lg: 'max-w-lg' }[width]
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4" role="dialog" aria-modal="true">
<div className="absolute inset-0 bg-black/30" onClick={onClose} />
<div className={`relative w-full ${maxW} bg-surface rounded-xl shadow-float`}>
<div className="flex items-center justify-between px-5 py-3.5 border-b border-rule">
<h2 className="text-base font-[650]">{title}</h2>
<Button variant="icon" aria-label="Close" onClick={onClose}>
<X size={16} strokeWidth={2} aria-hidden />
</Button>
</div>
<div className="px-5 py-4">{children}</div>
{footer && (
<div className="px-5 py-3.5 border-t border-rule flex justify-end gap-2">
{footer}
</div>
)}
</div>
</div>
)
}