• This repository has been archived on 19/Nov/2020
  • Stars
    star
    542
  • Rank 81,982 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Highly customizable page transition component for your React Router

React Router Page Transition

Highly customizable page transition component for your React Router

Currently does not fully support react-router 4, see Using with React Router 4.

Introduction

React Router is awesome, but doing transition between pages is hard, especially for complex ones.

No worries react-router-page-transition is here to help. You can use css to define your own transition effect with custom data and callbacks, so now you can apply cool technique like FLIP your animation and implement cool transitions like this:

Live demo: https://trungdq88.github.io/react-router-page-transition/

Simple Material Reveal
rrpt-leave material reveal

Installation

npm install react-router-page-transition --save

Add to your project

import PageTransition from 'react-router-page-transition';
<PageTransition>
  {this.props.children}
</PageTransition>
  • Important: this.props.children must have transition-item class in its root element. Example if you are passing <ListPage /> as this.props.children:
export default class ListPage extends React.Component {
  render() {
    return (
      <div id="list-page" class="transition-item">
      ...
      </div>
    );
  }
}

How it works

  • PageTransition component renders {this.props.children} inside a <div class="transition-wrapper">...</div>.

  • When the route change, CSS classes will be added as following:

image

  • You'll need to define the transition in your CSS:

Example: sliding animation

