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-router-animator

v0.0.15

Published

A library easing the process of adding transition animations on your react-router applications

Downloads

17

Readme

react-router-animator

react-router-animator is a library that helps you in orchestrating animations in your (you can use whatever animation library for this such as greensock or animate.css for which you can use animatecss-helpers to apply styles wiith control)

Install

yarn add react-router-animator

peer dependencies:

yarn add react react-dom react-router-dom react-transition-group

Usage

Animation Routes

The <Animator> component takes a animationRoutes parameter, this parameter expects an object for which each key is a path (eg: '/', '/details') and the respective values define the from (entering) and to (leaving) transition handler functions.

from and to objects also expect their keys to be paths (respectively: from paths allow you to define specific animations based on the transition's origin path, and to paths allow you to define specific animations based on the transition's destination path)

const animationRoutes = {
  '/': {
    from: {
      // when: '/details' -> '/', for: '/'
      '/details': enterFromDetailsFunction
    },

    to: {
      // when: '/' -> '/details', for: '/'
      '/details': leaveToDetailsFunction
    }
  },

  '/details': {
    from: {
      // when: '/' -> '/details', for: '/details'
      '/': enterFromHomeFunction
    },

    to: {
      // when: '/details' -> '/', for: '/details'
      '/details': leaveToHomeFunction
    }
  }
};

defaults

Chances are that you'll want to define the same animations for all routes and only set specific ones for specific routes in your application. To do so, animationRoutes accepts a 'default' key at its root or in any from and to object.

const animationRoutes = {
  '/': {
    from: {
      // when: '/details' -> '/', for: '/'
      '/details': enterFromDetailsFunction,
      // when: '* except /details' -> '/', for: '/'
      default: enterFromAnyOtherRoute
    },

    to: {
      // when: '/' -> '/details', for: '/'
      '/details': leaveToDetailsFunction,
      // when: '/' -> '* except /details', for: '/'
      default: leaveToAnyOtherRoute
    }
  },

  '/details': {
    from: {
      // when: '/' -> '/details', for: '/details'
      '/': enterFromHomeFunction
    },

    to: {
      // when: '/details' -> '/', for: '/details'
      '/details': leaveToHomeFunction
    }
  },

  default: {
    from: {
      // when: 'any route that has not been matched by previous handlers' -> 'any route that has not been matched by previous handlers', for: 'any route that has not been matched by previous handlers'
      default: enterFromAnyRoute
    },
    to: {
      // when: 'any route that has not been matched by previous handlers' -> 'any route that has not been matched by previous handlers', for: 'any route that has not been matched by previous handlers'
      default: leaveToAnyRoute
    }
  }
};

and location

the <Animator> component is a React.Context provider, it will allow multiple routes to exist as its children at a certain time to allow for a smooth transition. For this to happen there are a few limitations to keep in mind:

  • statelessness: two children paths are basically two different subtrees rendered at the time of the transition, state that must survive transition in between routes must be stored above in your tree or it will be lost.
  • location: during transition one of the subtrees needs to render a route for a path that is not the one in your URL bar at that time, luckyly, <Animator> provides you a reference on the location the subtree was generated for initially, you must make sure that its children are refering to this location (see next example).
export const Routes = const Routes = () => (
  <Animator
    animationRoutes={animationRoutes}
  >
    {({ location }) => (
      <Switch location={location}>
        <Route path="/details">
          <DetailsPage />
        </Route>

        <Route exact path="/">
          <HomePage />
        </Route>

        <Route>
          <NotFoundFoundPage />
        </Route>
      </Switch>
    )}
  </Animator>
);

Examples

Header + Content separated animations using animatecss-helpers

