An Animated Way to Shut Modal Window

Below is the code snippet used to trigger the modal:

<div className={classes.modal}>{props.children}</div>

.modal {
   width: 90%;
   background-color: white;
   z-index: 30;
   position: fixed;
   top: 20vh;
   left: 5%;
   border-radius: 12px;
   padding: 1rem;
   align-items: center;
   box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
   animation: slide-down 0.5s ease-out forwards;
 }

I am curious if there is a way to add an animation for the div to slide out using either inline JSX styling in React or from the CSS? Any suggestions?

Answer №1

To create an animated div, you can utilize useState along with CSS transform (translate it off the screen). Here's a simple implementation:

App.js

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [show, setShow] = useState(true);
  return (
    <div className="App">
      <div className={`animationdiv ${show ? " show " : "hide"}`}>hello</div>

      <button onClick={() => setShow(!show)}> Animate</button>
    </div>
  );
}

style.css

.animationdiv {
  height: 300px;
  width: 300px;
  background: red;
}
.show {
  animation: slide-down 0.5s ease-in-out forwards;
}
.hide {
  animation: slide-up 0.5s ease-in-out forwards;
}

@keyframes slide-down {
  from {
    transform: translateY(-500px);
    opacity: 0;
  }
  to {
    transform: translateY(0px);
    opacity: 1;
  }
}
@keyframes slide-up {
  from {
    transform: translateY(0px);
    opacity: 1;
  }
  to {
    transform: translateY(-500px);
    opacity: 0;
  }
}

See a live example here: (https://codesandbox.io/s/wispy-smoke-zr7xb4)

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

Difficulty encountered in CSS selecting child elements

Can someone help me figure out why my list elements in the navigation bar are not floating to the right as intended? This is the HTML code: <body> <header> <!--Navigation bar--> <nav class="navbar navbar-expand- ...

Designing Buttons and Titles for the Login Page

I'm currently working on developing a straightforward login page in react native and I've encountered some difficulties with styling. Does anyone have tips on how to center the text on the button? Also, is there a way to move the INSTARIDE text ...

Incorporate the class directly into the datepicker input element in a React JS component

Adding a class directly to the input element is what I am trying to achieve here.https://i.sstatic.net/qGGwD.png The class MuiInputBase-input needs to be added. This code pertains to a date picker. import { DateTimePicker, MuiPickersUtilsProvider } from & ...

Error: Uncaught TypeError - Unable to access 'reduce' property of undefined value

Currently, I am focusing on implementing yup validation. Specifically for FileList validation, encountering an issue where leaving the input empty triggers the following error message: enter image description here Below is the code snippet in question: (C ...

Obtain the final card within a column of cards using Bootstrap 4

I am currently using Bootstrap 4 to create a masonry card deck that consists of 3 columns with 3 cards each. I am wondering if there is a way to target the last card in each column individually. I know how to target the very last card across all columns: ...

Struggling to increment the badge counter in a React Typescript project

I'm a bit unsure about where to apply the count in my Badge Component. The displayValue should include the count, but I'm struggling with how to implement it. UPDATE: The counter is now in place, but when I decrease the count and it reaches 0, t ...

Swapping one div for another, all the while incorporating a limited number of buttons within the webpage

I'm faced with a dilemma on my HTML page. In the middle of the content, there's a div that contains 4 links with images and text inside. What I'm looking to achieve is for each link to trigger a complete change in the div when clicked, with ...

Switching between different CSS transition effects

Perhaps my approach is not ideal, so please feel free to provide feedback if this is not considered best practice. I have created DIVs that contain a checkbox and label. These styled DIVs resemble buttons, and I have implemented code to allow users to tog ...

React and Redux: Error Alert - A change in state was detected during dispatch operations

While using 'controlled' components (utilizing setState() within the component), I am encountering an intermittent error when trying to save form data. The UserForm's onSave function calls back to the saveUser method in the component code pr ...

Is there a way to implement a search bar within the Navbar component in React and have it display search results in another component?

Hello there! I am currently working on a Navbar component that includes a search input field and an onchange function. This Navbar is placed inside the home component and is fixed at the top of all pages. My goal is to utilize this Navbar to filter data ...

Tips for creating space between subheader elements within a Material UI cardHeader

This query pertains specifically to the subheader property of Material UI card header. It does not involve adding spaces to a JavaScript variable or in JSX. The current code snippet only adds one space before and after the period. I am looking to add more ...

Effortless Page Navigation - Initial click may lead to incorrect scrolling position, but the subsequent link functions perfectly

While implementing the Smooth Page Scroll code from CSS Tricks, we encountered an issue where clicking on a navigation link for the first time only scrolls down to a point that is approximately 700px above the intended section. Subsequent clicks work perfe ...

What is the best way to hide a child element by default and display it only when users hover over its parent element using Material UI in React.js?

Is it possible to hide a child element by default and only show it with styling when users hover over the parent element using material-ui? Here's a sample code snippet showcasing how you can achieve this: import Grid from "@mui/material/Grid"; impor ...

What is the best approach to display a fluctuating price depending on specific options chosen in Next.js?

When working with 2 different select tags and rendering images based on the selection, I also want to display a price determined by the options chosen. For instance, selecting "Your Current Division" as Iron and "Your Desire League" as Bronze/Silver/Gold s ...

MUIDataTable: Displaying data from JSON in a selectable tooltip on row click instead of in a column

When working with React, I have integrated a MUIDataTable into my project. I have some JSON data that I would like to display as a tooltip on row click or hover instead of within a column. In the MUI table, if I do not include the hidden column name in th ...

Adding an item to an array with Redux Toolkit is a straightforward process

I am struggling to figure out how to properly add an object to an array reducer Below is the object that I am attempting to add: const id = 1; const type = 'deposit'; dispatch(addTransaction({id, type})) This is the code snippet of my ...

Adjusting the Image Width in React to Match Bootstrap Column Width

Exploring the capabilities of the react-image-mapper library, I stumbled upon an interesting feature: the ImageMapper component offers a prop called width, representing the image width in pixels. Integrating this component into my application based on the ...

Show the user's input within a clickable button

I'm trying to create a functionality where I have an input field and a button next to it. When I type something in the input field and click on the button, I want the result to be displayed in another button. Here is what I have attempted so far: f ...

Ways to access device width and height in React without relying on the window object

How can I retrieve the device width or height in React or Next.js without using "window" or "document"? I know that we can achieve this with the useEffect hook to avoid encountering the error: ReferenceError: window is not defined However, using useEffect ...

I am having trouble getting the CSS Dropdown menu tutorial from line25.com to work properly

As a complete newbie to web design and programming, I decided to take on a blog page project as a way to teach myself about the intricacies of coding. With no specific goal in mind other than experimenting with different features and trying to figure thing ...