Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 2516c25

Browse files
authored
fix(dialog): Cancel open's rAF when close is called (#4087)
1 parent 2ae6335 commit 2516c25

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

packages/mdc-dialog/foundation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ class MDCDialogFoundation extends MDCFoundation {
102102
this.close(strings.DESTROY_ACTION);
103103
}
104104

105-
if (this.animationFrame_) {
106-
cancelAnimationFrame(this.animationFrame_);
107-
}
108-
109105
if (this.animationTimer_) {
110106
clearTimeout(this.animationTimer_);
111107
this.handleAnimationTimerEnd_();
@@ -153,6 +149,9 @@ class MDCDialogFoundation extends MDCFoundation {
153149
this.adapter_.removeClass(cssClasses.OPEN);
154150
this.adapter_.removeBodyClass(cssClasses.SCROLL_LOCK);
155151

152+
cancelAnimationFrame(this.animationFrame_);
153+
this.animationFrame_ = 0;
154+
156155
clearTimeout(this.animationTimer_);
157156
this.animationTimer_ = setTimeout(() => {
158157
this.handleAnimationTimerEnd_();

test/screenshot/spec/mdc-dialog/fixture.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {strings} from '../../../../packages/mdc-dialog/constants';
2525

2626
window.mdc.testFixture.fontsLoaded.then(() => {
2727
class DialogFixture {
28+
get dialog() {
29+
return this.dialogInstance_;
30+
}
31+
2832
/** @param {!HTMLElement} dialogEl */
2933
initialize(dialogEl) {
3034
/**
@@ -390,8 +394,9 @@ window.mdc.testFixture.fontsLoaded.then(() => {
390394
}
391395
}
392396

393-
[].forEach.call(document.querySelectorAll('.mdc-dialog'), (dialogEl) => {
397+
mdc.testFixture.dialogs = [].map.call(document.querySelectorAll('.mdc-dialog'), (dialogEl) => {
394398
const dialogFixture = new DialogFixture();
395399
dialogFixture.initialize(dialogEl);
400+
return dialogFixture.dialog;
396401
});
397402
});

test/unit/mdc-dialog/foundation.test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,17 @@ test('#destroy cancels layout handling if called on same frame as layout', () =>
117117
td.verify(mockAdapter.isContentScrollable(), {times: 0});
118118
});
119119

120-
test('#open adds CSS classes', () => {
120+
test('#open adds CSS classes after rAF', () => {
121121
const {foundation, mockAdapter} = setupTest();
122122
const clock = installClock();
123123

124124
foundation.open();
125-
clock.runToFrame();
126-
clock.tick(100);
125+
td.verify(mockAdapter.addClass(cssClasses.OPEN), {times: 0});
126+
td.verify(mockAdapter.addBodyClass(cssClasses.SCROLL_LOCK), {times: 0});
127127

128+
// Note: #open uses a combination of rAF and setTimeout due to Firefox behavior, so we need to wait 2 ticks
129+
clock.runToFrame();
130+
clock.runToFrame();
128131
td.verify(mockAdapter.addClass(cssClasses.OPEN));
129132
td.verify(mockAdapter.addBodyClass(cssClasses.SCROLL_LOCK));
130133
});
@@ -140,6 +143,20 @@ test('#close removes CSS classes', () => {
140143
td.verify(mockAdapter.removeBodyClass(cssClasses.SCROLL_LOCK));
141144
});
142145

146+
test('#close cancels rAF scheduled by open if still pending', () => {
147+
const {foundation, mockAdapter} = setupTest();
148+
const clock = installClock();
149+
150+
foundation.open();
151+
td.reset();
152+
foundation.close();
153+
154+
// Note: #open uses a combination of rAF and setTimeout due to Firefox behavior, so we need to wait 2 ticks
155+
clock.runToFrame();
156+
clock.runToFrame();
157+
td.verify(mockAdapter.addClass(cssClasses.OPEN), {times: 0});
158+
});
159+
143160
test('#open adds the opening class to start an animation, and removes it after the animation is done', () => {
144161
const {foundation, mockAdapter} = setupTest();
145162
const clock = installClock();

0 commit comments

Comments
 (0)