Skip to content

Commit 4c5ba98

Browse files
authoredMay 30, 2019
feat: add global transition disable switch (#506)
1 parent dc9752c commit 4c5ba98

File tree

6 files changed

+98
-18
lines changed

6 files changed

+98
-18
lines changed
 

Diff for: ‎src/Transition.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as PropTypes from 'prop-types'
22
import React from 'react'
33
import ReactDOM from 'react-dom'
44

5+
import config from './config'
56
import { timeoutsShape } from './utils/PropTypes'
67
import TransitionGroupContext from './TransitionGroupContext'
78

@@ -223,15 +224,13 @@ class Transition extends React.Component {
223224

224225
performEnter(node, mounting) {
225226
const { enter } = this.props
226-
const appearing = this.context
227-
? this.context.isMounting
228-
: mounting
227+
const appearing = this.context ? this.context.isMounting : mounting
229228

230229
const timeouts = this.getTimeouts()
231230
const enterTimeout = appearing ? timeouts.appear : timeouts.enter
232231
// no enter animation skip right to ENTERED
233232
// if we are mounting and running this it means appear _must_ be set
234-
if (!mounting && !enter) {
233+
if ((!mounting && !enter) || config.disabled) {
235234
this.safeSetState({ status: ENTERED }, () => {
236235
this.props.onEntered(node)
237236
})
@@ -256,7 +255,7 @@ class Transition extends React.Component {
256255
const timeouts = this.getTimeouts()
257256

258257
// no exit animation skip right to EXITED
259-
if (!exit) {
258+
if (!exit || config.disabled) {
260259
this.safeSetState({ status: EXITED }, () => {
261260
this.props.onExited(node)
262261
})
@@ -312,7 +311,8 @@ class Transition extends React.Component {
312311
onTransitionEnd(node, timeout, handler) {
313312
this.setNextCallback(handler)
314313

315-
const doesNotHaveTimeoutOrListener = timeout == null && !this.props.addEndListener
314+
const doesNotHaveTimeoutOrListener =
315+
timeout == null && !this.props.addEndListener
316316
if (!node || doesNotHaveTimeoutOrListener) {
317317
setTimeout(this.nextCallback, 0)
318318
return

Diff for: ‎src/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
disabled: false,
3+
}

Diff for: ‎src/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export { default as CSSTransition } from './CSSTransition';
2-
export { default as ReplaceTransition } from './ReplaceTransition';
3-
export { default as TransitionGroup } from './TransitionGroup';
4-
export { default as Transition } from './Transition';
1+
export { default as CSSTransition } from './CSSTransition'
2+
export { default as ReplaceTransition } from './ReplaceTransition'
3+
export { default as TransitionGroup } from './TransitionGroup'
4+
export { default as Transition } from './Transition'
5+
export { default as config } from './config'

Diff for: ‎stories/Transition.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import React from 'react';
2-
import { storiesOf } from '@storybook/react';
1+
import React from 'react'
2+
import { storiesOf } from '@storybook/react'
33

44
import { Fade, Collapse } from './transitions/Bootstrap'
5-
import StoryFixture from './StoryFixture';
5+
import StoryFixture from './StoryFixture'
66

7+
import { config } from '../src/index'
8+
9+
config.disabled = true
710

811
class ToggleFixture extends React.Component {
912
state = { show: this.props.defaultIn }
@@ -12,15 +15,17 @@ class ToggleFixture extends React.Component {
1215
<StoryFixture description={this.props.description}>
1316
<div style={{ marginBottom: 10 }}>
1417
<button
15-
onClick={() => this.setState(({ show }) => ({
16-
show: !show
17-
}))}
18+
onClick={() =>
19+
this.setState(({ show }) => ({
20+
show: !show,
21+
}))
22+
}
1823
>
1924
Toggle
2025
</button>
2126
</div>
2227
{React.cloneElement(this.props.children, {
23-
in: this.state.show
28+
in: this.state.show,
2429
})}
2530
</StoryFixture>
2631
)
@@ -45,4 +50,4 @@ storiesOf('Transition', module)
4550
</div>
4651
</Collapse>
4752
</ToggleFixture>
48-
));
53+
))

Diff for: ‎www/src/components/Layout.js

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const Layout = ({ data, children }) => (
5050
<Nav.Link as={Link} to="/with-react-router" activeClassName="active">
5151
With React Router
5252
</Nav.Link>
53+
<Nav.Link as={Link} to="/testing" activeClassName="active">
54+
Testing
55+
</Nav.Link>
5356
</Nav>
5457
</Navbar.Collapse>
5558
</Navbar>

Diff for: ‎www/src/pages/testing.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { graphql } from 'gatsby';
2+
import PropTypes from 'prop-types';
3+
import React from 'react';
4+
import { Container } from 'react-bootstrap';
5+
import Layout from '../components/Layout';
6+
import Example from '../components/Example';
7+
8+
const propTypes = {
9+
location: PropTypes.object.isRequired,
10+
data: PropTypes.shape({
11+
site: PropTypes.shape({
12+
siteMetadata: PropTypes.shape({
13+
componentPages: PropTypes.arrayOf(
14+
PropTypes.shape({
15+
path: PropTypes.string.isRequired,
16+
displayName: PropTypes.string.isRequired,
17+
})
18+
).isRequired,
19+
}).isRequired,
20+
}).isRequired,
21+
}).isRequired,
22+
};
23+
24+
const Testing = ({ data, location }) => (
25+
<Layout data={data} location={location}>
26+
<Container>
27+
<h1>Testing Components with Transitions</h1>
28+
<p>
29+
In some situations, like visual snapshot testing, it's helpful to
30+
disable transitions so they don't complicate the test, or introduce
31+
abitrary waits. To make this easier <code>react-transition-group</code>{' '}
32+
exposes a way to globally toggle transitions. When set,{' '}
33+
<strong>all</strong> transitions, when toggled, will immediately switch
34+
to their entered or exited states as appropriate.
35+
</p>
36+
<pre className="language-js">
37+
<code>
38+
{`
39+
import { config } from 'react-transition-group
40+
41+
config.disabled = true
42+
`.trim()}
43+
</code>
44+
</pre>
45+
<blockquote>
46+
<p>
47+
<b>Note</b>: This <strong>does not</strong> automatically disable
48+
animations. It only disabled waits in <code>Transition</code>. You may
49+
also have to disable animation as appropriate for the library.
50+
example:{' '}
51+
<a href="http://velocityjs.org/#mock">Mocking in Velocity.js</a>
52+
</p>
53+
</blockquote>
54+
</Container>
55+
</Layout>
56+
);
57+
58+
Testing.propTypes = propTypes;
59+
60+
export default Testing;
61+
62+
export const pageQuery = graphql`
63+
query TestingQuery {
64+
site {
65+
...Layout_site
66+
}
67+
}
68+
`;

0 commit comments

Comments
 (0)
Please sign in to comment.