I was wondering if anyone could give an idea on how to achieve an animated component / page switcher using React.js.
What I would like to achieve is a transition to a component / page, as on http://www.semplicelabs.com/ - a headline that translates opacity and margin-top and content that translates opacity.
When a new component / page is displayed, the currently displayed component / page should go to with the new component / page. What I still present is a LayoutComponent that displays the page component in a CSSTransitionGroup :
import React from 'react/addons'; export default class LayoutComponent extends React.Component { constructor(props) { super(props); this.state = { page: '' }; }, setPage(page) { this.setState({ page }); } render() { return ( <React.addons.CSSTransitionGroup transitionName='page'> {this.state.page} </React.addons.CSSTransitionGroup> ); } }
I also have two page components, FirstPage and SecondPage .
import React from 'react'; export default class FirstPage extends React.Component { render() { return ( <div> <header className='header'> <div className='container'> First Header </div> </header> <div className='content'> <div className='container'> First Content </div> </div> </div> ); } }
The only differences between SecondPage code and FirstPage code are the contents in the div .container and the class name.
What I was trying to do then was add a transition to vacation with a duration of .8s and introduce a transition with a duration and delay of .8s . The main problem that I see is that the new page component is mounted before the transition from the old one ends. This is my current CSS:
.page-enter { background-color: #f2f2f2; transition: background-color .8s linear .8s; } .page-enter-active { background-color: #f2f1f1; } .page-leave { background-color: #f2f1f1; transition: background-color .8s linear; } .page-leave-active { background-color: #f2f2f2; } .page-enter .header { margin-top: -80px; opacity: 0; transition: opacity .8s ease-in-out .8s; transition: margin-top .8s ease-in-out .8s; } .page-enter-active .header { margin-top: 0; opacity: 1; } .page-leave .header { margin-top: 0; opacity: 1; transition: opacity .8s ease-in-out; transition: margin-top .8s ease-in-out; } .page-leave-active .header { margin-top: -80px; opacity: 0; } .page-enter .content { opacity: 0; transition: opacity .8s ease-in-out .8s; } .page-enter-active .content { opacity: 1; } .page-leave .content { opacity: 1; transition: opacity .8s ease-in-out; } .page-leave-active .content { opacity: 0; }
I know that I could animate the initial installation using transitionAppear={true} , but this does not help with the problem of installing a new component before the old one is ported.
This is a problem that I struggled with for several days, and I cannot find a solution.
Some codes for the game: https://jsfiddle.net/gonsfx/xva2g6oo/