Skip to content

Commit 33696d2

Browse files
committed
Merge pull request #2454 from rackt/fix-animations-example
Fix animations examples
2 parents a85c3f7 + a6b6374 commit 33696d2

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

examples/animations/app.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import { render } from 'react-dom'
33
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
4+
import StaticContainer from 'react-static-container'
45
import { createHistory, useBasename } from 'history'
56
import { Router, Route, Link } from 'react-router'
67

@@ -10,23 +11,62 @@ const history = useBasename(createHistory)({
1011
basename: '/animations'
1112
})
1213

13-
class App extends React.Component {
14+
class RouteCSSTransitionGroup extends React.Component {
15+
static contextTypes = {
16+
location: React.PropTypes.object
17+
}
18+
19+
constructor(props, context) {
20+
super(props, context)
21+
22+
this.state = {
23+
previousPathname: null
24+
}
25+
}
26+
27+
componentWillReceiveProps(nextProps, nextContext) {
28+
if (nextContext.location.pathname !== this.context.location.pathname) {
29+
this.setState({ previousPathname: this.context.location.pathname })
30+
}
31+
}
32+
1433
render() {
15-
const { pathname } = this.props.location
34+
const { children, ...props } = this.props
35+
const { previousPathname } = this.state
36+
37+
return (
38+
<ReactCSSTransitionGroup {...props}>
39+
<StaticContainer
40+
key={previousPathname || this.context.location.pathname}
41+
shouldUpdate={!previousPathname}
42+
>
43+
{children}
44+
</StaticContainer>
45+
</ReactCSSTransitionGroup>
46+
)
47+
}
1648

49+
componentDidUpdate() {
50+
if (this.state.previousPathname) {
51+
this.setState({ previousPathname: null })
52+
}
53+
}
54+
}
55+
56+
class App extends React.Component {
57+
render() {
1758
return (
1859
<div>
1960
<ul>
2061
<li><Link to="/page1">Page 1</Link></li>
2162
<li><Link to="/page2">Page 2</Link></li>
2263
</ul>
23-
<ReactCSSTransitionGroup component="div"
24-
transitionName="example"
25-
transitionEnterTimeout={500}
26-
transitionLeaveTimeout={500}
64+
<RouteCSSTransitionGroup
65+
component="div" transitionName="example"
66+
transitionEnterTimeout={500} transitionLeaveTimeout={500}
2767
>
28-
{React.cloneElement(this.props.children || <div />, { key: pathname })}
29-
</ReactCSSTransitionGroup>
68+
{this.props.children}
69+
</RouteCSSTransitionGroup>
3070
</div>
3171
)
3272
}

examples/nested-animations/app.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class App extends React.Component {
2323
<li><Link to="/page1">Page 1</Link></li>
2424
<li><Link to="/page2">Page 2</Link></li>
2525
</ul>
26-
<ReactCSSTransitionGroup component="div" transitionName="swap">
26+
<ReactCSSTransitionGroup
27+
component="div" transitionName="swap"
28+
transitionEnterTimeout={500} transitionLeaveTimeout={500}
29+
>
2730
{React.cloneElement(this.props.children || <div />, { key: key })}
2831
</ReactCSSTransitionGroup>
2932
</div>
@@ -42,7 +45,10 @@ class Page1 extends React.Component {
4245
<li><Link to="/page1/tab1">Tab 1</Link></li>
4346
<li><Link to="/page1/tab2">Tab 2</Link></li>
4447
</ul>
45-
<ReactCSSTransitionGroup component="div" transitionName="example">
48+
<ReactCSSTransitionGroup
49+
component="div" transitionName="example"
50+
transitionEnterTimeout={500} transitionLeaveTimeout={500}
51+
>
4652
{React.cloneElement(this.props.children || <div/>, { key: pathname })}
4753
</ReactCSSTransitionGroup>
4854
</div>
@@ -61,7 +67,10 @@ class Page2 extends React.Component {
6167
<li><Link to="/page2/tab1">Tab 1</Link></li>
6268
<li><Link to="/page2/tab2">Tab 2</Link></li>
6369
</ul>
64-
<ReactCSSTransitionGroup component="div" transitionName="example">
70+
<ReactCSSTransitionGroup
71+
component="div" transitionName="example"
72+
transitionEnterTimeout={500} transitionLeaveTimeout={500}
73+
>
6574
{React.cloneElement(this.props.children || <div/>, { key: pathname })}
6675
</ReactCSSTransitionGroup>
6776
</div>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"react-addons-css-transition-group": "^0.14.0",
6666
"react-addons-test-utils": "0.14.0",
6767
"react-dom": "^0.14.0",
68+
"react-static-container": "^1.0.0",
6869
"rf-changelog": "^0.4.0",
6970
"style-loader": "^0.12.4",
7071
"webpack": "^1.4.13",

0 commit comments

Comments
 (0)