forked from reactjs/react-transition-group
/
CSSTransitionGroupChild.js
168 lines (136 loc) · 4.2 KB
/
CSSTransitionGroupChild.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import addClass from 'dom-helpers/class/addClass';
import removeClass from 'dom-helpers/class/removeClass';
import raf from 'dom-helpers/util/requestAnimationFrame';
import { transitionEnd, animationEnd } from 'dom-helpers/transition/properties';
import React from 'react';
import { findDOMNode } from 'react-dom';
import { nameShape } from './utils/PropTypes';
let events = [];
if (transitionEnd) events.push(transitionEnd);
if (animationEnd) events.push(animationEnd);
function addEndListener(node, listener) {
if (events.length) {
events.forEach(e =>
node.addEventListener(e, listener, false));
} else {
setTimeout(listener, 0);
}
return () => {
if (!events.length) return;
events.forEach(e => node.removeEventListener(e, listener, false));
};
}
const propTypes = {
children: React.PropTypes.node,
name: nameShape.isRequired,
// Once we require timeouts to be specified, we can remove the
// boolean flags (appear etc.) and just accept a number
// or a bool for the timeout flags (appearTimeout etc.)
appear: React.PropTypes.bool,
enter: React.PropTypes.bool,
leave: React.PropTypes.bool,
appearTimeout: React.PropTypes.number,
enterTimeout: React.PropTypes.number,
leaveTimeout: React.PropTypes.number,
};
class CSSTransitionGroupChild extends React.Component {
static displayName = 'CSSTransitionGroupChild';
componentWillMount() {
this.classNameAndNodeQueue = [];
this.transitionTimeouts = [];
}
componentWillUnmount() {
this.unmounted = true;
if (this.timeout) {
clearTimeout(this.timeout);
}
this.transitionTimeouts.forEach((timeout) => {
clearTimeout(timeout);
});
this.classNameAndNodeQueue.length = 0;
}
transition(animationType, finishCallback, timeout) {
let node = findDOMNode(this);
if (!node) {
if (finishCallback) {
finishCallback();
}
return;
}
let className = this.props.name[animationType] || this.props.name + '-' + animationType;
let activeClassName = this.props.name[animationType + 'Active'] || className + '-active';
let timer = null;
let removeListeners;
addClass(node, className);
// Need to do this to actually trigger a transition.
this.queueClassAndNode(activeClassName, node);
// Clean-up the animation after the specified delay
let finish = (e) => {
if (e && e.target !== node) {
return;
}
clearTimeout(timer);
if (removeListeners) removeListeners();
removeClass(node, className);
removeClass(node, activeClassName);
if (removeListeners) removeListeners();
// Usually this optional callback is used for informing an owner of
// a leave animation and telling it to remove the child.
if (finishCallback) {
finishCallback();
}
};
if (timeout) {
timer = setTimeout(finish, timeout);
this.transitionTimeouts.push(timer);
} else if (transitionEnd) {
removeListeners = addEndListener(node, finish);
}
}
queueClassAndNode(className, node) {
this.classNameAndNodeQueue.push({
className,
node,
});
if (!this.rafHandle) {
this.rafHandle = raf(() => this.flushClassNameAndNodeQueue());
}
}
flushClassNameAndNodeQueue() {
if (!this.unmounted) {
this.classNameAndNodeQueue.forEach((obj) => {
addClass(obj.node, obj.className);
});
}
this.classNameAndNodeQueue.length = 0;
this.rafHandle = null;
}
componentWillAppear = (done) => {
if (this.props.appear) {
this.transition('appear', done, this.props.appearTimeout);
} else {
done();
}
}
componentWillEnter = (done) => {
if (this.props.enter) {
this.transition('enter', done, this.props.enterTimeout);
} else {
done();
}
}
componentWillLeave = (done) => {
if (this.props.leave) {
this.transition('leave', done, this.props.leaveTimeout);
} else {
done();
}
}
render() {
const props = { ...this.props };
Object.keys(propTypes).forEach(key => delete props[key]);
return React.cloneElement(React.Children.only(this.props.children), props);
}
}
CSSTransitionGroupChild.propTypes = propTypes;
export default CSSTransitionGroupChild;