Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: further animation transition improvements #10138

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-beds-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: further animation transition improvements
39 changes: 38 additions & 1 deletion packages/svelte/src/internal/client/transitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ function create_transition(dom, init, direction, effect) {
},
// out
o() {
// @ts-ignore
const has_keyed_transition = dom.__animate;
const needs_reverse = direction === 'both' && curr_direction !== 'out';
curr_direction = 'out';
if (animation === null || cancelled) {
Expand All @@ -385,6 +387,35 @@ function create_transition(dom, init, direction, effect) {
/** @type {Animation | TickAnimation} */ (animation).play();
}
}
// If we're outroing an element that has an animation, then we need to fix
// its position to ensure it behaves nicely without causing layout shift.
if (has_keyed_transition) {
const style = getComputedStyle(dom);
const position = style.position;

if (position !== 'absolute' && position !== 'fixed') {
const { width, height } = style;
const a = dom.getBoundingClientRect();
dom.style.position = 'absolute';

dom.style.width = width;
dom.style.height = height;
const b = dom.getBoundingClientRect();
if (a.left !== b.left || a.top !== b.top) {
// Previously, in the Svelte 4, we'd just apply the transform the the DOM element. However,
// because we're now using Web Animations, we can't do that as it won't work properly if the
// animation is also making use of the same transformations. So instead, we apply an instantaneous
// animation and pause it on the first frame, just applying the same behavior.
const style = getComputedStyle(dom);
const transform = style.transform === 'none' ? '' : style.transform;
const frame = {
transform: `${transform} translate(${a.left - b.left}px, ${a.top - b.top}px)`
};
const animation = dom.animate([frame, frame], { duration: 1 });
animation.pause();
}
}
}
},
// cancel
c() {
Expand Down Expand Up @@ -432,10 +463,16 @@ function is_transition_block(block) {
export function bind_transition(dom, get_transition_fn, props_fn, direction, global) {
const transition_effect = /** @type {import('./types.js').EffectSignal} */ (current_effect);
const block = current_block;
const is_keyed_transition = direction === 'key';

let can_show_intro_on_mount = true;
let can_apply_lazy_transitions = false;

if (is_keyed_transition) {
// @ts-ignore
dom.__animate = true;
}

/** @type {import('./types.js').Block | null} */
let transition_block = block;
while (transition_block !== null) {
Expand Down Expand Up @@ -479,7 +516,7 @@ export function bind_transition(dom, get_transition_fn, props_fn, direction, glo
const init = (from) =>
untrack(() => {
const props = props_fn === null ? {} : props_fn();
return direction === 'key'
return is_keyed_transition
? /** @type {import('./types.js').AnimateFn<any>} */ (transition_fn)(
dom,
{ from: /** @type {DOMRect} */ (from), to: dom.getBoundingClientRect() },
Expand Down