@@ -48,6 +48,7 @@ type ConstructorParams<T> = {
4848 customEscapeHandler ?: ( e : KeyboardEvent ) => void ;
4949 customWrapperClickHandler ?: ( e : MouseEvent ) => void ;
5050 setup ?: ( modal : HTMLElement ) => Promise < void > ;
51+ cleanup ?: ( ) => Promise < void > ;
5152} ;
5253
5354const DEFAULT_ANIMATION_DURATION = 125 ;
@@ -73,6 +74,7 @@ export default class AnimatedModal<
7374 private customEscapeHandler : ( ( e : KeyboardEvent ) => void ) | undefined ;
7475 private customWrapperClickHandler : ( ( e : MouseEvent ) => void ) | undefined ;
7576 private setup : ( ( modal : HTMLElement ) => Promise < void > ) | undefined ;
77+ private cleanup : ( ( ) => Promise < void > ) | undefined ;
7678
7779 constructor ( constructorParams : ConstructorParams < IncomingModalChainData > ) {
7880 if ( constructorParams . dialogId . startsWith ( "#" ) ) {
@@ -123,6 +125,7 @@ export default class AnimatedModal<
123125 this . customWrapperClickHandler =
124126 constructorParams ?. customWrapperClickHandler ;
125127 this . setup = constructorParams ?. setup ;
128+ this . cleanup = constructorParams ?. cleanup ;
126129
127130 Skeleton . save ( this . dialogId ) ;
128131 }
@@ -132,24 +135,26 @@ export default class AnimatedModal<
132135 }
133136
134137 async runSetup ( ) : Promise < void > {
135- this . wrapperEl . addEventListener ( "keydown" , ( e ) => {
138+ this . wrapperEl . addEventListener ( "keydown" , async ( e ) => {
136139 if ( e . key === "Escape" && isPopupVisible ( this . dialogId ) ) {
137140 e . preventDefault ( ) ;
138141 e . stopPropagation ( ) ;
139142 if ( this . customEscapeHandler !== undefined ) {
140143 this . customEscapeHandler ( e ) ;
144+ void this . cleanup ?.( ) ;
141145 } else {
142- void this . hide ( ) ;
146+ await this . hide ( ) ;
143147 }
144148 }
145149 } ) ;
146150
147- this . wrapperEl . addEventListener ( "mousedown" , ( e ) => {
151+ this . wrapperEl . addEventListener ( "mousedown" , async ( e ) => {
148152 if ( e . target === this . wrapperEl ) {
149153 if ( this . customWrapperClickHandler !== undefined ) {
150154 this . customWrapperClickHandler ( e ) ;
155+ void this . cleanup ?.( ) ;
151156 } else {
152- void this . hide ( ) ;
157+ await this . hide ( ) ;
153158 }
154159 }
155160 } ) ;
@@ -392,6 +397,7 @@ export default class AnimatedModal<
392397 Skeleton . remove ( this . dialogId ) ;
393398 this . open = false ;
394399 await options ?. afterAnimation ?.( this . modalEl ) ;
400+ void this . cleanup ?.( ) ;
395401
396402 if (
397403 this . previousModalInChain !== undefined &&
@@ -428,6 +434,7 @@ export default class AnimatedModal<
428434 Skeleton . remove ( this . dialogId ) ;
429435 this . open = false ;
430436 await options ?. afterAnimation ?.( this . modalEl ) ;
437+ void this . cleanup ?.( ) ;
431438
432439 if (
433440 this . previousModalInChain !== undefined &&
@@ -453,6 +460,7 @@ export default class AnimatedModal<
453460 destroy ( ) : void {
454461 this . wrapperEl . close ( ) ;
455462 this . wrapperEl . classList . add ( "hidden" ) ;
463+ void this . cleanup ?.( ) ;
456464 Skeleton . remove ( this . dialogId ) ;
457465 this . open = false ;
458466 }
0 commit comments