Skip to content

Commit 3362836

Browse files
authored
fix(material/dialog): afterOpened emitting too early when animations are disabled (#32211)
Fixes that the `afterOpened` stream was emitting too early when animations are disabled, causing subscriptions triggered by the template to not pick it up. Fixes #32205.
1 parent 25f15bf commit 3362836

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/material/dialog/dialog-ref.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/** Possible states of the lifecycle of a dialog. */
1010
import {FocusOrigin} from '@angular/cdk/a11y';
11-
import {merge, Observable, Subject} from 'rxjs';
11+
import {merge, Observable, ReplaySubject} from 'rxjs';
1212
import {DialogRef} from '@angular/cdk/dialog';
1313
import {DialogPosition, MatDialogConfig} from './dialog-config';
1414
import {MatDialogContainer} from './dialog-container';
@@ -43,10 +43,10 @@ export class MatDialogRef<T, R = any> {
4343
id: string;
4444

4545
/** Subject for notifying the user that the dialog has finished opening. */
46-
private readonly _afterOpened = new Subject<void>();
46+
private readonly _afterOpened = new ReplaySubject<void>(1);
4747

4848
/** Subject for notifying the user that the dialog has started closing. */
49-
private readonly _beforeClosed = new Subject<R | undefined>();
49+
private readonly _beforeClosed = new ReplaySubject<R | undefined>(1);
5050

5151
/** Result to be passed to afterClosed. */
5252
private _result: R | undefined;

src/material/dialog/dialog.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
dispatchMouseEvent,
1212
patchElementFocus,
1313
} from '@angular/cdk/testing/private';
14-
import {Location} from '@angular/common';
14+
import {AsyncPipe, Location} from '@angular/common';
1515
import {SpyLocation} from '@angular/common/testing';
1616
import {
1717
ChangeDetectionStrategy,
@@ -39,6 +39,7 @@ import {
3939
} from '@angular/core/testing';
4040
import {By} from '@angular/platform-browser';
4141
import {Subject} from 'rxjs';
42+
import {map} from 'rxjs/operators';
4243
import {CLOSE_ANIMATION_DURATION, OPEN_ANIMATION_DURATION} from './dialog-container';
4344
import {
4445
MAT_DIALOG_DATA,
@@ -1240,6 +1241,14 @@ describe('MatDialog', () => {
12401241
}),
12411242
);
12421243

1244+
it('should be able to use afterOpened in the template while animations are disabled', async () => {
1245+
const ref = dialog.open(DialogWithAfterOpenSubscription);
1246+
await new Promise(resolve => setTimeout(resolve, 10));
1247+
1248+
expect(ref.componentInstance.animations.animationsDisabled).toBe(true);
1249+
expect(overlayContainerElement.textContent).toContain('The dialog is now open!');
1250+
});
1251+
12431252
describe('hasBackdrop option', () => {
12441253
it('should have a backdrop', () => {
12451254
dialog.open(PizzaMsg, {hasBackdrop: true, viewContainerRef: testViewContainerRef});
@@ -2450,3 +2459,15 @@ class ModuleBoundDialogChildComponent {
24502459
providers: [ModuleBoundDialogService],
24512460
})
24522461
class ModuleBoundDialogModule {}
2462+
2463+
@Component({
2464+
template: `{{message | async}}`,
2465+
imports: [AsyncPipe],
2466+
changeDetection: ChangeDetectionStrategy.OnPush,
2467+
})
2468+
class DialogWithAfterOpenSubscription {
2469+
dialogRef = inject(MatDialogRef);
2470+
animations = inject(MATERIAL_ANIMATIONS);
2471+
2472+
protected message = this.dialogRef.afterOpened().pipe(map(() => 'The dialog is now open!'));
2473+
}

0 commit comments

Comments
 (0)