|
| 1 | +--- |
| 2 | +title: Adding Page Transitions with gatsby-plugin-transition-link |
| 3 | +--- |
| 4 | + |
| 5 | +This guide will cover how to use `gatsby-plugin-transition-link` to animate transitions between pages on your Gatsby site. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The `TransitionLink` component provides a way of describing a page transition via props on a Link component. It works with many animation libraries, like [react-pose](https://popmotion.io/pose/), [gsap](https://greensock.com/), [animejs](https://animejs.com/), and many others. |
| 10 | + |
| 11 | +Note that currently, as the plugin is based on link navigation, transitions when navigating with the browser buttons are not supported. |
| 12 | + |
| 13 | +For other page transition options, see the [overview on adding page animations](/docs/adding-page-transitions). |
| 14 | + |
| 15 | +## Getting started |
| 16 | + |
| 17 | +First, install the plugin: |
| 18 | + |
| 19 | +```shell |
| 20 | +npm install --save gatsby-plugin-transition-link |
| 21 | +``` |
| 22 | + |
| 23 | +Make sure to add the plugin to your `gatsby-config.js`: |
| 24 | + |
| 25 | +```javascript:title=gatsby-config.js |
| 26 | +module.exports = { |
| 27 | + plugins: [ |
| 28 | + `gatsby-plugin-transition-link` |
| 29 | + ] |
| 30 | +]; |
| 31 | +``` |
| 32 | +
|
| 33 | +Finally, import the `TransitionLink` component wherever you want to use it: |
| 34 | +
|
| 35 | +```javascript |
| 36 | +import TransitionLink from "gatsby-plugin-transition-link" |
| 37 | +``` |
| 38 | +
|
| 39 | +## Predefined transitions |
| 40 | +
|
| 41 | +You can use the `AniLink` component to add page transitions without having to define your own custom transitions. It's a wrapper around `TransitionLink` that provides 4 predefined transitions: `fade`, `swipe`, `cover`, and `paintDrip`. You can preview them at [this demo site](https://gatsby-plugin-transition-link.netlify.com/). |
| 42 | +
|
| 43 | +To use AniLink, you will need to install the `gsap` animation library: |
| 44 | +
|
| 45 | +```shell |
| 46 | +npm install --save gsap |
| 47 | +``` |
| 48 | +
|
| 49 | +Then, import the AniLink component: |
| 50 | +
|
| 51 | +```javascript |
| 52 | +import AniLink from "gatsby-plugin-transition-link/AniLink" |
| 53 | +``` |
| 54 | +
|
| 55 | +Finally, make sure you provide your desired animation's name as a blank prop to `AniLink`: |
| 56 | +
|
| 57 | +```javascript |
| 58 | +<AniLink paintDrip to="page-4"> |
| 59 | + Go to Page 4 |
| 60 | +</AniLink> |
| 61 | +``` |
| 62 | +
|
| 63 | +Options like transition duration, direction, and more are customizable with props. See [the documentation of AniLink](https://transitionlink.tylerbarnes.ca/docs/anilink/) for more details. |
| 64 | +
|
| 65 | +## Custom transitions |
| 66 | +
|
| 67 | +You have two main methods of creating page transitions: |
| 68 | +
|
| 69 | +1. Use the `trigger` function defined in your `exit`/`entry` prop. More details in the '[Using the `trigger` function](#using-the-trigger-function)' subsection. |
| 70 | +2. Use the props passed by `TransitionLink` to define your transitions. More details in the '[Using passed props](#using-passed-props)' subsection. |
| 71 | +
|
| 72 | +Additionally, you can specify a number of props and options on the `TransitionLink` component, like `length`, `delay`, and more. For more options and details, see [the documentation of TransitionLink](https://transitionlink.tylerbarnes.ca/docs/transitionlink/). For further examples of usage, visit the [plugin's Github repository.](https://github.com/TylerBarnes/gatsby-plugin-transition-link) |
| 73 | +
|
| 74 | +#### Using the trigger function |
| 75 | +
|
| 76 | +You can specify a `trigger` function that will handle the animation. This is useful for _imperative_ animation libaries like [animejs](https://animejs.com/) or [GSAP](https://greensock.com/gsap) that specify animations with function calls. |
| 77 | +
|
| 78 | +```javascript |
| 79 | +<TransitionLink |
| 80 | + exit={{ |
| 81 | + length: length, |
| 82 | + // highlight-next-line |
| 83 | + trigger: ({ exit, node }) => |
| 84 | + this.someCustomDefinedAnimation({ exit, node, direction: "out" }), |
| 85 | + }} |
| 86 | + entry={{ |
| 87 | + length: 0, |
| 88 | + // highlight-next-line |
| 89 | + trigger: ({ exit, node }) => |
| 90 | + this.someCustomDefinedAnimation({ exit, node, direction: "in" }), |
| 91 | + }} |
| 92 | + {...props} |
| 93 | +> |
| 94 | + {props.children} |
| 95 | +</TransitionLink> |
| 96 | +``` |
| 97 | +
|
| 98 | +#### Using passed props |
| 99 | +
|
| 100 | +The exiting and entering pages/templates involved in the transition will receive props indicating the current transition status, as well as the `exit` or `enter` props defined on the `TransitionLink`. |
| 101 | +
|
| 102 | +```javascript |
| 103 | +const PageOrTemplate = ({ children, transitionStatus, entry, exit }) => { |
| 104 | + console.log(transitionStatus, entry, exit) |
| 105 | + return <div className={transitionStatus}>{children}</div> |
| 106 | +} |
| 107 | +``` |
| 108 | +
|
| 109 | +You can combine these props with a _declarative_ state-based animation libraries like [react-pose](https://popmotion.io/pose/) or [react-spring](http://react-spring.surge.sh/) to specify transitions for exiting and entering a page. |
| 110 | +
|
| 111 | +If you want to access these props in one of your components instead of a page/template, you should wrap your component in the `TransitionState` component. This component takes a function that will have access to the same props as above, which you can then use in your component. |
| 112 | +
|
| 113 | +Here's an example using `TransitionState` and `react-pose` to trigger enter/exit transitions for a `Box` component. |
| 114 | +
|
| 115 | +```javascript |
| 116 | +import { TransitionLink } from "gatsby-plugin-transition-link" |
| 117 | + |
| 118 | +const Box = posed.div({ |
| 119 | + hidden: { opacity: 0 }, |
| 120 | + visible: { opacity: 1 }, |
| 121 | +}) |
| 122 | + |
| 123 | +<TransitionState> |
| 124 | + {({ transitionStatus, exit, enter }) => { |
| 125 | + console.log('exit object is', exit) |
| 126 | + console.log('enter object is', enter) |
| 127 | + |
| 128 | + return ( |
| 129 | + <Box |
| 130 | + className="box" |
| 131 | + pose={ |
| 132 | + ['entering', 'entered'].includes(transitionStatus) |
| 133 | + ? 'visible' |
| 134 | + : 'hidden' |
| 135 | + } |
| 136 | + /> |
| 137 | + ) |
| 138 | + }} |
| 139 | +</TransitionState> |
| 140 | +``` |
| 141 | +
|
| 142 | +Now, the `Box` component will be aware of the transition status of the page it's a child of, and it will fade in/out accordingly. |
| 143 | +
|
| 144 | +## Excluding elements from page transitions |
| 145 | +
|
| 146 | +You may want to have elements on a page that persist throughout the page transition (_ex. a site-wide header_). This can be accomplished by wrapping elements in the `TransitionPortal` component. |
| 147 | +
|
| 148 | +```javascript |
| 149 | +import { TransitionPortal } from "gatsby-plugin-transition-link" |
| 150 | +``` |
| 151 | +
|
| 152 | +```javascript |
| 153 | +<TransitionPortal> |
| 154 | + <SomeComponent> |
| 155 | + This component will sit on top of both pages, and persist through page |
| 156 | + transitions. |
| 157 | + </SomeComponent> |
| 158 | +</TransitionPortal> |
| 159 | +``` |
| 160 | +
|
| 161 | +As always, check out [the `TransitionPortal` docs](https://transitionlink.tylerbarnes.ca/docs/transitionportal/) for more information about `TransitionPortal`. |
| 162 | +
|
| 163 | +## Further reading |
| 164 | +
|
| 165 | +- [Official documentation](https://transitionlink.tylerbarnes.ca/docs/) |
| 166 | +- [Source code for plugin](https://github.com/TylerBarnes/gatsby-plugin-transition-link) |
| 167 | +- [Demo site](https://gatsby-plugin-transition-link.netlify.com/) |
| 168 | +- [Blog post: 'Per-Link Gatsby page transitions with TransitionLink'](https://www.gatsbyjs.org/blog/2018-12-04-per-link-gatsby-page-transitions-with-transitionlink/) |
| 169 | +- [Using transition-link with react-spring](https://github.com/TylerBarnes/gatsby-plugin-transition-link/issues/34) |
0 commit comments