npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-route-transition

v1.0.6

Published

A tiny transition orchestrator for React

Downloads

5

Readme

React Route Transition

A tiny (1.4kb gzipped) and simple transition orchestrator for React.

Example. Blog Post.

Install

yarn add react-route-transition
# or npm install --save react-route-transition

API

<RouteTransitionProvider> - for react-route-transition to work you first need to wrap your app with this provider (place it inside of react-router's Router).

useTransitionHistory() - returns an object with a single function named push that accepts a path (string) and an optional state object. Calling this starts the orchestrator, which fires the relevant onLeave animations, waits for them to finish, changes the route and fires the relevant onEnter animations.

useTransition(options: ITransitionOptions) - this hook registers animations for the current components (it tells react-route-transition which route changes this component listens to and which animations it should fire accordingly). It accepts an object that looks as follows:

useTransition({
  // the list of "handlers"
  handlers: [
    {
      // each handler can either define a path, the path can either be a
      // string/regexp (single path), or an array of strings/regexps (multiple
      // paths)
      //
      path: '/signin',
      // each handler should implement either an onEnter callback, that will
      // be fired once entering said path, or an onLeave callback, or both
      //
      onEnter: async () => {
        await gsap.timeline().fromTo(
          '[data-signin-wrapper] > *',
          { y: 20, opacity: 0 },
          {
            y: 0,
            duration: 0.6,
            stagger: 0.125,
            opacity: 1,
          }
        );
      },
      onLeave: async () => {
        await gsap.timeline().to('[data-home-main] > *, [data-home-footer]', {
          duration: 0.6,
          stagger: 0.125,
          opacity: 0,
          y: -20,
        });
      },
    },
    // ... more handlers
  ],
});

Example Usage

If you are using react-router:

App.js:

import { BrowserRouter as Router, Route } from 'react-router-dom';
import { RouteTransitionProvider } from 'react-route-transition';
// import Home and SignIn components...

export default function () {
  return (
    <Router>
      <RouteTransitionProvider>
        <Route path="/" exact>
          <Home />
        </Route>
        <Route path="/signin" exact>
          <SignIn />
        </Route>
      </RouteTransitionProvider>
    </Router>
  );
}

Home.js:

import React from 'react';
import gsap from 'gsap';
import { useTransition, useTransitionHistory } from 'react-route-transition';

export default function () {
  const history = useTransitionHistory();

  // The following tells react-router-transition that whenever the user
  // navigates to '/', call the first function (start the first animation),
  // and whenever the user leaves '/' call the second function *before* pushing
  // the new path to the react-router history.
  //
  useTransition({
    handlers: [
      {
        path: '/',
        onEnter: async () => {
          await gsap
            .timeline()
            .fromTo(
              '[data-home-main] > *, [data-home-footer]',
              { opacity: 0, y: 20 },
              { duration: 0.6, stagger: 0.125, y: 0, opacity: 1 }
            );
        },
        onLeave: async () => {
          await gsap.timeline().to('[data-home-main] > *, [data-home-footer]', {
            duration: 0.6,
            stagger: 0.125,
            opacity: 0,
            y: -20,
          });
        },
      },
    ],
  });

  async function handleSignIn(e) {
    e.preventDefault();
    // remember this is react-route-animation's push() method, which wraps
    // react-router's method and plays the animations
    //
    history.push('/signin');
  }

  return (
    <div>
      <nav>
        <a href="/signin" onClick={handleSignIn}>
          Sign In
        </a>
      </nav>
      <main data-home-main>{/* this content will be animated */}</main>
      <footer data-home-footer>{/* some stuff here as well */}</footer>
    </div>
  );
}

SignIn.js:

// same imports as above...

export default function () {
  const history = useTransitionHistory();

  useTransition({
    handlers: [
      {
        path: '/signin',
        onEnter: async () => {
          gsap.timeline().fromTo(
            '[data-signin-wrapper] > *',
            { y: 20, opacity: 0 },
            {
              y: 0,
              duration: 0.6,
              stagger: 0.125,
              opacity: 1,
            }
          );
        },
        onLeave: async () => {
          await gsap.timeline().to('[data-home-main] > *, [data-home-footer]', {
            duration: 0.6,
            stagger: 0.125,
            opacity: 0,
            y: -20,
          });
        },
      },
    ],
  });

  async function handleBackToHome(e) {
    e.preventDefault();
    history.push('/');
  }

  return <div data-signin-wrapper>{/* this content will be animated */}</div>;
}

If you are not using react-router you can still use react-route-transition, only you need to provide <TransitionProvider> two props:

  • push: (path: History.Path, state?: History.LocationState) => void - an object with a single function called push that accepts a path (string) and an optional state. When calling that push method the current route should change.
  • location: { pathname: History.Path } - an object with a prop named pathname that listens to path changes and updates accordingly.

Also, you will need to import <TransitionProvider> from react-route-transition/core:

import { TransitionProvider } from 'react-route-transition/core';

More options

Say you want one leave animation to start when the user navigates from the Sign In screen to the App screen but a different one when the user navigates from the Sign In screen to the Home screen.

You can do it by defining a from and a to prop, for example:

useTransition({
  handlers: [
    {
      from: '/signin',
      to: '/app',
      onLeave: async () => {
        // some crazy animation
      },
    },
    {
      from: '/signin',
      to: '/',
      onLeave: async () => {
        // some other crazy animation
      },
    },
  ],
});

License

MIT