Skip to content

Commit dba39ba

Browse files
committed
Merge branch 'development'
2 parents d27f914 + 354080b commit dba39ba

File tree

5 files changed

+69
-76
lines changed

5 files changed

+69
-76
lines changed

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.3] - 2023-07-22
6+
7+
- Detect parallel containers of current visit
8+
59
## [0.0.2] - 2023-07-19
610

711
- Fix timing with forced reflow
@@ -10,7 +14,8 @@
1014

1115
- Initial release
1216

13-
[Unreleased]: https://github.com/swup/parallel-plugin/compare/0.0.2...HEAD
17+
[Unreleased]: https://github.com/swup/parallel-plugin/compare/0.0.3...HEAD
1418

19+
[0.0.3]: https://github.com/swup/parallel-plugin/releases/tag/0.0.3
1520
[0.0.2]: https://github.com/swup/parallel-plugin/releases/tag/0.0.2
1621
[0.0.1]: https://github.com/swup/parallel-plugin/releases/tag/0.0.1

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
A [swup](https://swup.js.org) plugin for running the in and out animations in parallel.
44

5-
- Keep the previous container during the page transition
6-
- Animate both the previous and current containers at the same time
5+
- Keeps the previous page visible while the next page is entering
6+
- Implement simultaneous animations like crossfades, slideshows, or overlays
77

88
## Installation
99

@@ -149,15 +149,15 @@ animations in parallel.
149149

150150
### Opting out of parallel animations
151151

152-
The plugin will set a flag on the global context, indicating the current visit
153-
as a parallel animation: `context.animation.parallel`. You can unset this flag
152+
The plugin will set a flag on the visit object, indicating the current visit
153+
as a parallel animation: `visit.animation.parallel`. You can unset this flag
154154
to fall back to a normal animation with leave and enter in series.
155155

156156
```js
157157
// Disable parallel animations for this visit
158-
swup.hooks.on('visit:start', (context) => {
158+
swup.hooks.on('visit:start', (visit) => {
159159
if (someCondition) {
160-
context.animation.parallel = false;
160+
visit.animation.parallel = false;
161161
}
162162
});
163163
```

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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "@swup/parallel-plugin",
33
"amdName": "SwupParallelPlugin",
4-
"version": "0.0.2",
54
"description": "A swup plugin for running the in and out animations in parallel",
5+
"version": "0.0.3",
66
"type": "module",
77
"source": "src/index.ts",
88
"main": "./dist/index.cjs",
@@ -32,16 +32,23 @@
3232
"email": "daun@daun.ltd",
3333
"url": "https://philippdaun.net"
3434
},
35+
"contributors": [
36+
{
37+
"name": "Rasso Hilber",
38+
"email": "mail@rassohilber.com",
39+
"url": "https://rassohilber.com"
40+
}
41+
],
3542
"license": "MIT",
3643
"repository": {
3744
"type": "git",
3845
"url": "https://github.com/swup/parallel-plugin.git"
3946
},
4047
"dependencies": {
41-
"@swup/plugin": "^3.0.0-rc.22"
48+
"@swup/plugin": "^3.0.0-rc.25"
4249
},
4350
"peerDependencies": {
44-
"swup": "^4.0.0-rc.21"
51+
"swup": "^4.0.0-rc.24"
4552
},
4653
"browserslist": [
4754
"extends @swup/browserslist-config"

src/index.ts

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Options as SwupOptions, Handler } from 'swup';
2-
import { nextTick } from 'swup';
2+
import { forceReflow } from 'swup';
33
import Plugin from '@swup/plugin';
44

