Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/snack-bar): Ensure snackbar enter/exit works with OnPush ancestor #28331

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 9 additions & 3 deletions src/material/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {LiveAnnouncer} from '@angular/cdk/a11y';
import {OverlayContainer} from '@angular/cdk/overlay';
import {
ChangeDetectionStrategy,
Component,
Directive,
Inject,
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 +360,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 +405,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 +1107,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
Loading