It vanishes as soon as you move your cursor away during the animation

I created a button component with text animation, but I'm encountering an issue.

When I hover over the button, the animation works smoothly. However, if I quickly move my cursor away or unhover in the middle of the animation, the text disappears unexpectedly.

This is the structure of my component:

index.tsx

import { motion } from "framer-motion";
import styles from "./styles.module.scss";

interface ButtonProps {
  isActive: boolean;
  toggleMenu: () => void;
}

const Button: React.FC<ButtonProps> = ({ isActive, toggleMenu }) => {
  return (
    <div className={styles.button}>
      <motion.div
        className={styles.slider}
        animate={{ top: isActive ? "-100%" : "0%" }}
        transition={{ duration: 0.5, type: "tween", ease: [0.76, 0, 0.24, 1] }}
      >
        <div className={styles.el} onClick={toggleMenu}>
          <PerspectiveText label="Menu" />
        </div>
        <div className={styles.el} onClick={toggleMenu}>
          <PerspectiveText label="Close" />
        </div>
      </motion.div>
    </div>
  );
};

interface PerspectiveTextProps {
  label: string;
}

const PerspectiveText: React.FC<PerspectiveTextProps> = ({ label }) => {
  return (
    <div className={styles.perspectiveText}>
      <p>{label}</p>
      <p>{label}</p>
    </div>
  );
};

export default Button;

Below are the styles used for the animation (which depends on the .perspectiveText class):

styles.module.scss

.button {
  position: absolute;
  top: 25px;
  right: 25px;
  width: 100px;
  height: 40px;
  cursor: pointer;
  border-radius: 25px;
  overflow: hidden;

  .slider {
    position: relative;
    width: 100%;
    height: 100%;

    .el {
      width: 100%;
      height: 100%;
      background-color: #c9fd74;
      p {
        margin: 0px;
      }
      &:nth-of-type(2) {
        background-color: black;
        p {
          color: #c9fd74;
        }
      }
      &:hover {
        .perspectiveText {
          transform: rotateX(90deg);
          p {
            &:nth-of-type(1) {
              transform: translateY(-100%);
              opacity: 0;
            }
            &:nth-of-type(2) {
              opacity: 1;
            }
          }
        }
      }
    }
  }
}

.perspectiveText {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  transform-style: preserve-3d;
  transition: transform 0.75s cubic-bezier(0.76, 0, 0.24, 1);
  p {
    transition: all 0.75s cubic-bezier(0.76, 0, 0.24, 1);
    pointer-events: none;
    margin: 0;
    &:nth-of-type(2) {
      position: absolute;
      transform-origin: bottom center;
      transform: rotateX(-90deg) translateY(50%);
      opacity: 0;
    }
  }
}

Answer №1

The problem lies within the opacity setting while undergoing the transition.

