Last active
September 12, 2018 23:56
-
-
Save mhaagens/61f88e3fbfddbe2c00708f3ebd099be4 to your computer and use it in GitHub Desktop.
Transition Motion React Router 4 Route Transition
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
const FadeRoute = ({ component: Component, ...rest }) => { | |
return ( | |
<Route {...rest} children={({ location, match }) => ( | |
<TransitionMotion | |
willEnter={() => {return {opacity: 0, translateX: 24}}} | |
willLeave={() => {return {opacity: spring(0, animationPresets.out), translateX: spring(24)}}} | |
defaultStyles={[ { | |
key: location.pathname, | |
style: { opacity: 0, translateX: 24}, | |
data: match | |
} ]} | |
styles={match ? [ { | |
key: location.pathname, | |
style: { opacity: spring(1, animationPresets.in), translateX: spring(0) }, | |
data: match | |
} ] : []} | |
> | |
{interpolatedStyles => ( | |
<div className="route"> | |
{interpolatedStyles.map(config => ( | |
<div | |
className="page" | |
key={config.key} | |
style={{opacity: `${config.style.opacity}`, transform: `translateX(-${config.style.translateX}px)`}} | |
> | |
<Component /> | |
</div> | |
))} | |
</div> | |
)} | |
</TransitionMotion> | |
)}/> | |
) | |
} | |
export default FadeRoute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great! I've been looking all over for a transition example using React Router 4. Thanks!