Skip to content

fix(material/select): overlay not detached on time after exit animation in some cases #30456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import {
HostAttributeToken,
ANIMATION_MODULE_TYPE,
Renderer2,
NgZone,
} from '@angular/core';
import {
AbstractControl,
Expand Down Expand Up @@ -216,7 +215,6 @@ export class MatSelect
private _dir = inject(Directionality, {optional: true});
private _idGenerator = inject(_IdGenerator);
private _renderer = inject(Renderer2);
private _ngZone = inject(NgZone);
protected _parentFormField = inject<MatFormField>(MAT_FORM_FIELD, {optional: true});
ngControl = inject(NgControl, {self: true, optional: true})!;
private _liveAnnouncer = inject(LiveAnnouncer);
Expand Down Expand Up @@ -845,35 +843,41 @@ export class MatSelect
/** Triggers the exit animation and detaches the overlay at the end. */
private _exitAndDetach() {
if (this._animationsDisabled || !this.panel) {
this._overlayDir.detachOverlay();
this._detachOverlay();
return;
}

this._ngZone.runOutsideAngular(() => {
this._cleanupDetach?.();
this._cleanupDetach = () => {
cleanupEvent();
clearTimeout(exitFallbackTimer);
this._cleanupDetach = undefined;
};
this._cleanupDetach?.();
this._cleanupDetach = () => {
cleanupEvent();
clearTimeout(exitFallbackTimer);
this._cleanupDetach = undefined;
};

const panel: HTMLElement = this.panel.nativeElement;
const cleanupEvent = this._renderer.listen(panel, 'animationend', (event: AnimationEvent) => {
if (event.animationName === '_mat-select-exit') {
this._cleanupDetach?.();
this._detachOverlay();
}
});

const panel: HTMLElement = this.panel.nativeElement;
const cleanupEvent = this._renderer.listen(panel, 'animationend', (event: AnimationEvent) => {
if (event.animationName === '_mat-select-exit') {
this._cleanupDetach?.();
this._overlayDir.detachOverlay();
}
});
// Since closing the overlay depends on the animation, we have a fallback in case the panel
// doesn't animate. This can happen in some internal tests that do `* {animation: none}`.
const exitFallbackTimer = setTimeout(() => {
this._cleanupDetach?.();
this._detachOverlay();
}, 200);

// Since closing the overlay depends on the animation, we have a fallback in case the panel
// doesn't animate. This can happen in some internal tests that do `* {animation: none}`.
const exitFallbackTimer = setTimeout(() => {
this._cleanupDetach?.();
this._overlayDir.detachOverlay();
}, 200);
panel.classList.add('mat-select-panel-exit');
}

panel.classList.add('mat-select-panel-exit');
});
/** Detaches the current overlay directive. */
private _detachOverlay() {
this._overlayDir.detachOverlay();
// Some of the overlay detachment logic depends on change detection.
// Mark for check to ensure that things get picked up in a timely manner.
this._changeDetectorRef.markForCheck();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main part of the fix. The rest is removing the runOutsideAngular call, because marking for check outside the zone used to be problematic, not sure if that's the case anymore but am playing it safe.

}

/**
Expand Down
Loading