11import { createAttachmentKey } from 'svelte/attachments' ;
2+ import { on } from 'svelte/events' ;
23
34/**
45 * @template Resolved
@@ -11,12 +12,12 @@ export function enhanceDialog(item, options) {
1112 /** @type {import('svelte/attachments').Attachment<HTMLDialogElement> } */
1213 [ createAttachmentKey ( ) ] : function ( dialog ) {
1314 if ( ! dialog . open ) dialog . showModal ( ) ;
15+ /** @type {(() => void)[] } */
16+ const offs = [ ] ;
1417
1518 // set up listener when user calls item.resolve manually
1619 /** @type {undefined | (() => void) } */
1720 let resumeResolution = undefined ;
18- /** @type {undefined | (() => void) } */
19- let offResolve = undefined ;
2021 /** @type {import('@svelte-put/async-stack').StackItemResolveListener<Resolved> } */
2122 const onResolve = ( { cancel } ) => {
2223 if ( options ?. preventResolution ) return cancel ( ) ;
@@ -34,19 +35,19 @@ export function enhanceDialog(item, options) {
3435 } ) ;
3536 } ;
3637 if ( options ?. delayResolution ) {
37- offResolve = item . onResolve ( onResolve ) ;
38+ offs . push ( item . onResolve ( onResolve ) ) ;
3839 }
3940 function onanimationend ( ) {
4041 if ( dialog . open ) return ;
4142 if ( options ?. delayResolution === 'animationend' ) {
4243 resumeResolution ?. ( ) ;
4344 }
4445 }
45- dialog . addEventListener ( 'animationend' , onanimationend ) ;
46+ offs . push ( on ( dialog , 'animationend' , onanimationend ) ) ;
4647
4748 // set up backdrop click handler
48- dialog . addEventListener ( 'click' , onclick ) ;
49- dialog . addEventListener ( 'clickbackdrop' , onclickbackdrop ) ;
49+ offs . push ( on ( dialog , 'click' , onclick ) ) ;
50+ offs . push ( on ( dialog , 'clickbackdrop' , onclickbackdrop ) ) ;
5051
5152 // if dialog is setup with "method=dialog" form / inputs
5253 // this will help capture without the need for JavaScript
@@ -55,7 +56,7 @@ export function enhanceDialog(item, options) {
5556 /** @type {Resolved } */ ( dialog . returnValue ) || options ?. defaultReturnValue || undefined ,
5657 ) ;
5758 }
58- dialog . addEventListener ( 'close' , onclose ) ;
59+ offs . push ( on ( dialog , 'close' , onclose ) ) ;
5960
6061 // prevent dialog from closing if specified
6162 /** @param {Event } event */
@@ -64,7 +65,7 @@ export function enhanceDialog(item, options) {
6465 event . preventDefault ( ) ;
6566 }
6667 }
67- dialog . addEventListener ( 'cancel' , oncancel ) ;
68+ offs . push ( on ( dialog , 'cancel' , oncancel ) ) ;
6869
6970 // prevent escape from closing dialog if specified
7071 // this is only needed on Chrome where the cancel event isn't fired
@@ -76,17 +77,11 @@ export function enhanceDialog(item, options) {
7677 event . preventDefault ( ) ;
7778 }
7879 }
79- window . addEventListener ( 'keydown' , onkeydown ) ;
80+ offs . push ( on ( window , 'keydown' , onkeydown ) ) ;
8081
8182 return ( ) => {
82- offResolve ?. ( ) ;
83+ offs . forEach ( ( off ) => off ( ) ) ;
8384 resumeResolution ?. ( ) ;
84- dialog . removeEventListener ( 'animationend' , onanimationend ) ;
85- dialog . removeEventListener ( 'click' , onclick ) ;
86- dialog . removeEventListener ( 'clickbackdrop' , onclickbackdrop ) ;
87- dialog . removeEventListener ( 'close' , onclose ) ;
88- dialog . removeEventListener ( 'cancel' , oncancel ) ;
89- window . removeEventListener ( 'keydown' , onkeydown ) ;
9085 } ;
9186 } ,
9287 } ;
0 commit comments