Skip to content

Instantly share code, notes, and snippets.

@sergeysova
Created March 3, 2016 11:13
Show Gist options
  • Save sergeysova/70fb024ff06b61da8b2f to your computer and use it in GitHub Desktop.
Save sergeysova/70fb024ff06b61da8b2f to your computer and use it in GitHub Desktop.
React Router Transition
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>
);
}
<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