Skip to content

Commit 276ff31

Browse files
committed
feat(Morph): abort animations when group is destroyed
1 parent 13f107e commit 276ff31

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

ui/src/directives/Morph.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,32 @@ function trigger (group) {
3333
changeClass(from, 'remove')
3434
changeClass(to, 'remove')
3535

36-
morph({
36+
const cancelFn = morph({
3737
from: from.el,
3838
to: to.el,
3939
onToggle () {
4040
changeClass(from, 'add')
4141
changeClass(to, 'remove')
4242
},
4343
...to.opts,
44-
onReady () {
44+
onReady (dir) {
4545
from.animating = false
4646
to.animating = false
4747

4848
group.animating = false
49+
group.cancel = void 0
4950
group.queue.shift()
5051

51-
// TODO: call ctx.onReady() if available
52+
to.opts.onReady !== void 0 && to.opts.onReady(dir)
5253

5354
trigger(group)
5455
}
5556
})
57+
58+
group.cancel = () => {
59+
cancelFn(true) // abort
60+
group.cancel = void 0
61+
}
5662
}
5763

5864
function updateModifiers (mod, ctx) {
@@ -163,6 +169,7 @@ function destroy (el) {
163169
group.queue = group.queue.filter(item => item !== ctx)
164170

165171
if (group.queue.length === 0) {
172+
group.cancel !== void 0 && group.cancel()
166173
delete morphGroups[ctx.group]
167174
}
168175
}

ui/src/directives/Morph.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115

116116
"onReady": {
117117
"type": "Function",
118-
"desc": "A function that will be called once the morphing is finished",
118+
"desc": "A function that will be called once the morphing is finished; Not called if morphing is aborted",
119119
"returns": null,
120120
"params": {
121121
"direction": {

ui/src/utils/morph.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ export default function morph (_options) {
463463
document.body.appendChild(elFromTween)
464464
}
465465

466-
const commonCleanup = () => {
466+
const commonCleanup = aborted => {
467467
// we put the element back in it's place
468468
// and restore the styles and classes
469469
if (elFrom === elTo && endElementTo !== true) {
@@ -488,7 +488,9 @@ export default function morph (_options) {
488488
elTo.qMorphCancel = void 0
489489

490490
// we are ready
491-
typeof options.onReady === 'function' && options.onReady(endElementTo === true ? 'to' : 'from')
491+
if (aborted !== true) {
492+
typeof options.onReady === 'function' && options.onReady(endElementTo === true ? 'to' : 'from')
493+
}
492494
}
493495

494496
if (options.useCSS !== true && typeof elTo.animate === 'function') {
@@ -628,7 +630,7 @@ export default function morph (_options) {
628630
delay: options.delay
629631
})
630632

631-
const cleanup = () => {
633+
const cleanup = abort => {
632634
animationFromClone !== void 0 && animationFromClone.cancel()
633635
animationFromTween !== void 0 && animationFromTween.cancel()
634636
animationToClone !== void 0 && animationToClone.cancel()
@@ -637,7 +639,7 @@ export default function morph (_options) {
637639
animationTo.removeEventListener('finish', cleanup)
638640
animationTo.removeEventListener('cancel', cleanup)
639641

640-
commonCleanup()
642+
commonCleanup(abort)
641643

642644
// we clean the animations
643645
animationFromClone = void 0
@@ -667,8 +669,7 @@ export default function morph (_options) {
667669
}
668670

669671
if (abort === true) {
670-
cleanup()
671-
672+
cleanup(true)
672673
return true
673674
}
674675

0 commit comments

Comments
 (0)