export const headerAnimationRoutes = {
  '/': {
    from: {
      default: ({ node, appears }) => {
        const delay = appears ? 0 : 0.25;

        const nodes = prepare_nodes_for_transition(node.querySelectorAll('[data-transition-header] > *'));

        if (appears) {
          fade_in({
            nodes,
            delay,
            duration: 0.3
          });
        } else {
          fade_in_left({
            nodes,
            delay,
            duration: 0.3
          });
        }
      }
    },

    to: {
      default: ({ node }) => {
        fade_out({
          nodes: prepare_nodes_for_transition(node.querySelectorAll('[data-transition-header] > *')),
          duration: 0.3
        });
      }
    }
  },

  default: {
    from: {
      '/': ({ node, appears }) => {
        const delay = appears ? 0 : 0.25;

        const nodes = prepare_nodes_for_transition(node.querySelectorAll('[data-transition-header] > *'));

        if (appears) {
          fade_in({
            nodes,
            delay,
            duration: 0.3
          });
        } else {
          fade_in_right({
            nodes,
            delay,
            duration: 0.3
          });
        }
      },

      default: ({ node, appears }) => {
        const delay = appears ? 0 : 0.25;

        fade_in({
          nodes: prepare_nodes_for_transition(node.querySelectorAll('[data-transition-header] > *')),
          delay,
          duration: 0.3
        });
      }
    },

    to: {
      '/': ({ node }) => {
        fade_out_right({
          nodes: prepare_nodes_for_transition(node.querySelectorAll('[data-transition-header] > *')),
          duration: 0.3
        });
      },

      default: ({ node }) => {
        fade_out({
          nodes: prepare_nodes_for_transition(node.querySelectorAll('[data-transition-header] > *')),
          duration: 0.3
        });
      }
    }
  }
};

export const contentAnimationRoutes = {
  '/': {
    from: {
      default: ({ node, appears }) => {
        const delay = appears ? 0 : 0.25;

        fade_in({
          delay,
          nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
          node
        });
      }
    },

    to: {
      default: ({ node }) => {
        fade_out({
          delay,
          nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
          node
        });
      }
    }
  },

  default: {
    from: {
      '/': ({ node, appears }) => {
        const delay = appears ? 0 : 0.25;

        if (!appears) {
          slide_in_right({
            nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
            delay,
            duration: 0.3
          });
        }

        fade_in({
          nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
          delay,
          duration: 0.3
        });
      },

      default: ({ node, appears }) => {
        const delay = appears ? 0 : 0.25;

        fade_in({
          nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
          delay,
          duration: 0.3
        });
      }
    },

    to: {
      '/': ({ node }) => {
        fade_out_right({
          nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
          duration: 0.3
        });
      },

      default: ({ node }) => {
        fade_out({
          nodes: prepareNodesForTransition(node.querySelectorAll('[data-transition-page-content] > *')),
          duration: 0.3
        });
      }
    }
  }
};
// router.js
import React, { Suspense } from 'react';
import { Switch, Route } from 'react-router-dom';
import { Animator } from 'react-router-animator';

import { headerAnimationRoutes, contentAnimationRoutes } from './animation_routes';

export const Routes = const Routes = () => (
  <Suspense
    fallback={
      <div>loading...</div>
    }
  >
    <header>
      <Animator
        animationRoutes={headerAnimationRoutes}
        disabled={!ANIMATIONS_ENABLED}
      >
        {({ location }) => (
          <Switch location={location}>
            <Route path="/details/:product_id">
              <DetailsHeader />
            </Route>

            <Route exact path="/">
              <HomeHeader />
            </Route>

            <Route>
              <DefaultHeader />
            </Route>
          </Switch>
        )}
      </Animator>
    </header>

    <main
      className="relative flex-1 overflow-x-hidden overflow-y-scroll focus:outline-none "
    >
      <Animator
        animationRoutes={contentAnimationRoutes}
        disabled={!ANIMATIONS_ENABLED}
      >
        {({ location }) => (
          <Switch location={location}>
            <Route path="/details/:product_id">
              <DetailsPage />
            </Route>

            <Route exact path="/">
              <HomePage />
            </Route>
          </Switch>
        )}
      </Animator>
    </main>
  </Suspense>
);

TODO

  • Remove tailwind references and use css instead