I am attempting to create a popup modal by pushing the action button in the lower right corner. I want the modal to slide upwards and the background behind the modal to blur. Right now, the modal just appears when the button is pushed and closes again. It’s nice but I want the background blurred and the menu to slide upwards smoothly. If possible, I’d like to have the button turn white when modal is open. Here is the code:
import React, { useState } from "react";
import ActionButton from "../../components/ActionButton";
import AddEventActionCard from "../../components/AddEventActionCard";
import AppHeader from "../../components/AppHeader";
import FooterNav from "../../components/FooterNav";
import "../Upcoming_Events/UpcomingEvents.css";
const UpcomingEvents = () => {
const [click, setClick] = useState(false);
const handleClick = () => setClick(!click);
return (
<div>
<AppHeader />
<div className="upcoming-container">
<div className="upcoming-card-container">
<div className="upcoming-card-top-header">Upcoming Events</div>
<div className="upcoming-card-top-events-scroll-container"></div>
</div>
{click ? <AddEventActionCard /> : ""}
<div onClick={handleClick}>
<ActionButton />
</div>
</div>
<FooterNav />
</div>
);
};
export default UpcomingEvents;
I’d love some assistance! Thank you so much!
2
Answers
To achieve the desired functionality of a blurred background and a smoothly sliding modal, you can use CSS and React animations.
So let’s make some changes to your code in order to archive that.
UpcomingEvents Component
CSS
ActionButton Component
Modal Component
In order for the modal component to smoothly transition into view rather than abruptly appearing, two boolean values are necessary. The first value is the
open
prop, which determines whether the component is mounted and triggers the useEffect to update theisVisible
state value. This state value is then used to add or remove the open class.When the backdrop is clicked, the
handleClose
function is invoked, setting theisVisible
value to false and triggering theonClose
function once the transition has ended. TheonClose
function is responsible for resetting theopen
prop externally.CSS Module
The CSS stylesheet includes four classes:
.modal
,.open
,.backdrop
, and.content
. The.content
class is not relevant to the current discussion as it only pertains to the modal’s content.The
.modal
class is responsible for giving the parent container a fixed position, with full width and height dimensions relative to the viewport. Additionally, it includes a translateY offset of 100%, which places the element off the bottom of the screen.The
.open
class is used to reset the.modal
translateY property to 0, bringing the element back into view.The
.backdrop
class has an opacity of 0, but includes a transition that gradually fades it in. This is primarily for aesthetic purposes. Thebackdrop-filter: blur(0.25rem);
style property is used to achieve the desired blurring effect.#Usage