@@ -3,6 +3,10 @@ import { forceReflow } from 'swup';
33import Plugin from '@swup/plugin' ;
44
55declare module 'swup' {
6+ export interface HookDefinitions {
7+ 'content:insert' : { containers : ContainerSet [ ] } ;
8+ 'content:remove' : { containers : ContainerSet [ ] } ;
9+ }
610 export interface VisitAnimation {
711 /** Parallel visit: run in and out animation at the same time */
812 parallel ?: boolean ;
@@ -13,7 +17,7 @@ type PluginOptions = {
1317 /** Containers to animate in parallel */
1418 containers : string [ ] ;
1519 /** Number of previous containers to keep around after the animation */
16- keep : number | { [ container : string ] : number } ;
20+ keep : number | { [ container : string ] : number } ;
1721} ;
1822
1923type ContainerSet = {
@@ -57,6 +61,10 @@ export default class SwupParallelPlugin extends Plugin {
5761 this . options . containers = this . swup . options . containers ;
5862 }
5963
64+ // Create new hooks
65+ this . swup . hooks . create ( 'content:insert' ) ;
66+ this . swup . hooks . create ( 'content:remove' ) ;
67+
6068 // On visit: check for containers and mark as parallel visit
6169 // Run after user hooks to allow disabling parallel animations beforehand
6270 this . on ( 'visit:start' , this . startVisit , { priority : 1 } ) ;
@@ -98,25 +106,28 @@ export default class SwupParallelPlugin extends Plugin {
98106 return ;
99107 }
100108
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 ;
103112
104113 // 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' ) ) ;
114129 }
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+ } ) ;
120131
121132 // Modify visit containers so swup will only replace non-parallel containers
122133 this . originalContainers = visit . containers ;
@@ -133,10 +144,13 @@ export default class SwupParallelPlugin extends Plugin {
133144
134145 /** After each visit: remove previous containers */
135146 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+ } ) ;
140154 this . parallelContainers = [ ] ;
141155 } ;
142156
@@ -188,10 +202,7 @@ export default class SwupParallelPlugin extends Plugin {
188202 protected visitHasPotentialParallelAnimation ( visit : Visit ) {
189203 // Checking for visit.animation.parallel !== false here allows explicitly
190204 // 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 ) ;
195206 }
196207
197208 /** Check if any of a visit's containers are animated in parallel */
0 commit comments