File size: 4,147 Bytes
e85fa50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useRef } from 'react';
import ReactDOM from 'react-dom';

interface ModalProps {
  isOpen: boolean;
  onClose: () => void;
  title?: string;
  children: React.ReactNode;
  footer?: React.ReactNode;
  size?: 'sm' | 'md' | 'lg';
  closeOnClickOutside?: boolean;
}

const Modal: React.FC<ModalProps> = ({

  isOpen,

  onClose,

  title,

  children,

  footer,

  size = 'md',

  closeOnClickOutside = true

}) => {
  const modalRef = useRef<HTMLDivElement>(null);
  
  // 处理ESC按键关闭模态框
  useEffect(() => {
    const handleEscKey = (event: KeyboardEvent) => {
      if (event.key === 'Escape' && isOpen) {
        onClose();
      }
    };
    
    window.addEventListener('keydown', handleEscKey);
    return () => {
      window.removeEventListener('keydown', handleEscKey);
    };
  }, [isOpen, onClose]);
  
  // 阻止点击模态框内部时传播到外部背景
  const handleModalClick = (e: React.MouseEvent) => {
    e.stopPropagation();
  };
  
  // 处理点击背景关闭模态框
  const handleBackdropClick = (e: React.MouseEvent) => {
    if (closeOnClickOutside && modalRef.current && !modalRef.current.contains(e.target as Node)) {
      onClose();
    }
  };
  
  // 防止模态框打开时页面滚动
  useEffect(() => {
    if (isOpen) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = '';
    }
    
    return () => {
      document.body.style.overflow = '';
    };
  }, [isOpen]);
  
  // 获取模态框大小样式
  const getSizeClass = () => {
    switch (size) {
      case 'sm':
        return 'max-w-md';
      case 'md':
        return 'max-w-lg';
      case 'lg':
        return 'max-w-2xl';
      default:
        return 'max-w-lg';
    }
  };
  
  if (!isOpen) return null;
  
  // 使用React Portal将模态框渲染到DOM树的最顶层
  return ReactDOM.createPortal(
    <div 

      className={`ios-modal-backdrop open fixed inset-0 z-50 overflow-auto bg-black bg-opacity-40 flex items-center justify-center p-4`}

      onClick={handleBackdropClick}

    >

      <div 

        ref={modalRef}

        className={`ios-modal ${getSizeClass()} bg-white rounded-xl shadow-xl transform transition-all`}

        onClick={handleModalClick}

      >

        {title && (

          <div className="ios-modal-header px-6 py-4 border-b border-gray-200">

            <h3 className="ios-modal-title text-lg font-semibold text-center">{title}</h3>

          </div>

        )}

        

        <div className="ios-modal-body p-6 max-h-[70vh] overflow-auto">

          {children}

        </div>

        

        {footer && (

          <div className="ios-modal-footer border-t border-gray-200">

            {footer}

          </div>

        )}

      </div>

    </div>,
    document.body
  );
};

// 预定义的模态框按钮布局
export const ModalFooter: React.FC<{
  children: React.ReactNode;
  className?: string;
}> = ({ children, className = '' }) => {
  return (
    <div className={`flex border-t border-gray-200 ${className}`}>

      {children}

    </div>
  );
};

// 预定义的模态框按钮
export const ModalButton: React.FC<{
  children: React.ReactNode;
  onClick: () => void;
  variant?: 'primary' | 'secondary' | 'danger';
  className?: string;
}> = ({ 

  children, 

  onClick, 

  variant = 'secondary',

  className = '' 

}) => {
  const getVariantClass = () => {
    switch (variant) {
      case 'primary':
        return 'text-blue-600 hover:bg-blue-50';
      case 'secondary':
        return 'text-gray-600 hover:bg-gray-50';
      case 'danger':
        return 'text-red-600 hover:bg-red-50';
      default:
        return 'text-gray-600 hover:bg-gray-50';
    }
  };
  
  return (
    <button

      className={`

        flex-1 py-3 px-5 text-center font-medium text-sm transition-colors duration-200

        ${getVariantClass()}

        ${className}

      `}

      onClick={onClick}

    >

      {children}

    </button>
  );
};

export default Modal;