1
1
import { LitElement , PropertyValueMap , css , html } from 'lit' ;
2
2
import { property , query , state } from 'lit/decorators.js' ;
3
3
import { UUIModalSidebarElement } from './uui-modal-sidebar.element' ;
4
- import { UUIModalElement } from './uui-modal.element' ;
4
+ import { UUIModalCloseEvent , UUIModalElement } from './uui-modal.element' ;
5
5
import { defineElement } from '@umbraco-ui/uui-base/lib/registration' ;
6
6
7
7
@defineElement ( 'uui-modal-container' )
@@ -23,7 +23,7 @@ export class UUIModalContainerElement extends LitElement {
23
23
24
24
constructor ( ) {
25
25
super ( ) ;
26
- this . addEventListener ( 'close' , this . #onClose ) ;
26
+ this . addEventListener ( UUIModalCloseEvent , this . #onCloseModalClose ) ;
27
27
}
28
28
29
29
protected firstUpdated (
@@ -38,13 +38,29 @@ export class UUIModalContainerElement extends LitElement {
38
38
}
39
39
40
40
#onSlotChange = ( ) => {
41
+ const existingModals = this . _modals ?? [ ] ;
42
+
41
43
this . _modals =
42
44
( this . modalSlot
43
45
?. assignedElements ( { flatten : true } )
44
46
. filter (
45
47
el => el instanceof UUIModalElement
46
48
) as Array < UUIModalElement > ) ?? [ ] ;
47
49
50
+ const oldModals = existingModals . filter (
51
+ modal => this . _modals ! . indexOf ( modal ) === - 1
52
+ ) ;
53
+ oldModals . forEach ( modal =>
54
+ modal . removeEventListener ( UUIModalCloseEvent , this . #onCloseModalClose)
55
+ ) ;
56
+
57
+ const newModals = this . _modals . filter (
58
+ modal => existingModals . indexOf ( modal ) === - 1
59
+ ) ;
60
+ newModals . forEach ( modal =>
61
+ modal . addEventListener ( UUIModalCloseEvent , this . #onCloseModalClose)
62
+ ) ;
63
+
48
64
this . _sidebars = this . _modals . filter (
49
65
el => el instanceof UUIModalSidebarElement
50
66
) as Array < UUIModalSidebarElement > ;
@@ -58,7 +74,13 @@ export class UUIModalContainerElement extends LitElement {
58
74
this . #updateSidebars( ) ;
59
75
} ;
60
76
61
- #onClose = ( ) => {
77
+ #onCloseModalClose = ( event : Event ) => {
78
+ event . stopImmediatePropagation ( ) ;
79
+
80
+ event . target ?. removeEventListener (
81
+ UUIModalCloseEvent ,
82
+ this . #onCloseModalClose
83
+ ) ;
62
84
if ( ! this . _modals || this . _modals . length <= 1 ) {
63
85
this . removeAttribute ( 'backdrop' ) ;
64
86
return ;
@@ -99,7 +121,15 @@ export class UUIModalContainerElement extends LitElement {
99
121
for ( let i = 0 ; i < reversed . length ; i ++ ) {
100
122
const sidebar = reversed [ i ] ;
101
123
const nextSidebar = reversed [ i + 1 ] ;
102
- sidebar . style . setProperty ( '--uui-modal-offset' , sidebarOffset + 'px' ) ;
124
+ const tempSidebarOffset = sidebarOffset ; // Cache to prevent it from being overwritten before the updateComplete.
125
+ // The sidebar checks it's own width at sets it's --uui-modal-offset to negative that width.
126
+ // This enables it to slide in from the right. So we need to set the new --uui-modal-offset after the updateComplete.
127
+ sidebar . updateComplete . then ( ( ) => {
128
+ sidebar . style . setProperty (
129
+ '--uui-modal-offset' ,
130
+ tempSidebarOffset + 'px'
131
+ ) ;
132
+ } ) ;
103
133
104
134
// Stop the calculations if the next sidebar is hidden
105
135
if ( nextSidebar ?. hasAttribute ( 'hide' ) ) break ;
0 commit comments