From df28c3d5abc2d389762b9c2afdb9771e2d3dd8d7 Mon Sep 17 00:00:00 2001 From: Will Howell Date: Tue, 22 Aug 2017 14:48:41 -0400 Subject: [PATCH] feat(dialog): expose backdrop clicks (#6511) --- src/lib/dialog/dialog-ref.ts | 7 +++++++ src/lib/dialog/dialog.spec.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/lib/dialog/dialog-ref.ts b/src/lib/dialog/dialog-ref.ts index f0dc2227195..5d44899981e 100644 --- a/src/lib/dialog/dialog-ref.ts +++ b/src/lib/dialog/dialog-ref.ts @@ -89,6 +89,13 @@ export class MdDialogRef { return this._beforeClose.asObservable(); } + /** + * Gets an observable that emits when the overlay's backdrop has been clicked. + */ + backdropClick(): Observable { + return this._overlayRef.backdropClick(); + } + /** * Updates the dialog's position. * @param position New dialog position. diff --git a/src/lib/dialog/dialog.spec.ts b/src/lib/dialog/dialog.spec.ts index 93beb70a516..845297a0924 100644 --- a/src/lib/dialog/dialog.spec.ts +++ b/src/lib/dialog/dialog.spec.ts @@ -237,6 +237,30 @@ describe('MdDialog', () => { }); })); + it('should emit the backdropClick stream when clicking on the overlay backdrop', async(() => { + const dialogRef = dialog.open(PizzaMsg, { + viewContainerRef: testViewContainerRef + }); + + const spy = jasmine.createSpy('backdropClick spy'); + dialogRef.backdropClick().subscribe(spy); + + viewContainerFixture.detectChanges(); + + let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement; + + backdrop.click(); + expect(spy).toHaveBeenCalledTimes(1); + + viewContainerFixture.detectChanges(); + + viewContainerFixture.whenStable().then(() => { + // Additional clicks after the dialog has closed should not be emitted + backdrop.click(); + expect(spy).toHaveBeenCalledTimes(1); + }); + })); + it('should notify the observers if a dialog has been opened', () => { dialog.afterOpen.subscribe(ref => { expect(dialog.open(PizzaMsg, {