For class component
In app.js
import React from "react";
import Modal from "./Component/Modal";
import "./styles.css";
class App extends React.Component {
state = {
show: false
};
showModal = e => {
this.setState({
show: !this.state.show
});
};
render() {
return (
<div className="App">
<button
class="toggle-button"
id="centered-toggle-button"
onClick={e => {
this.showModal(e);
}}
>
{" "}
show Modal{" "}
</button>
<Modal onClose={this.showModal} show={this.state.show}>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis
deserunt corrupti, ut fugit magni qui quasi nisi amet repellendus non
</Modal>
</div>
);
}
}
export default App;
In component/modal
Make file named index.js
import React from "react";
import "./modal.css";
import PropTypes from "prop-types";
export default class Modal extends React.Component {
onClose = e => {
this.props.onClose && this.props.onClose(e);
};
render() {
if (!this.props.show) {
return null;
}
return (
<div class="modal" id="modal">
<h2>Modal Window</h2>
<div class="content">{this.props.children}</div>
<div class="actions">
<button class="toggle-button" onClick={this.onClose}>
close
</button>
</div>
</div>
);
}
}
Modal.propTypes = {
onClose: PropTypes.func.isRequired,
show: PropTypes.bool.isRequired
};
Make file modal.css
html,
body {
height: 100%;
}
body {
background: #eee;
display: flex;
justify-content: center;
align-items: center;
}
.modal {
width: 500px;
background: white;
border: 1px solid #ccc;
transition: 1.1s ease-out;
box-shadow: -2rem 2rem 2rem rgba(0, 0, 0, 0.2);
filter: blur(0);
transform: scale(1);
opacity: 1;
visibility: visible;
}
.modal.off {
opacity: 0;
visibility: hidden;
filter: blur(8px);
transform: scale(0.33);
box-shadow: 1rem 0 0 rgba(0, 0, 0, 0.2);
}
@supports (offset-rotation: 0deg) {
offset-rotation: 0deg;
offset-path: path("M 250,100 S -300,500 -700,-200");
.modal.off {
offset-distance: 100%;
}
}
@media (prefers-reduced-motion) {
.modal {
offset-path: none;
}
}
.modal h2 {
border-bottom: 1px solid #ccc;
padding: 1rem;
margin: 0;
}
.modal .content {
padding: 1rem;
}
.modal .actions {
border-top: 1px solid #ccc;
background: #eee;
padding: 0.5rem 1rem;
}
.modal .actions button {
border: 0;
background: #78f89f;
border-radius: 5px;
padding: 0.5rem 1rem;
font-size: 0.8rem;
line-height: 1;
}
#centered-toggle-button {
position: absolute;
}
This is an example of modal with class component. Please check if this helps you.
@Python
Second example
You can try this, if that does not work. This is bit easy also.
Using react bootstrap module. In App.js
import React from 'react';
import './App.css';
import { Button,Modal} from 'react-bootstrap';
class App extends React.Component {
constructor(){
super();
this.state={
show:false
}
}
handleModal(){
this.setState({show:!this.state.show})
}
render(){
return (
<div>
<h2 align='center'>Example of Modal in Reactjs</h2>
<div className="modalClass">
<Button onClick={()=>this.handleModal()}>Click To Open Modal</Button>
</div>
<Modal show={this.state.show} onHide={()=>this.handleModal()}>
<Modal.Header closeButton>This is a Modal Heading</Modal.Header>
<Modal.Body>This is a Modal Body</Modal.Body>
<Modal.Footer>
<Button onClick={()=>this.handleModal()}>Close</Button>
<Button onClick={()=>this.handleModal()}>Save</Button>
</Modal.Footer>
</Modal>
</div>
)
}
}
export default App;
In css file just add
.modalClass {
text-align: center;
margin-top: 100px;
}
Answer from user18246964 on Stack Overflow
» npm install reactjs-popup
How to add a simple popup in REACT? - javascript
What is best way to create popup modal in react js
Creating a popup in ReactJs
How do I create a popup window in react.js when the button and function are in separate file?
Can I create a React popup with TypeScript?
What's the difference between a popup and a modal in React?
What's the right way to handle popup state in a large React application?
Videos
For class component
In app.js
import React from "react";
import Modal from "./Component/Modal";
import "./styles.css";
class App extends React.Component {
state = {
show: false
};
showModal = e => {
this.setState({
show: !this.state.show
});
};
render() {
return (
<div className="App">
<button
class="toggle-button"
id="centered-toggle-button"
onClick={e => {
this.showModal(e);
}}
>
{" "}
show Modal{" "}
</button>
<Modal onClose={this.showModal} show={this.state.show}>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis
deserunt corrupti, ut fugit magni qui quasi nisi amet repellendus non
</Modal>
</div>
);
}
}
export default App;
In component/modal
Make file named index.js
import React from "react";
import "./modal.css";
import PropTypes from "prop-types";
export default class Modal extends React.Component {
onClose = e => {
this.props.onClose && this.props.onClose(e);
};
render() {
if (!this.props.show) {
return null;
}
return (
<div class="modal" id="modal">
<h2>Modal Window</h2>
<div class="content">{this.props.children}</div>
<div class="actions">
<button class="toggle-button" onClick={this.onClose}>
close
</button>
</div>
</div>
);
}
}
Modal.propTypes = {
onClose: PropTypes.func.isRequired,
show: PropTypes.bool.isRequired
};
Make file modal.css
html,
body {
height: 100%;
}
body {
background: #eee;
display: flex;
justify-content: center;
align-items: center;
}
.modal {
width: 500px;
background: white;
border: 1px solid #ccc;
transition: 1.1s ease-out;
box-shadow: -2rem 2rem 2rem rgba(0, 0, 0, 0.2);
filter: blur(0);
transform: scale(1);
opacity: 1;
visibility: visible;
}
.modal.off {
opacity: 0;
visibility: hidden;
filter: blur(8px);
transform: scale(0.33);
box-shadow: 1rem 0 0 rgba(0, 0, 0, 0.2);
}
@supports (offset-rotation: 0deg) {
offset-rotation: 0deg;
offset-path: path("M 250,100 S -300,500 -700,-200");
.modal.off {
offset-distance: 100%;
}
}
@media (prefers-reduced-motion) {
.modal {
offset-path: none;
}
}
.modal h2 {
border-bottom: 1px solid #ccc;
padding: 1rem;
margin: 0;
}
.modal .content {
padding: 1rem;
}
.modal .actions {
border-top: 1px solid #ccc;
background: #eee;
padding: 0.5rem 1rem;
}
.modal .actions button {
border: 0;
background: #78f89f;
border-radius: 5px;
padding: 0.5rem 1rem;
font-size: 0.8rem;
line-height: 1;
}
#centered-toggle-button {
position: absolute;
}
This is an example of modal with class component. Please check if this helps you.
@Python
Second example
You can try this, if that does not work. This is bit easy also.
Using react bootstrap module. In App.js
import React from 'react';
import './App.css';
import { Button,Modal} from 'react-bootstrap';
class App extends React.Component {
constructor(){
super();
this.state={
show:false
}
}
handleModal(){
this.setState({show:!this.state.show})
}
render(){
return (
<div>
<h2 align='center'>Example of Modal in Reactjs</h2>
<div className="modalClass">
<Button onClick={()=>this.handleModal()}>Click To Open Modal</Button>
</div>
<Modal show={this.state.show} onHide={()=>this.handleModal()}>
<Modal.Header closeButton>This is a Modal Heading</Modal.Header>
<Modal.Body>This is a Modal Body</Modal.Body>
<Modal.Footer>
<Button onClick={()=>this.handleModal()}>Close</Button>
<Button onClick={()=>this.handleModal()}>Save</Button>
</Modal.Footer>
</Modal>
</div>
)
}
}
export default App;
In css file just add
.modalClass {
text-align: center;
margin-top: 100px;
}
The first step is to import packages.
import React from "react";
import { Modal, Button, Form } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.css";
Then in App function you can set state to show and hide the modal popup.
function App() {
const [show, setShow] = useState(false);
const handleShow = () => setShow(true);
return (
<>
<div
className="d-flex align-items-center justify-content-center"
style={{ height: "100vh" }}
>
<Button variant="primary" onClick={handleShow}>
Launch Form modal
</Button>
</div>
<Modal show={show}>
<Modal.Header closeButton>
<Modal.Title>Login Form</Modal.Title>
</Modal.Header>
<Modal.Body>
<></>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary">Close Modal</Button>
</Modal.Footer>
</Modal>
</>
);
}
This is just an example, hope this helps you.
Wanto create popup in react js site. Is react portal is best way to create popup?
As it says in the source you should do something like this in the component you want to show the popup in:
//This is the function
togglePopup() {
this.setState({
showPopup: !this.state.showPopup
});
}
// This is what you should do in the render method
{this.state.showPopup ?
<Popup
text='Click "Close Button" to hide popup'
closePopup={this.togglePopup.bind(this)}
/>
: null
}
As per my understanding, you are trying to customize the code in the tutorial according to your requirements. If you want to open the popup on click of the button "click to view samples", you should do two things first.
- Define a function to trigger when button is clicked
- Attach that function to the button
The following code demonstrates above steps.
import React from 'react';
function feed (props) {
function openPopup(){
//code relevant to open popup
}
return (
<div className="card-header">
<h2>{props.firstname} {props.middleInitial} {props.lastname}</h2>
<h4 className="card-title">{props.gender}</h4>
</div>
<div className="card-footer">
<button onClick={openPopup} className="btn btn-secondary">Click To view Samples</button>
</div>
);
}
export default feed;