@@ -3,7 +3,7 @@ import {Directive, Injectable, Optional, SkipSelf} from '@angular/core';
3
3
4
4
/** Singleton that allows all instances of CdkAddFocusClasses to share document event listeners. */
5
5
@Injectable ( )
6
- export class CdkFocusCauseDetector {
6
+ export class FocusOriginMonitor {
7
7
/** Whether a keydown event has just occurred. */
8
8
get keydownOccurred ( ) { return this . _keydownOccurred ; }
9
9
private _keydownOccurred = false ;
@@ -12,14 +12,16 @@ export class CdkFocusCauseDetector {
12
12
private _mousedownOccurred = false ;
13
13
14
14
constructor ( ) {
15
+ // Listen to keydown and mousedown in the capture phase so we can detect them even if the user
16
+ // stops propagation.
15
17
document . addEventListener ( 'keydown' , ( ) => {
16
18
this . _keydownOccurred = true ;
17
- setTimeout ( ( ) => this . _keydownOccurred = false , 0 ) ;
19
+ Promise . resolve ( ) . then ( ( ) => this . _keydownOccurred = false ) ;
18
20
} , true ) ;
19
21
20
22
document . addEventListener ( 'mousedown' , ( ) => {
21
23
this . _mousedownOccurred = true ;
22
- setTimeout ( ( ) => this . _mousedownOccurred = false , 0 ) ;
24
+ Promise . resolve ( ) . then ( ( ) => this . _mousedownOccurred = false ) ;
23
25
} , true ) ;
24
26
}
25
27
}
@@ -50,7 +52,7 @@ export class CdkAddFocusClasses {
50
52
/** Whether the has been programmatically focused. */
51
53
programmaticallyFocused = false ;
52
54
53
- constructor ( private _focusCauseDetector : CdkFocusCauseDetector ) { }
55
+ constructor ( private _focusCauseDetector : FocusOriginMonitor ) { }
54
56
55
57
/** Handles focus event on the element. */
56
58
_onFocus ( ) {
@@ -67,14 +69,14 @@ export class CdkAddFocusClasses {
67
69
68
70
69
71
export function FOCUS_CAUSE_DETECTOR_PROVIDER_FACTORY (
70
- parentDispatcher : CdkFocusCauseDetector ) {
71
- return parentDispatcher || new CdkFocusCauseDetector ( ) ;
72
+ parentDispatcher : FocusOriginMonitor ) {
73
+ return parentDispatcher || new FocusOriginMonitor ( ) ;
72
74
}
73
75
74
76
75
77
export const FOCUS_CAUSE_DETECTOR_PROVIDER = {
76
- // If there is already a CdkFocusCauseDetector available, use that. Otherwise, provide a new one.
77
- provide : CdkFocusCauseDetector ,
78
- deps : [ [ new Optional ( ) , new SkipSelf ( ) , CdkFocusCauseDetector ] ] ,
78
+ // If there is already a FocusOriginMonitor available, use that. Otherwise, provide a new one.
79
+ provide : FocusOriginMonitor ,
80
+ deps : [ [ new Optional ( ) , new SkipSelf ( ) , FocusOriginMonitor ] ] ,
79
81
useFactory : FOCUS_CAUSE_DETECTOR_PROVIDER_FACTORY
80
82
} ;
0 commit comments