@@ -19,10 +19,9 @@ import {
19
19
PositionStrategy ,
20
20
ScrollStrategy ,
21
21
} from '@angular/cdk/overlay' ;
22
- import { _getEventTarget } from '@angular/cdk/platform' ;
22
+ import { _getEventTarget , _getFocusedElementPierceShadowDom } from '@angular/cdk/platform' ;
23
23
import { TemplatePortal } from '@angular/cdk/portal' ;
24
24
import { ViewportRuler } from '@angular/cdk/scrolling' ;
25
- import { DOCUMENT } from '@angular/common' ;
26
25
import {
27
26
AfterViewInit ,
28
27
ChangeDetectorRef ,
@@ -147,7 +146,6 @@ export class MatAutocompleteTrigger
147
146
private _changeDetectorRef = inject ( ChangeDetectorRef ) ;
148
147
private _dir = inject ( Directionality , { optional : true } ) ;
149
148
private _formField = inject < MatFormField | null > ( MAT_FORM_FIELD , { optional : true , host : true } ) ;
150
- private _document = inject ( DOCUMENT ) ;
151
149
private _viewportRuler = inject ( ViewportRuler ) ;
152
150
private _scrollStrategy = inject ( MAT_AUTOCOMPLETE_SCROLL_STRATEGY ) ;
153
151
private _renderer = inject ( Renderer2 ) ;
@@ -216,8 +214,7 @@ export class MatAutocompleteTrigger
216
214
// If the user blurred the window while the autocomplete is focused, it means that it'll be
217
215
// refocused when they come back. In this case we want to skip the first focus event, if the
218
216
// pane was closed, in order to avoid reopening it unintentionally.
219
- this . _canOpenOnNextFocus =
220
- this . _document . activeElement !== this . _element . nativeElement || this . panelOpen ;
217
+ this . _canOpenOnNextFocus = this . panelOpen || ! this . _hasFocus ( ) ;
221
218
} ;
222
219
223
220
/** `View -> model callback called when value changes` */
@@ -424,7 +421,7 @@ export class MatAutocompleteTrigger
424
421
// true. Its main purpose is to handle the case where the input is focused from an
425
422
// outside click which propagates up to the `body` listener within the same sequence
426
423
// and causes the panel to close immediately (see #3106).
427
- this . _document . activeElement !== this . _element . nativeElement &&
424
+ ! this . _hasFocus ( ) &&
428
425
( ! formField || ! formField . contains ( clickTarget ) ) &&
429
426
( ! customOrigin || ! customOrigin . contains ( clickTarget ) ) &&
430
427
! ! this . _overlayRef &&
@@ -550,7 +547,7 @@ export class MatAutocompleteTrigger
550
547
}
551
548
}
552
549
553
- if ( this . _canOpen ( ) && this . _document . activeElement === event . target ) {
550
+ if ( this . _canOpen ( ) && this . _hasFocus ( ) ) {
554
551
// When the `input` event fires, the input's value will have already changed. This means
555
552
// that if we take the `this._element.nativeElement.value` directly, it'll be one keystroke
556
553
// behind. This can be a problem when the user selects a value, changes a character while
@@ -579,6 +576,11 @@ export class MatAutocompleteTrigger
579
576
}
580
577
}
581
578
579
+ /** Whether the input currently has focus. */
580
+ private _hasFocus ( ) : boolean {
581
+ return _getFocusedElementPierceShadowDom ( ) === this . _element . nativeElement ;
582
+ }
583
+
582
584
/**
583
585
* In "auto" mode, the label will animate down as soon as focus is lost.
584
586
* This causes the value to jump when selecting an option with the mouse.
0 commit comments