Dialog in React Material-UI

  1. 1. 解析Material-UI中的Dialog组件
  2. 2. 自己写的dialog

解析Material-UI中的Dialog组件

  • dialog是常见组件,前段时间研究了一下Material-UI的Dialog组件的代码

自己写的dialog

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
import React, { PropTypes } from 'react'
import EventListener from 'react-event-listener'
import keycode from 'keycode'
import { TweenMax } from 'gsap'
import { Paper } from 'material-ui'
import ReactTransitionGroup from 'react-addons-transition-group'
class TransitionItem extends React.Component {
static propTypes = {
modal: PropTypes.bool,
onRequestClose: PropTypes.func
}
static defaultProps = {
modal: false,
onRequestClose: null
}
constructor(props) {
super(props)
this.requestClose = (buttonClicked) => {
if (!buttonClicked && this.props.modal) return
if (this.props.onRequestClose) {
this.props.onRequestClose(!!buttonClicked)
}
}
this.handleTouchTapOverlay = () => {
this.requestClose(false)
}
this.handleKeyUp = (event) => {
switch (keycode(event)) {
case 'esc': return this.requestClose(false)
default: return 0
}
}
this.animation = (status) => {
const transformItem = document.getElementById('transformItem')
const overlay = document.getElementById('overlay')
const time = 0.45
const ease = Power4.easeOut
if (status === 'In') {
TweenMax.from(overlay, time, { opacity: 0, ease })
TweenMax.from(transformItem, time, { top: '-64', opacity: 0, ease })
}
if (status === 'Out') {
TweenMax.to(transformItem, time, { top: '-64', opacity: 0, ease })
TweenMax.to(overlay, time, { opacity: 0, ease })
}
}
}
componentWillUnmount() {
clearTimeout(this.enterTimeout)
clearTimeout(this.leaveTimeout)
}
componentWillEnter(callback) {
this.componentWillAppear(callback)
}
componentWillAppear(callback) {
this.animation('In')
this.enterTimeout = setTimeout(callback, 450) // matches transition duration
}
componentWillLeave(callback) {
this.animation('Out')
this.leaveTimeout = setTimeout(callback, 450) // matches transition duration
}
render() {
return (
<div
style={{
position: 'fixed',
width: '100%',
height: '100%',
top: 0,
left: 0,
zIndex: 1500,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
transition: 'all 0ms cubic-bezier(0.23, 1, 0.32, 1)'
}}
>
<div
id="transformItem"
style={{
position: 'relative',
zIndex: 1500,
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
}}
>
<EventListener target="window" onKeyUp={this.handleKeyUp} />
<Paper zDepth={4}>
{this.props.children}
</Paper>
</div>
<div
id="overlay"
style={{
position: 'fixed',
height: '100%',
width: '100%',
top: 0,
left: 0,
backgroundColor: 'rgba(0, 0, 0, 0.541176)',
zIndex: 1400
}}
onTouchTap={this.handleTouchTapOverlay}
/>
</div>
)
}
}
export default class CDialog extends React.Component {
static propTypes = {
open: PropTypes.bool.isRequired
}
render() {
return (
<ReactTransitionGroup>
{this.props.open && <TransitionItem {...this.props} />}
</ReactTransitionGroup>
)
}
}
  • 使用ReactTransitionGroup来处理动画,主要是组件进入和退出的动画

  • 使用display: 'flex', alignItems: 'center', justifyContent: 'center'实现居中

  • 基本结构:

    • root: 根div,一个撑满页面的容器,包含transformItem和overlay

    • transformItem:居中显示的包含内容的对话框窗口

    • overlay: dialog后面灰度背景遮罩