@@ -3,7 +3,7 @@ import {Directive, Injectable, Optional, SkipSelf} from '@angular/core';
33
44/** Singleton that allows all instances of CdkAddFocusClasses to share document event listeners. */
55@Injectable ( )
6- export class CdkFocusCauseDetector {
6+ export class FocusOriginMonitor {
77 /** Whether a keydown event has just occurred. */
88 get keydownOccurred ( ) { return this . _keydownOccurred ; }
99 private _keydownOccurred = false ;
@@ -12,14 +12,16 @@ export class CdkFocusCauseDetector {
1212 private _mousedownOccurred = false ;
1313
1414 constructor ( ) {
15+ // Listen to keydown and mousedown in the capture phase so we can detect them even if the user
16+ // stops propagation.
1517 document . addEventListener ( 'keydown' , ( ) => {
1618 this . _keydownOccurred = true ;
17- setTimeout ( ( ) => this . _keydownOccurred = false , 0 ) ;
19+ Promise . resolve ( ) . then ( ( ) => this . _keydownOccurred = false ) ;
1820 } , true ) ;
1921
2022 document . addEventListener ( 'mousedown' , ( ) => {
2123 this . _mousedownOccurred = true ;
22- setTimeout ( ( ) => this . _mousedownOccurred = false , 0 ) ;
24+ Promise . resolve ( ) . then ( ( ) => this . _mousedownOccurred = false ) ;
2325 } , true ) ;
2426 }
2527}
@@ -50,7 +52,7 @@ export class CdkAddFocusClasses {
5052 /** Whether the has been programmatically focused. */
5153 programmaticallyFocused = false ;
5254
53- constructor ( private _focusCauseDetector : CdkFocusCauseDetector ) { }
55+ constructor ( private _focusCauseDetector : FocusOriginMonitor ) { }
5456
5557 /** Handles focus event on the element. */
5658 _onFocus ( ) {
@@ -67,14 +69,14 @@ export class CdkAddFocusClasses {
6769
6870
6971export function FOCUS_CAUSE_DETECTOR_PROVIDER_FACTORY (
70- parentDispatcher : CdkFocusCauseDetector ) {
71- return parentDispatcher || new CdkFocusCauseDetector ( ) ;
72+ parentDispatcher : FocusOriginMonitor ) {
73+ return parentDispatcher || new FocusOriginMonitor ( ) ;
7274}
7375
7476
7577export 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 ] ] ,
7981 useFactory : FOCUS_CAUSE_DETECTOR_PROVIDER_FACTORY
8082} ;
0 commit comments