Fix race condition when performing swap for fallback (#7945)
* Fix race condition when performing swap for fallback * Adding a changeset * Use let
This commit is contained in:
parent
9b1de49075
commit
a00cfb8942
2 changed files with 13 additions and 3 deletions
5
.changeset/cold-maps-report.md
Normal file
5
.changeset/cold-maps-report.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix race condition when performing swap for fallback
|
|
@ -168,13 +168,18 @@ const { fallback = 'animate' } = Astro.props as Props;
|
||||||
|
|
||||||
// Trigger the animations
|
// Trigger the animations
|
||||||
document.documentElement.dataset.astroTransitionFallback = 'old';
|
document.documentElement.dataset.astroTransitionFallback = 'old';
|
||||||
doc.documentElement.dataset.astroTransitionFallback = 'new';
|
const fallbackSwap = () => {
|
||||||
|
removeEventListener('animationend', fallbackSwap);
|
||||||
|
clearTimeout(timeout);
|
||||||
|
swap();
|
||||||
|
document.documentElement.dataset.astroTransitionFallback = 'new';
|
||||||
|
};
|
||||||
// If there are any animations, want for the animationend event.
|
// If there are any animations, want for the animationend event.
|
||||||
addEventListener('animationend', swap, { once: true });
|
addEventListener('animationend', fallbackSwap, { once: true });
|
||||||
// If there are no animations, go ahead and swap on next tick
|
// If there are no animations, go ahead and swap on next tick
|
||||||
// This is necessary because we do not know if there are animations.
|
// This is necessary because we do not know if there are animations.
|
||||||
// The setTimeout is a fallback in case there are none.
|
// The setTimeout is a fallback in case there are none.
|
||||||
setTimeout(() => !isAnimating && swap());
|
let timeout = setTimeout(() => !isAnimating && fallbackSwap());
|
||||||
} else {
|
} else {
|
||||||
swap();
|
swap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue