66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9+ import { _IdGenerator , AriaLivePoliteness } from '@angular/cdk/a11y' ;
10+ import { Platform } from '@angular/cdk/platform' ;
11+ import {
12+ BasePortalOutlet ,
13+ CdkPortalOutlet ,
14+ ComponentPortal ,
15+ DomPortal ,
16+ TemplatePortal ,
17+ } from '@angular/cdk/portal' ;
18+ import { DOCUMENT } from '@angular/common' ;
919import {
10- afterRender ,
11- AfterRenderRef ,
20+ afterNextRender ,
1221 ChangeDetectionStrategy ,
1322 ChangeDetectorRef ,
1423 Component ,
1524 ComponentRef ,
1625 ElementRef ,
1726 EmbeddedViewRef ,
1827 inject ,
28+ Injector ,
1929 NgZone ,
2030 OnDestroy ,
2131 ViewChild ,
2232 ViewEncapsulation ,
2333} from '@angular/core' ;
24- import { DOCUMENT } from '@angular/common' ;
25- import {
26- BasePortalOutlet ,
27- CdkPortalOutlet ,
28- ComponentPortal ,
29- DomPortal ,
30- TemplatePortal ,
31- } from '@angular/cdk/portal' ;
32- import { Observable , Subject , of } from 'rxjs' ;
33- import { _IdGenerator , AriaLivePoliteness } from '@angular/cdk/a11y' ;
34- import { Platform } from '@angular/cdk/platform' ;
35- import { MatSnackBarConfig } from './snack-bar-config' ;
36- import { take } from 'rxjs/operators' ;
34+ import { Observable , of , Subject } from 'rxjs' ;
3735import { _animationsDisabled } from '../core' ;
36+ import { MatSnackBarConfig } from './snack-bar-config' ;
3837
3938const ENTER_ANIMATION = '_mat-snack-bar-enter' ;
4039const EXIT_ANIMATION = '_mat-snack-bar-exit' ;
@@ -68,15 +67,14 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
6867 private _elementRef = inject < ElementRef < HTMLElement > > ( ElementRef ) ;
6968 private _changeDetectorRef = inject ( ChangeDetectorRef ) ;
7069 private _platform = inject ( Platform ) ;
71- private _rendersRef : AfterRenderRef ;
7270 protected _animationsDisabled = _animationsDisabled ( ) ;
7371 snackBarConfig = inject ( MatSnackBarConfig ) ;
7472
7573 private _document = inject ( DOCUMENT ) ;
7674 private _trackedModals = new Set < Element > ( ) ;
7775 private _enterFallback : ReturnType < typeof setTimeout > | undefined ;
7876 private _exitFallback : ReturnType < typeof setTimeout > | undefined ;
79- private _renders = new Subject < void > ( ) ;
77+ private _injector = inject ( Injector ) ;
8078
8179 /** The number of milliseconds to wait before announcing the snack bar's content. */
8280 private readonly _announceDelay : number = 150 ;
@@ -147,11 +145,6 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
147145 this . _role = 'alert' ;
148146 }
149147 }
150-
151- // Note: ideally we'd just do an `afterNextRender` in the places where we need to delay
152- // something, however in some cases (TestBed teardown) the injector can be destroyed at an
153- // unexpected time, causing the `afterRender` to fail.
154- this . _rendersRef = afterRender ( ( ) => this . _renders . next ( ) , { manualCleanup : true } ) ;
155148 }
156149
157150 /** Attach a component portal as content to this snack bar container. */
@@ -206,9 +199,12 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
206199 this . _screenReaderAnnounce ( ) ;
207200
208201 if ( this . _animationsDisabled ) {
209- this . _renders . pipe ( take ( 1 ) ) . subscribe ( ( ) => {
210- this . _ngZone . run ( ( ) => queueMicrotask ( ( ) => this . onAnimationEnd ( ENTER_ANIMATION ) ) ) ;
211- } ) ;
202+ afterNextRender (
203+ ( ) => {
204+ this . _ngZone . run ( ( ) => queueMicrotask ( ( ) => this . onAnimationEnd ( ENTER_ANIMATION ) ) ) ;
205+ } ,
206+ { injector : this . _injector } ,
207+ ) ;
212208 } else {
213209 clearTimeout ( this . _enterFallback ) ;
214210 this . _enterFallback = setTimeout ( ( ) => {
@@ -246,9 +242,12 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
246242 clearTimeout ( this . _announceTimeoutId ) ;
247243
248244 if ( this . _animationsDisabled ) {
249- this . _renders . pipe ( take ( 1 ) ) . subscribe ( ( ) => {
250- this . _ngZone . run ( ( ) => queueMicrotask ( ( ) => this . onAnimationEnd ( EXIT_ANIMATION ) ) ) ;
251- } ) ;
245+ afterNextRender (
246+ ( ) => {
247+ this . _ngZone . run ( ( ) => queueMicrotask ( ( ) => this . onAnimationEnd ( EXIT_ANIMATION ) ) ) ;
248+ } ,
249+ { injector : this . _injector } ,
250+ ) ;
252251 } else {
253252 clearTimeout ( this . _exitFallback ) ;
254253 this . _exitFallback = setTimeout ( ( ) => this . onAnimationEnd ( EXIT_ANIMATION ) , 200 ) ;
@@ -263,8 +262,6 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
263262 this . _destroyed = true ;
264263 this . _clearFromModals ( ) ;
265264 this . _completeExit ( ) ;
266- this . _renders . complete ( ) ;
267- this . _rendersRef . destroy ( ) ;
268265 }
269266
270267 private _completeExit ( ) {
0 commit comments