Skip to content

Commit 93f8e04

Browse files
crisbetojelbourn
authored andcommitted
feat(dialog): add disableClose option (#1678)
Adds a config option that allows users to disable closing a dialog via a backdrop click or pressing escape. Fixes #1419.
1 parent 0d552f5 commit 93f8e04

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

src/lib/dialog/dialog-config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class MdDialogConfig {
1414
/** The ARIA role of the dialog element. */
1515
role?: DialogRole = 'dialog';
1616

17-
// TODO(jelbourn): add configuration for size, clickOutsideToClose, lifecycle hooks,
18-
// ARIA labelling.
17+
/** Whether the user can use escape or clicking outside to close a modal. */
18+
disableClose = false;
19+
20+
// TODO(jelbourn): add configuration for size, lifecycle hooks, ARIA labelling.
1921
}

src/lib/dialog/dialog-container.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2-
Component,
3-
ComponentRef,
4-
ViewChild,
5-
ViewEncapsulation,
6-
NgZone,
7-
OnDestroy
2+
Component,
3+
ComponentRef,
4+
ViewChild,
5+
ViewEncapsulation,
6+
NgZone,
7+
OnDestroy,
88
} from '@angular/core';
99
import {BasePortalHost, ComponentPortal, PortalHostDirective, TemplatePortal} from '../core';
1010
import {MdDialogConfig} from './dialog-config';
@@ -74,8 +74,9 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
7474

7575
/** Handles the user pressing the Escape key. */
7676
handleEscapeKey() {
77-
// TODO(jelbourn): add MdDialogConfig option to disable this behavior.
78-
this.dialogRef.close();
77+
if (!this.dialogConfig.disableClose) {
78+
this.dialogRef.close();
79+
}
7980
}
8081

8182
ngOnDestroy() {

src/lib/dialog/dialog.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,47 @@ describe('MdDialog', () => {
136136

137137
viewContainerFixture.detectChanges();
138138

139-
let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
139+
let backdrop = overlayContainerElement.querySelector('.md-overlay-backdrop') as HTMLElement;
140140
backdrop.click();
141141

142142
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeFalsy();
143143
});
144144

145+
describe('disableClose option', () => {
146+
it('should prevent closing via clicks on the backdrop', () => {
147+
let config = new MdDialogConfig();
148+
config.viewContainerRef = testViewContainerRef;
149+
config.disableClose = true;
150+
151+
dialog.open(PizzaMsg, config);
152+
153+
viewContainerFixture.detectChanges();
154+
155+
let backdrop = overlayContainerElement.querySelector('.md-overlay-backdrop') as HTMLElement;
156+
backdrop.click();
157+
158+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
159+
});
160+
161+
it('should prevent closing via the escape key', () => {
162+
let config = new MdDialogConfig();
163+
config.viewContainerRef = testViewContainerRef;
164+
config.disableClose = true;
165+
166+
dialog.open(PizzaMsg, config);
167+
168+
viewContainerFixture.detectChanges();
169+
170+
let dialogContainer: MdDialogContainer = viewContainerFixture.debugElement.query(
171+
By.directive(MdDialogContainer)).componentInstance;
172+
173+
// Fake the user pressing the escape key by calling the handler directly.
174+
dialogContainer.handleEscapeKey();
175+
176+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
177+
});
178+
});
179+
145180
describe('focus management', () => {
146181

147182
// When testing focus, all of the elements must be in the DOM.

src/lib/dialog/dialog.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ export class MdDialog {
8888
// to modify and close it.
8989
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
9090

91-
// When the dialog backdrop is clicked, we want to close it.
92-
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
91+
if (!dialogContainer.dialogConfig.disableClose) {
92+
// When the dialog backdrop is clicked, we want to close it.
93+
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
94+
}
9395

9496
// Set the dialogRef to the container so that it can use the ref to close the dialog.
9597
dialogContainer.dialogRef = dialogRef;

0 commit comments

Comments
 (0)