Forked from mhaagens/gist:61f88e3fbfddbe2c00708f3ebd099be4
Last active
January 2, 2018 18:03
-
-
Save shivekkhurana/8126b4cf4c7b8dbaa18612ca4c3091fa to your computer and use it in GitHub Desktop.
Transition Motion React Router 4 Route 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 {Route} from 'react-router-dom'; | |
import PropTypes from 'prop-types'; | |
import {TransitionMotion, spring} from 'react-motion'; | |
const FadeRoute = ({component: Component, ...rest }) => { | |
return (<Route {...rest} children={({location, match}) => ( | |
<TransitionMotion | |
willEnter={() => ({opacity: 0})} | |
willLeave={() => ({opacity: spring(0)})} | |
styles={match ? [{ | |
key: location.pathname, | |
style: {opacity: spring(1)}, | |
data: {match, location} | |
}] : []} | |
> | |
{interpolatedStyles => ( | |
<div className="route"> | |
{interpolatedStyles.map(({key, style, data}) => ( | |
<div | |
className="page" | |
key={key} | |
style={style} | |
> | |
<Component {...data} /> | |
</div> | |
))} | |
</div> | |
)} | |
</TransitionMotion> | |
)} />); | |
}; | |
FadeRoute.propTypes = { | |
component: PropTypes.node | |
}; | |
export default FadeRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment