确定组件参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type ModalProps = {
// 动画类型
effect?: number;
// 是否显示模态框
visible: boolean;
// 控制模态框显示
setVisible: Function;
// 宽高和位置
width?: number;
height?: number;
left?: number;
top?: number;
// 覆盖样式
style?: CSSProperties;
// 模态框内容
children?: React.ReactNode;
// 是否显示遮罩
mask?: boolean;
}

组件代码

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
export default function (props: ModalProps) {
// 设定初始值
let { width = 900, height = 600, left, top, style = {}, children, mask = true, visible, setVisible, effect = 1 } = props
// 是否显示遮罩层
const showMask = useMemo(() => {
if (!mask) return false
return visible
}, [visible, mask])

useEffect(() => {
if (visible) {
modalRef.current!.classList.add('md-show')
} else {
modalRef.current!.classList.remove('md-show')
}
}, [visible])

const modalRef = useRef<HTMLDivElement>(null)

return <Wrapper>
<div ref={modalRef} className={`modal md-effect-${effect}`} style={{ width, height, left: left || `calc(50% - ${width / 2}px)`, top: top || `100px`, ...style }}>
{children}
</div>
{showMask ? <div className="mask" onClick={() => {
setVisible(false)
}}></div> : ''}
</Wrapper>
}

组件样式

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
import { CSSProperties, useEffect, useMemo, useRef } from "react";
import styled from "styled-components"

const Wrapper = styled.div`
.mask {
position: fixed;
background-color: rgb(0,0,0,.5);
width: 100vw;
height: 100vh;
left: 0;
top: 0;
z-index: 998;
}
.modal {
position: fixed;
background-color: #FFF;
z-index: 999;
visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
}

.md-show {
visibility: visible;
opacity: 1;
}
.md-effect-1 {
-webkit-transform: scale(0.7);
-moz-transform: scale(0.7);
-ms-transform: scale(0.7);
transform: scale(0.7);
opacity: 0;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}

.md-show.md-effect-1 {
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
opacity: 1;
}

/* Effect 2: Slide from the right */
.md-effect-2 {
-webkit-transform: translateX(20%);
-moz-transform: translateX(20%);
-ms-transform: translateX(20%);
transform: translateX(20%);
opacity: 0;
-webkit-transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
-moz-transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
}

.md-show.md-effect-2 {
-webkit-transform: translateX(0);
-moz-transform: translateX(0);
-ms-transform: translateX(0);
transform: translateX(0);
opacity: 1;
}

/* Effect 3: Slide from the bottom */
.md-effect-3 {
-webkit-transform: translateY(20%);
-moz-transform: translateY(20%);
-ms-transform: translateY(20%);
transform: translateY(20%);
opacity: 0;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}

.md-show.md-effect-3 {
-webkit-transform: translateY(0);
-moz-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}

/* Effect 4: Newspaper */
.md-effect-4 {
-webkit-transform: scale(0) rotate(720deg);
-moz-transform: scale(0) rotate(720deg);
-ms-transform: scale(0) rotate(720deg);
transform: scale(0) rotate(720deg);
opacity: 0;
}

.md-show.md-effect-4 ~ .md-overlay,
.md-effect-4 {
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}

.md-show.md-effect-4 {
-webkit-transform: scale(1) rotate(0deg);
-moz-transform: scale(1) rotate(0deg);
-ms-transform: scale(1) rotate(0deg);
transform: scale(1) rotate(0deg);
opacity: 1;
}

/* Effect 5: fall */
.md-effect-5.md-modal {
-webkit-perspective: 1300px;
-moz-perspective: 1300px;
perspective: 1300px;
}

.md-effect-5 {
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform: translateZ(600px) rotateX(20deg);
-moz-transform: translateZ(600px) rotateX(20deg);
-ms-transform: translateZ(600px) rotateX(20deg);
transform: translateZ(600px) rotateX(20deg);
opacity: 0;
}

.md-show.md-effect-5 {
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
transition: all 0.3s ease-in;
-webkit-transform: translateZ(0px) rotateX(0deg);
-moz-transform: translateZ(0px) rotateX(0deg);
-ms-transform: translateZ(0px) rotateX(0deg);
transform: translateZ(0px) rotateX(0deg);
opacity: 1;
}

/* Effect 6: side fall */
.md-effect-6.md-modal {
-webkit-perspective: 1300px;
-moz-perspective: 1300px;
perspective: 1300px;
}

.md-effect-6 {
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform: translate(30%) translateZ(600px) rotate(10deg);
-moz-transform: translate(30%) translateZ(600px) rotate(10deg);
-ms-transform: translate(30%) translateZ(600px) rotate(10deg);
transform: translate(30%) translateZ(600px) rotate(10deg);
opacity: 0;
}

.md-show.md-effect-6 {
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
transition: all 0.3s ease-in;
-webkit-transform: translate(0%) translateZ(0) rotate(0deg);
-moz-transform: translate(0%) translateZ(0) rotate(0deg);
-ms-transform: translate(0%) translateZ(0) rotate(0deg);
transform: translate(0%) translateZ(0) rotate(0deg);
opacity: 1;
}

/* Effect 7: slide and stick to top */
.md-effect-7{
top: 0;
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}

.md-effect-7 {
-webkit-transform: translateY(-200%);
-moz-transform: translateY(-200%);
-ms-transform: translateY(-200%);
transform: translateY(-200%);
-webkit-transition: all .3s;
-moz-transition: all .3s;
transition: all .3s;
opacity: 0;
}

.md-show.md-effect-7 {
-webkit-transform: translateY(0%);
-moz-transform: translateY(0%);
-ms-transform: translateY(0%);
transform: translateY(0%);
border-radius: 0 0 3px 3px;
opacity: 1;
}
`

使用

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
import { useState } from 'react'
import './App.css'
import Modal from './components/Modal01'

function App() {
const [visible, setVisible] = useState(false)
const [type, setType] = useState(1)

return (
<>
<div style={{ width: '100vw', height: '100vh', background: '#1F1F1F' }}>
<div className='grid'>
{
[1, 2, 3, 4, 5, 6, 7].map(v => {
return <button style={{marginRight:'15px'}} onClick={() => {
setType(v)
setVisible(!visible)
}}>effect{v}</button>
})
}
</div>

<Modal style={{ borderRadius: '10px' }} effect={type} visible={visible} setVisible={setVisible}>
<div>hello</div>
</Modal>
</div>
</>
)
}

export default App