55
declare module 'swup' {
6-
export interface AnimationContext {
6+
export interface VisitAnimation {
77
/** Parallel visit: run in and out animation at the same time */
88
parallel?: boolean;
99
}
@@ -44,55 +44,46 @@ export default class SwupParallelPlugin extends Plugin {
4444
}
4545

4646
// On visit: check for containers and mark as parallel visit
47-
this.swup.hooks.on('visit:start', this.startVisit, { priority: 1 });
48-
// When awaiting animation: skip if not in animation phase
49-
this.swup.hooks.replace('animation:await', this.maybeSkipAnimation);
47+
// Run after user hooks to allow disabling parallel animations beforehand
48+
this.on('visit:start', this.startVisit, { priority: 1 });
49+
// Before awaiting out animation: skip
50+
this.before('animation:out:await', this.skipOutAnimation, { priority: 1 });
5051
// Before content replace: insert new containers
51-
this.swup.hooks.before('content:replace', this.insertContainers);
52-
// After content replace: reset containers in context object
53-
this.swup.hooks.on('content:replace', this.resetContainers);
52+
this.before('content:replace', this.insertContainers, { priority: 1 });
53+
// After content replace: reset containers
54+
this.on('content:replace', this.resetContainers);
5455
// After visit: remove old containers
55-
this.swup.hooks.on('visit:end', this.cleanupContainers);
56+
this.on('visit:end', this.cleanupContainers);
5657
}
5758

58-
unmount() {
59-
this.swup.hooks.off('visit:start', this.startVisit);
60-
this.swup.hooks.off('animation:await', this.maybeSkipAnimation);
61-
this.swup.hooks.off('content:replace', this.insertContainers);
62-
this.swup.hooks.off('content:replace', this.resetContainers);
63-
this.swup.hooks.off('visit:end', this.cleanupContainers);
64-
}
65-
66-
startVisit: Handler<'visit:start'> = (context) => {
67-
const { animate, parallel } = context.animation;
59+
startVisit: Handler<'visit:start'> = (visit) => {
60+
const { animate, parallel } = visit.animation;
6861
const { containers } = this.options;
6962
if (!animate || parallel === false) {
70-
console.log('Not animated or parallel disabled');
7163
return;
7264
}
73-
7465
// Only mark as parallel visit if containers found
75-
const hasContainers = containers.some((selector) => document.querySelector(selector));
76-
console.log('Checking for parallel containers', hasContainers, containers);
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+
});
7771
if (hasContainers) {
78-
context.animation.wait = true;
79-
context.animation.parallel = true;
72+
visit.animation.wait = true;
73+
visit.animation.parallel = true;
8074
}
8175
};
8276

83-
maybeSkipAnimation: Handler<'animation:await'> = (context, args, defaultHandler) => {
84-
const { animate, parallel } = context.animation;
85-
const { direction } = args;
86-
const isAnimationPhase = 'in' === direction;
87-
if (animate && parallel && !isAnimationPhase) {
88-
return Promise.resolve();
77+
skipOutAnimation: Handler<'animation:out:await'> = (visit, args) => {
78+
const { animate, parallel } = visit.animation;
79+
if (animate && parallel) {
80+
args.skip = true;
8981
}
90-
return defaultHandler?.(context, args);
9182
};
9283

93-
insertContainers: Handler<'content:replace'> = (context, args) => {
94-
const { animate, parallel } = context.animation;
95-
const { containers } = context;
84+
insertContainers: Handler<'content:replace'> = (visit, args) => {
85+
const { animate, parallel } = visit.animation;
86+
const { containers } = visit;
9687
const { page } = args;
9788

9889
if (!animate || !parallel) {
@@ -125,26 +116,22 @@ export default class SwupParallelPlugin extends Plugin {
125116
previous.before(next);
126117

127118
next.classList.add('is-next-container');
128-
this.forceReflow(next);
119+
forceReflow(next);
129120
next.classList.remove('is-next-container');
130121
previous.classList.add('is-previous-container');
131122
});
132123

133-
console.log('containersInParallel', containersInParallel);
134-
console.log('containersInSeries', containersInSeries);
135-
console.log('parallelContainers', parallelContainers);
136-
137124
this.originalContainers = defaultContainers;
138-
context.containers = containersInSeries;
125+
visit.containers = containersInSeries;
139126
};
140127

141-
resetContainers: Handler<'content:replace'> = (context) => {
142-
const { animate, parallel } = context.animation;
128+
resetContainers: Handler<'content:replace'> = (visit) => {
129+
const { animate, parallel } = visit.animation;
143130
if (!animate || !parallel) {
144131
return;
145132
}
146133

147-
context.containers = this.originalContainers;
134+
visit.containers = this.originalContainers;
148135
};
149136

150137
cleanupContainers = () => {
@@ -156,16 +143,10 @@ export default class SwupParallelPlugin extends Plugin {
156143

157144
parseContainers({ html }: { html: string }): ContainerSet[] {
158145
const incomingDocument = new DOMParser().parseFromString(html, 'text/html');
159-
return this.options.containers
160-
.reduce((containers, selector: string) => {
161-
const previous = document.querySelector<HTMLElement>(selector);
162-
const next = incomingDocument.querySelector<HTMLElement>(selector);
163-
return previous && next ? [...containers, { previous, next }] : containers;
164-
}, [] as ContainerSet[]);
165-
}
166-
167-
forceReflow(element?: HTMLElement) {
168-
element = element || document.body;
169-
return element?.offsetHeight;
146+
return this.options.containers.reduce((containers, selector: string) => {
147+
const previous = document.querySelector<HTMLElement>(selector);
148+
const next = incomingDocument.querySelector<HTMLElement>(selector);
149+
return previous && next ? [...containers, { previous, next }] : containers;
150+
}, [] as ContainerSet[]);
170151
}
171152
}

0 commit comments

Comments
 (0)