@@ -3,6 +3,10 @@ import { forceReflow } from 'swup';
3
3
import Plugin from '@swup/plugin' ;
4
4
5
5
declare module 'swup' {
6
+ export interface HookDefinitions {
7
+ 'content:insert' : { containers : ContainerSet [ ] } ;
8
+ 'content:remove' : { containers : ContainerSet [ ] } ;
9
+ }
6
10
export interface VisitAnimation {
7
11
/** Parallel visit: run in and out animation at the same time */
8
12
parallel ?: boolean ;
@@ -13,7 +17,7 @@ type PluginOptions = {
13
17
/** Containers to animate in parallel */
14
18
containers : string [ ] ;
15
19
/** Number of previous containers to keep around after the animation */
16
- keep : number | { [ container : string ] : number } ;
20
+ keep : number | { [ container : string ] : number } ;
17
21
} ;
18
22
19
23
type ContainerSet = {
@@ -57,6 +61,10 @@ export default class SwupParallelPlugin extends Plugin {
57
61
this . options . containers = this . swup . options . containers ;
58
62
}
59
63
64
+ // Create new hooks
65
+ this . swup . hooks . create ( 'content:insert' ) ;
66
+ this . swup . hooks . create ( 'content:remove' ) ;
67
+
60
68
// On visit: check for containers and mark as parallel visit
61
69
// Run after user hooks to allow disabling parallel animations beforehand
62
70
this . on ( 'visit:start' , this . startVisit , { priority : 1 } ) ;
@@ -98,25 +106,28 @@ export default class SwupParallelPlugin extends Plugin {
98
106
return ;
99
107
}
100
108
101
- // Get info about parallel containers
102
- this . parallelContainers = this . getParallelContainersForVisit ( visit , page ) ;
109
+ // Get info about parallel containers and save for later cleanup
110
+ const containers = this . getParallelContainersForVisit ( visit , page ) ;
111
+ this . parallelContainers = containers ;
103
112
104
113
// Replace parallel containers ourselves
105
- for ( const { all, next, previous, keep, remove } of this . parallelContainers ) {
106
- all . forEach ( ( el , i ) => el . style . setProperty ( '--swup-parallel-container' , `${ i } ` ) ) ;
107
- previous . setAttribute ( 'aria-hidden' , 'true' ) ;
108
- previous . before ( next ) ;
109
-
110
- if ( visit . animation . animate ) {
111
- next . classList . add ( 'is-next-container' ) ;
112
- forceReflow ( next ) ;
113
- next . classList . remove ( 'is-next-container' ) ;
114
+ this . swup . hooks . call ( 'content:insert' , { containers } , ( ) => {
115
+ for ( const { all, next, previous, keep, remove } of containers ) {
116
+ all . forEach ( ( el , i ) => el . style . setProperty ( '--swup-parallel-container' , `${ i } ` ) ) ;
117
+ previous . setAttribute ( 'aria-hidden' , 'true' ) ;
118
+ previous . before ( next ) ;
119
+
120
+ if ( visit . animation . animate ) {
121
+ next . classList . add ( 'is-next-container' ) ;
122
+ forceReflow ( next ) ;
123
+ next . classList . remove ( 'is-next-container' ) ;
124
+ }
125
+
126
+ previous . classList . add ( 'is-previous-container' ) ;
127
+ keep . forEach ( ( el ) => el . classList . add ( 'is-kept-container' ) ) ;
128
+ remove . forEach ( ( el ) => el . classList . add ( 'is-removing-container' ) ) ;
114
129
}
115
-
116
- previous . classList . add ( 'is-previous-container' ) ;
117
- keep . forEach ( ( el ) => el . classList . add ( 'is-kept-container' ) ) ;
118
- remove . forEach ( ( el ) => el . classList . add ( 'is-removing-container' ) ) ;
119
- }
130
+ } ) ;
120
131
121
132
// Modify visit containers so swup will only replace non-parallel containers
122
133
this . originalContainers = visit . containers ;
@@ -133,10 +144,13 @@ export default class SwupParallelPlugin extends Plugin {
133
144
134
145
/** After each visit: remove previous containers */
135
146
protected cleanupContainers = ( ) => {
136
- for ( const { remove, next } of this . parallelContainers ) {
137
- remove . forEach ( ( el ) => el . remove ( ) ) ;
138
- next . classList . remove ( 'is-next-container' ) ;
139
- }
147
+ const containers = this . parallelContainers ;
148
+ this . swup . hooks . call ( 'content:remove' , { containers } , ( ) => {
149
+ for ( const { remove, next } of containers ) {
150
+ remove . forEach ( ( el ) => el . remove ( ) ) ;
151
+ next . classList . remove ( 'is-next-container' ) ;
152
+ }
153
+ } ) ;
140
154
this . parallelContainers = [ ] ;
141
155
} ;
142
156
@@ -188,10 +202,7 @@ export default class SwupParallelPlugin extends Plugin {
188
202
protected visitHasPotentialParallelAnimation ( visit : Visit ) {
189
203
// Checking for visit.animation.parallel !== false here allows explicitly
190
204
// disabling parallel animations in user hooks before this plugin executes
191
- return (
192
- visit . animation . parallel !== false &&
193
- this . visitHasParallelContainers ( visit )
194
- ) ;
205
+ return visit . animation . parallel !== false && this . visitHasParallelContainers ( visit ) ;
195
206
}
196
207
197
208
/** Check if any of a visit's containers are animated in parallel */
0 commit comments