-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtransition.js
101 lines (87 loc) · 2.49 KB
/
transition.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
var React = require('react');
var D = React.DOM;
var findDOMNode = require('react-dom').findDOMNode
var createReactClass = require('create-react-class');
var Transition = createReactClass({
getInitialState: function() {
return {
in: (
this.props.out ?
true :
!this.props.animateEntrance
)
};
},
componentDidMount: function() {
this._timeout = raf(this.tada);
},
componentWillUnmount: function() {
caf(this._timeout);
},
tada: function() {
// Force element reflow to ensure correct animation in FF.
findDOMNode(this).offsetWidth;
this.setState({
in: true
});
},
getAppearance: function() {
var transform = this.getTransform();
return {
display: 'inline-block',
// Can't dynamically change `position` from `absolute` to `static` -
// it will break transition animation in Safari.
position: 'absolute',
left: 0,
WebkitTransition: '-webkit-transform 0.2s, opacity 0.2s',
transition: 'transform 0.2s, opacity 0.2s',
WebkitTransform: transform,
transform: transform,
opacity: (
this.isHidden() ?
0 :
1
),
pointerEvents: 'none'
};
},
getTransform: function() {
if (this.props.out) {
return translateY(this.props.goingUp);
}
if (!this.state.in) {
return translateY(!this.props.goingUp);
}
// This has better text rendering in FF than simply `none`.
return 'translateY(0) translateZ(0)';
},
isHidden: function() {
return this.props.out || !this.state.in;
},
render: function() {
return D.span(
{
style: this.getAppearance()
},
this.props.value
);
}
});
module.exports = Transition;
function raf(fn) {
if (typeof window.requestAnimationFrame === 'function') {
return window.requestAnimationFrame(fn);
} else {
return setTimeout(fn, 0);
}
}
function caf(timer) {
if (typeof window.cancelAnimationFrame === 'function') {
return window.cancelAnimationFrame(timer);
} else {
return clearTimeout(timer);
}
}
function translateY(isUp) {
return 'translateY(' + (isUp ? '-' : '') + '60%) translateZ(0)';
}