Skip to content

Commit f5c628d

Browse files
committed
fixup! fix(material/snack-bar): switch away from animations module
1 parent 392ae2c commit f5c628d

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

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

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
ChangeDetectorRef,
1313
Component,
1414
ComponentRef,
15-
DoCheck,
1615
ElementRef,
1716
EmbeddedViewRef,
1817
inject,
@@ -34,6 +33,9 @@ import {_IdGenerator, AriaLivePoliteness} from '@angular/cdk/a11y';
3433
import {Platform} from '@angular/cdk/platform';
3534
import {MatSnackBarConfig} from './snack-bar-config';
3635

36+
const ENTER_ANIMATION = '_mat-snack-bar-enter';
37+
const EXIT_ANIMATION = '_mat-snack-bar-exit';
38+
3739
/**
3840
* Internal component that wraps user-provided snack bar content.
3941
* @docs-private
@@ -54,11 +56,11 @@ import {MatSnackBarConfig} from './snack-bar-config';
5456
'[class.mat-snack-bar-container-enter]': '_animationState === "visible"',
5557
'[class.mat-snack-bar-container-exit]': '_animationState === "hidden"',
5658
'[class.mat-snack-bar-container-animations-enabled]': '!_animationsDisabled',
57-
'(animationend)': 'onAnimationEnd($event)',
58-
'(animationcancel)': 'onAnimationEnd($event)',
59+
'(animationend)': 'onAnimationEnd($event.animationName)',
60+
'(animationcancel)': 'onAnimationEnd($event.animationName)',
5961
},
6062
})
61-
export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, OnDestroy {
63+
export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy {
6264
private _ngZone = inject(NgZone);
6365
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
6466
private _changeDetectorRef = inject(ChangeDetectorRef);
@@ -71,7 +73,6 @@ export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, O
7173
private _trackedModals = new Set<Element>();
7274
private _enterFallback: ReturnType<typeof setTimeout> | undefined;
7375
private _exitFallback: ReturnType<typeof setTimeout> | undefined;
74-
private _pendingNoopAnimation: boolean;
7576

7677
/** The number of milliseconds to wait before announcing the snack bar's content. */
7778
private readonly _announceDelay: number = 150;
@@ -173,11 +174,15 @@ export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, O
173174
};
174175

175176
/** Handle end of animations, updating the state of the snackbar. */
176-
onAnimationEnd(event: AnimationEvent) {
177-
if (event.animationName === '_mat-snack-bar-exit') {
177+
onAnimationEnd(animationName: string) {
178+
if (animationName === EXIT_ANIMATION) {
178179
this._completeExit();
179-
} else if (event.animationName === '_mat-snack-bar-enter') {
180-
this._completeEnter();
180+
} else if (animationName === ENTER_ANIMATION) {
181+
clearTimeout(this._enterFallback);
182+
this._ngZone.run(() => {
183+
this._onEnter.next();
184+
this._onEnter.complete();
185+
});
181186
}
182187
}
183188

@@ -192,10 +197,10 @@ export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, O
192197
this._screenReaderAnnounce();
193198

194199
if (this._animationsDisabled) {
195-
this._pendingNoopAnimation = true;
200+
setTimeout(() => this.onAnimationEnd(ENTER_ANIMATION));
196201
} else {
197202
clearTimeout(this._enterFallback);
198-
this._enterFallback = setTimeout(() => this._completeEnter(), 200);
203+
this._enterFallback = setTimeout(() => this.onAnimationEnd(ENTER_ANIMATION), 200);
199204
}
200205
}
201206
}
@@ -221,50 +226,23 @@ export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, O
221226
clearTimeout(this._announceTimeoutId);
222227

223228
if (this._animationsDisabled) {
224-
this._pendingNoopAnimation = true;
229+
setTimeout(() => this.onAnimationEnd(EXIT_ANIMATION));
225230
} else {
226231
clearTimeout(this._exitFallback);
227-
this._exitFallback = setTimeout(() => this._completeExit(), 200);
232+
this._exitFallback = setTimeout(() => this.onAnimationEnd(EXIT_ANIMATION), 200);
228233
}
229234
});
230235

231236
return this._onExit;
232237
}
233238

234-
ngDoCheck(): void {
235-
// Aims to mimic the timing of when the snack back was using the animations
236-
// module since many internal tests depend on the old timing.
237-
if (this._pendingNoopAnimation) {
238-
this._pendingNoopAnimation = false;
239-
queueMicrotask(() => {
240-
if (this._animationState === 'visible') {
241-
this._completeEnter();
242-
} else {
243-
this._completeExit();
244-
}
245-
});
246-
}
247-
}
248-
249239
/** Makes sure the exit callbacks have been invoked when the element is destroyed. */
250240
ngOnDestroy() {
251241
this._destroyed = true;
252242
this._clearFromModals();
253243
this._completeExit();
254244
}
255245

256-
private _completeEnter() {
257-
clearTimeout(this._enterFallback);
258-
this._ngZone.run(() => {
259-
this._onEnter.next();
260-
this._onEnter.complete();
261-
});
262-
}
263-
264-
/**
265-
* Removes the element in a microtask. Helps prevent errors where we end up
266-
* removing an element which is in the middle of an animation.
267-
*/
268246
private _completeExit() {
269247
clearTimeout(this._exitFallback);
270248
queueMicrotask(() => {

tools/public_api_guard/material/snack-bar.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { ComponentPortal } from '@angular/cdk/portal';
1212
import { ComponentRef } from '@angular/core';
1313
import { ComponentType } from '@angular/cdk/overlay';
1414
import { Direction } from '@angular/cdk/bidi';
15-
import { DoCheck } from '@angular/core';
1615
import { DomPortal } from '@angular/cdk/portal';
1716
import { ElementRef } from '@angular/core';
1817
import { EmbeddedViewRef } from '@angular/core';
@@ -94,7 +93,7 @@ export class MatSnackBarConfig<D = any> {
9493
}
9594

9695
// @public
97-
export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, OnDestroy {
96+
export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy {
9897
constructor(...args: unknown[]);
9998
// (undocumented)
10099
protected _animationsDisabled: boolean;
@@ -108,10 +107,8 @@ export class MatSnackBarContainer extends BasePortalOutlet implements DoCheck, O
108107
_label: ElementRef;
109108
_live: AriaLivePoliteness;
110109
readonly _liveElementId: string;
111-
// (undocumented)
112-
ngDoCheck(): void;
113110
ngOnDestroy(): void;
114-
onAnimationEnd(event: AnimationEvent): void;
111+
onAnimationEnd(animationName: string): void;
115112
readonly _onAnnounce: Subject<void>;
116113
readonly _onEnter: Subject<void>;
117114
readonly _onExit: Subject<void>;

0 commit comments

Comments
 (0)