fix: defer animations (#12453)

* run animations in microtask

* skip troublesome test

* fix test

* changeset
pull/12460/head
Rich Harris 4 months ago committed by GitHub
parent 649a0507f7
commit b1cf2ece63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: run animations in microtask so that deferred transitions can measure nodes correctly

@ -303,13 +303,13 @@ function animate(element, options, counterpart, t2, callback) {
};
}
var { delay = 0, duration, css, tick, easing = linear } = options;
const { delay = 0, css, tick, easing = linear } = options;
var start = raf.now() + delay;
var t1 = counterpart?.t(start) ?? 1 - t2;
var delta = t2 - t1;
duration *= Math.abs(delta);
var duration = options.duration * Math.abs(delta);
var end = start + duration;
/** @type {Animation} */
@ -319,6 +319,7 @@ function animate(element, options, counterpart, t2, callback) {
var task;
if (css) {
queue_micro_task(() => {
// WAAPI
var keyframes = [];
var n = Math.ceil(duration / (1000 / 60)); // `n` must be an integer, or we risk missing the `t2` value
@ -365,6 +366,7 @@ function animate(element, options, counterpart, t2, callback) {
throw e;
}
});
});
} else {
// Timer
if (t1 === 0) {

@ -6,16 +6,19 @@ export default test({
const div = target.querySelector('div');
ok(div);
assert.equal(div.style.opacity, '0');
assert.equal(div.style.color, 'blue');
component.visible = false;
assert.equal(div.style.opacity, '1');
assert.equal(div.style.color, 'yellow');
// change param
raf.tick(1);
component.param = true;
component.visible = true;
assert.equal(div.style.opacity, '1');
assert.equal(div.style.color, 'red');
component.visible = false;
assert.equal(div.style.color, 'green');
}
});

@ -4,18 +4,18 @@
function getInParam() {
return {
duration: param ? 20 : 10,
css: t => {
return `opacity: ${t}`;
duration: 100,
css: (t) => {
return `color: ${param ? 'red' : 'blue'}`;
}
};
}
function getOutParam() {
return {
duration: param ? 15 : 5,
css: t => {
return `opacity: ${t}`;
duration: 100,
css: (t) => {
return `color: ${param ? 'green' : 'yellow'}`;
}
};
}

Loading…
Cancel
Save