Giter VIP home page Giter VIP logo

react-router-transition's Introduction

React Router Transition

A simple component for easily declaring mounting and unmounting transitions. Built for react-router, powered by react-motion. Some demos

import { RouteTransition } from 'react-router-transition';

// in your root app component:
<div>
  <RouteTransition
    pathname={this.props.location.pathname}
    atEnter={{ opacity: 0 }}
    atLeave={{ opacity: 0 }}
    atActive={{ opacity: 1 }}
  >
    {this.props.children}
  </RouteTransition>
</div>

Installation

npm install --save react-router-transition

Usage

RouteTransition requires a few props:

  • pathname: the key signifying the transitionable route, most typically the pathname (required)
  • component: the element type ('div', 'span', etc.) to wrap transitioning routes. use false to transition routes themselves (this will require consuming a style prop in your route components).
  • atEnter: an object of interpolatable style values for a route that is mounting (required)
  • atLeave: an object of interpolatable style values for a route that is unmounting (required)
  • atActive: interpolatable style values for a route that has mounted (required)
  • mapStyles: an optional function to transform styles that aren't 1:1 (e.g. animating translateX or other values of transform)
  • runOnMount: a boolean to signal whether or not to run the transition on initial RouteTransition mount

and supports a couple optional props:

  • className: applies to the wrapper component
  • style: applies to the wrapper component

If you want more granular control over the transition, pass in spring objects accordingly. For more information on springs, check out react-motion's documentation.

An example of a transition that shifts the routes to the left:

<RouteTransition
  pathname={this.props.location.pathname}
  atEnter={{ translateX: 100 }}
  atLeave={{ translateX: -100 }}
  atActive={{ translateX: 0 }}
  mapStyles={styles => ({ transform: `translateX(${styles.translateX}%)` })}
>
  {this.props.children}
</RouteTransition>

It's also trivial to build out presets and toggle between them to handle pop events differently:

const styles = this.props.location.action === 'POP'
  ? popStateStyles
  : pushStateStyles;

<RouteTransition
  pathname={this.props.location.pathname}
  {...styles}
/>

Nesting Transitions

Currently, nesting transitions requires some extra logic as the transitions themselves are usually coupled to the pathname. The way around this is to set the RouteTransition's pathname to the pathname of the current, transitionable route.

This means that if you have a transition at the root level of your tree, the pathname for that RouteTransition should match the root-level route (e.g. /route-a or /route-b). Any subsequent, deeper RouteTransitions should refer to their own, specific paths as well (e.g. the pathnames for nested transitions within /route-a would appear as /route-a/foo or /route-a/bar).

react-router-transition's People

Contributors

maisano avatar meriadec avatar vyorkin avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.