p { &:nth-child(1) { transform: translateY(-100%); opacity: 0; // this is where the issue occurs }          } 

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

When attempting to build Next.js in standalone mode with Turborepo versions greater than 1.2.6, the process gets stuck and does not complete the Gitlab CI pipeline task

I am currently working on a project in a monorepo using pnpm workspaces and turborepo to handle the scripts within the monorepo. Initially, all individual projects were functioning correctly as they are nextjs projects. However, when I upgraded turborepo ...

The for loop encountered an uncaught type error when trying to access the .length property

Currently, I am working on a school project where the task is to create a basic social network posting application using Django and JavaScript. The purpose of using JavaScript is to dynamically load posts onto the webpage and update HTML components. I have ...

Error in React Material-UI Theme

Currently, I am attempting to create a unique custom theme using Mui's theme provider. However, upon implementing CreateTheme and adding the ThemeProvider, I encounter several errors. It seems that there is a conflict with the default Mui theme becau ...

How can I eliminate the gap above the footer in iframes alignment?

I have a setup with four iframes: header, menuframe, bodyframe, and footer. The menuframe and bodyframe are positioned next to each other with space between the footer and the menuframe/bodyframe. How can I remove this space? Here is the CSS: iframe{ ...

Is there a way to establish a restriction on the maximum number of selections allowed in a multi-select menu using Material UI in React?

I have a multi select menu from material UI component and I need to ensure that users can only choose three options. This is necessary as I will be sending that value as props. How can I limit the selection to just three options? Here is the code snippet I ...

jQuery Function malfunctioning after initial click

I have developed a function that plays and pauses music. It is designed to be simple and lightweight for small audio samples, with basic play and stop functionalities. The issue I'm encountering is that after playing the music for the second time, the ...

AngularJS Object Comparison: A Comprehensive Guide

My form initiates a GET request to the server upon loading, receiving data that is stored in 'master' and then copied to 'local' as shown below. $scope.dirty = false; init(data); function init(data) { $scope.master = angular.copy ...

Is it possible to use an Enum as a type in TypeScript?

Previously, I utilized an enum as a type because the code below is valid: enum Test { A, B, } let a: Test = Test.A However, when using it as the type for React state, my IDE displays an error: Type FetchState is not assignable to type SetStateActi ...

What is the easiest way to choose a child vertex with just one click on mxgraph?

I have nested vertices and I'm looking to directly select the child vertex with just one click. Currently, when I click on a child vertex, it first selects the parent vertex instead. It's selecting the parent vertex initially: To select the ch ...

Building a Twitter Feed Component Using ReactJS

In my current project, I have implemented a code snippet that generates a Tweet input box capable of accepting various types of content such as text, video, image, and emoji in different combinations. However, I am struggling with creating a display box f ...

Why does the CLI crash when attempting to retrieve all data from an Oracle Database with JQuery?

Trying to utilize JavaScript and Jquery for database search, a generic query.php file has been set up to pass in the database and query, returning an array. Strangely, when attempting to select all using *, the PHP server crashes with: https://i.stack.img ...

Leveraging PHP variables to determine the image location, implement an onclick function to cycle through the image gallery

I am facing an issue with a database query that is supposed to display multiple images. These images are stored in a PHP variable and shown one at a time using a JavaScript gallery with an onclick function to navigate through them. Unfortunately, the funct ...

Can jQuery.jScrollPane be set to consistently display a vertical scroll bar?

Can jQuery.jScrollPane be configured to consistently display a vertical scroll bar? Is there a hidden setting or API function that can achieve this? Ideally, without needing to adjust the content pane's height or other properties. ...

Tips on integrating Codrops tutorial codes into a SilverStripe website project

Exploring the vast array of tutorials and code examples on the Codrops site has been an enlightening experience. Codrops Website I'm eager to incorporate some of these resources into my SilverStripe projects as well. SilverStripe CMS After learning h ...

Creating custom validation in Vuetify for password confirmation is essential in ensuring that

While developing a Vue.js template, I encountered a scenario on the sign-up page where I needed to compare passwords during user registration. To achieve this, I implemented a custom validation rule in the code snippet below: <v-text-field label=" ...

What is the specific file where you can insert code that will apply to every page within the app directory of Next.js?

Where is the main global file to use useEffect for all pages? Previously in Next.js, you could utilize useEffect in _app.tsx, impacting all pages. Take a look at this example: const MyApp = ({ Component, pageProps }: AppProps) => { useEffect(() => ...

Using a System Environment Variable within an Environment Variable in Vercel: A Step-by-Step Guide

Currently, I am in the process of deploying a NextJs project using Vercel and integrating Kinde for authentication purposes. Kinde requires a specific environment variable to be set: KINDE_SITE_URL:http://localhost:300 // Value in local .env file In my V ...

Exploring the differences between JavaScript destructuring and assignment

In my code, I initially used this line: const availableDays = status.availableDays; However, a suggestion was made to replace it with this line: const { availableDays } = status; Both options achieve the same result in one line of code, but I am curious ...

Restrict the duplication of div elements with JQuery

Here is the structure I'm working with: <div class="checkmark-outer"> <div class="checkmark-33"> <div class="fa-stack fa-1x checkmark-icon"> <i class="fa fa-circle fa-stack-2x icon-background"></i> ...

Omit certain table columns when exporting to Excel using JavaScript

I am looking to export my HTML table data into Excel sheets. After conducting a thorough research, I was able to find a solution that works for me. However, I'm facing an issue with the presence of image fields in my table data which I want to exclude ...