.detail-page {
  overflow: auto;
  box-sizing: border-box;
  padding: 20px;
  height: 100vh;
  background-color: #03a9f4;
  transition: transform 0.5s, opacity 0.5s;

  &.transition-appear {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
  }

  &.transition-appear.transition-appear-active {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
  &.transition-leave {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }

  &.transition-leave.transition-leave-active {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
  }
}

Sometimes it is impossible to implement your designer's awesome animation idea in just only CSS. In that case, you'll need the callbacks to customize your animation with additional data. See API document and example for more information.

API

Properties

  • timeout: transition duration in milisecond, this must be the same with the transition-duration in your CSS.

    Example:

        <PageTransition timeout={500}>
          {props.children}
        </PageTransition>
  • data: custom data to send to the page component via onTransitionWillStart, onTransitionDidEnd, transitionManuallyStart, transitionManuallyEnd.

    Example:

        <PageTransition
          data={{ clickedItemData: this.state.clickedItemData }}
        >
          {this.props.children}
        </PageTransition>
  • onLoad: this callback will be call after the new page is finished replaced.

    Example:

        <PageTransition
          onLoad={() => this.refs.scrollArea.scrollTop = 0}
        >
          {this.props.children}
        </PageTransition>
  • transitionAction: this prop can be used to add additional state to the transition, for example you can identify if a transition is a "go back" or "go forward" by using history.action from History API.

    Example:

        <PageTransition
          transitionAction={this.props.history.action}
        >
          {this.props.children}
        </PageTransition>

    When transitionAction presence, class transition-${action} will be added along with transition-appear, and will be removed along with transition-leave.

Callback on children component

PageTransition component calls a several callbacks to its child component to pass user defined additional data for the animation. Child components are mounted via React Router when the route change.

Notice: all these callbacks will be called in a Promise chain, so if you are handleing async tasks inside the callback (for example setState), make sure you return a Promise to make everything work properly.

  • onTransitionWillStart(data): before the transition starts (before transition-appear-active class is added). data is the variable received from the data property of PageTransition.

  • transitionManuallyStart(data): if you don't use transition-appear-active class in CSS to animate your page, you can define this method in the child component to do the animation mannually. transition-appear-active will not be added to the child's DOM when this method exists.

  • onTransitionDidStart(data): after the transition started.

  • onTransitionWillEnd(data): before the transition stops (before transition-appear-active class is removed).

  • transitionManuallyStop(data): similar to transitionManuallyStart. transition-appear-active will not be removed to the child's DOM when this method exists.

  • onTransitionDidEnd(data): after the transition stopped (after transition-appear-active class is removed)

    Example:

    export default class DetailPage extends React.Component {
      ...
      onTransitionWillStart(data) {
        return new Promise(resolve => {
            this.setState({ animating: false, postiton: data.position, opacity: 0 }, resolve);
        });
      }
      transitionManuallyStart(data) {
        return new Promise(resolve => {
            this.setState({ animating: true, postiton: DEFAULT_POSITION, opacity: 1 }, resolve);
        });
      }
      onTransitionDidStart(data) {
        // Animation is happening
      }
      onTransitionWillEnd(data) {
        // Animation is about to stop
      }
      transitionManuallyStop(data) {
        return new Promise(resolve => {
            this.setState({ animating: false }, resolve);
        });
      }
      onTransitionDidEnd(data) {
        // Page successfully replaced and finished animate
        this.callMyBusinessApi();
      }
    
      ...

Similar callbacks for leave event:

  • onTransitionLeaveWillStart(data)
  • transitionLeaveManuallyStart(data)
  • onTransitionLeaveDidStart(data)
  • onTransitionLeaveWillEnd(data)
  • transitionLeaveManuallyStop(data)
  • onTransitionLeaveDidEnd(data)

Available CSS functional class names

  • transition-appear, transition-appear-active, transition-leave, transition-leave-active.
  • Root element of the transited page must have transition-item class.

Using with Redux

By default, PageTransition will animates its children when componentWillReceiveProps is triggered. It compares this.props.children !== nextProps.children to know if the page has changed (ex: move from page Login to page AdminPanel).

When using PageTransition with Redux, you may end up having the animation triggered everytime the Redux state changes (ex: state change when you enter username, componentWillReceiveProps is triggered but the page is still Login page). In order to resolve this, you can use data-transition-id for the child components.

    <PageTransition>
      {isLoggedIn() ?
        <AdminPanel data-transition-id="admin-page" ... />
        :
        <Login data-transition-id="login-page" ... />
      }
    </PageTransition>

When data-transition-id prop is provided, PageTransition will use this value to compare the childrens. Now you can control exactly when will the pages are changed.

Using with React Router 4

At the moment, callbacks are not supported on React Router 4, however the basic CSS transitions still works. You have to wrap your <Route> with <Switch>. Please notice that you have to pass the location prop to <Switch> to make it work.

        <PageTransition>
          <Switch location={this.props.location}>
            <Route exact path="/" component={ListPage} />
            <Route path="/detail/:itemId" component={ItemDetailPage} />
          </Switch>
        </PageTransition>

See a live demo: https://codesandbox.io/s/n3rrym5y1l

When to use this?

Pros:

  • Give you ability to implement complex animations / transitions.
  • Keep page structure clean.

Cons:

  • Requires extra setup for the components

Examples

See EXAMPLES.md

LICENSE

MIT License

Copyright (c) 2016 Dinh Quang Trung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

Awesome-Black-Friday-Cyber-Monday

Awesome deals on Black Friday: Apps, SaaS, Books, Courses, etc.
1,721
star
2

github-explorer

Progressive Web Apps experiment
JavaScript
722
star
3

logmine

A log pattern analyzer CLI
Python
141
star
4

real-time-twitter-banner

Fun little experiment with Twitter API
JavaScript
108
star
5

chatgpt-prompt-templates

Templates for community prompts on TypingMind.com
47
star
6

tonydinh-com

My personal website
JavaScript
37
star
7

summer

Little fun experiment with cellular automaton
JavaScript
34
star
8

ChordDroid

Android library to render Guitar Chord.
Java
25
star
9

dinhquangtrung.net

My website
JavaScript
23
star
10

hn-big-threads

Use flame graphs to read very big HN threads
HTML
16
star
11

screensaver

Simple screensaver app
Shell
14
star
12

16-bit-computer

16-bit computer in Logism
JavaScript
13
star
13

smart-doge

Doge can find the next number in a sequence. Yay!
JavaScript
10
star
14

movie-showtimes

Web Service & Android Application to look up Vietnam movie showtimes
Java
7
star
15

nextjs-phaser

TypeScript
7
star
16

firebase-example

A web application to demonstrate Firebase features
JavaScript
6
star
17

8-bit-computer

8-bit-computer in Logism
JavaScript
5
star
18

do-an-xe

do-an-xe
JavaScript
4
star
19

960-grid-generator

Old school CSS grid generator (warning: float)
JavaScript
4
star
20

snake-redux

Snake game with pure Redux and p5.js, no boilerplate
JavaScript
3
star
21

-VB6-VirusRemoveAll

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
3
star
22

FreakingMathAndroid

Android game with numbers
Java
3
star
23

plain-react

My favorite boilerplate to create static web app with React.
JavaScript
3
star
24

thesimpleapi

API as a service
JavaScript
3
star
25

firebase-checkin

Check in application built with Firebase
HTML
2
star
26

web-bluetooth

web-bluetooth
HTML
2
star
27

vnk-swift

Vietnamese input method for OSX
Swift
2
star
28

facebook-float-video

Floating facebook video
JavaScript
2
star
29

-VB6-PerfectAntivirus

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
2
star
30

-VB6-SystemReporter

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
2
star
31

-VB6-ProcessManager

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
1
star
32

web-bluetooth-codelabs

web-bluetooth-codelabs
HTML
1
star
33

test-twitter-physic

JavaScript
1
star
34

school-revision-tool

Self-review tool for school exam.
JavaScript
1
star
35

progressive-web-app-demo

Small demo building Progressive Web Apps
JavaScript
1
star
36

chrome-dinosaur

I copied the source code from Chromium and edited it.
HTML
1
star
37

css-stuffs

My experiment works with css
CSS
1
star
38

-VB6-NeverAutorun

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
1
star
39

-VB6-AllFileLocker

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
1
star
40

bricks

Coding challenge
JavaScript
1
star
41

FormulasTextBox

A control for .Net Winform Application to process Input with formulas like an Excel cell.
C#
1
star
42

-VB6-1Click

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
1
star
43

-VB6-ProStatus

See my article about this project: http://dinhquangtrung.net/blog/some-softwares-i-made
Visual Basic
1
star