/
CSSTransition.js
223 lines (188 loc) · 5.71 KB
/
CSSTransition.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as PropTypes from 'prop-types';
import addClass from 'dom-helpers/class/addClass';
import removeClass from 'dom-helpers/class/removeClass';
import React from 'react';
import Transition from './Transition';
import { classNamesShape } from './utils/PropTypes';
const propTypes = {
...Transition.propTypes,
/**
* The animation classNames applied to the component as it enters or exits.
* A single name can be provided and it will be suffixed for each stage: e.g.
*
* `classNames="fade"` applies `fade-enter`, `fade-enter-active`,
* `fade-exit`, `fade-exit-active`, `fade-appear`, and `fade-appear-active`.
* Each individual classNames can also be specified independently like:
*
* ```js
* classNames={{
* appear: 'my-appear',
* appearActive: 'my-active-appear',
* enter: 'my-enter',
* enterActive: 'my-active-enter',
* exit: 'my-exit',
* exitActive: 'my-active-exit',
* }}
* ```
*/
classNames: classNamesShape,
/**
* A `<Transition>` callback fired immediately after the 'enter' or 'appear' class is
* applied.
*
* @type Function(node: HtmlElement, isAppearing: bool)
*/
onEnter: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'enter-active' or
* 'appear-active' class is applied.
*
* @type Function(node: HtmlElement, isAppearing: bool)
*/
onEntering: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'enter' or
* 'appear' classes are **removed** from the DOM node.
*
* @type Function(node: HtmlElement, isAppearing: bool)
*/
onEntered: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'exit' class is
* applied.
*
* @type Function(node: HtmlElement, isAppearing: bool)
*/
onExit: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'exit-active' is
* class is applied.
*
* @type Function(node: HtmlElement, isAppearing: bool)
*/
onExiting: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'exit' classes
* are **removed** from the DOM node.
*
* @type Function(node: HtmlElement, isAppearing: bool)
*/
onExited: PropTypes.func,
};
/**
* A `Transition` component using CSS transitions and animations.
* It's inspired by the excellent [ng-animate](http://www.nganimate.org/) libary.
*
* `CSSTransition` applies a pair of class names during the `appear`, `enter`,
* and `exit` stages of the transition. The first class is applied and then a
* second "active" class in order to activate the css animation.
*
* When the `in` prop is toggled to `true` the Component will get
* the `example-enter` CSS class and the `example-enter-active` CSS class
* added in the next tick. This is a convention based on the `classNames` prop.
*
* ```css
* .example-enter {
* opacity: 0.01;
* }
*
* .example-enter.example-enter-active {
* opacity: 1;
* transition: opacity 500ms ease-in;
* }
*
* .example-leave {
* opacity: 1;
* }
*
* .example-leave.example-leave-active {
* opacity: 0.01;
* transition: opacity 300ms ease-in;
* }
* ```
*/
class CSSTransition extends React.Component {
onEnter = (node, appearing) => {
const { className } = this.getClassNames(appearing ? 'appear' : 'enter')
this.removeClasses(node, 'exit');
addClass(node, className)
if (this.props.onEnter) {
this.props.onEnter(node)
}
}
onEntering = (node, appearing) => {
const { activeClassName } = this.getClassNames(
appearing ? 'appear' : 'enter'
);
this.reflowAndAddClass(node, activeClassName)
if (this.props.onEntering) {
this.props.onEntering(node)
}
}
onEntered = (node, appearing) => {
this.removeClasses(node, appearing ? 'appear' : 'enter');
if (this.props.onEntered) {
this.props.onEntered(node)
}
}
onExit = (node) => {
const { className } = this.getClassNames('exit')
this.removeClasses(node, 'appear');
this.removeClasses(node, 'exit');
addClass(node, className)
if (this.props.onExit) {
this.props.onExit(node)
}
}
onExiting = (node) => {
const { activeClassName } = this.getClassNames('exit')
this.reflowAndAddClass(node, activeClassName)
if (this.props.onExiting) {
this.props.onExiting(node)
}
}
onExited = (node) => {
this.removeClasses(node, 'exit');
if (this.props.onExited) {
this.props.onExited(node)
}
}
getClassNames = (type) => {
const { classNames } = this.props
let className = typeof classNames !== 'string' ?
classNames[type] : classNames + '-' + type;
let activeClassName = typeof classNames !== 'string' ?
classNames[type + 'Active'] : className + '-active';
return { className, activeClassName }
}
removeClasses(node, type) {
const { className, activeClassName } = this.getClassNames(type)
className && removeClass(node, className);
activeClassName && removeClass(node, activeClassName);
}
reflowAndAddClass(node, className) {
// This is for to force a repaint,
// which is necessary in order to transition styles when adding a class name.
/* eslint-disable no-unused-expressions */
node.scrollTop;
/* eslint-enable no-unused-expressions */
addClass(node, className);
}
render() {
const props = { ...this.props };
Object.keys(propTypes).forEach(key => delete props[key]);
return (
<Transition
{...props}
onEnter={this.onEnter}
onEntered={this.onEntered}
onEntering={this.onEntering}
onExit={this.onExit}
onExiting={this.onExiting}
onExited={this.onExited}
/>
);
}
}
CSSTransition.propTypes = propTypes;
export default CSSTransition