Skip to content

Commit

Permalink
Prevent View Transition fallback from waiting on looping animations (#…
Browse files Browse the repository at this point in the history
…8331)

* Prevent View Transition fallback from waiting on looping animations

* Filter out infinite animations
  • Loading branch information
matthewp authored Aug 31, 2023
1 parent af41b03 commit 7a894ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/late-foxes-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevent View Transition fallback from waiting on looping animations
15 changes: 14 additions & 1 deletion packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 7a894ee

Please sign in to comment.