8
8
} from '@angular/core' ;
9
9
import { BasePortalHost , ComponentPortal , PortalHostDirective , TemplatePortal } from '../core' ;
10
10
import { MdDialogConfig } from './dialog-config' ;
11
+ import { MdDialogRef } from './dialog-ref' ;
11
12
import { MdDialogContentAlreadyAttachedError } from './dialog-errors' ;
12
13
import { FocusTrap } from '../core/a11y/focus-trap' ;
13
14
import 'rxjs/add/operator/first' ;
@@ -23,7 +24,8 @@ import 'rxjs/add/operator/first';
23
24
styleUrls : [ 'dialog-container.css' ] ,
24
25
host : {
25
26
'class' : 'md-dialog-container' ,
26
- '[attr.role]' : 'dialogConfig?.role'
27
+ '[attr.role]' : 'dialogConfig?.role' ,
28
+ '(keydown.escape)' : 'handleEscapeKey()' ,
27
29
} ,
28
30
encapsulation : ViewEncapsulation . None ,
29
31
} )
@@ -40,6 +42,9 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
40
42
/** The dialog configuration. */
41
43
dialogConfig : MdDialogConfig ;
42
44
45
+ /** Reference to the open dialog. */
46
+ dialogRef : MdDialogRef < any > ;
47
+
43
48
constructor ( private _ngZone : NgZone ) {
44
49
super ( ) ;
45
50
}
@@ -67,6 +72,12 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
67
72
throw Error ( 'Not yet implemented' ) ;
68
73
}
69
74
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
+
70
81
ngOnDestroy ( ) {
71
82
// When the dialog is destroyed, return focus to the element that originally had it before
72
83
// the dialog was opened. Wait for the DOM to finish settling before changing the focus so
0 commit comments