43
loading...
This website collects cookies to deliver better user experience
.overlay {
visibility: hidden;
opacity: 0;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.7);
transition: opacity 500ms;
}
.popup {
margin: 70px auto;
padding: 20px;
background: #fff;
border-radius: 5px;
width: 30%;
position: relative;
transition: all 5s ease-in-out;
}
.popup h2 {
margin-top: 0;
color: #333;
font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
position: absolute;
top: 20px;
right: 30px;
transition: all 200ms;
font-size: 30px;
font-weight: bold;
text-decoration: none;
color: #333;
}
.popup .close:hover {
cursor: pointer;
color: #000;
}
.popup .content {
max-height: 30%;
overflow: auto;
}
@media screen and (max-width: 700px) {
.popup {
width: 70%;
}
}
import { useEffect, useState } from "react";
import popupStyles from "./custom-popup.module.css";
import PropTypes from "prop-types";
const CustomPopup = (props) => {
const [show, setShow] = useState(false);
const closeHandler = (e) => {
setShow(false);
props.onClose(false);
};
useEffect(() => {
setShow(props.show);
}, [props.show]);
return (
<div
style={{
visibility: show ? "visible" : "hidden",
opacity: show ? "1" : "0"
}}
className={popupStyles.overlay}
>
<div className={popupStyles.popup}>
<h2>{props.title}</h2>
<span className={popupStyles.close} onClick={closeHandler}>
×
</span>
<div className={popupStyles.content}>{props.children}</div>
</div>
</div>
);
};
CustomPopup.propTypes = {
title: PropTypes.string.isRequired,
show: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired
};
export default CustomPopup;
npm install prop-types --save
<CustomPopup
onClose={popupCloseHandler}
show={visibility}
title="Hello Jeetendra"
>
<h1>Hello This is Popup Content Area</h1>
<h2>This is my lorem ipsum text here!</h2>
</CustomPopup>
import { useState } from "react";
import CustomPopup from "./CustomPopup";
import "./styles.css";
export default function App() {
const [visibility, setVisibility] = useState(false);
const popupCloseHandler = (e) => {
setVisibility(e);
};
return (
<div className="App">
<button onClick={(e) => setVisibility(!visibility)}>Toggle Popup</button>
<CustomPopup
onClose={popupCloseHandler}
show={visibility}
title="Hello Jeetendra"
>
<h1>Hello This is Popup Content Area</h1>
<h2>This is my lorem ipsum text here!</h2>
</CustomPopup>
</div>
);
}