File size: 6,654 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"use client";

import { useEffect, useRef, useId } from "react";
import { cn } from "@/shared/utils/cn";
import Button from "./Button";

interface ModalProps {
  isOpen: boolean;
  onClose: () => void;
  title?: React.ReactNode;
  children?: React.ReactNode;
  footer?: React.ReactNode;
  size?: "sm" | "md" | "lg" | "xl" | "full";
  closeOnOverlay?: boolean;
  showCloseButton?: boolean;
  className?: string;
  bodyClassName?: string;
  compactHeader?: boolean;
  maxWidth?: string;
}

export default function Modal({

  isOpen,

  onClose,

  title,

  children,

  footer,

  size = "md",

  closeOnOverlay = true,

  showCloseButton = true,

  className,

  bodyClassName,

  compactHeader = false,

}: ModalProps) {
  const titleId = useId();
  const dialogRef = useRef(null);

  const sizes = {
    sm: "max-w-sm",
    md: "max-w-md",
    lg: "max-w-lg",
    xl: "max-w-xl",
    full: "max-w-4xl",
  };

  // Lock body scroll when modal is open
  useEffect(() => {
    if (isOpen) {
      document.body.style.overflow = "hidden";
    } else {
      document.body.style.overflow = "";
    }
    return () => {
      document.body.style.overflow = "";
    };
  }, [isOpen]);

  // Handle escape key
  useEffect(() => {
    const handleEscape = (e) => {
      if (e.key === "Escape" && isOpen) {
        onClose();
      }
    };
    document.addEventListener("keydown", handleEscape);
    return () => document.removeEventListener("keydown", handleEscape);
  }, [isOpen, onClose]);

  // Focus trap
  useEffect(() => {
    if (!isOpen || !dialogRef.current) return;

    const dialog = dialogRef.current;
    const focusableSelector =
      'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';

    // Focus first focusable element
    const firstFocusable = dialog.querySelector(focusableSelector);
    if (firstFocusable) {
      setTimeout(() => firstFocusable.focus(), 50);
    }

    const handleTab = (e) => {
      if (e.key !== "Tab") return;

      const focusable = [...dialog.querySelectorAll(focusableSelector)];
      if (focusable.length === 0) return;

      const first = focusable[0];
      const last = focusable[focusable.length - 1];

      if (e.shiftKey && document.activeElement === first) {
        e.preventDefault();
        last.focus();
      } else if (!e.shiftKey && document.activeElement === last) {
        e.preventDefault();
        first.focus();
      }
    };

    dialog.addEventListener("keydown", handleTab);
    return () => dialog.removeEventListener("keydown", handleTab);
  }, [isOpen]);

  if (!isOpen) return null;

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center p-4">

      {/* Overlay */}

      <div

        className="absolute inset-0 bg-black/30 backdrop-blur-sm"

        onClick={closeOnOverlay ? onClose : undefined}

        aria-hidden="true"

      />



      {/* Modal content */}

      <div

        ref={dialogRef}

        role="dialog"

        aria-modal="true"

        aria-labelledby={title ? titleId : undefined}

        className={cn(

          "relative w-full bg-surface",

          "border border-black/10 dark:border-white/10",

          "rounded-xl shadow-2xl",

          "animate-in fade-in zoom-in-95 duration-200",

          sizes[size],

          className

        )}

      >

        {/* Header */}

        {(title || showCloseButton) && (

          <div

            className={cn(

              "flex items-center justify-between border-b border-black/5 dark:border-white/5",

              compactHeader ? "px-4 py-2.5" : "p-6"

            )}

          >

            <div className="flex items-center min-w-0">

              <div

                className={cn(

                  "flex items-center gap-1.5 mr-3 shrink-0",

                  compactHeader ? "" : "gap-2 mr-4"

                )}

                aria-hidden="true"

              >

                <div

                  className={cn(

                    "rounded-full bg-[#FF5F56]",

                    compactHeader ? "w-2.5 h-2.5" : "w-3 h-3"

                  )}

                />

                <div

                  className={cn(

                    "rounded-full bg-[#FFBD2E]",

                    compactHeader ? "w-2.5 h-2.5" : "w-3 h-3"

                  )}

                />

                <div

                  className={cn(

                    "rounded-full bg-[#27C93F]",

                    compactHeader ? "w-2.5 h-2.5" : "w-3 h-3"

                  )}

                />

              </div>

              {title && (

                <h2

                  id={titleId}

                  className={cn(

                    "font-semibold text-text-main truncate min-w-0",

                    compactHeader ? "text-sm" : "text-lg"

                  )}

                >

                  {title}

                </h2>

              )}

            </div>

            {showCloseButton && (

              <button

                onClick={onClose}

                aria-label="Close"

                className="p-1.5 rounded-lg text-text-muted hover:bg-black/5 dark:hover:bg-white/5 transition-colors shrink-0"

              >

                <span className="material-symbols-outlined text-[20px]" aria-hidden="true">

                  close

                </span>

              </button>

            )}

          </div>

        )}



        {/* Body */}

        <div className={bodyClassName ?? "p-6 max-h-[calc(80vh-140px)] overflow-y-auto"}>

          {children}

        </div>



        {/* Footer */}

        {footer && (

          <div className="flex items-center justify-end gap-3 p-6 border-t border-black/5 dark:border-white/5">

            {footer}

          </div>

        )}

      </div>

    </div>
  );
}

// Confirm Modal helper
export function ConfirmModal({

  isOpen,

  onClose,

  onConfirm,

  title = "Confirm",

  message,

  confirmText = "Confirm",

  cancelText = "Cancel",

  variant = "danger",

  loading = false,

}) {
  return (
    <Modal

      isOpen={isOpen}

      onClose={onClose}

      title={title}

      size="sm"

      footer={

        <>

          <Button variant="ghost" onClick={onClose} disabled={loading}>

            {cancelText}

          </Button>

          <Button variant={variant as any} onClick={onConfirm} loading={loading}>

            {confirmText}

          </Button>

        </>
      }
    >
      <p className="text-text-muted">{message}</p>
    </Modal>
  );
}