Skip to content

Commit 5e7f15c

Browse files
committed
Merge branch 'development'
2 parents dba39ba + 731d9b3 commit 5e7f15c

5 files changed

Lines changed: 45 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
<!-- ## [Unreleased] -->
44

5+
## [0.0.4] - 2023-07-22
6+
7+
- Refactor
8+
59
## [0.0.3] - 2023-07-22
610

711
- Detect parallel containers of current visit
@@ -14,8 +18,9 @@
1418

1519
- Initial release
1620

17-
[Unreleased]: https://github.com/swup/parallel-plugin/compare/0.0.3...HEAD
21+
[Unreleased]: https://github.com/swup/parallel-plugin/compare/0.0.4...HEAD
1822

23+
[0.0.4]: https://github.com/swup/parallel-plugin/releases/tag/0.0.4
1924
[0.0.3]: https://github.com/swup/parallel-plugin/releases/tag/0.0.3
2025
[0.0.2]: https://github.com/swup/parallel-plugin/releases/tag/0.0.2
2126
[0.0.1]: https://github.com/swup/parallel-plugin/releases/tag/0.0.1

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ the animation, parallel animations can be controlled using the classes `is-previ
131131

132132
## Timing
133133

134-
Technically, the plugin will skip the out-animation, add the next container, wait for animations to
135-
finish, then remove the previous container. All animations now happen in the in-phase of the
134+
Technically, the plugin will **skip the out-animation**, add the next container, wait for animations to
135+
finish, then remove the previous container. **All animations now happen in the in-phase** of the
136136
lifecycle, after the `content:replace` hook that normally marks the middle of the animation
137137
process. Any containers that are not animated or not animated in parallel (e.g. a static header)
138138
will be replaced at the start of the parallel animation.
139139

140+
This also applies when using the [JS plugin](https://swup.js.org/plugins/js-plugin/): the
141+
out-animation is skipped entirely and only the in-animation is executed. You'll need to perform
142+
both animations in the `in` handler.
143+
140144
## Options
141145

142146
### containers

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@swup/parallel-plugin",
33
"amdName": "SwupParallelPlugin",
44
"description": "A swup plugin for running the in and out animations in parallel",
5-
"version": "0.0.3",
5+
"version": "0.0.4",
66
"type": "module",
77
"source": "src/index.ts",
88
"main": "./dist/index.cjs",

src/index.ts

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Options as SwupOptions, Handler } from 'swup';
1+
import type { Handler, Visit } from 'swup';
22
import { forceReflow } from 'swup';
33
import Plugin from '@swup/plugin';
44

@@ -10,7 +10,7 @@ declare module 'swup' {
1010
}
1111

1212
type PluginOptions = {
13-
containers: SwupOptions['containers'];
13+
containers: string[];
1414
};
1515

1616
type ContainerSet = {
@@ -28,7 +28,7 @@ export default class SwupParallelPlugin extends Plugin {
2828
};
2929
options: PluginOptions;
3030

31-
originalContainers: SwupOptions['containers'] = [];
31+
originalContainers: string[] | null = null;
3232
previousContainers: Element[] = [];
3333
nextContainers: Element[] = [];
3434

@@ -57,58 +57,41 @@ export default class SwupParallelPlugin extends Plugin {
5757
}
5858

5959
startVisit: Handler<'visit:start'> = (visit) => {
60-
const { animate, parallel } = visit.animation;
61-
const { containers } = this.options;
62-
if (!animate || parallel === false) {
60+
this.originalContainers = null;
61+
62+
if (!visit.animation.animate || visit.animation.parallel === false) {
6363
return;
6464
}
65+
6566
// Only mark as parallel visit if containers found
66-
const hasContainers = containers.some((selector) => {
67-
const el = document.querySelector(selector)
68-
if (!el) return false;
69-
return visit.containers.some(s => el.matches(s));
70-
});
71-
if (hasContainers) {
67+
if (this.visitHasParallelContainers(visit)) {
7268
visit.animation.wait = true;
7369
visit.animation.parallel = true;
7470
}
7571
};
7672

7773
skipOutAnimation: Handler<'animation:out:await'> = (visit, args) => {
78-
const { animate, parallel } = visit.animation;
79-
if (animate && parallel) {
74+
if (visit.animation.animate && visit.animation.parallel) {
8075
args.skip = true;
8176
}
8277
};
8378

84-
insertContainers: Handler<'content:replace'> = (visit, args) => {
85-
const { animate, parallel } = visit.animation;
86-
const { containers } = visit;
87-
const { page } = args;
88-
89-
if (!animate || !parallel) {
79+
insertContainers: Handler<'content:replace'> = (visit, { page }) => {
80+
if (!visit.animation.animate || !visit.animation.parallel) {
9081
return;
9182
}
9283

93-
const defaultContainers = [...containers];
94-
const containersInParallel = this.options.containers;
95-
const containersInSeries = defaultContainers.filter(
96-
(selector) => !containersInParallel.includes(selector)
97-
);
98-
const hasContainers = containersInParallel.every((selector) =>
99-
defaultContainers.includes(selector)
100-
);
101-
if (!hasContainers) {
84+
const parallelContainers = this.options.containers;
85+
const hasParallelContainers = parallelContainers.every((s) => visit.containers.includes(s));
86+
if (!hasParallelContainers) {
10287
console.warn(
103-
'[parallel-plugin] Parallel containers must be included in default containers'
88+
'[parallel-plugin] Parallel containers not found in list of replaced containers'
10489
);
10590
return;
10691
}
10792

10893
// Replace parallel containers ourselves
109-
110-
const parallelContainers = this.parseContainers(page);
111-
parallelContainers.forEach(({ previous, next }) => {
94+
this.parseContainers(page).forEach(({ previous, next }) => {
11295
this.previousContainers.push(previous);
11396
this.nextContainers.push(next);
11497

@@ -117,27 +100,25 @@ export default class SwupParallelPlugin extends Plugin {
117100

118101
next.classList.add('is-next-container');
119102
forceReflow(next);
120-
next.classList.remove('is-next-container');
121103
previous.classList.add('is-previous-container');
104+
next.classList.remove('is-next-container');
122105
});
123106

124-
this.originalContainers = defaultContainers;
125-
visit.containers = containersInSeries;
107+
// Hand all other non-parallel containers to swup
108+
this.originalContainers = visit.containers;
109+
visit.containers = visit.containers.filter((s) => !parallelContainers.includes(s));
126110
};
127111

128112
resetContainers: Handler<'content:replace'> = (visit) => {
129-
const { animate, parallel } = visit.animation;
130-
if (!animate || !parallel) {
131-
return;
113+
if (this.originalContainers) {
114+
visit.containers = this.originalContainers;
132115
}
133-
134-
visit.containers = this.originalContainers;
135116
};
136117

137118
cleanupContainers = () => {
138119
this.previousContainers.forEach((c) => c.remove());
139-
this.previousContainers = [];
140120
this.nextContainers.forEach((c) => c.classList.remove('is-next-container'));
121+
this.previousContainers = [];
141122
this.nextContainers = [];
142123
};
143124

@@ -149,4 +130,11 @@ export default class SwupParallelPlugin extends Plugin {
149130
return previous && next ? [...containers, { previous, next }] : containers;
150131
}, [] as ContainerSet[]);
151132
}
133+
134+
visitHasParallelContainers(visit: Visit) {
135+
return this.options.containers.some((selector) => {
136+
const container = document.querySelector(selector);
137+
return container?.matches(visit.containers.join(','));
138+
});
139+
}
152140
}

0 commit comments

Comments
 (0)