Skip to content

Commit d93406c

Browse files
committed
fix(autocomplete): incorrectly detecting shadow DOM when inserted through an embedded view
When an autocomplete is inserted, we try to figure out whether it's in the shadow DOM so that we can handle outside clicks properly. It seems like our logic can run too early in some cases, causing it to be detected incorrectly. These changes move the logic later in the process, right before the overlay is attached to the DOM. Fixes #19330.
1 parent eb218e5 commit d93406c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
228228
const window = this._getWindow();
229229

230230
if (typeof window !== 'undefined') {
231-
this._zone.runOutsideAngular(() => {
232-
window.addEventListener('blur', this._windowBlurHandler);
233-
});
234-
235-
this._isInsideShadowRoot = !!_getShadowRoot(this._element.nativeElement);
231+
this._zone.runOutsideAngular(() => window.addEventListener('blur', this._windowBlurHandler));
236232
}
237233
}
238234

@@ -619,6 +615,12 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
619615
throw getMatAutocompleteMissingPanelError();
620616
}
621617

618+
// We want to resolve this once, as late as possible so that we can be
619+
// sure that the element has been moved into its final place in the DOM.
620+
if (this._isInsideShadowRoot == null) {
621+
this._isInsideShadowRoot = !!_getShadowRoot(this._element.nativeElement);
622+
}
623+
622624
let overlayRef = this._overlayRef;
623625

624626
if (!overlayRef) {

0 commit comments

Comments
 (0)