This repository has been archived by the owner on May 15, 2019. It is now read-only.
/
timeout-transition-group.jsx
211 lines (178 loc) · 6.27 KB
/
timeout-transition-group.jsx
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
/**
* The CSSTransitionGroup component uses the 'transitionend' event, which
* browsers will not send for any number of reasons, including the
* transitioning node not being painted or in an unfocused tab.
*
* This TimeoutTransitionGroup instead uses a user-defined timeout to determine
* when it is a good time to remove the component. Currently there is only one
* timeout specified, but in the future it would be nice to be able to specify
* separate timeouts for enter and leave, in case the timeouts for those
* animations differ. Even nicer would be some sort of inspection of the CSS to
* automatically determine the duration of the animation or transition.
*
* This is adapted from Facebook's CSSTransitionGroup which is in the React
* addons and under the Apache 2.0 License.
*/
// TODO(zach): convert to CSSCore
var $ = require('jquery');
var React = require('react/addons');
var ReactTransitionGroup = React.addons.TransitionGroup;
var TICK = 17;
/**
* EVENT_NAME_MAP is used to determine which event fired when a
* transition/animation ends, based on the style property used to
* define that event.
*/
var EVENT_NAME_MAP = {
transitionend: {
'transition': 'transitionend',
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'mozTransitionEnd',
'OTransition': 'oTransitionEnd',
'msTransition': 'MSTransitionEnd'
},
animationend: {
'animation': 'animationend',
'WebkitAnimation': 'webkitAnimationEnd',
'MozAnimation': 'mozAnimationEnd',
'OAnimation': 'oAnimationEnd',
'msAnimation': 'MSAnimationEnd'
}
};
var endEvents = [];
(function detectEvents() {
if (typeof window === "undefined") {
return;
}
var testEl = document.createElement('div');
var style = testEl.style;
// On some platforms, in particular some releases of Android 4.x, the
// un-prefixed "animation" and "transition" properties are defined on the
// style object but the events that fire will still be prefixed, so we need
// to check if the un-prefixed events are useable, and if not remove them
// from the map
if (!('AnimationEvent' in window)) {
delete EVENT_NAME_MAP.animationend.animation;
}
if (!('TransitionEvent' in window)) {
delete EVENT_NAME_MAP.transitionend.transition;
}
for (var baseEventName in EVENT_NAME_MAP) {
if (EVENT_NAME_MAP.hasOwnProperty(baseEventName)) {
var baseEvents = EVENT_NAME_MAP[baseEventName];
for (var styleName in baseEvents) {
if (styleName in style) {
endEvents.push(baseEvents[styleName]);
break;
}
}
}
}
})();
function animationSupported() {
return endEvents.length !== 0;
}
var TimeoutTransitionGroupChild = React.createClass({
transition: function(animationType, finishCallback) {
var node = this.getDOMNode();
var className = this.props.name + '-' + animationType;
var activeClassName = className + '-active';
var endListener = function() {
$(node).removeClass(className);
$(node).removeClass(activeClassName);
// Usually this optional callback is used for informing an owner of
// a leave animation and telling it to remove the child.
finishCallback && finishCallback();
};
if (!animationSupported()) {
endListener();
} else {
if (animationType === "enter") {
this.animationTimeout = setTimeout(endListener,
this.props.enterTimeout);
} else if (animationType === "leave") {
this.animationTimeout = setTimeout(endListener,
this.props.leaveTimeout);
}
}
$(node).addClass(className);
// Need to do this to actually trigger a transition.
this.queueClass(activeClassName);
},
queueClass: function(className) {
this.classNameQueue.push(className);
if (!this.timeout) {
this.timeout = setTimeout(this.flushClassNameQueue, TICK);
}
},
flushClassNameQueue: function() {
if (this.isMounted()) {
$(this.getDOMNode()).addClass(this.classNameQueue.join(" "));
}
this.classNameQueue.length = 0;
this.timeout = null;
},
componentWillMount: function() {
this.classNameQueue = [];
},
componentWillUnmount: function() {
if (this.timeout) {
clearTimeout(this.timeout);
}
if (this.animationTimeout) {
clearTimeout(this.animationTimeout);
}
},
componentWillEnter: function(done) {
if (this.props.enter) {
this.transition('enter', done);
} else {
done();
}
},
componentWillLeave: function(done) {
if (this.props.leave) {
this.transition('leave', done);
} else {
done();
}
},
render: function() {
return React.Children.only(this.props.children);
}
});
var TimeoutTransitionGroup = React.createClass({
propTypes: {
enterTimeout: React.PropTypes.number.isRequired,
leaveTimeout: React.PropTypes.number.isRequired,
transitionName: React.PropTypes.string.isRequired,
transitionEnter: React.PropTypes.bool,
transitionLeave: React.PropTypes.bool,
},
getDefaultProps: function() {
return {
transitionEnter: true,
transitionLeave: true
};
},
_wrapChild: function(child) {
return (
<TimeoutTransitionGroupChild
enterTimeout={this.props.enterTimeout}
leaveTimeout={this.props.leaveTimeout}
name={this.props.transitionName}
enter={this.props.transitionEnter}
leave={this.props.transitionLeave}>
{child}
</TimeoutTransitionGroupChild>
);
},
render: function() {
return (
<ReactTransitionGroup
{...this.props}
childFactory={this._wrapChild} />
);
}
});
module.exports = TimeoutTransitionGroup;