Skip to content

Commit 921a5bf

Browse files
committed
fix(material/dialog): afterOpened emitting too early when animations are disabled
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 4d55040 commit 921a5bf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/material/dialog/dialog-container.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export class MatDialogContainer extends CdkDialogContainer<MatDialogConfig> impl
254254
// dialog surface.
255255
const ref = super.attachComponentPortal(portal);
256256
ref.location.nativeElement.classList.add('mat-mdc-dialog-component-host');
257+
ref.changeDetectorRef.detectChanges();
257258
return ref;
258259
}
259260
}

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)