All Projects → doctolib → React Router Transitions

doctolib / React Router Transitions

Easily handle transitions in your React application 🍃

Projects that are alternatives of or similar to React Router Transitions

React Router Page Transition
Highly customizable page transition component for your React Router
Stars: ✭ 531 (+353.85%)
Mutual labels:  transition, react-router
React Redux Auth0 Kit
Minimal starter boilerplate project with CRA, React, Redux, React Router and Auth0 authentication
Stars: ✭ 115 (-1.71%)
Mutual labels:  react-router
Sunset.css
This library offers a collection of different CSS-powered transitions.
Stars: ✭ 99 (-15.38%)
Mutual labels:  transition
Redux Idle Monitor
A Redux component to schedule events at stages of user idleness across multiple browser tabs.
Stars: ✭ 105 (-10.26%)
Mutual labels:  transition
Libreact
NO LONGER MAINTAINED - SEE https://github.com/streamich/libreact INSTEAD
Stars: ✭ 100 (-14.53%)
Mutual labels:  react-router
Road Beyond React App
🌈 The Road beyond React - Thing you can use after learning plain React.js
Stars: ✭ 108 (-7.69%)
Mutual labels:  react-router
Npm Registry Browser
Browse the npm registry with an SPA made in React, with full dev workflow.
Stars: ✭ 97 (-17.09%)
Mutual labels:  react-router
Reeakt
A modern React boilerplate to awesome web applications
Stars: ✭ 116 (-0.85%)
Mutual labels:  react-router
Universal React Redux
🧐 A sensible universal starter kit for React + Redux
Stars: ✭ 112 (-4.27%)
Mutual labels:  react-router
Dva Boot Admin
🍰 react admin dashboard ui LANIF-ADMIN --- react 16 + react-router 4 + dva 2 + antd 4 后台管理 脚手架
Stars: ✭ 1,553 (+1227.35%)
Mutual labels:  react-router
React Login
A client side implementation of authentication using react.js for my blog on medium. This is the second part of my previous blog on how to implement scalable node.js server.
Stars: ✭ 105 (-10.26%)
Mutual labels:  react-router
Compose Shared Element
Experiment with SharedElement transition in Jetpack Compose, inspired by Flutter Hero widget.
Stars: ✭ 102 (-12.82%)
Mutual labels:  transition
React Koa Login
koa2 + react + react-router(4.0) + redux + webpack + antd
Stars: ✭ 109 (-6.84%)
Mutual labels:  react-router
React Antd Admin
一個简洁的 antd-react-admin 应用 -- React + Antd 后台管理系统
Stars: ✭ 99 (-15.38%)
Mutual labels:  react-router
Go Postgres Jwt React Starter
A go, gin, and postgres API with jwt auth, complete with a react frontend
Stars: ✭ 115 (-1.71%)
Mutual labels:  react-router
React Demo Gather
react demo合集,有自己写的,也有在学习过程中觉得很好的demo收集的,持续更新中
Stars: ✭ 97 (-17.09%)
Mutual labels:  react-router
React Bootstrap Webpack Starter
ReactJS 16.4 + new React Context API +react Router 4 + webpack 4 + babel 7+ hot Reload + Bootstrap 4 + styled-components
Stars: ✭ 103 (-11.97%)
Mutual labels:  react-router
Mirror
A simple and powerful React framework with minimal API and zero boilerplate.
Stars: ✭ 1,445 (+1135.04%)
Mutual labels:  react-router
React Router Active Component
Factory function for React components which are active for a particular React Router route
Stars: ✭ 116 (-0.85%)
Mutual labels:  react-router
React Redux Starter
A basic template that consists of the essential elements that are required to start building a Single Page Application using React, React Router, Redux, Bootstrap 4, Sass, and Webpack
Stars: ✭ 116 (-0.85%)
Mutual labels:  react-router

react-router-transitions

Brings transitions to react-router.

npm version npm downloads Build Status

Dependencies DevDependencies

Installation

npm install --save react-router-transitions

The problem solved

The main goal of this module is to handle history navigation by providing the correct animation if the user goes back or goes forward. It is a requirement especially on mobile to provide a great user experience.

Getting started

Set up routes

The requirement to use this module is to set up transition context at the root level of your application using useTransitions.

Then you can enable transitions at several levels of your application using withTransition. In a simple application, you should only wrap your root component.

import React from 'react';
import {Router, Route, browserHistory, applyRouterMiddleware} from 'react-router';
import {useTransitions, withTransition} from 'react-router-transitions';
import CSSTransitionGroup from 'react-transition-group';
import App from './App';
import Home from './Home';
import AboutUs from './AboutUs';

export default () => (
  <Router
    history={browserHistory}
    render={applyRouterMiddleware(useTransitions({
      TransitionGroup: ReactCSSTransitionGroup,
      defaultTransition: {
        transitionName: 'fade',
        transitionEnterTimeout: 500,
        transitionLeaveTimeout: 300
      }
    }))}
  >
    <Route path="/" component={withTransition(App)}>
      <Route path="home" component={Home} />
      <Route path="about-us" component={AboutUs} />
    </Route>
  </Router>
);

Navigate into your application

