A swup plugin for managing animations in JS.
Install the plugin from npm and import it into your bundle.
npm install @swup/js-plugin
import SwupJsPlugin from '@swup/js-plugin';
Or include the minified production file from a CDN:
<script src="https://unpkg.com/@swup/js-plugin@3"></script>
To run this plugin, include an instance in the swup options.
const swup = new Swup({
plugins: [
new SwupJsPlugin({ animations: [ /* your custom animation functions */ ] })
]
});
The plugin expects an array
of animation objects.
The example below is the default setup and defines two animations, where out
is the
animation function being executed before the content is being replaced, and in
is
the animation being executed after the content is replaced:
{
animations: [
{
from: '(.*)', // matches any route
to: '(.*)', // matches any route
out: done => done(), // immediately continues
in: done => done() // immediately continues
}
]
}
This is also the fallback animation in case no other matching animations were found.
Animations are chosen based on the from
and to
properties of the object, which are
compared against the current visit (urls of current and next page).
Learn more on choosing the animation below.
The animation function is executed for each corresponding animation phase. Inside the animation
function, you manage the animation yourself and signal when it has finished. It receives two
arguments: a done
function and a data
object.
out: (done, data) => {
// Signal the end of the animation by calling done()
// Access info about the animation inside the data argument
}
Calling the done()
function signals to swup that the animation has finished and it can proceed
to the next step: replacing the content or finishing the visit. You can pass along the done()
function as a callback to your animation library. The example below will wait for two seconds before replacing the content.
out: (done) => {
setTimeout(done, 2000);
}
If your animation library returns Promises, you can also return the Promise directly from your
animation function. Swup will consider the animation to be finished when the Promise resolves.
The done
function is then no longer required.
out: () => {
return myAnimationLibrary.animate(/* */).then(() => {});
}
This also allows async/await
syntax for convenience.
out: async () => {
await myAnimationLibrary.animate(/* */);
}
The second parameter is an object that contains useful data about the animation, such as the visit
object (containing actual before/after routes), the from
and to
parameters of the
animation object, and the route params.
{
visit: { /* */ }, // swup global visit object
direction: 'in',
from: {
url: '/',
pattern: '(.*)',
params: {}
},
to: {
url: '/about',
pattern: '(.*)',
params: {}
}
}
Basic usage examples for a fade transition implemented in popular animation libraries:
{
from: '(.*)',
to: '(.*)',
in: async () => {
const container = document.querySelector('#swup');
await container.animate([{ opacity: 0 }, { opacity: 1 }], 250).finished;
},
out: async () => {
const container = document.querySelector('#swup');
await container.animate([{ opacity: 1 }, { opacity: 0 }], 250).finished;
}
}
{
from: '(.*)',
to: '(.*)',
out: async () => {
await gsap.to('#swup', { opacity: 0, duration: 0.25 });
},
in: async () => {
await gsap.fromTo('#swup', { opacity: 0 }, { opacity: 1, duration: 0.25 });
}
}
{
from: '(.*)',
to: '(.*)',
out: async () => {
await anime({ targets: '#swup', opacity: 0, duration: 250, easing: 'linear' }).finished;
},
in: async () => {
await anime({ targets: '#swup', opacity: [0, 1], duration: 250, easing: 'linear' }).finished;
}
}
As mentioned above, the animation is chosen based on the from
and to
properties of the animation object.
Those properties can take several forms:
/foo/:bar
) parsed by path-to-regexpdata-swup-animation
attribute of the clicked linkThe most fitting route is always chosen. Keep in mind, that two routes can be evaluated as "same fit". In this case, the first one defined in the options is used, so usually you would like to define the more specific routes first. See the example below for more info.
[
// animation 1
{ from: '/', to: 'custom' },
// animation 2
{ from: '/', to: '/post' },
// animation 3
{ from: '/', to: '/post/:id' },
// animation 4
{ from: '/', to: /pos(.*)/ },
// animation 5
{ from: '(.*)', to: '(.*)' },
];
/
to /post
→ animation 2/
to /posting
→ animation 4/
to /post/12
→ animation 3/
to /some-route
→ animation 5/
to /post
with data-swup-animation="custom"
→ animation 1