88} from '@angular/core' ;
99import { BasePortalHost , ComponentPortal , PortalHostDirective , TemplatePortal } from '../core' ;
1010import { MdDialogConfig } from './dialog-config' ;
11+ import { MdDialogRef } from './dialog-ref' ;
1112import { MdDialogContentAlreadyAttachedError } from './dialog-errors' ;
1213import { FocusTrap } from '../core/a11y/focus-trap' ;
1314import 'rxjs/add/operator/first' ;
@@ -23,7 +24,8 @@ import 'rxjs/add/operator/first';
2324 styleUrls : [ 'dialog-container.css' ] ,
2425 host : {
2526 'class' : 'md-dialog-container' ,
26- '[attr.role]' : 'dialogConfig?.role'
27+ '[attr.role]' : 'dialogConfig?.role' ,
28+ '(keydown.escape)' : 'handleEscapeKey()' ,
2729 } ,
2830 encapsulation : ViewEncapsulation . None ,
2931} )
@@ -40,6 +42,9 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
4042 /** The dialog configuration. */
4143 dialogConfig : MdDialogConfig ;
4244
45+ /** Reference to the open dialog. */
46+ dialogRef : MdDialogRef < any > ;
47+
4348 constructor ( private _ngZone : NgZone ) {
4449 super ( ) ;
4550 }
@@ -67,6 +72,12 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
6772 throw Error ( 'Not yet implemented' ) ;
6873 }
6974
75+ /** Handles the user pressing the Escape key. */
76+ handleEscapeKey ( ) {
77+ // TODO(jelbourn): add MdDialogConfig option to disable this behavior.
78+ this . dialogRef . close ( ) ;
79+ }
80+
7081 ngOnDestroy ( ) {
7182 // When the dialog is destroyed, return focus to the element that originally had it before
7283 // the dialog was opened. Wait for the DOM to finish settling before changing the focus so
0 commit comments