Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactTransitionEvents
10
*/
11
12
"use strict";
13
14
var ExecutionEnvironment = require('ExecutionEnvironment');
15
16
/**
17
* EVENT_NAME_MAP is used to determine which event fired when a
18
* transition/animation ends, based on the style property used to
19
* define that event.
20
*/
21
var EVENT_NAME_MAP = {
22
transitionend: {
23
'transition': 'transitionend',
24
'WebkitTransition': 'webkitTransitionEnd',
25
'MozTransition': 'mozTransitionEnd',
26
'OTransition': 'oTransitionEnd',
27
'msTransition': 'MSTransitionEnd'
28
},
29
30
animationend: {
31
'animation': 'animationend',
32
'WebkitAnimation': 'webkitAnimationEnd',
33
'MozAnimation': 'mozAnimationEnd',
34
'OAnimation': 'oAnimationEnd',
35
'msAnimation': 'MSAnimationEnd'
36
}
37
};
38
39
var endEvents = [];
40
41
function detectEvents() {
42
var testEl = document.createElement('div');
43
var style = testEl.style;
44
45
// On some platforms, in particular some releases of Android 4.x,
46
// the un-prefixed "animation" and "transition" properties are defined on the
47
// style object but the events that fire will still be prefixed, so we need
48
// to check if the un-prefixed events are useable, and if not remove them
49
// from the map
50
if (!('AnimationEvent' in window)) {
51
delete EVENT_NAME_MAP.animationend.animation;
52
}
53
54
if (!('TransitionEvent' in window)) {
55
delete EVENT_NAME_MAP.transitionend.transition;
56
}
57
58
for (var baseEventName in EVENT_NAME_MAP) {
59
var baseEvents = EVENT_NAME_MAP[baseEventName];
60
for (var styleName in baseEvents) {
61
if (styleName in style) {
62
endEvents.push(baseEvents[styleName]);
63
break;
64
}
65
}
66
}
67
}
68
69
if (ExecutionEnvironment.canUseDOM) {
70
detectEvents();
71
}
72
73
// We use the raw {add|remove}EventListener() call because EventListener
74
// does not know how to remove event listeners and we really should
75
// clean up. Also, these events are not triggered in older browsers
76
// so we should be A-OK here.
77
78
function addEventListener(node, eventName, eventListener) {
79
node.addEventListener(eventName, eventListener, false);
80
}
81
82
function removeEventListener(node, eventName, eventListener) {
83
node.removeEventListener(eventName, eventListener, false);
84
}
85
86
var ReactTransitionEvents = {
87
addEndEventListener: function(node, eventListener) {
88
if (endEvents.length === 0) {
89
// If CSS transitions are not supported, trigger an "end animation"
90
// event immediately.
91
window.setTimeout(eventListener, 0);
92
return;
93
}
94
endEvents.forEach(function(endEvent) {
95
addEventListener(node, endEvent, eventListener);
96
});
97
},
98
99
removeEndEventListener: function(node, eventListener) {
100
if (endEvents.length === 0) {
101
return;
102
}
103
endEvents.forEach(function(endEvent) {
104
removeEventListener(node, endEvent, eventListener);
105
});
106
}
107
};
108
109
module.exports = ReactTransitionEvents;
110
111