Files
ims/components/ui/modal.tsx
2026-09-30 19:04:43 +08:00

51 lines
1.7 KiB
TypeScript

'use client'
import { useEffect } from 'react'
import { X } from 'lucide-react'
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-neutral-900/50" onClick={onClose} />
<div className={`relative w-full ${maxW} bg-surface shadow-lg`}>
<div className="flex items-center justify-between px-6 py-4 border-b-2 border-divider">
<h2 className="text-xl font-extrabold text-ink">{title}</h2>
<button
onClick={onClose}
aria-label="Close"
className="inline-flex w-9 h-9 items-center justify-center text-ink cursor-pointer transition-colors hover:bg-ink/7 active:bg-ink/15"
>
<X size={18} strokeWidth={2} aria-hidden />
</button>
</div>
<div className="px-6 py-5">{children}</div>
{footer && (
<div className="px-6 py-4 border-t border-divider flex justify-end gap-2">
{footer}
</div>
)}
</div>
</div>
)
}