Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gatsby-plugin-transitions

AndreasFaust1.7kMIT2.1.7

Gatsby-plugin for animated page-transitions.

gatsby, gatsby-plugin, transitions, page-transitions, animation

readme

gatsby-plugin-transitions enables animated page-transitions. It uses react-spring for smooth, customizable animations.

  • Default animation for every page-transition
  • Define per link animations additionally
  • Two animation-modes: successive (animate out, then animate in) and immediate (in and out at the same time)
  • Restores scroll-position on traversing browser-history

NPM JavaScript Style Guide

🚀 Have a look at the example!

This Project is under development. Please join and contribute!

Install

Install all dependencies via Yarn or NPM.

yarn add gatsby-plugin-transitions gatsby-plugin-layout react-spring react react-dom

Usage

1. Register gatsby-plugin-layout

Register gatsby-plugin-layout in your gatsby-config.js:

module.exports = {
  plugins: ["gatsby-plugin-layout"]
};

2. Disable default scroll-to-top

Copy this into your gatsby-browser.js:

exports.shouldUpdateScroll = () => {
  return false;
};

3. Create default Layout-file

Activate gatsby-plugin-layout. Either do it your own way, or take the default way:

  • Create the folder src/layouts and the file src/layouts/index.js.
  • Here you need to wrap all children into the components TransitionProvider and TransitionViews.
import React from "react";
import { TransitionProvider, TransitionViews } from "gatsby-plugin-transitions";

const Layout = ({ location, children }) => {
  return (
    <TransitionProvider location={location}>
      <TransitionViews>
        {children}
      </TransitionViews>
    </TransitionProvider>
  );
};

export default Layout;

🎉 Voila! You have smooth animated page-transitions! Now customize these!

4. Customize!

import React, { useState } from "react";
import { TransitionProvider, TransitionViews } from "gatsby-plugin-transitions";
// Example Header-Component (see below)
import Header from "./header";

const Layout = ({ location, children }) => {
  return (
    <TransitionProvider
      location={location}
      mode="immediate"
      enter={{
        opacity: 0,
        transform: "translate3d(0,20vh,0) scale3d(1, 1, 1) rotate(0deg)",
        config: {
          mass: 1,
          tension: 210,
          friction: 20,
          clamp: true
        },
        onRest: () => {
          console.log("Hello, World!");
        }
      }}
      usual={{
        opacity: 1,
        transform: "translate3d(0vh,0vh,0) scale3d(1, 1, 1) rotate(0deg)"
      }}
      leave={{
        opacity: 0,
        transform: "translate3d(0vh,0vh,0) scale3d(2, 2, 1) rotate(180deg)",
        config: {
          duration: 1000
        }
      }}
    >
      // This Header is an example for a component, that should remain between routes
      <Header />
      <TransitionViews>
        {children}
      </TransitionViews>
    </TransitionProvider>
  );
};

export default Layout;

TransitionProvider

List of props:

Name Type Default Description
location Object null required. Gatsby’s location-object.
mode String 'successive' Transition-mode: 'successive' or 'immediate'
enter object { opacity: 0, config: 'stiff' } From-values, when the view is entering. Accepts also callbacks and react-spring-config.
usual object { opacity: 1 } Normal state of the view.
leave object { opacity: 0, config: 'stiff' } To-Values, when the view is leaving. Accepts also callbacks and react-spring-config.
style object null Style the views-wrapper (which is a div with class views).

Transition-Mode

  • successive: Wait till previous view has disappeared.
  • immediate: Next view is entering while previous view is disappearing.

Default-Springs

You can enter default-springs for all animation-states. These props accept a regular react-spring-object. Animated are currently only the keys opacity and transform.

enter

From-values, when the view is entering.

  • Props: opacity, transform,
  • Callbacks: onStart, onFrame, onRest
  • Config: react-spring-config-object

usual

Normal animation-state of the view.

  • Props: opacity, transform,
  • Callbacks: none
  • config: none — define with enter and leave!

leave

To-Values, when the view is leaving.

  • Props: opacity, transform,
  • Callbacks: onStart, onFrame, onRest
  • Config: react-spring-config-object

config

The key config can be either a regular react-spring-config-object.

Or pass in the name of a react-spring-default (default, gentle, wobbly, stiff, slow, molasses) as string.

gatsby-plugin-transition works out of the box with Gatsby's default Link-component. If you want to apply custom animations to certain links, use TransitionLink.

import React from "react";
import { TransitionLink } from "gatsby-plugin-transitions";

