Skip to content

Commit 60dd10f

Browse files
fix(material/dialog): prevent visual jump by removing open class together with closing class
When _startExitAnimation() is called, mdc-dialog--open was previously removed synchronously while mdc-dialog--closing was scheduled in a requestAnimationFrame callback. When close() is called from within a requestAnimationFrame callback (such as during zoneless change detection scheduling or effect reactions), the nested rAF is deferred to the next frame. During that intermediate frame, the dialog container renders with neither class, applying the base un-opened styles (scale(0.8) / opacity: 0) and causing the dialog surface to visibly shrink and then snap back. Remove mdc-dialog--open and add mdc-dialog--closing together within the requestAnimationFrame callback so the container transitions directly from the open state to the closing state without an intermediate class-less frame. Fixes #34123 Signed-off-by: Abdul karim mia <97598943+abdul-karim-mia@users.noreply.github.com>
1 parent 9187233 commit 60dd10f

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/material/dialog/dialog-container.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ export class MatDialogContainer extends CdkDialogContainer<MatDialogConfig> impl
131131
*/
132132
_startExitAnimation(): void {
133133
this._animationStateChanged.emit({state: 'closing', totalTime: this._exitAnimationDuration});
134-
this._hostElement.classList.remove(OPEN_CLASS);
135134

136135
if (this._animationsEnabled) {
137136
this._hostElement.style.setProperty(
@@ -140,9 +139,16 @@ export class MatDialogContainer extends CdkDialogContainer<MatDialogConfig> impl
140139
);
141140

142141
// We need to give the `setProperty` call from above some time to be applied.
143-
this._requestAnimationFrame(() => this._hostElement.classList.add(CLOSING_CLASS));
142+
// Remove the open class and add the closing class together inside requestAnimationFrame
143+
// so the container does not transition through a class-less state that causes visual jumps
144+
// if close() is invoked from inside a requestAnimationFrame callback (e.g. in zoneless apps).
145+
this._requestAnimationFrame(() => {
146+
this._hostElement.classList.remove(OPEN_CLASS);
147+
this._hostElement.classList.add(CLOSING_CLASS);
148+
});
144149
this._waitForAnimationToComplete(this._exitAnimationDuration, this._finishDialogClose);
145150
} else {
151+
this._hostElement.classList.remove(OPEN_CLASS);
146152
// This subscription to the `OverlayRef#backdropClick` observable in the `DialogRef` is
147153
// set up before any user can subscribe to the backdrop click. The subscription triggers
148154
// the dialog close and this method synchronously. If we'd synchronously emit the `CLOSED`

‎src/material/dialog/dialog.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,25 @@ describe('MatDialog with animations enabled', () => {
22982298

22992299
expect(dialogRef.getState()).toBe(MatDialogState.CLOSED);
23002300
});
2301+
2302+
it('should not remove the open class before adding the closing class', async () => {
2303+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
2304+
viewContainerFixture.detectChanges();
2305+
await wait(OPEN_ANIMATION_DURATION);
2306+
2307+
const containerElement = document.querySelector('mat-dialog-container')!;
2308+
expect(containerElement.classList.contains('mdc-dialog--open')).toBe(true);
2309+
2310+
dialogRef.close();
2311+
viewContainerFixture.detectChanges();
2312+
2313+
// The open class should not be removed synchronously before the closing class is added
2314+
// in requestAnimationFrame, preventing an intermediate class-less state that causes visual jumps.
2315+
expect(containerElement.classList.contains('mdc-dialog--open')).toBe(true);
2316+
2317+
await wait(CLOSE_ANIMATION_DURATION);
2318+
expect(dialogRef.getState()).toBe(MatDialogState.CLOSED);
2319+
});
23012320
});
23022321

23032322
describe('MatDialog with explicit injector provided', () => {

0 commit comments

Comments
 (0)