@@ -14,7 +14,6 @@ import {
14
14
TemplatePortal ,
15
15
} from '@angular/cdk/portal' ;
16
16
import {
17
- AfterViewChecked ,
18
17
ChangeDetectionStrategy ,
19
18
Component ,
20
19
ComponentRef ,
@@ -65,7 +64,7 @@ const MDC_SNACKBAR_LABEL_CLASS = 'mdc-snackbar__label';
65
64
} )
66
65
export class MatSnackBarContainer
67
66
extends BasePortalOutlet
68
- implements _SnackBarContainer , AfterViewChecked , OnDestroy
67
+ implements _SnackBarContainer , OnDestroy
69
68
{
70
69
/** The number of milliseconds to wait before announcing the snack bar's content. */
71
70
private readonly _announceDelay : number = 150 ;
@@ -157,17 +156,6 @@ export class MatSnackBarContainer
157
156
this . _mdcFoundation . setTimeoutMs ( - 1 ) ;
158
157
}
159
158
160
- ngAfterViewChecked ( ) {
161
- // Check to see if the attached component or template uses the MDC template structure,
162
- // specifically the MDC label. If not, the container should apply the MDC label class to this
163
- // component's label container, which will apply MDC's label styles to the attached view.
164
- if ( ! this . _label . nativeElement . querySelector ( `.${ MDC_SNACKBAR_LABEL_CLASS } ` ) ) {
165
- this . _label . nativeElement . classList . add ( MDC_SNACKBAR_LABEL_CLASS ) ;
166
- } else {
167
- this . _label . nativeElement . classList . remove ( MDC_SNACKBAR_LABEL_CLASS ) ;
168
- }
169
- }
170
-
171
159
/** Makes sure the exit callbacks have been invoked when the element is destroyed. */
172
160
ngOnDestroy ( ) {
173
161
this . _mdcFoundation . close ( ) ;
@@ -202,14 +190,18 @@ export class MatSnackBarContainer
202
190
attachComponentPortal < T > ( portal : ComponentPortal < T > ) : ComponentRef < T > {
203
191
this . _assertNotAttached ( ) ;
204
192
this . _applySnackBarClasses ( ) ;
205
- return this . _portalOutlet . attachComponentPortal ( portal ) ;
193
+ const componentRef = this . _portalOutlet . attachComponentPortal ( portal ) ;
194
+ this . _applyLabelClass ( ) ;
195
+ return componentRef ;
206
196
}
207
197
208
198
/** Attach a template portal as content to this snack bar container. */
209
199
attachTemplatePortal < C > ( portal : TemplatePortal < C > ) : EmbeddedViewRef < C > {
210
200
this . _assertNotAttached ( ) ;
211
201
this . _applySnackBarClasses ( ) ;
212
- return this . _portalOutlet . attachTemplatePortal ( portal ) ;
202
+ const viewRef = this . _portalOutlet . attachTemplatePortal ( portal ) ;
203
+ this . _applyLabelClass ( ) ;
204
+ return viewRef ;
213
205
}
214
206
215
207
private _setClass ( cssClass : string , active : boolean ) {
@@ -269,4 +261,18 @@ export class MatSnackBarContainer
269
261
} ) ;
270
262
}
271
263
}
264
+
265
+ /** Applies the correct CSS class to the label based on its content. */
266
+ private _applyLabelClass ( ) {
267
+ // Check to see if the attached component or template uses the MDC template structure,
268
+ // specifically the MDC label. If not, the container should apply the MDC label class to this
269
+ // component's label container, which will apply MDC's label styles to the attached view.
270
+ const label = this . _label . nativeElement ;
271
+
272
+ if ( ! label . querySelector ( `.${ MDC_SNACKBAR_LABEL_CLASS } ` ) ) {
273
+ label . classList . add ( MDC_SNACKBAR_LABEL_CLASS ) ;
274
+ } else {
275
+ label . classList . remove ( MDC_SNACKBAR_LABEL_CLASS ) ;
276
+ }
277
+ }
272
278
}
0 commit comments