UNPKG

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