@@ -35,7 +35,6 @@ import {
35
35
} from './position/connected-position' ;
36
36
import { ConnectedPositionStrategy } from './position/connected-position-strategy' ;
37
37
import { RepositionScrollStrategy , ScrollStrategy } from './scroll/index' ;
38
- import { DOCUMENT } from '@angular/common' ;
39
38
40
39
41
40
/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
@@ -238,8 +237,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
238
237
templateRef : TemplateRef < any > ,
239
238
viewContainerRef : ViewContainerRef ,
240
239
@Inject ( CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY ) private _scrollStrategy ,
241
- @Optional ( ) private _dir : Directionality ,
242
- @Optional ( ) @Inject ( DOCUMENT ) private _document : any ) {
240
+ @Optional ( ) private _dir : Directionality ) {
243
241
this . _templatePortal = new TemplatePortal ( templateRef , viewContainerRef ) ;
244
242
}
245
243
@@ -336,11 +334,16 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
336
334
private _attachOverlay ( ) {
337
335
if ( ! this . _overlayRef ) {
338
336
this . _createOverlay ( ) ;
337
+
338
+ this . _overlayRef ! . keydownEvents ( ) . subscribe ( ( event : KeyboardEvent ) => {
339
+ if ( event . keyCode === ESCAPE ) {
340
+ this . _detachOverlay ( ) ;
341
+ }
342
+ } ) ;
339
343
}
340
344
341
345
this . _position . withDirection ( this . dir ) ;
342
346
this . _overlayRef . setDirection ( this . dir ) ;
343
- this . _document . addEventListener ( 'keydown' , this . _escapeListener ) ;
344
347
345
348
if ( ! this . _overlayRef . hasAttached ( ) ) {
346
349
this . _overlayRef . attach ( this . _templatePortal ) ;
@@ -362,7 +365,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
362
365
}
363
366
364
367
this . _backdropSubscription . unsubscribe ( ) ;
365
- this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
366
368
}
367
369
368
370
/** Destroys the overlay created by this directive. */
@@ -373,13 +375,5 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
373
375
374
376
this . _backdropSubscription . unsubscribe ( ) ;
375
377
this . _positionSubscription . unsubscribe ( ) ;
376
- this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
377
- }
378
-
379
- /** Event listener that will close the overlay when the user presses escape. */
380
- private _escapeListener = ( event : KeyboardEvent ) => {
381
- if ( event . keyCode === ESCAPE ) {
382
- this . _detachOverlay ( ) ;
383
- }
384
378
}
385
379
}
0 commit comments