Created
May 15, 2020 16:15
-
-
Save johnnybenson/71e197e7ae3786107394158ccd0ecec1 to your computer and use it in GitHub Desktop.
React Router v5 TransitionGroup CSSTransition Directional Animation with CSS Modules
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
import React from 'react'; | |
import { Route, Switch } from 'react-router-dom'; | |
import { CSSTransition, TransitionGroup } from 'react-transition-group'; | |
import styles from './styles.module.scss'; | |
const TRANSITIONS = { | |
PUSH: { | |
enter: styles.LeftEnter, | |
enterActive: styles.LeftEnterActive, | |
exit: styles.LeftExit, | |
exitActive: styles.LeftExitActive, | |
}, | |
POP: { | |
enter: styles.RightEnter, | |
enterActive: styles.RightEnterActive, | |
exit: styles.RightExit, | |
exitActive: styles.RightExitActive, | |
}, | |
}; | |
const TRANSITION_DURATION = 500; | |
export default function RouteTransition({ children }) { | |
return ( | |
<Route | |
render={({ location, history }) => { | |
return ( | |
<TransitionGroup className={styles.Transition}> | |
<CSSTransition | |
key={location.key} | |
classNames={TRANSITIONS[history.action]} | |
mountOnEnter={true} | |
timeout={TRANSITION_DURATION} | |
className={styles.Transition} | |
> | |
<Switch location={location}>{children}</Switch> | |
</CSSTransition> | |
</TransitionGroup> | |
); | |
}} | |
/> | |
); | |
} |
Author
johnnybenson
commented
May 15, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment