-
-
Save inchr/f30fc3315da1db630101 to your computer and use it in GitHub Desktop.
transition in react router demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react' | |
| import TransitionItem from './transition' | |
| export default class Transition extends React.Component{ | |
| render(){ | |
| let { children, ...otherProps } = this.props | |
| return ( | |
| <div> | |
| {React.Children.map(children, child => <TransitionItem {...otherProps}>{child}</TransitionItem>)} | |
| </div> | |
| ) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react' | |
| export default class TransitionItem extends React.Component { | |
| static contextTypes = { | |
| router: React.PropTypes.object | |
| }; | |
| constructor(props){ | |
| super() | |
| let opacity = 1 | |
| if(props.in !== false){ | |
| if(props.location.action == 'PUSH') opacity = 0.5 | |
| else if(process.CLIENT && props.location.action == 'POP') opacity = 0.5 | |
| } | |
| this.state = { | |
| opacity | |
| } | |
| } | |
| componentDidMount() { | |
| if(this.state.opacity == 0.5) this.fadeIn() | |
| if(this.props.out !== false) this.context.router.setRouteLeaveHook(this.props.route, this.routerWillLeave.bind(this)) | |
| } | |
| routerWillLeave() { | |
| this.setState({opacity: 0.5}) | |
| } | |
| fadeIn() { | |
| setTimeout(() => this.setState({opacity: 1}),50) | |
| } | |
| render(){ | |
| let style = { | |
| transition: 'all 500ms ease-in-out', | |
| ...this.state | |
| } | |
| return ( | |
| <div style={style}> | |
| {this.props.children} | |
| </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment