Is it normal for Tailwind animation to loop twice when transitioning between pages in Next.js?

I'm currently utilizing react-hot-toast for displaying alerts and animating them during page transitions.

The animation involves a double fade-in effect when transitioning between pages.

In my project, I've integrated tailwindcss-animate within my tailwind.config.js to incorporate animation classes.

Specifically, I'm utilizing 4 essential animation classes: animate-in, animate-out, fade-in, & fade-out

The customized alert box being animated is of type Success.

Success.tsx

import toast, { ToastOptions } from 'react-hot-toast';
import { CheckCircleIcon } from '@heroicons/react/outline';
import clsx from 'clsx';

interface ISuccess {
  message: string;
  options?: ToastOptions;
}

export const Success = ({ message, options }: ISuccess) => {
  toast.custom(
    (t) => (
      <div
        className={clsx(
          'flex rounded-lg bg-gray-900 py-2.5 px-3 shadow-lg ring-1 ring-black ring-opacity-5',
          {
            'animate-in fade-in': t.visible,
            'fade-out animate-out': !t.visible,
          }
        )}
      >
        <CheckCircleIcon className="h-6 w-6 text-gray-700" />
        <div className="ml-2 text-gray-300">{message}</div>
      </div>
    ),
    {
      duration: 250,
      ...options,
    }
  );
};

If you view the demo below and click on the link provided, you'll observe the animation playing twice as demonstrated in the gif:

How can I modify it to only have a single playback?

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

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

Answer №1

Changes in the animation visibility with t.visible will not maintain the fadeIn and fadeOut css properties of the final keyframe. This means that the Opacity: 0 will disappear, causing the element to reappear. To keep the computed values from the last keyframe, you can include

style={{ animationFillMode: 'forwards' }}
.

For example:

<div
    style={{ animationFillMode: 'forwards' }}
    className={clsx(
      'flex rounded-lg bg-gray-900 py-2.5 px-3 shadow-lg ring-1 ring-black ring-opacity-5',
      {
        'animate-in fade-in': t.visible,
        'fade-out animate-out': !t.visible,
      }
    )}
  >

If using tailwind-animation, you can modify the animation fill mode by adding the class name fill-mode-forwards.

For instance:

<div
    className={clsx(
      'flex rounded-lg bg-gray-900 py-2.5 px-3 shadow-lg ring-1 ring-black ring-opacity-5',
      {
        'animate-in fade-in fill-mode-forwards': t.visible,
        'fade-out animate-out fill-mode-forwards': !t.visible,
      }
    )}
  >

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React is unable to assign a class field beyond the scope of axios

class App extends React.Component { app: Application; ... componentDidMound() { axios.get(…).then(res => { this.app.currentUser = res.data.data; // setting value inside lambda function. console.log(this.app.currentUser); // ...

JQuery is having trouble with playing multiple sound files or causing delays with events

I've been working on a project that involves playing sounds for each letter in a list of text. However, I'm encountering an issue where only the last sound file is played instead of looping through every element. I've attempted to delay the ...

Encountered an error: Uncaught TypeError - Unable to access property 'active' as it is undefined within React code

<script type="text/babel"> class Frame extends React.Component{ render(){ return ( <div> <h2 className="os-animation" dat ...

Implement a validation function in the "jQuery validation library."

Hello, I am currently using the jQuery validation plugin to validate my form. I am trying to add an additional rule to the validation, but I am struggling to get it to work. The input value should be less than or equal to the previous element's value. ...

Displaying the information fetched using axios on the screen using node.js

Is there a way to display the information from the input boxes in the image on the screen using node js? ...

Issue with Jquery UI sortables not functioning properly

Struggling with getting a sortable list to work using jQuery UI. The ul element is receiving the class 'ui-sortable' but nothing seems to be happening. To demonstrate this issue, I created an example since the original javascript code is quite c ...

npm is facing difficulties while trying to install the scrypt package. It is prompting the requirement

When attempting to run a blockchain application, I encountered an error message that reads: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] install: node-gyp rebuild npm ERR! Exit status 1 npm E ...

Saving, displaying, and removing a JSON document

As someone new to the world of JavaScript, I am encountering an issue with JavaScript and AJAX. I am aiming to create a function that allows me to add new elements with unique indexes. After saving this information to a JSON file, I want to display it on a ...

Could someone share an instance of an AngularJS configuration that continuously checks for new data and automatically refreshes the user interface once the data is obtained?

Struggling to find a suitable example for this scenario. I am looking to create a chart directive that will be updated every minute by fetching data from a web service. Currently, I have a service that acts as a wrapper for the web service. My controller ...

Having trouble with importing SendInBlue into my NodeJS application?

Currently in the process of updating my Node app to utilize ES6 import modules over requiring files. Encountering difficulties while trying to integrate this change with SendInBlue for email functionality, resulting in the following error message: TypeEr ...

In Ember.js, where should I place the initialization code for a controller? I attempted to set it up in the routes

With my experience working with ember.js and highcharts, I have come across some examples that were too simplistic for me to grasp how to set up the chart objects and render them properly. I have explored initializers and understand the significance of ro ...

Guide to building a react-redux application with a Node server as the backend

Hey there! I'm looking to build a react-redux app with a node server as the backend. Is it feasible for the node server to serve the react-redux app instead of having react-redux run on one port and node on another? Any suggestions on how to kick thi ...

Is it possible to impose a different style on an element from another culture?

I am currently developing a themes library along with a demo page. The challenge I'm facing is that the demo page needs to showcase styles from the library without using all of the elements. For instance, consider the following style in an external s ...

Styling <Link> component with styled-components: A step-by-step guide

Utilizing the Link component from @material-ui/core/Link in my TypeScript code was initially successful: <Link href="#" variant="body2"> Forgot? </Link> However, I am exploring the transition to styled-components located in a separate file. ...

A guide on dynamically accessing an image from the src directory using props in a Vite + React application

export default function Card(props) { return ( <card> <img className={cardCss.card__img} src={`../assets/imgs/card/${props.img}`} alt="" /> <div className={cardCss.card__stats}> ...

`The challenge of language translation with i18next`

Transitioning from a single file containing all languages to individual files per language using i18next. The content of the translation.json file is as follows: { "section":{ "title":"wello world" } } This c ...

unable to upload the image on firestorage

I am in the process of creating a social media website similar to Facebook or Instagram. While the email, password, and name authentication during signup worked smoothly, I encountered an issue with uploading the profile picture. I suspect that the problem ...

Ways to access information received from AngularJS in a different Javascript file

I am currently using Angular to retrieve output from a controller and display it using ng-bind. However, I have another separate JavaScript file that needs to utilize a value returned from Angular. In the example code provided below, the TestAppCtrl is ca ...

What is the preferred approach in JavaScript: having a single large file or multiple smaller files?

Having a multitude of JavaScript files loaded on a single page can result in decreased performance. My inquiry is this: Is it preferable to have individual files or combine them into one JavaScript file? If consolidating all scripts into one file is the ...

Password validation in Redux-form will only be triggered once both password fields have been touched

My unique custom component utilizes an SFC that resembles: export const InputField = field => ( <div> <TextField required={field.required} invalid={field.meta.touched && !!field.meta.error} label={field.label} {...field.inpu ...