Skip to content

Commit 3b6fa23

Browse files
committed
feat(dialog): add disableClose option
Adds a config option that allows users to disable closing a dialog via a backdrop click or pressing escape. Fixes #1419.
1 parent 96d196a commit 3b6fa23

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-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: 10 additions & 9 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';
@@ -25,7 +25,7 @@ import 'rxjs/add/operator/first';
2525
host: {
2626
'class': 'md-dialog-container',
2727
'[attr.role]': 'dialogConfig?.role',
28-
'(keydown.escape)': 'handleEscapeKey()',
28+
'(keydown.escape)': 'handleEscapeKey()'
2929
},
3030
encapsulation: ViewEncapsulation.None,
3131
})
@@ -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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,40 @@ describe('MdDialog', () => {
128128
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeFalsy();
129129
});
130130

131+
describe('disableClose option', () => {
132+
it('should prevent closing via clicks on the backdrop', () => {
133+
let config = new MdDialogConfig();
134+
config.viewContainerRef = testViewContainerRef;
135+
config.disableClose = true;
136+
137+
dialog.open(PizzaMsg, config);
138+
139+
viewContainerFixture.detectChanges();
140+
141+
let backdrop = <HTMLElement> overlayContainerElement.querySelector('.md-overlay-backdrop');
142+
backdrop.click();
143+
144+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
145+
});
146+
147+
it('should prevent closing via the escape key', () => {
148+
let config = new MdDialogConfig();
149+
config.viewContainerRef = testViewContainerRef;
150+
config.disableClose = true;
151+
152+
dialog.open(PizzaMsg, config);
153+
154+
viewContainerFixture.detectChanges();
155+
156+
let dialogContainer: MdDialogContainer = viewContainerFixture.debugElement.query(
157+
By.directive(MdDialogContainer)).componentInstance;
158+
159+
dialogContainer.handleEscapeKey();
160+
161+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeTruthy();
162+
});
163+
});
164+
131165
describe('focus management', () => {
132166

133167
// 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
@@ -87,8 +87,10 @@ export class MdDialog {
8787
// to modify and close it.
8888
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
8989

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

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

0 commit comments

Comments
 (0)