Files
faunosteo/apps/app/src/components/ui/Dialog.tsx
T

78 lines
2.4 KiB
TypeScript

import { useEffect, type ReactNode } from 'react'
import { AnimatePresence, motion } from 'motion/react'
import { useT } from '@/i18n/useT'
export interface DialogProps {
open: boolean
onClose: () => void
title: string
children: ReactNode
size?: 'sm' | 'md' | 'lg' | 'xl'
}
const SIZE: Record<NonNullable<DialogProps['size']>, string> = {
sm: 'max-w-sm',
md: 'max-w-md',
lg: 'max-w-2xl',
xl: 'max-w-4xl',
}
export default function Dialog({
open,
onClose,
title,
children,
size = 'md',
}: DialogProps) {
const t = useT()
useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}, [open, onClose])
return (
<AnimatePresence>
{open && (
<div className="fixed inset-0 z-50 flex items-center justify-center p-3 sm:p-4">
<motion.div
data-testid="dialog-backdrop"
className="absolute inset-0 bg-black/40"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
onClick={onClose}
/>
<motion.div
role="dialog"
aria-modal="true"
aria-label={title}
initial={{ opacity: 0, scale: 0.96 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.96 }}
transition={{ duration: 0.18 }}
className={`relative w-full ${SIZE[size]} max-h-[85vh] overflow-y-auto rounded-2xl bg-[var(--color-surface)] shadow-xl`}
>
<header className="flex items-center justify-between px-4 sm:px-6 py-3 sm:py-4 border-b border-[var(--color-border)]">
<h2 className="text-lg font-semibold text-[var(--color-ink)]">
{title}
</h2>
<button
type="button"
aria-label={t('common.close')}
onClick={onClose}
className="rounded-md p-1 text-[var(--color-ink-muted)] hover:bg-[var(--color-surface-strong)]"
>
✕
</button>
</header>
<div className="px-4 sm:px-6 py-3 sm:py-4">{children}</div>
</motion.div>
</div>
)}
</AnimatePresence>
)
}