import { useEffect, useRef, type ReactNode } from 'react'; import { Button } from '@/components/ui/Button'; import { SectionHeader } from '@/components/ui/SectionHeader'; import { type Accent } from '@/lib/cn'; interface ModalProps { open: boolean; title: string; accent?: Accent; onClose: () => void; children: ReactNode; /** Optional name to give the dialog role for screen readers / Playwright. */ testid?: string; } /** * Centered modal with a backdrop. Closes on Escape and on backdrop click. * The accessible name comes from the SectionHeader's `highlight`, so the dialog * can be located via `getByRole('dialog', { name: ... })`. */ export function Modal({ open, title, accent = 'cyan', onClose, children, testid }: ModalProps) { const ref = useRef(null); useEffect(() => { if (!open) return; function onKey(e: KeyboardEvent) { if (e.key === 'Escape') onClose(); } document.addEventListener('keydown', onKey); return () => document.removeEventListener('keydown', onKey); }, [open, onClose]); if (!open) return null; return (
{ if (e.target === e.currentTarget) onClose(); }} role="presentation" >
{children}
); }