Skip to content
Advertisement

Tailwind animation plays twice when changing pages in Next.js?

I’m using react-hot-toast to show alerts & animate it while changing pages.

The animation plays fades in twice when page changes.

I’m using tailwindcss-animate in my tailwind.config.js to add animation classes.

I’m only using 4 animation classes: animate-in, animate-out, fade-in, & fade-out

I am animating a custom Success alert box.

Success.tsx

JavaScript

If you click the link in the demo below, then you should see the animation play twice like the gif below:

gif demo

How do I fix it so it only plays once?

Stackblitz Demo → https://stackblitz.com/edit/animate-in-tailwind

Github Repo → https://github.com/deadcoder0904/animate-in-tailwind/

Advertisement

Answer

When you switch the animation using t.visible, the fadeIn and fadeOut css properties of the finishing keyframe will not preserve. In other words, the Opacity: 0 will be removed, causing the node appear again. You can try to add style={{ animationFillMode: 'forwards' }} to retain the computed values set by the last keyframe.

Example:

JavaScript

You can also change the animation fill mode by adding class name fill-mode-forwards if you are using tailwind-animation.

Example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement