Basket removal with smooth sliding animation

I need help with adding a sliding animation to my application when removing an element by clicking the 'remove from basket' button. I have tried several methods but haven't been successful. Can anyone assist me in incorporating the animation?

Here is the code snippet:

import React from "react";
import "./CheckoutProduct.css";
import { useStateValue } from "./StateProvider";

export default function CheckoutProduct({
  id,
  image,
  title,
  price,
  rating,
  hideButton
}) {
  const [{ basket }, dispatch] = useStateValue();

  const removeFromBasket = () => {
    dispatch({
      type: "REMOVE_FROM_BASKET",
      id: id
    });
  };

  return (
    <div className="checkoutProduct">
      <img className="CheckoutProduct__image" src={image} />
      <div className="CheckoutProduct__info">
        <p className="CheckoutProduct__title">{title}</p>
        <p className="CheckoutProduct__price">
          <small>$</small>
          <strong>{price}</strong>
        </p>
        <div className="CheckoutProduct__rating">
          {Array(rating)
            .fill()
            .map((_, i) => (
              <p>🌟</p>
            ))}
        </div>
        {!hideButton && (
          <button onClick={removeFromBasket}>Remove from basket</button>
        )}
      </div>
    </div>
  );
}

The removal functionality works fine without animations.

Appreciate any assistance!

Answer №1

Want to add some cool animations to your React project? Try using the Framer-Motion Animation Library.

npm install framer-motion
import React from "react";
import "./CheckoutProduct.css";
import { useStateValue } from "./StateProvider";

import { motion } from "framer-motion";
import { AnimateSharedLayout } from "framer-motion";

export default function CheckoutProduct({
  id,
  image,
  title,
  price,
  rating,
  hideButton
}) {
  const [{ basket }, dispatch] = useStateValue();

  const removeFromBasket = () => {
    dispatch({
      type: "REMOVE_FROM_BASKET",
      id: id
    });
  };

  return (
    <AnimateSharedLayout>
      <motion.div layout className="checkoutProduct">
        <img className="CheckoutProduct__image" src={image} />
        <motion.div layout className="CheckoutProduct__info">
          <p className="CheckoutProduct__title">{title}</p>
          <p className="CheckoutProduct__price">
            <small>$</small>
            <strong>{price}</strong>
          </p>
          <div className="CheckoutProduct__rating">
            {Array(rating)
              .fill()
              .map((_, i) => (
                <p>🌟</p>
              ))}
          </div>
          {!hideButton && (
            <motion.button onClick={removeFromBasket}>Remove from basket</button>
          )}
        </motion.div>
      </motion.div>
    </AnimateSharedLayout>
  );
}

If you encounter any issues, feel free to share your code with us so we can help troubleshoot.

Answer №2

In my experience, I find animate.css to be a straightforward and effective tool for adding animations to websites. You can check it out here and see how easy it is to incorporate into your projects.

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

I'm looking to leverage axios and useState-useEffect to collect data from numerous web pages. Any suggestions on how to do this efficiently

I stumbled upon a URL that leads to an API with a total of 42 pages and 826 data entries. The URL is . My goal is to store all the data in one variable for future filtering purposes, especially when implementing a "show more" button feature. Initially, on ...

"Trying to refresh your chart.js chart with updated data?”

