@@ -35,7 +35,6 @@ import {
3535} from './position/connected-position' ;
3636import { ConnectedPositionStrategy } from './position/connected-position-strategy' ;
3737import { RepositionScrollStrategy , ScrollStrategy } from './scroll/index' ;
38- import { DOCUMENT } from '@angular/common' ;
3938
4039
4140/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
@@ -238,8 +237,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
238237 templateRef : TemplateRef < any > ,
239238 viewContainerRef : ViewContainerRef ,
240239 @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 ) {
243241 this . _templatePortal = new TemplatePortal ( templateRef , viewContainerRef ) ;
244242 }
245243
@@ -336,11 +334,16 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
336334 private _attachOverlay ( ) {
337335 if ( ! this . _overlayRef ) {
338336 this . _createOverlay ( ) ;
337+
338+ this . _overlayRef ! . keydownEvents ( ) . subscribe ( ( event : KeyboardEvent ) => {
339+ if ( event . keyCode === ESCAPE ) {
340+ this . _detachOverlay ( ) ;
341+ }
342+ } ) ;
339343 }
340344
341345 this . _position . withDirection ( this . dir ) ;
342346 this . _overlayRef . setDirection ( this . dir ) ;
343- this . _document . addEventListener ( 'keydown' , this . _escapeListener ) ;
344347
345348 if ( ! this . _overlayRef . hasAttached ( ) ) {
346349 this . _overlayRef . attach ( this . _templatePortal ) ;
@@ -362,7 +365,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
362365 }
363366
364367 this . _backdropSubscription . unsubscribe ( ) ;
365- this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
366368 }
367369
368370 /** Destroys the overlay created by this directive. */
@@ -373,13 +375,5 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
373375
374376 this . _backdropSubscription . unsubscribe ( ) ;
375377 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- }
384378 }
385379}
0 commit comments