import { useEffect, useRef, type ReactNode } from 'react' interface ConfirmModalProps { open: boolean title: string message: string onConfirm: () => void onCancel: () => void destructive?: boolean confirmLabel?: string children?: ReactNode } const FOCUSABLE = 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' export default function ConfirmModal({ open, title, message, onConfirm, onCancel, destructive, confirmLabel, children }: ConfirmModalProps) { const dialogRef = useRef(null) const cancelRef = useRef(null) useEffect(() => { if (!open) return const handleKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { onCancel() return } if (e.key === 'Tab' && dialogRef.current) { const focusable = Array.from(dialogRef.current.querySelectorAll(FOCUSABLE)) if (!focusable.length) return const first = focusable[0] const last = focusable[focusable.length - 1] if (e.shiftKey) { if (document.activeElement === first) { e.preventDefault() last.focus() } } else { if (document.activeElement === last) { e.preventDefault() first.focus() } } } } document.addEventListener('keydown', handleKey) cancelRef.current?.focus() return () => document.removeEventListener('keydown', handleKey) }, [open, onCancel]) if (!open) return null return (
e.stopPropagation()} >

{title}

{message}

{children}
) }