Skip to content

Commit

Permalink
fix(dialog): Cancel open's rAF when close is called (#4087)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfranqueiro committed Nov 15, 2018
1 parent 2ae6335 commit 2516c25
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
7 changes: 3 additions & 4 deletions packages/mdc-dialog/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ class MDCDialogFoundation extends MDCFoundation {
this.close(strings.DESTROY_ACTION);
}

if (this.animationFrame_) {
cancelAnimationFrame(this.animationFrame_);
}

if (this.animationTimer_) {
clearTimeout(this.animationTimer_);
this.handleAnimationTimerEnd_();
Expand Down Expand Up @@ -153,6 +149,9 @@ class MDCDialogFoundation extends MDCFoundation {
this.adapter_.removeClass(cssClasses.OPEN);
this.adapter_.removeBodyClass(cssClasses.SCROLL_LOCK);

cancelAnimationFrame(this.animationFrame_);
this.animationFrame_ = 0;

clearTimeout(this.animationTimer_);
this.animationTimer_ = setTimeout(() => {
this.handleAnimationTimerEnd_();
Expand Down
7 changes: 6 additions & 1 deletion test/screenshot/spec/mdc-dialog/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import {strings} from '../../../../packages/mdc-dialog/constants';

window.mdc.testFixture.fontsLoaded.then(() => {
class DialogFixture {
get dialog() {
return this.dialogInstance_;
}

/** @param {!HTMLElement} dialogEl */
initialize(dialogEl) {
/**
Expand Down Expand Up @@ -390,8 +394,9 @@ window.mdc.testFixture.fontsLoaded.then(() => {
}
}

[].forEach.call(document.querySelectorAll('.mdc-dialog'), (dialogEl) => {
mdc.testFixture.dialogs = [].map.call(document.querySelectorAll('.mdc-dialog'), (dialogEl) => {
const dialogFixture = new DialogFixture();
dialogFixture.initialize(dialogEl);
return dialogFixture.dialog;
});
});
23 changes: 20 additions & 3 deletions test/unit/mdc-dialog/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ test('#destroy cancels layout handling if called on same frame as layout', () =>
td.verify(mockAdapter.isContentScrollable(), {times: 0});
});

test('#open adds CSS classes', () => {
test('#open adds CSS classes after rAF', () => {
const {foundation, mockAdapter} = setupTest();
const clock = installClock();

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

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

test('#close cancels rAF scheduled by open if still pending', () => {
const {foundation, mockAdapter} = setupTest();
const clock = installClock();

foundation.open();
td.reset();
foundation.close();

// Note: #open uses a combination of rAF and setTimeout due to Firefox behavior, so we need to wait 2 ticks
clock.runToFrame();
clock.runToFrame();
td.verify(mockAdapter.addClass(cssClasses.OPEN), {times: 0});
});

test('#open adds the opening class to start an animation, and removes it after the animation is done', () => {
const {foundation, mockAdapter} = setupTest();
const clock = installClock();
Expand Down

0 comments on commit 2516c25

Please sign in to comment.