mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Fix race condition when performing swap for fallback
This commit is contained in:
parent
8226002c0a
commit
e5f7d11ef5
1 changed files with 8 additions and 3 deletions
|
@ -168,13 +168,18 @@ const { fallback = 'animate' } = Astro.props as Props;
|
|||
|
||||
// Trigger the animations
|
||||
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.
|
||||
addEventListener('animationend', swap, { once: true });
|
||||
addEventListener('animationend', fallbackSwap, { once: true });
|
||||
// If there are no animations, go ahead and swap on next tick
|
||||
// This is necessary because we do not know if there are animations.
|
||||
// The setTimeout is a fallback in case there are none.
|
||||
setTimeout(() => !isAnimating && swap());
|
||||
var timeout = setTimeout(() => !isAnimating && fallbackSwap());
|
||||
} else {
|
||||
swap();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue