-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathTransition.js
288 lines (228 loc) · 7.14 KB
/
Transition.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import cx from 'classnames'
import _ from 'lodash'
import PropTypes from 'prop-types'
import { cloneElement, Component } from 'react'
import {
makeDebugger,
META,
SUI,
useKeyOnly,
} from '../../lib'
import TransitionGroup from './TransitionGroup'
const debug = makeDebugger('transition')
/**
* A transition is an animation usually used to move content in or out of view.
*/
export default class Transition extends Component {
static propTypes = {
/** Named animation event to used. Must be defined in CSS. */
animation: PropTypes.oneOf(SUI.TRANSITIONS),
/** Primary content. */
children: PropTypes.element.isRequired,
/** Duration of the CSS transition animation in milliseconds. */
duration: PropTypes.number,
/** Show the component; triggers the enter or exit animation. */
visible: PropTypes.bool,
/** Wait until the first "enter" transition to mount the component (add it to the DOM). */
mountOnShow: PropTypes.bool,
/**
* Callback on each transition that changes visibility to shown.
*
* @param {null}
* @param {object} data - All props with status.
*/
onComplete: PropTypes.func,
/**
* Callback on each transition that changes visibility to hidden.
*
* @param {null}
* @param {object} data - All props with status.
*/
onHide: PropTypes.func,
/**
* Callback on each transition that changes visibility to shown.
*
* @param {null}
* @param {object} data - All props with status.
*/
onShow: PropTypes.func,
/**
* Callback on animation start.
*
* @param {null}
* @param {object} data - All props with status.
*/
onStart: PropTypes.func,
/** React's key of the element. */
reactKey: PropTypes.string,
/** Run the enter animation when the component mounts, if it is initially shown. */
transitionOnMount: PropTypes.bool,
/** Unmount the component (remove it from the DOM) when it is not shown. */
unmountOnHide: PropTypes.bool,
}
static defaultProps = {
animation: 'fade',
duration: 500,
visible: true,
mountOnShow: true,
transitionOnMount: false,
unmountOnHide: false,
}
static _meta = {
name: 'Transition',
type: META.TYPES.MODULE,
}
static ENTERED = 'ENTERED'
static ENTERING = 'ENTERING'
static EXITED = 'EXITED'
static EXITING = 'EXITING'
static UNMOUNTED = 'UNMOUNTED'
static Group = TransitionGroup
constructor(...args) {
super(...args)
const { initial: status, next } = this.computeInitialStatuses()
this.nextStatus = next
this.state = { status }
}
// ----------------------------------------
// Lifecycle
// ----------------------------------------
componentDidMount() {
debug('componentDidMount()')
this.updateStatus()
}
componentWillReceiveProps(nextProps) {
debug('componentWillReceiveProps()')
const { current: status, next } = this.computeStatuses(nextProps)
this.nextStatus = next
if (status) this.setState({ status })
}
componentDidUpdate() {
debug('componentDidUpdate()')
this.updateStatus()
}
componentWillUnmount() {
debug('componentWillUnmount()')
}
// ----------------------------------------
// Callback handling
// ----------------------------------------
handleStart = () => {
const { duration } = this.props
const status = this.nextStatus
this.nextStatus = null
this.setState({ status, animating: true }, () => {
_.invoke(this.props, 'onStart', null, { ...this.props, status })
setTimeout(this.handleComplete, duration)
})
}
handleComplete = () => {
const { status: current } = this.state
_.invoke(this.props, 'onComplete', null, { ...this.props, status: current })
if (this.nextStatus) {
this.handleStart()
return
}
const status = this.computeCompletedStatus()
const callback = current === Transition.ENTERING ? 'onShow' : 'onHide'
this.setState({ status, animating: false }, () => {
_.invoke(this.props, callback, null, { ...this.props, status })
})
}
updateStatus = () => {
const { animating } = this.state
if (this.nextStatus) {
this.nextStatus = this.computeNextStatus()
if (!animating) this.handleStart()
}
}
// ----------------------------------------
// Helpers
// ----------------------------------------
computeClasses = () => {
const { animation, children } = this.props
const { animating, status } = this.state
const childClasses = _.get(children, 'props.className')
const directional = _.includes(SUI.DIRECTIONAL_TRANSITIONS, animation)
if (directional) {
return cx(
animation,
childClasses,
useKeyOnly(animating, 'animating'),
useKeyOnly(status === Transition.ENTERING, 'in'),
useKeyOnly(status === Transition.EXITING, 'out'),
useKeyOnly(status === Transition.EXITED, 'hidden'),
useKeyOnly(status !== Transition.EXITED, 'visible'),
'transition',
)
}
return cx(
animation,
childClasses,
useKeyOnly(animating, 'animating transition'),
)
}
computeCompletedStatus = () => {
const { unmountOnHide } = this.props
const { status } = this.state
if (status === Transition.ENTERING) return Transition.ENTERED
return unmountOnHide ? Transition.UNMOUNTED : Transition.EXITED
}
computeInitialStatuses = () => {
const {
visible,
mountOnShow,
transitionOnMount,
unmountOnHide,
} = this.props
if (visible) {
if (transitionOnMount) {
return {
initial: Transition.EXITED,
next: Transition.ENTERING,
}
}
return { initial: Transition.ENTERED }
}
if (mountOnShow || unmountOnHide) return { initial: Transition.UNMOUNTED }
return { initial: Transition.EXITED }
}
computeNextStatus = () => {
const { animating, status } = this.state
if (animating) return status === Transition.ENTERING ? Transition.EXITING : Transition.ENTERING
return status === Transition.ENTERED ? Transition.EXITING : Transition.ENTERING
}
computeStatuses = props => {
const { status } = this.state
const { visible } = props
if (visible) {
return {
current: status === Transition.UNMOUNTED && Transition.EXITED,
next: (status !== Transition.ENTERING && status !== Transition.ENTERED) && Transition.ENTERING,
}
}
return {
next: (status === Transition.ENTERING || status === Transition.ENTERED) && Transition.EXITING,
}
}
computeStyle = () => {
const { children, duration } = this.props
const childStyle = _.get(children, 'props.style')
return { ...childStyle, animationDuration: `${duration}ms` }
}
// ----------------------------------------
// Render
// ----------------------------------------
render() {
debug('render()')
debug('props', this.props)
debug('state', this.state)
const { children } = this.props
const { status } = this.state
if (status === Transition.UNMOUNTED) return null
return cloneElement(children, {
className: this.computeClasses(),
style: this.computeStyle(),
})
}
}