Greetings! I have implemented a chart using chart.js and here is the corresponding code: let myChart = document.getElementById('myChart').getContext('2d'); let newChart = new Chart(myChart, { type: 'line', data: { labels: ...

Is it possible to exchange CSS classes for a specific group of elements using JQuery?

I have two list items listed below: <li name="luxury" class="cars luxury> <div id="featured_lux" name="featured" class="carImg_lux col2_lux "> My Luxury Car<img border="0" class="car-im ...

Does ng-include fetch the included HTML files individually or merge them into a single HTML file before serving?

Is there a performance impact when using the ng-include Angular directive, in terms of having included HTML files downloaded as separate entities to the user's browsers? I am utilizing a CDN like AWS CloudFront instead of a node server to serve the H ...

What is up with this strange JavaScript variable string / DOM selection?

I'm facing a strange issue with a variable that appears to be a string when alerted, but in the console, it is actually a DOMSelection presented in tree form. How can I retrieve the actual string value from this console output? DOMSelection anchorNod ...

What is the process for transferring an object to a different file?

Currently, I am working on a web application using Node.js. In my project, I have two main files. The first one is Server.js, where I handle server actions such as going online. The second file contains a large object filled with data. I successfully impo ...

Retrieve the highest date value from a collection of objects

Within a React.js component, I have the following array: const data = [{ date: '2017-12-21T07:43:00Z', value: 7000 }, { date: '2017-12-21T09:41:00Z', value: 1500, }, { date: '2017-12-23T10:59:00Z', value: 2000 }] ...

Overrule the child element selector using a class identifier

I'm facing an issue where the descendant selector is overriding my .red_p class. Is there a way to prevent this from happening without having to assign a class to each p element individually? HTML: <html> <head> <style> ...

My JavaScript code is functioning properly in jsfiddle, but when I try to run it on my own website, I encounter

Encountered an error message stating "Uncaught typeError: cannot call method 'hover' of null" following the line $('#nav li a').hover(function(){ in my JavaScript code. Check out the code on my site here: http://pastebin.com/GjZBEu3s Y ...

The issue persists where Tailwind Inline color decorators are unable to function properly alongside CSS variables

In my current project using Tailwind CSS with CSS variables for styling, I have noticed a peculiar issue. The color previewers/decorators that usually appear when defining a color are not showing up in my class names. These previewers are quite helpful in ...

Having trouble with clearing floats in Twitter Bootstrap CSS - any help appreciated!

Today, in an effort to learn CSS and apply it practically by creating simple themes, I decided to delve into the proper ways of clearing floats in CSS. My research led me to explore how Twitter handles this issue, so I dug into Bootstrap's CSS file an ...

How can I display table rows along with their child table rows in Angular?

Is there a way to display API data in a table using Angular? The table should have collapsible rows with nested child rows. This is the JSON file structure: { "collapse1": [ { "name": "Soil", "budget": 12345, "child": [ { ...

Difficulties encountered while attempting to modify a class using Javascript

Recently, I've encountered an issue with my JavaScript where I am unable to keep a particular element's class changed. Despite attempting to change the class to "overlist", it only stays that way briefly before switching back to its original stat ...

Can anyone suggest a reliable method for comparing two dependencies trees generated by the `npm list` command?

Prior to launching the project, it is crucial to analyze the updated dependencies that could potentially affect other pages. Utilizing the npm list command can help generate a comprehensive tree of all dependencies. What is the most efficient way to comp ...

The grpc client is not able to receive the data being streamed from the

I've been attempting to stream the output of a nodejs child process through grpc, but I consistently receive an empty result. Here is my proto file: syntax = "proto3"; package servicePackage; service Mobile { rpc sign(Empty) returns ( ...

Using JQuery, eliminate the transition effect from a child CSS class

I am facing an issue with transitioning an ID that has a child class. My goal is to transition the ID only, without affecting the class within it. Despite going through CSS and jQuery documentation, I have been unable to achieve this. The transitions are c ...

I am experiencing an issue in Next.js where the styling of the Tailwind class obtained from the API is not being applied to my

Having an issue with applying a bg tailwind class to style an element using an API. The class text is added to the classlists of my element but the style doesn't apply. Below are the relevant code snippets: //_app.js component import "../index.css"; i ...

Caution: The use of <fieldset> inside a <p> tag is not allowed in the TextField Component of Material UI in React JS. Please review and edit accordingly

<div className="textInputTag"> <span className="label">{props.label}</span> <TextField placeholder={placeholder} error={error ? true : false} defaultValue={props.defaultValue} onBlur={onBlur} onInput={on ...

The functionality of the Bootstrap blog template is not aligning with its intended purpose

Currently, I am implementing a solution from the following link: http://www.bootply.com/86704 However, I am facing an issue where it is occupying the entire vertical space of my webpage. I would like to integrate this into my site, but positioned below ...

Creating figures within an elliptical boundary

While this could be phrased as a mathematical problem, I am dealing with a programming challenge. In my case, I am working with a List containing an unknown number of elements (cluster nodes) that is only revealed once I receive the JSON data from the ser ...