All files / src/components/ui/ConfirmDialog index.tsx

97.61% Statements 205/210
93.18% Branches 41/44
100% Functions 6/6
97.61% Lines 205/210

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 2111x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 5x 1x 1x 22x 22x 22x 20x 20x 22x 22x 22x 22x 22x 22x 22x 22x 22x 5x 5x 5x 2x 5x 1x 1x 1x 22x 22x 22x 20x 20x 22x 22x 22x 22x 22x 22x 22x 22x 22x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 5x 2x 5x           5x 2x 2x 1x 1x 1x 2x 20x 20x 20x 20x 20x 20x 20x 22x 22x 22x 22x 22x 2x 2x 2x 2x 1x 1x 2x 22x 22x 20x 20x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 22x 20x 20x 20x 20x 20x 22x 22x 22x 22x 22x 22x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1x 1x  
"use client";
 
import React, { useEffect, useRef } from "react";
import { Icon } from "@/components/ui/icons";
import { Button } from "@/components/ui/Button";
import { ConfirmDialogProps } from "@/types/ui";
 
const ConfirmDialog = ({
  isOpen,
  title,
  message,
  confirmText = "Confirm",
  cancelText = "Cancel",
  variant = "info",
  onConfirm,
  onCancel}: ConfirmDialogProps) => {
  const confirmButtonRef = useRef<HTMLButtonElement>(null);
  const cancelButtonRef = useRef<HTMLButtonElement>(null);
  const dialogRef = useRef<HTMLDivElement>(null);
 
  // ESC key to cancel dialog
  useEffect(() => {
    const handleEscape = (event: KeyboardEvent) => {
      if (event.key === "Escape" && isOpen) {
        onCancel();
      }
    };
 
    if (isOpen) {
      document.addEventListener("keydown", handleEscape);
    }
 
    return () => {
      document.removeEventListener("keydown", handleEscape);
    };
  }, [isOpen, onCancel]);
 
  // Enter key to confirm (when dialog is focused but not on a button)
  useEffect(() => {
    const handleEnter = (event: KeyboardEvent) => {
      if (
        event.key === "Enter" &&
        isOpen &&
        document.activeElement?.tagName !== "BUTTON"
      ) {
        event.preventDefault();
        onConfirm();
      }
    };
 
    if (isOpen) {
      document.addEventListener("keydown", handleEnter);
    }
 
    return () => {
      document.removeEventListener("keydown", handleEnter);
    };
  }, [isOpen, onConfirm]);
 
  // Focus trap - keep focus within dialog
  useEffect(() => {
    if (!isOpen) return;
 
    const dialogContent = dialogRef.current;
    if (!dialogContent) return;
 
    const focusableElements = dialogContent.querySelectorAll<HTMLElement>(
      'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
    );
    const firstElement = focusableElements[0];
    const lastElement = focusableElements[focusableElements.length - 1];
 
    // Auto-focus confirm button when dialog opens
    if (confirmButtonRef.current) {
      setTimeout(() => confirmButtonRef.current?.focus(), 100);
    }
 
    const handleTabKey = (e: KeyboardEvent) => {
      if (e.key !== "Tab") return;
 
      if (e.shiftKey) {
        // Shift + Tab
        if (document.activeElement === firstElement) {
          e.preventDefault();
          lastElement.focus();
        }
      } else {
        // Tab
        if (document.activeElement === lastElement) {
          e.preventDefault();
          firstElement.focus();
        }
      }
    };
 
    document.addEventListener("keydown", handleTabKey);
 
    return () => {
      document.removeEventListener("keydown", handleTabKey);
    };
  }, [isOpen]);
 
  // Close dialog when clicking outside
  useEffect(() => {
    function handleClickOutside(event: MouseEvent) {
      if (
        dialogRef.current &&
        !dialogRef.current.contains(event.target as Node)
      ) {
        onCancel();
      }
    }
 
    if (isOpen) {
      document.addEventListener("mousedown", handleClickOutside);
    }
 
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [isOpen, onCancel]);
 
  // Variant-specific styles
  const getVariantStyles = () => {
    switch (variant) {
      case "danger":
        return {
          icon: <Icon name="alert-triangle" className="w-6 h-6 text-red" aria-hidden="true" />,
          confirmButton:
            "bg-red text-white hover:bg-red-dark focus:ring-red/20",
          iconBg: "bg-red-50 dark:bg-red-900/30"};
      case "warning":
        return {
          icon: <Icon name="alert-triangle" className="w-6 h-6 text-yellow-600" aria-hidden="true" />,
          confirmButton:
            "bg-yellow-600 text-white hover:bg-yellow-700 focus:ring-yellow-600/20",
          iconBg: "bg-yellow-50 dark:bg-yellow-900/30"};
      default:
        return {
          icon: <Icon name="info-circle" className="w-6 h-6 text-blue" aria-hidden="true" />,
          confirmButton:
            "bg-blue text-white hover:bg-blue-dark focus:ring-blue/20",
          iconBg: "bg-blue-50 dark:bg-blue-900/30"};
    }
  };
 
  const variantStyles = getVariantStyles();
 
  if (!isOpen) return null;
 
  return (
    <div
      className="fixed top-0 left-0 w-full h-screen bg-dark/70 flex items-center justify-center z-99999 px-4 py-5"
      role="dialog"
      aria-modal="true"
      aria-labelledby="dialog-title"
      aria-describedby="dialog-message"
    >
      <div
        ref={dialogRef}
        className="w-full max-w-[500px] rounded-xl shadow-3 bg-white dark:bg-gray-800 p-6 sm:p-8 relative"
      >
        <div className="flex items-start gap-4">
          <div
            className={`flex-shrink-0 w-12 h-12 rounded-full flex items-center justify-center ${variantStyles.iconBg}`}
          >
            {variantStyles.icon}
          </div>
 
          <div className="flex-1">
            <h3
              id="dialog-title"
              className="font-medium text-xl text-dark dark:text-gray-100 mb-2"
            >
              {title}
            </h3>
            <p id="dialog-message" className="text-body dark:text-gray-400 text-sm mb-6">
              {message}
            </p>
 
            <div className="flex flex-wrap gap-3 justify-end">
              <Button
                ref={cancelButtonRef}
                type="button"
                onClick={onCancel}
                variant="secondary"
                size="md"
                aria-label={`Cancel: ${cancelText}`}
              >
                {cancelText}
              </Button>
              <Button
                ref={confirmButtonRef}
                type="button"
                onClick={onConfirm}
                variant={variant === "danger" ? "danger" : "primary"}
                size="md"
                aria-label={`Confirm: ${confirmText}`}
              >
                {confirmText}
              </Button>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};
 
export default ConfirmDialog;