const MyComponent = () => (
  <div className="content content--1">
    <h1>gatsby-plugin-transitions</h1>
    <p>Transitions are easy.</p>
    <p>Now go build something great.</p>
    <TransitionLink
      to="/page-2"
      style={{ color: "red" }}
      className="my-custom-link"
      leave={{
        opacity: 0,
        transform: "translate3d(100vh,0vh,0)",
        config: { mass: 1, tension: 180, friction: 12, clamp: true }
      }}
      enter={{
        opacity: 0,
        transform: "translate3d(100vh,0vh,0)",
        config: "stiff",
        onRest: () => {
          console.log("Hello, World!");
        }
      }}
      usual={{
        transform: "translate3d(0vh,0vh,0)",
        opacity: 1
      }}
      mode="immediate"
      y={() => window.scrollY}
    >
      I have a special animation!
      <br />
      And mode 'immediate'!
      <br />
      Go to page 2
    </TransitionLink>
  </div>
);

export default MyComponent;

List of props:

Name Type Default Description
to Object '' required. Pathname of your link-target.
mode String 'successive' Transition-mode: 'successive' or 'immediate'
enter object { opacity: 0, config: 'stiff' } From-values, when the view is entering
usual object { opacity: 1, config: 'stiff' } Normal state of the view.
leave object { opacity: 0, config: 'stiff' } To-Values, when the view is leaving.
y number or function 0 Scroll position of the next view. If function, it must return a number (for example current window.scrollY).
style object null Style the Link.

🔥TransitionLink can only be used in components, that are children of TransitionProvider!

useTransitionStore

A hook, that exposes the plugin’s state-management. It returns an Array with 2 elements:

  1. state of type object
  2. dispatch of type function

Get some useful information from the module’s store! For example get the current location-object:

import React from "react";
import { useTransitionStore } from "gatsby-plugin-transitions";

const MyComponent = () => {
  const [{ currentLocation }] = useTransitionStore();
  return <h1>{currentLocation.pathname}</h1>;
};

export default MyComponent;

🔥useTransitionStore can only be used in components, that are children of TransitionProvider!

To navigate programmatically you need to dispatch the NAVIGATE-action from the TransitionStore. It takes the same props as TransitionLink.

Example: Navigate, when the user scrolls to the bottom of the page:

import React, { useEffect, useState } from "react";
import { useTransitionStore } from "../transitions";

