Skip to content

Instantly share code, notes, and snippets.

@inchr
Forked from zackify/transition.jsx
Created January 22, 2016 00:57
Show Gist options
  • Select an option

  • Save inchr/f30fc3315da1db630101 to your computer and use it in GitHub Desktop.

Select an option

Save inchr/f30fc3315da1db630101 to your computer and use it in GitHub Desktop.
transition in react router demo
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>
)
}
}
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