Created
July 16, 2018 15:09
-
-
Save stephencookdev/cc7177c8fd0663fc0050a36f0d81ef7c to your computer and use it in GitHub Desktop.
`SwitchWithSlide` example
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
export const animateSwitch = (CustomSwitch, AnimatorComponent) => ({ | |
children, | |
}) => ( | |
<Route | |
render={({ location }) => ( | |
<AnimatorComponent uniqKey={location.pathname}> | |
<CustomSwitch location={location}>{children}</CustomSwitch> | |
</AnimatorComponent> | |
)} | |
/> | |
); |
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 { animateSwitch } from "./animateSwitch"; | |
import { SlideOut } from "./SlideOut"; | |
const SwitchWithSlide = animateSwitch(Switch, SlideOut); | |
export default () => ( | |
<SwitchWithSlide> | |
<Route path="/a" component={PageA} /> | |
<Route path="/b" component={PageB} /> | |
</SwitchWithSlide> | |
); |
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
export class SlideOut extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
childPosition: Slider.CENTER, | |
curChild: props.children, | |
curUniqId: props.uniqId, | |
prevChild: null, | |
prevUniqId: null, | |
animationCallback: null | |
}; | |
} | |
componentDidUpdate(prevProps, prevState) { | |
const prevUniqId = prevProps.uniqKey || prevProps.children.type; | |
const uniqId = this.props.uniqKey || this.props.children.type; | |
if (prevUniqId !== uniqId) { | |
this.setState({ | |
childPosition: Slider.TO_LEFT, | |
curChild: this.props.children, | |
curUniqId: uniqId, | |
prevChild: prevProps.children, | |
prevUniqId, | |
animationCallback: this.swapChildren | |
}); | |
} | |
} | |
swapChildren = () => { | |
this.setState({ | |
childPosition: Slider.FROM_RIGHT, | |
prevChild: null, | |
prevUniqId: null, | |
animationCallback: null | |
}); | |
}; | |
render() { | |
return ( | |
<Slider | |
position={this.state.childPosition} | |
animationCallback={this.state.animationCallback} | |
> | |
{this.state.prevChild || this.state.curChild} | |
</Slider> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment