@@ -21,7 +21,6 @@ import {
2121 OnDestroy ,
2222 Optional ,
2323 Output ,
24- Renderer2 ,
2524 SimpleChanges ,
2625 TemplateRef ,
2726 ViewContainerRef ,
@@ -36,6 +35,7 @@ import {
3635} from './position/connected-position' ;
3736import { ConnectedPositionStrategy } from './position/connected-position-strategy' ;
3837import { RepositionScrollStrategy , ScrollStrategy } from './scroll/index' ;
38+ import { DOCUMENT } from '@angular/common' ;
3939
4040
4141/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
@@ -97,7 +97,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
9797 private _offsetX : number = 0 ;
9898 private _offsetY : number = 0 ;
9999 private _position : ConnectedPositionStrategy ;
100- private _escapeListener = ( ) => { } ;
101100
102101 /** Origin for the connected overlay. */
103102 @Input ( 'cdkConnectedOverlayOrigin' ) origin : CdkOverlayOrigin ;
@@ -230,11 +229,11 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
230229
231230 constructor (
232231 private _overlay : Overlay ,
233- private _renderer : Renderer2 ,
234232 templateRef : TemplateRef < any > ,
235233 viewContainerRef : ViewContainerRef ,
236234 @Inject ( CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY ) private _scrollStrategy ,
237- @Optional ( ) private _dir : Directionality ) {
235+ @Optional ( ) private _dir : Directionality ,
236+ @Optional ( ) @Inject ( DOCUMENT ) private _document : any ) {
238237 this . _templatePortal = new TemplatePortal ( templateRef , viewContainerRef ) ;
239238 }
240239
@@ -335,7 +334,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
335334
336335 this . _position . withDirection ( this . dir ) ;
337336 this . _overlayRef . getConfig ( ) . direction = this . dir ;
338- this . _initEscapeListener ( ) ;
337+ this . _document . addEventListener ( 'keydown' , this . _escapeListener ) ;
339338
340339 if ( ! this . _overlayRef . hasAttached ( ) ) {
341340 this . _overlayRef . attach ( this . _templatePortal ) ;
@@ -357,7 +356,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
357356 }
358357
359358 this . _backdropSubscription . unsubscribe ( ) ;
360- this . _escapeListener ( ) ;
359+ this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
361360 }
362361
363362 /** Destroys the overlay created by this directive. */
@@ -368,15 +367,13 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
368367
369368 this . _backdropSubscription . unsubscribe ( ) ;
370369 this . _positionSubscription . unsubscribe ( ) ;
371- this . _escapeListener ( ) ;
370+ this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
372371 }
373372
374- /** Sets the event listener that closes the overlay when pressing Escape. */
375- private _initEscapeListener ( ) {
376- this . _escapeListener = this . _renderer . listen ( 'document' , 'keydown' , ( event : KeyboardEvent ) => {
377- if ( event . keyCode === ESCAPE ) {
378- this . _detachOverlay ( ) ;
379- }
380- } ) ;
373+ /** Event listener that will close the overlay when the user presses escape. */
374+ private _escapeListener = ( event : KeyboardEvent ) => {
375+ if ( event . keyCode === ESCAPE ) {
376+ this . _detachOverlay ( ) ;
377+ }
381378 }
382379}
0 commit comments