Skip to content

Commit ba36f8d

Browse files
alexieyizheDSchau
authored andcommitted
feat(docs): Add official documentation for gatsby-plugin-transition-link and page transitions (#11423)
## Description This PR adds a new article about creating page transitions with `gatsby-plugin-transition-link`. It was based on issue #11333. I also made the actual article a subsection underneath a general 'Adding Page Transitions' section and created a 'Adding page transitions with page-transitions' article (just a stub for now), since @calcsam mentioned in the issue that eventually documentation for that plugin will be added so it seems logical. I tried to make it detailed enough to cover most facets of the plugin, but also not too detailed as the documentation is already pretty good and it was mentioned that this should be slightly more beginner friendly. This is my first PR contributing, so any help or criticism is appreciated :D Here's how it looks: ![localhost_8000_docs_adding-page-transitions-with-plugin-transition-link_](https://user-images.githubusercontent.com/17508679/52004927-66ce4800-2496-11e9-8b3c-61ee7763340a.png) ## Related Issues Resolves #11333
1 parent 4b9b6a1 commit ba36f8d

4 files changed

+196
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Adding Page Transitions with gatsby-plugin-page-transitions
3+
---
4+
5+
`gatsby-plugin-page-transitions` is a Gatsby plugin that allows you to add page transitions to your Gatsby site.
6+
7+
This guide will cover the setup and the various ways you can utilize this plugin.
8+
9+
This is a stub. Help our community expand it.
10+
11+
Please use the [Gatsby Style Guide](/docs/gatsby-style-guide/) to ensure your
12+
pull request gets accepted.
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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)

docs/docs/adding-page-transitions.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Adding Page Transitions
3+
overview: true
4+
---
5+
6+
Page transitions give users a better experience when navigating between pages. There are a number of ways to add them to your Gatsby site.
7+
8+
[[guidelist]]

www/src/data/sidebars/doc-links.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@
253253
link: /docs/adding-an-rss-feed/
254254
- title: Linking Between Pages
255255
link: /docs/linking-between-pages/
256+
- title: Adding Page Transitions
257+
link: /docs/adding-page-transitions/
258+
items:
259+
- title: Adding Page Transitions with gatsby-plugin-transition-link
260+
link: /docs/adding-page-transitions-with-plugin-transition-link/
261+
- title: Adding Page Transitions with gatsby-plugin-page-transitions*
262+
link: /docs/adding-page-transitions-with-plugin-page-transitions/
256263
- title: Making Your Site Accessible
257264
link: /docs/making-your-site-accessible
258265
- title: Routing

0 commit comments

Comments
 (0)