If you are familiar with react-router, you have probably already used methods like router.push or router.replace. Starting now, you should not use them to navigate into your application.

You have to think your navigation in a view logic, just like in a mobile application. To make it possible, a new property is now accessible in the transitionRouter context. This property has three main methods: show, dismiss and swap.

transitionRouter.show(location)

Go to a location using a show animation. You have to use this method each time you want to display a new view.

It adds a new entry in the history to give the user the ability to use the back button.

Internally, it uses the router.push method.

transitionRouter.dismiss(location, options)

Go to a location using a dismiss animation. You have to use this method when you want to hide a view (e.g. simulate a close or a go back).

The options argument accept an object.

  • depth: default to 1. Is the number of history records you want to revert.

It doesn't add a new entry in the history.

Internally, if we have an history, it uses the router.go(-n) method, otherwise it uses transitionRouter.swap with a dismiss action.

transitionRouter.swap(location, [transitionAction])

Go to a location using the default transition or the transition specified in the second argument. You have to use this method if you want to change route without affecting the history (e.g. tabs, accordion, etc).

It doesn't add a new entry in the history.

Internally, it merges the new location with the current one and uses router.replace. The transitionAction is added to the location state if specified. Available values are "show" or "dismiss".

Specify transitions

You can specify a transition associated with a view directly in the location state. You have to specify two transition configurations: showTransition and dismissTransition.

If you specify these properties, they will be the transition used to show or dismiss the view.

Example

import React, {Component} from 'react';
import PropTypes from 'prop-types';

export default class Home extends Component {
  static contextTypes = {
    transitionRouter: PropTypes.object,
  };

  onClickAboutUs(event) {
    event.preventDefault();
    this.context.transitionRouter.show({
      pathname: '/about-us',
      state: {
        showTransition: {
          transitionName: 'show-from-top',
          transitionEnterTimeout: 500,
          transitionLeaveTimeout: 300,
        },
        dismissTransition: {
          transitionName: 'dismiss-from-top',
          transitionEnterTimeout: 500,
          transitionLeaveTimeout: 300,
        },
      },
    });
  }

  render() {
    return (
      <div className="home">
        <a href="/about-us" onClick={this.onClickAboutUs}>
          About us
        </a>
      </div>
    );
  }
}

API

useTransitions(options)

Available options are:

  • TransitionGroup: The transition group component used to make transition. You can use any kind of transition group, ReactCSSTransitionGroup being the default. Transition specified in showTransition, dismissTransition or defaultTransition are the properties used to render your TransitionGroup.
  • defaultTransition: The default transition applied on the component. The default transition is applied when no transition is specified or when we can't determine the type of transition to apply.
  • onShow: Hook function called after a "show" action has been requested.
  • onDismiss: Hook function called after a "dismiss" action has been requested.
  • getComponentKey: Function used to generate the component key. Defaults to the complete route path.

This method has to be called in the render method of the Router component.

<Router
  history={browserHistory}
  render={applyRouterMiddleware(useTransitions({
    TransitionGroup: ReactCSSTransitionGroup,
    defaultTransition: {
      transitionName: 'fade',
      transitionEnterTimeout: 500,
      transitionLeaveTimeout: 300
    }
  }))}
/>

withTransition(Component, config)

Enable transitions in the component (children of the component will be animated).

The config provided is merged with the config provided into useTransitions.

In a simple application, this high order component has to be applied at the root level.

<Route path="/" component={withTransition(App)}>
  <Route path="home" component={Home} />
  <Route path="about-us" component={AboutUs} />
</Route>

Advanced usage

Use hooks to determine transition automatically

You can use hooks to determine transitions automatically without having to specify it in every "show" or "dismiss".

Example:

<Router
  history={browserHistory}
  render={applyRouterMiddleware(useTransitions({
    TransitionGroup: ReactCSSTransitionGroup,
    onShow(prevState, nextState, replaceTransition) {
      replaceTransition({
        transitionName: `show-${nextState.children.props.route.transition}`,
        transitionEnterTimeout: 500,
        transitionLeaveTimeout: 300,
      });
    },
    onDismiss(prevState, nextState, replaceTransition) {
      replaceTransition({
        transitionName: `dismiss-${prevState.children.props.route.transition}`,
        transitionEnterTimeout: 500,
        transitionLeaveTimeout: 300,
      });
    },
    defaultTransition: {
      transitionName: 'fade',
      transitionEnterTimeout: 500,
      transitionLeaveTimeout: 300,
    },
  }))}
>
  <Route path="/" component={withTransition(App)}>
    <Route path="home" component={Home} transition="from-right" />
    <Route path="about-us" component={AboutUs} transition="from-top" />
  </Route>
</Router>

Create a custom component key

If the animation is not played, one of the reason could be that your key is not different from the key of the last location. To fix that, you can create a custom component by specifying getComponentKey in the configuration.

By default, the key used is the full path to the current route. Params are ignored.

Example:

<Route path="books/:id" component={withTransition(BooksDetail, {
  getComponentKey(child, props) {
    return `books/${child.props.routeParams.id}`;
  }
})}

Use with custom history

React router transitions work with every histories available in react-router and available in history.

The only requirement to use this module with a custom history is to set an "action" and a "key" property in location. You can refer to history to see examples.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].