UNPKG

1.85 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
12
13var getVendorPrefixedEventName = require('react-dom/lib/getVendorPrefixedEventName');
14
15var endEvents = [];
16
17function detectEvents() {
18 var animEnd = getVendorPrefixedEventName('animationend');
19 var transEnd = getVendorPrefixedEventName('transitionend');
20
21 if (animEnd) {
22 endEvents.push(animEnd);
23 }
24
25 if (transEnd) {
26 endEvents.push(transEnd);
27 }
28}
29
30if (ExecutionEnvironment.canUseDOM) {
31 detectEvents();
32}
33
34// We use the raw {add|remove}EventListener() call because EventListener
35// does not know how to remove event listeners and we really should
36// clean up. Also, these events are not triggered in older browsers
37// so we should be A-OK here.
38
39function addEventListener(node, eventName, eventListener) {
40 node.addEventListener(eventName, eventListener, false);
41}
42
43function removeEventListener(node, eventName, eventListener) {
44 node.removeEventListener(eventName, eventListener, false);
45}
46
47var ReactTransitionEvents = {
48 addEndEventListener: function (node, eventListener) {
49 if (endEvents.length === 0) {
50 // If CSS transitions are not supported, trigger an "end animation"
51 // event immediately.
52 window.setTimeout(eventListener, 0);
53 return;
54 }
55 endEvents.forEach(function (endEvent) {
56 addEventListener(node, endEvent, eventListener);
57 });
58 },
59
60 removeEndEventListener: function (node, eventListener) {
61 if (endEvents.length === 0) {
62 return;
63 }
64 endEvents.forEach(function (endEvent) {
65 removeEventListener(node, endEvent, eventListener);
66 });
67 }
68};
69
70module.exports = ReactTransitionEvents;
\No newline at end of file