Skip to content

Commit 6603bd5

Browse files
crisbetowagnermaciel
authored andcommitted
fix(material-experimental/mdc-snack-bar): avoid querying the DOM on each change detection (#24770)
Currently the MDC snack bar container is querying the DOM on each change detection in order to apply the correct class to the content. We don't need to do this so frequently since everything is set after the portal content has been attached. (cherry picked from commit d3428ba)
1 parent 2b90924 commit 6603bd5

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/material-experimental/mdc-snack-bar/snack-bar-container.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
TemplatePortal,
1515
} from '@angular/cdk/portal';
1616
import {
17-
AfterViewChecked,
1817
ChangeDetectionStrategy,
1918
Component,
2019
ComponentRef,
@@ -65,7 +64,7 @@ const MDC_SNACKBAR_LABEL_CLASS = 'mdc-snackbar__label';
6564
})
6665
export class MatSnackBarContainer
6766
extends BasePortalOutlet
68-
implements _SnackBarContainer, AfterViewChecked, OnDestroy
67+
implements _SnackBarContainer, OnDestroy
6968
{
7069
/** The number of milliseconds to wait before announcing the snack bar's content. */
7170
private readonly _announceDelay: number = 150;
@@ -157,17 +156,6 @@ export class MatSnackBarContainer
157156
this._mdcFoundation.setTimeoutMs(-1);
158157
}
159158

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-
171159
/** Makes sure the exit callbacks have been invoked when the element is destroyed. */
172160
ngOnDestroy() {
173161
this._mdcFoundation.close();
@@ -202,14 +190,18 @@ export class MatSnackBarContainer
202190
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
203191
this._assertNotAttached();
204192
this._applySnackBarClasses();
205-
return this._portalOutlet.attachComponentPortal(portal);
193+
const componentRef = this._portalOutlet.attachComponentPortal(portal);
194+
this._applyLabelClass();
195+
return componentRef;
206196
}
207197

208198
/** Attach a template portal as content to this snack bar container. */
209199
attachTemplatePortal<C>(portal: TemplatePortal<C>): EmbeddedViewRef<C> {
210200
this._assertNotAttached();
211201
this._applySnackBarClasses();
212-
return this._portalOutlet.attachTemplatePortal(portal);
202+
const viewRef = this._portalOutlet.attachTemplatePortal(portal);
203+
this._applyLabelClass();
204+
return viewRef;
213205
}
214206

215207
private _setClass(cssClass: string, active: boolean) {
@@ -269,4 +261,18 @@ export class MatSnackBarContainer
269261
});
270262
}
271263
}
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+
}
272278
}

0 commit comments

Comments
 (0)