Skip to content
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
6 changes: 3 additions & 3 deletions src/material/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

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

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

/** Result to be passed to afterClosed. */
private _result: R | undefined;
Expand Down
23 changes: 22 additions & 1 deletion src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
dispatchMouseEvent,
patchElementFocus,
} from '@angular/cdk/testing/private';
import {Location} from '@angular/common';
import {AsyncPipe, Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {
ChangeDetectionStrategy,
Expand Down Expand Up @@ -39,6 +39,7 @@ import {
} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {Subject} from 'rxjs';
import {map} from 'rxjs/operators';
import {CLOSE_ANIMATION_DURATION, OPEN_ANIMATION_DURATION} from './dialog-container';
import {
MAT_DIALOG_DATA,
Expand Down Expand Up @@ -1240,6 +1241,14 @@ describe('MatDialog', () => {
}),
);

it('should be able to use afterOpened in the template while animations are disabled', async () => {
const ref = dialog.open(DialogWithAfterOpenSubscription);
await new Promise(resolve => setTimeout(resolve, 10));

expect(ref.componentInstance.animations.animationsDisabled).toBe(true);
expect(overlayContainerElement.textContent).toContain('The dialog is now open!');
});

describe('hasBackdrop option', () => {
it('should have a backdrop', () => {
dialog.open(PizzaMsg, {hasBackdrop: true, viewContainerRef: testViewContainerRef});
Expand Down Expand Up @@ -2450,3 +2459,15 @@ class ModuleBoundDialogChildComponent {
providers: [ModuleBoundDialogService],
})
class ModuleBoundDialogModule {}

@Component({
template: `{{message | async}}`,
imports: [AsyncPipe],
changeDetection: ChangeDetectionStrategy.OnPush,
})
class DialogWithAfterOpenSubscription {
dialogRef = inject(MatDialogRef);
animations = inject(MATERIAL_ANIMATIONS);

protected message = this.dialogRef.afterOpened().pipe(map(() => 'The dialog is now open!'));
}
Loading