This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFadeMixin.js
More file actions
69 lines (57 loc) · 1.77 KB
/
FadeMixin.js
File metadata and controls
69 lines (57 loc) · 1.77 KB
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
/*global document */
// TODO: listen for onTransitionEnd to remove el
function getElementsAndSelf (root, classes){
var els = root.querySelectorAll('.' + classes.join('.'));
els = [].map.call(els, function(e){ return e; });
for(var i = 0; i < classes.length; i++){
if( !root.className.match(new RegExp('\\b' + classes[i] + '\\b'))){
return els;
}
}
els.unshift(root);
return els;
}
module.exports = {
_fadeIn: function () {
var els;
if (this.isMounted()) {
els = getElementsAndSelf(this.getDOMNode(), ['fade']);
if (els.length) {
els.forEach(function (el) {
el.className += ' in';
});
}
}
},
_fadeOut: function () {
var els = getElementsAndSelf(this._fadeOutEl, ['fade', 'in']);
if (els.length) {
els.forEach(function (el) {
el.className = el.className.replace(/\bin\b/, '');
});
}
setTimeout(this._handleFadeOutEnd, 300);
},
_handleFadeOutEnd: function () {
if (this._fadeOutEl && this._fadeOutEl.parentNode) {
this._fadeOutEl.parentNode.removeChild(this._fadeOutEl);
}
},
componentDidMount: function () {
if (document.querySelectorAll) {
// Firefox needs delay for transition to be triggered
setTimeout(this._fadeIn, 20);
}
},
componentWillUnmount: function () {
var els = getElementsAndSelf(this.getDOMNode(), ['fade']),
container = (this.props.container && this.props.container.getDOMNode()) || document.body;
if (els.length) {
this._fadeOutEl = document.createElement('div');
container.appendChild(this._fadeOutEl);
this._fadeOutEl.appendChild(this.getDOMNode().cloneNode(true));
// Firefox needs delay for transition to be triggered
setTimeout(this._fadeOut, 20);
}
}
};