Skip to content

Commit 1db4e92

Browse files
authored
Allow fallback animations on html element (#8258)
* Allow fallback animations on html element * Add fallback attr after swap * Break apart addModern and addFallback into separate functions
1 parent 0a97524 commit 1db4e92

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Allow fallback animations on html element

packages/astro/components/ViewTransitions.astro

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,15 @@ const { fallback = 'animate' } = Astro.props as Props;
220220
links.length && (await Promise.all(links));
221221

222222
if (fallback === 'animate') {
223-
let isAnimating = false;
224-
addEventListener('animationstart', () => (isAnimating = true), { once: true });
225-
226223
// Trigger the animations
227224
document.documentElement.dataset.astroTransitionFallback = 'old';
225+
const finished = Promise.all(document.getAnimations().map(a => a.finished));
228226
const fallbackSwap = () => {
229-
removeEventListener('animationend', fallbackSwap);
230-
clearTimeout(timeout);
231227
swap();
232228
document.documentElement.dataset.astroTransitionFallback = 'new';
233229
};
234-
// If there are any animations, want for the animationend event.
235-
addEventListener('animationend', fallbackSwap, { once: true });
236-
// If there are no animations, go ahead and swap on next tick
237-
// This is necessary because we do not know if there are animations.
238-
// The setTimeout is a fallback in case there are none.
239-
let timeout = setTimeout(() => !isAnimating && fallbackSwap());
230+
await finished;
231+
fallbackSwap();
240232
} else {
241233
swap();
242234
}

packages/astro/src/runtime/server/transition.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function renderTransition(
5252
}
5353
}
5454
} else if (animationName === 'none') {
55-
sheet.addAnimationRaw('old', 'animation: none; opacity: 0; mix-blend-mode: normal;');
55+
sheet.addFallback('old', 'animation: none; mix-blend-mode: normal;');
56+
sheet.addModern('old', 'animation: none; opacity: 0; mix-blend-mode: normal;');
5657
sheet.addAnimationRaw('new', 'animation: none; mix-blend-mode: normal;');
5758
}
5859

@@ -88,11 +89,22 @@ class ViewTransitionStyleSheet {
8889
}
8990

9091
addAnimationRaw(image: 'old' | 'new' | 'group', animation: string) {
91-
const { scope, name } = this;
92+
this.addModern(image, animation);
93+
this.addFallback(image, animation);
94+
}
95+
96+
addModern(image: 'old' | 'new' | 'group', animation: string) {
97+
const { name } = this;
9298
this.addRule('modern', `::view-transition-${image}(${name}) { ${animation} }`);
99+
}
100+
101+
addFallback(image: 'old' | 'new' | 'group', animation: string) {
102+
const { scope } = this;
93103
this.addRule(
94104
'fallback',
95-
`[data-astro-transition-fallback="${image}"] [data-astro-transition-scope="${scope}"] { ${animation} }`
105+
// Two selectors here, the second in case there is an animation on the root.
106+
`[data-astro-transition-fallback="${image}"] [data-astro-transition-scope="${scope}"],
107+
[data-astro-transition-fallback="${image}"][data-astro-transition-scope="${scope}"] { ${animation} }`
96108
);
97109
}
98110

@@ -107,7 +119,8 @@ class ViewTransitionStyleSheet {
107119
this.addRule('modern', `${prefix}::view-transition-${image}(${name}) { ${animation} }`);
108120
this.addRule(
109121
'fallback',
110-
`${prefix}[data-astro-transition-fallback="${image}"] [data-astro-transition-scope="${scope}"] { ${animation} }`
122+
`${prefix}[data-astro-transition-fallback="${image}"] [data-astro-transition-scope="${scope}"],
123+
${prefix}[data-astro-transition-fallback="${image}"][data-astro-transition-scope="${scope}"] { ${animation} }`
111124
);
112125
}
113126
}

0 commit comments

Comments
 (0)