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

@ -6,16 +6,19 @@ export default test({
const div = target.querySelector('div'); const div = target.querySelector('div');
ok(div); ok(div);
assert.equal(div.style.opacity, '0'); assert.equal(div.style.color, 'blue');
component.visible = false; component.visible = false;
assert.equal(div.style.opacity, '1'); assert.equal(div.style.color, 'yellow');
// change param // change param
raf.tick(1); raf.tick(1);
component.param = true; component.param = true;
component.visible = 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() { function getInParam() {
return { return {
duration: param ? 20 : 10, duration: 100,
css: t => { css: (t) => {
return `opacity: ${t}`; return `color: ${param ? 'red' : 'blue'}`;
} }
}; };
} }
function getOutParam() { function getOutParam() {
return { return {
duration: param ? 15 : 5, duration: 100,
css: t => { css: (t) => {
return `opacity: ${t}`; return `color: ${param ? 'green' : 'yellow'}`;
} }
}; };
} }

Loading…
Cancel
Save