@@ -12,15 +12,10 @@ import {
12
12
VNode ,
13
13
Watch ,
14
14
} from "@stencil/core" ;
15
- import {
16
- ConditionalSlotComponent ,
17
- connectConditionalSlotComponent ,
18
- disconnectConditionalSlotComponent ,
19
- } from "../../utils/conditionalSlot" ;
20
15
import {
21
16
ensureId ,
22
17
focusFirstTabbable ,
23
- getSlotted ,
18
+ slotChangeGetAssignedElements ,
24
19
slotChangeHasAssignedElement ,
25
20
} from "../../utils/dom" ;
26
21
import {
@@ -80,7 +75,6 @@ logger.deprecated("component", {
80
75
} )
81
76
export class Modal
82
77
implements
83
- ConditionalSlotComponent ,
84
78
OpenCloseComponent ,
85
79
FocusTrapComponent ,
86
80
LoadableComponent ,
@@ -194,8 +188,6 @@ export class Modal
194
188
this . mutationObserver ?. observe ( this . el , { childList : true , subtree : true } ) ;
195
189
this . cssVarObserver ?. observe ( this . el , { attributeFilter : [ "style" ] } ) ;
196
190
this . updateSizeCssVars ( ) ;
197
- this . updateFooterVisibility ( ) ;
198
- connectConditionalSlotComponent ( this ) ;
199
191
connectLocalized ( this ) ;
200
192
connectMessages ( this ) ;
201
193
connectFocusTrap ( this ) ;
@@ -205,7 +197,6 @@ export class Modal
205
197
this . removeOverflowHiddenClass ( ) ;
206
198
this . mutationObserver ?. disconnect ( ) ;
207
199
this . cssVarObserver ?. disconnect ( ) ;
208
- disconnectConditionalSlotComponent ( this ) ;
209
200
deactivateFocusTrap ( this ) ;
210
201
disconnectLocalized ( this ) ;
211
202
disconnectMessages ( this ) ;
@@ -238,7 +229,7 @@ export class Modal
238
229
< div class = { CSS . header } >
239
230
{ this . renderCloseButton ( ) }
240
231
< header class = { CSS . title } >
241
- < slot name = { CSS . header } />
232
+ < slot name = { CSS . header } onSlotchange = { this . handleHeaderSlotChange } />
242
233
</ header >
243
234
</ div >
244
235
{ this . renderContentTop ( ) }
@@ -249,7 +240,7 @@ export class Modal
249
240
} }
250
241
ref = { ( el ) => ( this . modalContent = el ) }
251
242
>
252
- < slot name = { SLOTS . content } />
243
+ < slot name = { SLOTS . content } onSlotchange = { this . handleContentSlotChange } />
253
244
</ div >
254
245
{ this . renderContentBottom ( ) }
255
246
{ this . renderFooter ( ) }
@@ -260,19 +251,19 @@ export class Modal
260
251
}
261
252
262
253
renderFooter ( ) : VNode {
263
- return this . hasFooter ? (
264
- < div class = { CSS . footer } key = "footer" >
254
+ return (
255
+ < div class = { CSS . footer } hidden = { ! this . hasFooter } key = "footer" >
265
256
< span class = { CSS . back } >
266
- < slot name = { SLOTS . back } />
257
+ < slot name = { SLOTS . back } onSlotchange = { this . handleBackSlotChange } />
267
258
</ span >
268
259
< span class = { CSS . secondary } >
269
- < slot name = { SLOTS . secondary } />
260
+ < slot name = { SLOTS . secondary } onSlotchange = { this . handleSecondarySlotChange } />
270
261
</ span >
271
262
< span class = { CSS . primary } >
272
- < slot name = { SLOTS . primary } />
263
+ < slot name = { SLOTS . primary } onSlotchange = { this . handlePrimarySlotChange } />
273
264
</ span >
274
265
</ div >
275
- ) : null ;
266
+ ) ;
276
267
}
277
268
278
269
renderContentTop ( ) : VNode {
@@ -357,7 +348,7 @@ export class Modal
357
348
modalContent : HTMLDivElement ;
358
349
359
350
private mutationObserver : MutationObserver = createObserver ( "mutation" , ( ) =>
360
- this . handleMutationObserver ( ) ,
351
+ this . updateFocusTrapElements ( ) ,
361
352
) ;
362
353
363
354
private cssVarObserver : MutationObserver = createObserver ( "mutation" , ( ) => {
@@ -380,7 +371,24 @@ export class Modal
380
371
381
372
@State ( ) cssHeight : string | number ;
382
373
383
- @State ( ) hasFooter = true ;
374
+ @State ( ) hasFooter = false ;
375
+
376
+ @Watch ( "hasBack" )
377
+ @Watch ( "hasPrimary" )
378
+ @Watch ( "hasSecondary" )
379
+ handleHasFooterChange ( ) : void {
380
+ this . hasFooter = this . hasBack || this . hasPrimary || this . hasSecondary ;
381
+ }
382
+
383
+ @State ( ) titleEl : HTMLElement ;
384
+
385
+ @State ( ) contentEl : HTMLElement ;
386
+
387
+ @State ( ) hasBack = false ;
388
+
389
+ @State ( ) hasPrimary = false ;
390
+
391
+ @State ( ) hasSecondary = false ;
384
392
385
393
@State ( ) hasContentTop = false ;
386
394
@@ -474,6 +482,26 @@ export class Modal
474
482
//
475
483
//--------------------------------------------------------------------------
476
484
485
+ private handleHeaderSlotChange = ( event : Event ) : void => {
486
+ this . titleEl = slotChangeGetAssignedElements < HTMLElement > ( event ) [ 0 ] ;
487
+ } ;
488
+
489
+ private handleContentSlotChange = ( event : Event ) : void => {
490
+ this . contentEl = slotChangeGetAssignedElements < HTMLElement > ( event ) [ 0 ] ;
491
+ } ;
492
+
493
+ private handleBackSlotChange = ( event : Event ) : void => {
494
+ this . hasBack = slotChangeHasAssignedElement ( event ) ;
495
+ } ;
496
+
497
+ private handlePrimarySlotChange = ( event : Event ) : void => {
498
+ this . hasPrimary = slotChangeHasAssignedElement ( event ) ;
499
+ } ;
500
+
501
+ private handleSecondarySlotChange = ( event : Event ) : void => {
502
+ this . hasSecondary = slotChangeHasAssignedElement ( event ) ;
503
+ } ;
504
+
477
505
private setTransitionEl = ( el : HTMLDivElement ) : void => {
478
506
this . transitionEl = el ;
479
507
} ;
@@ -533,11 +561,9 @@ export class Modal
533
561
await componentOnReady ( this . el ) ;
534
562
this . el . addEventListener ( "calciteModalOpen" , this . openEnd ) ;
535
563
this . opened = true ;
536
- const titleEl = getSlotted ( this . el , SLOTS . header ) ;
537
- const contentEl = getSlotted ( this . el , SLOTS . content ) ;
538
564
539
- this . titleId = ensureId ( titleEl ) ;
540
- this . contentId = ensureId ( contentEl ) ;
565
+ this . titleId = ensureId ( this . titleEl ) ;
566
+ this . contentId = ensureId ( this . contentEl ) ;
541
567
542
568
if ( ! this . embedded ) {
543
569
if ( totalOpenModals === 0 ) {
@@ -582,15 +608,6 @@ export class Modal
582
608
document . documentElement . style . setProperty ( "overflow" , initialDocumentOverflowStyle ) ;
583
609
}
584
610
585
- private handleMutationObserver = ( ) : void => {
586
- this . updateFooterVisibility ( ) ;
587
- this . updateFocusTrapElements ( ) ;
588
- } ;
589
-
590
- private updateFooterVisibility = ( ) : void => {
591
- this . hasFooter = ! ! getSlotted ( this . el , [ SLOTS . back , SLOTS . primary , SLOTS . secondary ] ) ;
592
- } ;
593
-
594
611
private updateSizeCssVars = ( ) : void => {
595
612
this . cssWidth = getComputedStyle ( this . el ) . getPropertyValue ( "--calcite-modal-width" ) ;
596
613
this . cssHeight = getComputedStyle ( this . el ) . getPropertyValue ( "--calcite-modal-height" ) ;
0 commit comments