'use client'; import { useEffect, useRef } from 'react'; import { Button } from './Button'; import { Card } from './Card'; interface ConfirmDialogProps { isOpen: boolean; title: string; message: string; onConfirm: () => void; onCancel: () => void; confirmText?: string; cancelText?: string; variant?: 'danger' | 'warning' | 'info'; dangerous?: boolean; } export function ConfirmDialog({ isOpen, title, message, onConfirm, onCancel, confirmText = '确认', cancelText = '取消', variant = 'warning', dangerous = false, }: ConfirmDialogProps) { const dialogRef = useRef(null); const cancelButtonRef = useRef(null); useEffect(() => { if (isOpen) { // Focus the cancel button when dialog opens cancelButtonRef.current?.focus(); // Prevent body scroll document.body.style.overflow = 'hidden'; } else { // Restore body scroll document.body.style.overflow = ''; } return () => { document.body.style.overflow = ''; }; }, [isOpen]); useEffect(() => { if (!isOpen) return; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault(); onCancel(); } }; document.addEventListener('keydown', handleKeyDown); return () => document.removeEventListener('keydown', handleKeyDown); }, [isOpen, onCancel]); if (!isOpen) return null; const variantStyles = { danger: 'bg-red-500 hover:bg-red-600', warning: 'bg-[var(--accent-color)] hover:brightness-110', info: 'bg-blue-500 hover:bg-blue-600', }; // Use dangerous prop to override variant const finalVariant = dangerous ? 'danger' : variant; return ( <> {/* Backdrop */}