React Router Back Swipe Gesture Animation
Hey there,
Love React Router v4. You guys are doing a killer job. I have a question about ReactTransitionGroup's animations with React Router. I have created a successful implementation for back and fourth page sliding between routes. Here is the logic:
If the user is on the navigation page and clicks home, home will slide in from the right. If they click back or the navigation button, the navigation page will slide back in from the left. This handles well with back/forward browser buttons with the exception of swipe gestures on mobile.
Issue: The user will pull the page from the left to to the right which reveals the previous route's component. When the user finishes the swipe gesture, the previous route's component will begin to mount causing the ReactTransitionGroup to initiate the respective animation.
Example User is on home page and swipes from left to right to go back to navigation page. In the middle of the swiping process, the user can see the navigation page in the background. Once the swiping process is completed, a new route is initiated and the navigation page starts a new animation.
I am wondering if there is a way to implement my routing transitions so that this does not occur. Further, I cant seem to find a way to distinguish the difference between a backward/forward button browser click. I need to distinguish between these to decide which animation to perform.
Steps to reproduce
Head to dailenspencer.com on mobile Safari Click on a navigation page such as "home" Perform a left -> right swipe gesture
Expected Behavior
When performing a swipe gesture, animation should not reinitiate.
Actual Behavior
When performing a swipe gesture, animations reinitiates.
Main.jsx
import React from 'react'
import ReactDOM from 'react-dom'
import {Link, Route, Switch} from 'react-router-dom';
import TransitionGroup from 'react-transition-group/TransitionGroup';
import HomePage from 'components/Pages/Home/home.page';
import NavigationPage from 'components/Pages/Navigation/navigation.page';
import ProjectGridPage from 'components/Pages/ProjectGrid/projectGrid.page';
import ProjectContentPage from 'components/Pages/ContentPage/content.page';
import SubscribePage from 'components/Pages/Subscribe/subscribe.page';
import AboutPage from 'components/Pages/About/about.page';
import ErrorPage from 'components/Pages/Error/error.page';
import NavigationButton from 'components/NavigationButton';
import {initializeApplication} from 'lib/Helpers/Helpers';
const firstChild = props => {
const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null;
};
export default class Main extends React.Component {
constructor(props){
super(props)
this.state = {
componentIsLoading: true
}
}
componentDidMount() {
// initializeApplication()
}
handleClick () {
this.context.router.history.push('/navigation')
}
render() {
return (
<div id="Home">
<NavigationButton/>
<Route
exact
path="/"
children={({ match, ...rest }) => (
<TransitionGroup component={firstChild}>
{match && <Home {...rest} />}
</TransitionGroup>
)}/>
<Route
path="/navigation"
children={({ match, ...rest }) => (
<TransitionGroup navigationAction={this.context.router.history} component={firstChild}>
{match && <Navigation {...rest} />}
</TransitionGroup>
)}/>
<Route
exact
path="/projects"
children={({ match, ...rest }) => (
<TransitionGroup component={firstChild}>
{match && <ProjectGrid {...rest}/>}
</TransitionGroup>
)}/>
<Route
path="/projects/:id"
children={({ match, ...rest }) => (
<TransitionGroup component={firstChild}>
{match && <ProjectContent {...rest} projectID={match.params.id} />}
</TransitionGroup>
)}/>
<Route
path="/subscribe"
children={({ match, ...rest }) => (
<TransitionGroup component={firstChild}>
{match && <Subscribe {...rest} />}
</TransitionGroup>
)}/>
<Route
path="/about"
children={({ match, ...rest }) => (
<TransitionGroup component={firstChild}>
{match && <About {...rest} />}
</TransitionGroup>
)}/>
</div>
);
}
}
Main.contextTypes = {
router: React.PropTypes.object.isRequired
};
const AnimatedWrapper = (WrappedComponent, ComponentSelector) => class AnimatedWrapper
extends React.Component {
constructor(props){
super(props)
this.state = {
windowWidth: $(window).width(),
}
}
componentWillEnter(callback) {
if (this.props.history.action == 'POP' || this.props.location.pathname == '/navigation') {
$(ComponentSelector).css({
'z-index':'10',
'-webkit-transform': 'translateX(-' + this.state.windowWidth + 'px)',
'-moz-transform': 'translateX(-' + this.state.windowWidth + 'px)',
'-o-transform': 'translateX(-' + this.state.windowWidth + 'px)',
'-ms-transform': 'translateX(-' + this.state.windowWidth + 'px)',
'transform': 'translateX(-' + this.state.windowWidth + 'px)'
})
} else {
$(ComponentSelector).css({
'z-index':'10',
'-webkit-transform': 'translateX(' + this.state.windowWidth + 'px)',
'-moz-transform': 'translateX(' + this.state.windowWidth + 'px)',
'-o-transform': 'translateX(' + this.state.windowWidth + 'px)',
'-ms-transform': 'translateX(' + this.state.windowWidth + 'px)',
'transform': 'translateX(' + this.state.windowWidth + 'px)'
})
}
callback();
}
componentDidEnter () {
$(ComponentSelector).addClass('transitioning');
if (this.props.history.action == 'POP' || this.props.location.pathname == '/navigation') {
$(ComponentSelector).velocity({
translateX: [0,-this.state.windowWidth],
}, {
duration: 700,
easing: [ 0.97, 0.01, 0.71, 0.76 ],
complete: function () {
setTimeout(() => {
$(ComponentSelector).removeClass('transitioning');
$(ComponentSelector).css({
'z-index':'9',
})
},150)
}
})
} else {
$(ComponentSelector).velocity({
translateX: [0,this.state.windowWidth],
}, {
duration: 700,
easing: [ 0.97, 0.01, 0.71, 0.76 ],
complete: function () {
setTimeout(() => {
$(ComponentSelector).removeClass('transitioning');
$(ComponentSelector).css({
'z-index':'9',
})
},150)
}
});
}
}
componentWillLeave(callback) {
setTimeout(() => callback(), 800);
}
render () {
return (
<div className='animated-page-wrapper'>
<WrappedComponent {...this.props} />
</div>
)
}
}
class HomeComponent extends React.Component {
render () {
return (
<div className="page-slider-slide page-active home">
<HomePage/>
</div>
)
}
}
const HomeComponentSelector = '.page-slider-slide.page-active.home'
const Home = AnimatedWrapper(HomeComponent, HomeComponentSelector, 'right');
class NavigationComponent extends React.Component {
render() {
return (
<div className="page-slider-slide page-active navigation">
<NavigationPage/>
</div>
)
}
}
const NavigationComponentSelector = '.page-slider-slide.page-active.navigation'
const Navigation = AnimatedWrapper(NavigationComponent, NavigationComponentSelector, 'left');
class ProjectGridComponent extends React.Component {
render() {
return (
<div className="page-slider-slide page-active projects">
<ProjectGridPage/>
</div>
)
}
}
const ProjectsGridComponentSelector = '.page-slider-slide.page-active.projects'
const ProjectGrid = AnimatedWrapper(ProjectGridComponent, ProjectsGridComponentSelector, 'right');
class ProjectContentComponent extends React.Component {
render() {
return (
<div className="page-slider-slide page-active project-content">
<ProjectContentPage {...this.props}/>
</div>
)
}
}
const ProjectContentComponentSelector = '.page-slider-slide.page-active.project-content'
const ProjectContent = AnimatedWrapper(ProjectContentComponent, ProjectContentComponentSelector, 'right');
class SubscribeComponent extends React.Component {
render () {
return (
<div className="page-slider-slide page-active subscribe">
<SubscribePage/>
</div>
)
}
}
const SubscribeComponentSelector = '.page-slider-slide.page-active.subscribe'
const Subscribe = AnimatedWrapper(SubscribeComponent, SubscribeComponentSelector, 'right');
class AboutComponent extends React.Component {
render() {
return (
<div className="page-slider-slide page-active about">
<AboutPage/>
</div>
)
}
}
const AboutComponentSelector = '.page-slider-slide.page-active.about';
const About = AnimatedWrapper(AboutComponent, AboutComponentSelector, 'right');
class ErrorComponent extends React.Component {
render() {
console.log('here');
return (
<div className="page-slider-slide page-active error">
<ErrorPage/>
</div>
)
}
}
const ErrorComponentSelector = '.page-slider-slide.page-active.error';
const Error = AnimatedWrapper(ErrorComponent, ErrorComponentSelector, 'right');
This is a bug tracker, not a support system. For usage questions, please use Stack Overflow or Reactiflux. Thanks!