React / 2 MIN READ
Simple Modal
Flashing back to 2000s
From the original Fervor library. Examples may use older package versions.
- Install necessary dependencies (if not already installed):
npm install react-icons
- Create a new functional component for the Card and the Modal, CardWithModal.js:
import React, { useState } from 'react';
import { AiOutlineClose } from 'react-icons/ai';
import './CardWithModal.css';
const CardWithModal = () => {
const [modalOpen, setModalOpen] = useState(false);
const toggleModal = () => {
setModalOpen(!modalOpen);
};
return (
<div>
<div className="card" onClick={toggleModal}>
<h3>Card Title</h3>
<p>Card content goes here...</p>
</div>
{modalOpen && (
<div className="modal">
<div className="modal-content">
<button className="close-btn" onClick={toggleModal}>
<AiOutlineClose />
</button>
<h3>Modal Title</h3>
<p>Modal content goes here...</p>
</div>
</div>
)}
</div>
);
};
export default CardWithModal;
- Create a CSS file, CardWithModal.css, to style the Card component and the Modal:
.card {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
padding: 15px;
width: 200px;
cursor: pointer;
}
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-content {
background-color: #fff;
border-radius: 5px;
padding: 20px;
width: 400px;
position: relative;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
font-size: 24px;
cursor: pointer;
}
- Import and use the Card component in your main app, for example, in App.js:
import React from 'react';
import CardWithModal from './CardWithModal';
const App = () => {
return (
<div>
<header>
{/* Your header content */}
</header>
<main>
<CardWithModal />
{/* Your main app content */}
</main>
</div>
);
};
export default App;
yeahhhhaaa have a poppin modal day!!!
Keep your curiosity going.Explore more React →