@@ -29,10 +29,7 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
29
29
constructor ( @Inject ( DOCUMENT ) private _document : any ) { }
30
30
31
31
ngOnDestroy ( ) {
32
- if ( this . _keydownEventSubscription ) {
33
- this . _keydownEventSubscription . unsubscribe ( ) ;
34
- this . _keydownEventSubscription = null ;
35
- }
32
+ this . _unsubscribeFromKeydownEvents ( ) ;
36
33
}
37
34
38
35
/** Add a new overlay to the list of attached overlay refs. */
@@ -48,9 +45,15 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
48
45
/** Remove an overlay from the list of attached overlay refs. */
49
46
remove ( overlayRef : OverlayRef ) : void {
50
47
const index = this . _attachedOverlays . indexOf ( overlayRef ) ;
48
+
51
49
if ( index > - 1 ) {
52
50
this . _attachedOverlays . splice ( index , 1 ) ;
53
51
}
52
+
53
+ // Remove the global listener once there are no more overlays.
54
+ if ( this . _attachedOverlays . length === 0 ) {
55
+ this . _unsubscribeFromKeydownEvents ( ) ;
56
+ }
54
57
}
55
58
56
59
/**
@@ -68,6 +71,14 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
68
71
} ) ;
69
72
}
70
73
74
+ /** Removes the global keydown subscription. */
75
+ private _unsubscribeFromKeydownEvents ( ) : void {
76
+ if ( this . _keydownEventSubscription ) {
77
+ this . _keydownEventSubscription . unsubscribe ( ) ;
78
+ this . _keydownEventSubscription = null ;
79
+ }
80
+ }
81
+
71
82
/** Select the appropriate overlay from a keydown event. */
72
83
private _selectOverlayFromEvent ( event : KeyboardEvent ) : OverlayRef {
73
84
// Check if any overlays contain the event
0 commit comments