Created
March 3, 2016 11:13
-
-
Save sergeysova/70fb024ff06b61da8b2f to your computer and use it in GitHub Desktop.
React Router Transition
This file contains 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 {TransitionMotion, spring} from 'react-motion'; | |
const willEnter = children => ({children, opacity: spring(0), translate: spring(999)}); | |
const willLeave = (key, {children}) => ({children, opacity: spring(0), translate: spring(-999)}); | |
const getStyles = (children, pathname) => ({[pathname]: {children, opacity: spring(1), translate: spring(0)}}); | |
export default function RouteTransition({children, pathname}) { | |
return ( | |
<TransitionMotion | |
styles={getStyles(children, pathname)} | |
willEnter={willEnter} | |
willLeave={willLeave} | |
> | |
{interpolated => | |
<div> | |
{Object.keys(interpolated).map(key => | |
<div | |
key={`${key}-transition`} | |
style={{ | |
height: '100%', | |
width: '100%', | |
position: 'absolute', | |
opacity: interpolated[key].opacity, | |
transform: `translateX(${interpolated[key].translate}px)` | |
}} | |
> | |
{interpolated[key].children} | |
</div> | |
)} | |
</div> | |
} | |
</TransitionMotion> | |
); | |
} |
This file contains 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
<RouteTransition pathname={this.props.location.pathname} style={{visibility: 'hidden', width: '100%', height: '100%'}}> | |
{this.props.children} | |
</RouteTransition> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment