@@ -21,7 +21,6 @@ import {
21
21
OnDestroy ,
22
22
Optional ,
23
23
Output ,
24
- Renderer2 ,
25
24
SimpleChanges ,
26
25
TemplateRef ,
27
26
ViewContainerRef ,
@@ -36,6 +35,7 @@ import {
36
35
} from './position/connected-position' ;
37
36
import { ConnectedPositionStrategy } from './position/connected-position-strategy' ;
38
37
import { RepositionScrollStrategy , ScrollStrategy } from './scroll/index' ;
38
+ import { DOCUMENT } from '@angular/common' ;
39
39
40
40
41
41
/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
@@ -97,7 +97,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
97
97
private _offsetX : number = 0 ;
98
98
private _offsetY : number = 0 ;
99
99
private _position : ConnectedPositionStrategy ;
100
- private _escapeListener = ( ) => { } ;
101
100
102
101
/** Origin for the connected overlay. */
103
102
@Input ( 'cdkConnectedOverlayOrigin' ) origin : CdkOverlayOrigin ;
@@ -230,11 +229,11 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
230
229
231
230
constructor (
232
231
private _overlay : Overlay ,
233
- private _renderer : Renderer2 ,
234
232
templateRef : TemplateRef < any > ,
235
233
viewContainerRef : ViewContainerRef ,
236
234
@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 ) {
238
237
this . _templatePortal = new TemplatePortal ( templateRef , viewContainerRef ) ;
239
238
}
240
239
@@ -335,7 +334,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
335
334
336
335
this . _position . withDirection ( this . dir ) ;
337
336
this . _overlayRef . getConfig ( ) . direction = this . dir ;
338
- this . _initEscapeListener ( ) ;
337
+ this . _document . addEventListener ( 'keydown' , this . _escapeListener ) ;
339
338
340
339
if ( ! this . _overlayRef . hasAttached ( ) ) {
341
340
this . _overlayRef . attach ( this . _templatePortal ) ;
@@ -357,7 +356,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
357
356
}
358
357
359
358
this . _backdropSubscription . unsubscribe ( ) ;
360
- this . _escapeListener ( ) ;
359
+ this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
361
360
}
362
361
363
362
/** Destroys the overlay created by this directive. */
@@ -368,15 +367,13 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
368
367
369
368
this . _backdropSubscription . unsubscribe ( ) ;
370
369
this . _positionSubscription . unsubscribe ( ) ;
371
- this . _escapeListener ( ) ;
370
+ this . _document . removeEventListener ( 'keydown' , this . _escapeListener ) ;
372
371
}
373
372
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
+ }
381
378
}
382
379
}
0 commit comments