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

Commit 8b1ab98

Browse files
author
Sérgio Gomes
committed
fix(menu): Cancel running animations on destroy.
1 parent 13657ec commit 8b1ab98

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/mdc-menu/simple/foundation.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default class MDCSimpleMenuFoundation extends MDCFoundation {
8383
this.scaleY_ = 0;
8484
this.running_ = false;
8585
this.selectedTriggerTimerId_ = 0;
86+
this.animationRequestId_ = 0;
8687
}
8788

8889
init() {
@@ -107,6 +108,8 @@ export default class MDCSimpleMenuFoundation extends MDCFoundation {
107108

108109
destroy() {
109110
clearTimeout(this.selectedTriggerTimerId_);
111+
// Cancel any currently running animations.
112+
cancelAnimationFrame(this.animationRequestId_);
110113
this.adapter_.deregisterInteractionHandler('click', this.clickHandler_);
111114
this.adapter_.deregisterInteractionHandler('keyup', this.keyupHandler_);
112115
this.adapter_.deregisterInteractionHandler('keydown', this.keydownHandler_);
@@ -186,8 +189,9 @@ export default class MDCSimpleMenuFoundation extends MDCFoundation {
186189

187190
// Stop animation when we've covered the entire 0 - 1 range of time.
188191
if (currentTime < 1) {
189-
requestAnimationFrame(() => this.animationLoop_());
192+
this.animationRequestId_ = requestAnimationFrame(() => this.animationLoop_());
190193
} else {
194+
this.animationRequestId_ = 0;
191195
this.running_ = false;
192196
this.adapter_.removeClass(MDCSimpleMenuFoundation.cssClasses.ANIMATING);
193197
}
@@ -203,7 +207,7 @@ export default class MDCSimpleMenuFoundation extends MDCFoundation {
203207

204208
if (!this.running_) {
205209
this.running_ = true;
206-
requestAnimationFrame(() => this.animationLoop_());
210+
this.animationRequestId_ = requestAnimationFrame(() => this.animationLoop_());
207211
}
208212
}
209213

@@ -360,7 +364,7 @@ export default class MDCSimpleMenuFoundation extends MDCFoundation {
360364
open({focusIndex = null} = {}) {
361365
this.adapter_.saveFocus();
362366
this.adapter_.addClass(MDCSimpleMenuFoundation.cssClasses.ANIMATING);
363-
requestAnimationFrame(() => {
367+
this.animationRequestId_ = requestAnimationFrame(() => {
364368
this.dimensions_ = this.adapter_.getInnerDimensions();
365369
this.applyTransitionDelays_();
366370
this.autoPosition_();

test/unit/mdc-menu/simple.foundation.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,3 +873,16 @@ test('on spacebar keydown prevents default on the event', t => {
873873
clock.uninstall();
874874
t.end();
875875
});
876+
877+
testFoundation('should cancel animation after destroy', t => {
878+
const {foundation, mockAdapter, mockRaf} = t.data;
879+
foundation.init();
880+
mockRaf.flush();
881+
foundation.open();
882+
foundation.destroy();
883+
mockRaf.flush();
884+
mockRaf.flush();
885+
886+
t.doesNotThrow(() => td.verify(mockAdapter.setScale(td.matchers.anything(), td.matchers.anything()), {times: 0}));
887+
t.end();
888+
});

0 commit comments

Comments
 (0)