-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (69 loc) · 2.26 KB
/
index.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
'use strict';
var longestTransition = require('longest-transition');
var defaultEventFailureGracePeriod = 100;
// transitionend below based on https://github.com/kubens/transition-utility
var transitionend = (function () {
var transition;
var element;
var transitions;
if (typeof document === 'undefined') {
return;
}
element = document.createElement('fakeelement');
transitions = {
transition: 'transitionend',
OTransition: 'oTransitionEnd',
MozTransition: 'transitionend',
WebkitTransition: 'webkitTransitionEnd',
};
// look for supported transition
for (transition in transitions) {
if (element.style[transition] !== undefined) {
return transitions[transition];
}
}
// transition is unsupported
return false;
})();
module.exports = function (element, options, callback) {
var timeout;
var gracePeriod;
var timeoutTimer;
var longest;
// browser does not support transitionend, no animation
if (!transitionend) {
setTimeout(callback, 0);
return function () {};
}
// handle default parameters
if (typeof options === 'function') {
callback = options;
options = {};
}
// if no timeout provided, infer it
if (!options.timeout) {
longest = longestTransition(element);
if (!longest) {
setTimeout(callback, 0);
return function () {};
}
}
timeout = options.timeout || (longest.duration + longest.delay);
gracePeriod = options.gracePeriod || defaultEventFailureGracePeriod;
element.addEventListener(transitionend, __handleTransitionEnd);
timeoutTimer = setTimeout(__handleTransitionEnd, timeout + gracePeriod);
function __handleTransitionEnd(e) {
// if event timed out or it is on target (respecting the longest transitioning property if necessary)
if (!e || (e.target === element && (!longest || longest.property === e.propertyName))) {
__cleanup();
if (callback) {
callback();
}
}
}
function __cleanup() {
clearTimeout(timeoutTimer);
element.removeEventListener(transitionend, __handleTransitionEnd);
}
return __cleanup;
};