ConfirmModal.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { useEffect, type ReactNode } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { AlertTriangle, Loader2 } from 'lucide-react';
  4. import { Card, CardContent } from './Card';
  5. import { Button } from './Button';
  6. interface ConfirmModalProps {
  7. title: string;
  8. message: string;
  9. confirmText?: string;
  10. cancelText?: string;
  11. cancelVariant?: 'primary' | 'secondary' | 'danger' | 'ghost';
  12. cardClassName?: string;
  13. // Tailwind z-index utility applied to the fixed overlay. Defaults to
  14. // ``z-50``. Use a higher value (e.g. ``z-[110]``) when this confirm
  15. // dialog is rendered from inside another modal that uses ``z-[100]`` —
  16. // without it the confirm dialog sits behind its parent (#1336 follow-up).
  17. overlayZIndex?: string;
  18. variant?: 'danger' | 'warning' | 'default';
  19. isLoading?: boolean;
  20. loadingText?: string;
  21. // Optional extra content rendered between the message and the buttons —
  22. // used for opt-in checkboxes (e.g. the "Also remove from statistics"
  23. // toggle in the archive delete confirmation, #1343).
  24. children?: ReactNode;
  25. onConfirm: () => void;
  26. onCancel: () => void;
  27. }
  28. export function ConfirmModal({
  29. title,
  30. message,
  31. confirmText,
  32. cancelText,
  33. cancelVariant,
  34. cardClassName,
  35. overlayZIndex,
  36. variant = 'default',
  37. isLoading = false,
  38. loadingText,
  39. children,
  40. onConfirm,
  41. onCancel,
  42. }: ConfirmModalProps) {
  43. const { t } = useTranslation();
  44. const resolvedConfirmText = confirmText ?? t('common.confirm');
  45. const resolvedCancelText = cancelText ?? t('common.cancel');
  46. const resolvedLoadingText = loadingText ?? t('common.loading');
  47. // Close on Escape key (but not while loading)
  48. useEffect(() => {
  49. const handleKeyDown = (e: KeyboardEvent) => {
  50. if (e.key === 'Escape' && !isLoading) onCancel();
  51. };
  52. window.addEventListener('keydown', handleKeyDown);
  53. return () => window.removeEventListener('keydown', handleKeyDown);
  54. }, [onCancel, isLoading]);
  55. const variantStyles = {
  56. danger: {
  57. icon: 'text-red-400',
  58. button: 'bg-red-500 hover:bg-red-600',
  59. },
  60. warning: {
  61. icon: 'text-yellow-400',
  62. button: 'bg-yellow-500 hover:bg-yellow-600 text-black',
  63. },
  64. default: {
  65. icon: 'text-bambu-green',
  66. button: 'bg-bambu-green hover:bg-bambu-green-dark',
  67. },
  68. };
  69. const styles = variantStyles[variant];
  70. return (
  71. <div
  72. className={`fixed inset-0 bg-black/50 flex items-center justify-center p-4 ${overlayZIndex ?? 'z-50'}`}
  73. onClick={isLoading ? undefined : onCancel}
  74. >
  75. <Card
  76. className={`w-full max-w-md ${cardClassName ?? ''}`}
  77. onClick={(e: React.MouseEvent) => e.stopPropagation()}
  78. >
  79. <CardContent className="p-6">
  80. <div className="flex items-start gap-4">
  81. <div className={`p-2 rounded-full bg-bambu-dark ${styles.icon}`}>
  82. <AlertTriangle className="w-6 h-6" />
  83. </div>
  84. <div className="flex-1">
  85. <h3 className="text-lg font-semibold text-white mb-2">{title}</h3>
  86. <p className="text-bambu-gray text-sm whitespace-pre-line">{message}</p>
  87. {children && <div className="mt-4">{children}</div>}
  88. </div>
  89. </div>
  90. <div className="flex gap-3 mt-6">
  91. <Button
  92. variant={cancelVariant ?? 'secondary'}
  93. onClick={onCancel}
  94. className="flex-1"
  95. disabled={isLoading}
  96. >
  97. {resolvedCancelText}
  98. </Button>
  99. <Button
  100. onClick={onConfirm}
  101. className={`flex-1 ${styles.button}`}
  102. disabled={isLoading}
  103. >
  104. {isLoading ? (
  105. <>
  106. <Loader2 className="w-4 h-4 mr-2 animate-spin" />
  107. {resolvedLoadingText}
  108. </>
  109. ) : (
  110. resolvedConfirmText
  111. )}
  112. </Button>
  113. </div>
  114. </CardContent>
  115. </Card>
  116. </div>
  117. );
  118. }