Skip to content

Commit

Permalink
fix(material/snack-bar): Ensure snackbar open animation works with On…
Browse files Browse the repository at this point in the history
…Push ancestor

The enter animation of the snackbar is not guaranteed to work when there
is an `OnPush` parent because it never calls `markForCheck` when
updating the animation state. Even though it calls `detectChanges`, this
will not refresh the host bindings, which includes the animation.

This change updates the `enter` behavior to also call `markForCheck` to
ensure the host view is eventually refreshed when change detection runs.
  • Loading branch information
atscott committed Dec 26, 2023
1 parent a8b8e62 commit b2e7f2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/material/snack-bar/snack-bar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
enter(): void {
if (!this._destroyed) {
this._animationState = 'visible';
// _animationState lives in host bindings and `detectChanges` does not refresh host bindings
// so we have to call `markForCheck` to ensure the host view is refreshed eventually.
this._changeDetectorRef.markForCheck();
this._changeDetectorRef.detectChanges();
this._screenReaderAnnounce();
}
Expand All @@ -205,6 +208,7 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
// where multiple snack bars are opened in quick succession (e.g. two consecutive calls to
// `MatSnackBar.open`).
this._animationState = 'hidden';
this._changeDetectorRef.markForCheck();

// Mark this element with an 'exit' attribute to indicate that the snackbar has
// been dismissed and will soon be removed from the DOM. This is used by the snackbar
Expand Down
13 changes: 10 additions & 3 deletions src/material/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {LiveAnnouncer} from '@angular/cdk/a11y';
import {OverlayContainer} from '@angular/cdk/overlay';
import {
ChangeDetectionStrategy,
Component,
Directive,
Inject,
NgZone,
TemplateRef,
ViewChild,
ViewContainerRef,
signal,
} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -358,7 +361,7 @@ describe('MatSnackBar', () => {
viewContainerFixture.detectChanges();
expect(overlayContainerElement.childElementCount).toBeGreaterThan(0);

viewContainerFixture.componentInstance.childComponentExists = false;
viewContainerFixture.componentInstance.childComponentExists.set(false);
viewContainerFixture.detectChanges();
flush();

Expand Down Expand Up @@ -403,6 +406,9 @@ describe('MatSnackBar', () => {
const dismissCompleteSpy = jasmine.createSpy('dismiss complete spy');

viewContainerFixture.detectChanges();

const containerElement = document.querySelector('mat-snack-bar-container')!;
expect(containerElement.classList).toContain('ng-animating');
const container1 = snackBarRef.containerInstance as MatSnackBarContainer;
expect(container1._animationState)
.withContext(`Expected the animation state would be 'visible'.`)
Expand Down Expand Up @@ -1102,14 +1108,15 @@ class DirectiveWithViewContainer {

@Component({
selector: 'arbitrary-component',
template: `@if (childComponentExists) {<dir-with-view-container></dir-with-view-container>}`,
template: `@if (childComponentExists()) {<dir-with-view-container></dir-with-view-container>}`,
standalone: true,
imports: [DirectiveWithViewContainer],
changeDetection: ChangeDetectionStrategy.OnPush,
})
class ComponentWithChildViewContainer {
@ViewChild(DirectiveWithViewContainer) childWithViewContainer: DirectiveWithViewContainer;

childComponentExists: boolean = true;
childComponentExists = signal(true);

get childViewContainer() {
return this.childWithViewContainer.viewContainerRef;
Expand Down

0 comments on commit b2e7f2d

Please sign in to comment.