-
Notifications
You must be signed in to change notification settings - Fork 651
/
CSSTransition.js
240 lines (198 loc) · 6.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import * as PropTypes from 'prop-types';
import addOneClass from 'dom-helpers/class/addClass';
import removeOneClass from 'dom-helpers/class/removeClass';
import React from 'react';
import Transition from './Transition';
import { classNamesShape } from './utils/PropTypes';
const addClass = (node, classes) => node && classes && classes.split(' ').forEach(c => addOneClass(node, c));
const removeClass = (node, classes) => node && classes && classes.split(' ').forEach(c => removeOneClass(node, c));
const propTypes = {
...Transition.propTypes,
/**
* The animation classNames applied to the component as it enters, exits or has finished the transition.
* 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-enter-done`,
* `fade-exit`, `fade-exit-active`, `fade-exit-done`, `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',
* enterDone: 'my-done-enter',
* exit: 'my-exit',
* exitActive: 'my-active-exit',
* exitDone: 'my-done-exit',
* }}
* ```
*
* @type {string | {
* appear?: string,
* appearActive?: string,
* enter?: string,
* enterActive?: string,
* enterDone?: string,
* exit?: string,
* exitActive?: string,
* exitDone?: string,
* }}
*/
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** and the `done` class is added to 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)
*/
onExit: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'exit-active' is applied.
*
* @type Function(node: HtmlElement
*/
onExiting: PropTypes.func,
/**
* A `<Transition>` callback fired immediately after the 'exit' classes
* are **removed** and the `exit-done` class is added to the DOM node.
*
* @type Function(node: HtmlElement)
*/
onExited: PropTypes.func,
};
/**
* A `Transition` component using CSS transitions and animations.
* It's inspired by the excellent [ng-animate](http://www.nganimate.org/) library.
*
* `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. After the animation,
* matching `done` class names are applied to persist the animation state.
*
* 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.
*
* ## Example
*
* <iframe src="https://codesandbox.io/embed/m77l2vp00x?fontsize=14" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>
*/
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) => {
const { doneClassName } = this.getClassNames('enter');
this.removeClasses(node, appearing ? 'appear' : 'enter');
addClass(node, doneClassName);
if (this.props.onEntered) {
this.props.onEntered(node)
}
}
onExit = (node) => {
const { className } = this.getClassNames('exit')
this.removeClasses(node, 'appear');
this.removeClasses(node, 'enter');
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) => {
const { doneClassName } = this.getClassNames('exit');
this.removeClasses(node, 'exit');
addClass(node, doneClassName);
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';
let doneClassName = typeof classNames !== 'string' ?
classNames[type + 'Done'] : className + '-done';
return {
className,
activeClassName,
doneClassName
};
}
removeClasses(node, type) {
const { className, activeClassName, doneClassName } = this.getClassNames(type)
className && removeClass(node, className);
activeClassName && removeClass(node, activeClassName);
doneClassName && removeClass(node, doneClassName);
}
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 && node.scrollTop;
/* eslint-enable no-unused-expressions */
addClass(node, className);
}
render() {
const props = { ...this.props };
delete props.classNames;
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