const MyComponent = () => {
  const [, dispatch] = useTransitionStore();
  useEffect(() => {
    function onScroll() {
      if (
        window.innerHeight + window.pageYOffset >=
        document.body.offsetHeight - 2
      ) {
        dispatch({
          type: "NAVIGATE",
          to: "/another-page",
          leave: {
            opacity: 0,
            transform: "translate3d(0, -50vh, 0)",
            config: "stiff"
          },
          y: 500
        });
      }
    }
    window.addEventListener("scroll", onScroll);
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  return (
    <div className="content" style={{ minHeight: "300vh" }}>
      <h1>Scroll down to navigate!</h1>
    </div>
  );
};

export default MyComponent;

🔥 Caution 🔥

Keep animated props consistent!

react-spring needs consistent props and transform-units. So if you for example once animated opacity, always write it, even if it does not get changed!

import React from "react";
import { TransitionLink } from "gatsby-plugin-transitions";

const MyComponent = () => (
  <TransitionLink
    to="/page-2"
    style={{ color: "red" }}
    className="my-custom-link"
    leave={{
      opacity: 0,
      transform: "translate3d(100vh,0vh,0)"
    }}
    enter={{
      opacity: 0,
      transform: "translate3d(100vh,0vh,0)"
    }}
    usual={{
      opacity: 1
      transform: "translate3d(0vh,100vh,0)",
    }}
    mode="immediate"
    y={1000}
  >
    I have a special animation!
  </TransitionLink>
);

export default MyComponent;

Use clamp: true to speed up your animations!

Spring-Animations "wobble", which takes extra time. To make the animations snappier, define clamp: true in the config-object.

To-Do

  • <input disabled="" type="checkbox"> Testing

Contributing

Every contribution is very much appreciated.

😍 If you like gatsby-plugin-transitions, star it on GitHub!

License

MIT © AndreasFaust

changelog

2.1.7 (2020-08-27)

2.1.7 (2020-08-27)

2.1.6 (2019-12-30)

Bug Fixes
  • issue #13 (484d2c76)

2.1.5 (2019-06-19)

Documentation Changes
  • Small adjustments. (b9847fd0)

2.1.4 (2019-06-15)

2.1.3 (2019-06-15)

2.1.2 (2019-06-15)

Bug Fixes
  • Fix 'mode: immediate' and 'y: n' scroll not to n, but to 0. (d2298860)

2.1.1 (2019-06-14)

Documentation Changes
  • Add history hint. (074f5f2d)

2.1.0 (2019-06-14)

New Features
  • keep scrollY on going backwards or forwards in browser-history. (eba11e9b)

2.0.3 (2019-06-14)

Bug Fixes
  • Fix flickering on mode:immediate and scrollY > 0 in Safari. (c94f4d5d)

2.0.2 (2019-06-14)

Documentation Changes
  • small adjustments. (7edf3226)

2.0.1 (2019-06-14)

2.0.0 (2019-06-14)

Breaking Changes
  • Add new provider-component. (b0500558)

1.9.0 (2019-06-03)

New Features
  • TransitionLink: y takes function as prop. (1790a629)

1.8.12 (2019-06-02)

1.8.11 (2019-06-02)

1.8.10 (2019-06-02)

1.8.9 (2019-05-26)

Documentation Changes
  • Small fix. (a56c85c3)

1.8.8 (2019-05-26)

Documentation Changes
  • Add topic 'navigate programmatically'. (82691e6a)

1.8.7 (2019-05-26)

Bug Fixes
  • Fix callbacks. (c46a0f77)

1.8.6 (2019-05-26)

Documentation Changes
  • fix docs. (e6454d97)

1.8.5 (2019-05-26)

Documentation Changes
  • fix docs. (740f87b3)

1.8.4 (2019-05-26)

Documentation Changes
  • extend docs. (4ff9238d)

1.8.3 (2019-05-26)

Bug Fixes
  • fix successive-bug. (d06056ea)

1.8.2 (2019-05-25)

Documentation Changes
  • Fix docs. (8bce8dce)

1.8.1 (2019-05-25)

Documentation Changes
  • Fix docs. (0b366aa5)

1.8.0 (2019-05-25)

New Features
  • add style-prop. (acf3cd54)

1.7.1 (2019-05-25)

Documentation Changes
  • Improve docs. (ac41e806)

1.7.0 (2019-05-25)

1.6.3 (2019-05-25)

Bug Fixes
  • Fix will-change. (feb5023d)

1.6.2 (2019-05-25)

Bug Fixes
  • fix animation-states at per-link. (d51b6b10)

1.6.2 (2019-05-25)

Bug Fixes
  • fix animation-states at per-link. (d51b6b10)

1.6.2 (2019-05-25)

Bug Fixes
  • fix animation-states at per-link. (d51b6b10)

1.6.1 (2019-05-25)

1.6.0 (2019-05-24)

New Features
  • add transitionLink (e2f3f3cb)
Bug Fixes
  • Fix peer-dependencies. (2c1dc888)

1.5.1 (2019-05-24)

Bug Fixes
  • Fix onStart (0950145c)

1.5.0 (2019-05-24)

New Features
  • add onStart and onFrame callbacks. (a68b7a2c)

1.4.0 (2019-05-23)

New Features
  • Add custom y-position. (b64fac21)

1.3.1 (2019-05-23)

Bug Fixes
  • forgot build. (d7f4993b)

1.3.0 (2019-05-23)

New Features
  • Finalize keep-feature. (2c654590)

1.2.0 (2019-05-17)

Documentation Changes
  • fix project name (608c19cc)
New Features
  • Add hasEntered to state. (4f1db844)

1.1.1 (2019-05-09)

Bug Fixes
  • Fix will-change. (8c802983)

1.1.0 (2019-05-09)

New Features
  • container position fix only on mode 'immediate' (fade9d4a)

1.0.2 (2019-05-07)

Bug Fixes
  • build. (88b11284)
  • small fix. (1f9b6319)

1.0.1 (2019-05-07)

Bug Fixes
  • Forgot build… (5cdce0f2)

1.0.0 (2019-05-07)

Bug Fixes
  • Fix location.pathname-slash-at-the-end-bug. (4aa3bf45)

0.4.3 (2019-05-05)

Bug Fixes
  • Fix component-naming. (7ac5252f)

0.4.2 (2019-05-05)

0.4.2 (2019-05-05)

0.4.1 (2019-05-05)

Documentation Changes
  • improve. (cc8d15a6)

0.4.0 (2019-05-05)

Documentation Changes
  • Write docs. (33e6dedb)

0.3.0 (2019-05-05)

New Features
  • add default transitions. (f0e9adfd)

0.2.2 (2019-05-05)

Bug Fixes
  • Forgot build (3aab65ff)

0.2.1 (2019-05-05)

0.2.0 (2019-05-05)

New Features
  • export context. (99126f39)