Skip to content

Commit 248895a

Browse files
committed
Merge branch 'development'
2 parents 344cada + 245e577 commit 248895a

4 files changed

Lines changed: 64 additions & 39 deletions

File tree

CHANGELOG.md

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

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

5+
## [0.1.1] - 2023-07-26
6+
7+
- Bump dependencies
8+
9+
## [0.1.0] - 2023-07-25
10+
11+
- Refactor
12+
513
## [0.0.4] - 2023-07-22
614

715
- Refactor
@@ -18,8 +26,10 @@
1826

1927
- Initial release
2028

21-
[Unreleased]: https://github.com/swup/parallel-plugin/compare/0.0.4...HEAD
29+
[Unreleased]: https://github.com/swup/parallel-plugin/compare/0.1.1...HEAD
2230

31+
[0.1.1]: https://github.com/swup/parallel-plugin/releases/tag/0.1.1
32+
[0.1.0]: https://github.com/swup/parallel-plugin/releases/tag/0.1.0
2333
[0.0.4]: https://github.com/swup/parallel-plugin/releases/tag/0.0.4
2434
[0.0.3]: https://github.com/swup/parallel-plugin/releases/tag/0.0.3
2535
[0.0.2]: https://github.com/swup/parallel-plugin/releases/tag/0.0.2

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
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 animating the previous and next page in parallel",
5-
"version": "0.0.4",
5+
"version": "0.1.1",
66
"type": "module",
77
"source": "src/index.ts",
88
"main": "./dist/index.cjs",
@@ -45,10 +45,10 @@
4545
"url": "https://github.com/swup/parallel-plugin.git"
4646
},
4747
"dependencies": {
48-
"@swup/plugin": "^3.0.0-rc.25"
48+
"@swup/plugin": "^3.0.0"
4949
},
5050
"peerDependencies": {
51-
"swup": "^4.0.0-rc.24"
51+
"swup": "^4.0.0"
5252
},
5353
"browserslist": [
5454
"extends @swup/browserslist-config"

src/index.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type PluginOptions = {
1414
};
1515

1616
type ContainerSet = {
17+
selector: string;
1718
previous: HTMLElement;
1819
next: HTMLElement;
1920
};
@@ -23,9 +24,7 @@ export default class SwupParallelPlugin extends Plugin {
2324

2425
requires = { swup: '>=4' };
2526

26-
defaults: PluginOptions = {
27-
containers: []
28-
};
27+
defaults: PluginOptions = { containers: [] };
2928
options: PluginOptions;
3029

3130
originalContainers: string[] | null = null;
@@ -59,39 +58,28 @@ export default class SwupParallelPlugin extends Plugin {
5958
startVisit: Handler<'visit:start'> = (visit) => {
6059
this.originalContainers = null;
6160

62-
if (!visit.animation.animate || visit.animation.parallel === false) {
63-
return;
64-
}
65-
66-
// Only mark as parallel visit if containers found
67-
if (this.visitHasParallelContainers(visit)) {
61+
// Only mark as parallel visit if containers found and animation matches
62+
if (this.visitHasPotentialParallelAnimation(visit)) {
6863
visit.animation.wait = true;
6964
visit.animation.parallel = true;
7065
}
7166
};
7267

7368
skipOutAnimation: Handler<'animation:out:await'> = (visit, args) => {
74-
if (visit.animation.animate && visit.animation.parallel) {
69+
if (this.isParallelVisit(visit)) {
7570
args.skip = true;
7671
}
7772
};
7873

7974
insertContainers: Handler<'content:replace'> = (visit, { page }) => {
80-
if (!visit.animation.animate || !visit.animation.parallel) {
81-
return;
82-
}
83-
84-
const parallelContainers = this.options.containers;
85-
const hasParallelContainers = parallelContainers.every((s) => visit.containers.includes(s));
86-
if (!hasParallelContainers) {
87-
console.warn(
88-
'[parallel-plugin] Parallel containers not found in list of replaced containers'
89-
);
75+
if (!this.isParallelVisit(visit)) {
9076
return;
9177
}
9278

9379
// Replace parallel containers ourselves
94-
this.parseContainers(page).forEach(({ previous, next }) => {
80+
const containerSets = this.getContainersForVisit(visit, page);
81+
const parallelContainers = containerSets.map(({ selector }) => selector);
82+
containerSets.forEach(({ previous, next }) => {
9583
this.previousContainers.push(previous);
9684
this.nextContainers.push(next);
9785

@@ -122,15 +110,42 @@ export default class SwupParallelPlugin extends Plugin {
122110
this.nextContainers = [];
123111
};
124112

125-
parseContainers({ html }: { html: string }): ContainerSet[] {
113+
getContainersForVisit(visit: Visit, { html }: { html: string }): ContainerSet[] {
114+
const { containers: parallelContainers } = this.options;
115+
const containersInVisit = parallelContainers.filter((s) => visit.containers.includes(s));
116+
if (!containersInVisit.length) {
117+
console.warn('No parallel containers found in list of replaced containers');
118+
return [];
119+
}
120+
126121
const incomingDocument = new DOMParser().parseFromString(html, 'text/html');
127-
return this.options.containers.reduce((containers, selector: string) => {
122+
123+
return containersInVisit.reduce((containers, selector: string) => {
128124
const previous = document.querySelector<HTMLElement>(selector);
129125
const next = incomingDocument.querySelector<HTMLElement>(selector);
130-
return previous && next ? [...containers, { previous, next }] : containers;
126+
return previous && next ? [...containers, { selector, previous, next }] : containers;
131127
}, [] as ContainerSet[]);
132128
}
133129

130+
isParallelVisit(visit: Visit) {
131+
return visit.animation.animate && visit.animation.parallel;
132+
}
133+
134+
markVisitAsParallelAnimation(visit: Visit) {
135+
visit.animation.wait = true;
136+
visit.animation.parallel = true;
137+
}
138+
139+
visitHasPotentialParallelAnimation(visit: Visit) {
140+
// Checking for visit.animation.parallel !== false here allows explicitly
141+
// disabling parallel animations in user hooks before this plugin executes
142+
return (
143+
visit.animation.animate &&
144+
visit.animation.parallel !== false &&
145+
this.visitHasParallelContainers(visit)
146+
);
147+
}
148+
134149
visitHasParallelContainers(visit: Visit) {
135150
return this.options.containers.some((selector) => {
136151
const container = document.querySelector(selector);

0 commit comments

Comments
 (0)