Prevent View Transition fallback from waiting on looping animations (#8331)

* Prevent View Transition fallback from waiting on looping animations

* Filter out infinite animations
This commit is contained in:
Matthew Phillips 2023-08-31 13:32:45 -04:00 committed by GitHub
parent af41b03d05
commit 7a894eec3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Prevent View Transition fallback from waiting on looping animations

View file

@ -98,6 +98,17 @@ const { fallback = 'animate' } = Astro.props as Props;
return wait;
}
function isInfinite(animation: Animation) {
const effect = animation.effect;
if(
!effect ||
!(effect instanceof KeyframeEffect) ||
!effect.target
) return false;
const style = window.getComputedStyle(effect.target, effect.pseudoElement);
return style.animationIterationCount === "infinite";
}
const parser = new DOMParser();
async function updateDOM(html: string, state?: State, fallback?: Fallback) {
@ -221,8 +232,10 @@ const { fallback = 'animate' } = Astro.props as Props;
if (fallback === 'animate') {
// Trigger the animations
const currentAnimations = document.getAnimations();
document.documentElement.dataset.astroTransitionFallback = 'old';
const finished = Promise.all(document.getAnimations().map(a => a.finished));
const newAnimations = document.getAnimations().filter(a => !currentAnimations.includes(a) && !isInfinite(a));
const finished = Promise.all(newAnimations.map(a => a.finished));
const fallbackSwap = () => {
swap();
document.documentElement.dataset.astroTransitionFallback = 'new';