» npm install reactjs-popup
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?
One option would be to use the prompt() function, which displays a modal dialog through which user input can be entered and acquired. The prompt() method also allows you to supply a custom greeting, which can be passed as the first argument like so:
const enteredName = prompt('Please enter your name')
Integrating this with your existing react component can be done in a number of ways - one approach might be as follows:
/* Definition of handleClick in component */
handleClick = (event) => {
/* call prompt() with custom message to get user input from alert-like dialog */
const enteredName = prompt('Please enter your name')
/* update state of this component with data provided by user. store data
in 'enteredName' state field. calling setState triggers a render of
this component meaning the enteredName value will be visible via the
updated render() function below */
this.setState({ enteredName : enteredName })
}
render: function() {
return (
<div>
{/* For demonstration purposes, this is how you can render data
previously entered by the user */ }
<p>Previously entered user name: { this.state.enteredName }</p>
<input type="text" onChange={ this.handleChange } />
<input
type="button"
value="Alert the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
I think you mean prompt():
var userName = prompt('Please Enter your Name')
The userName variable will contain the user answer.
» npm install react-popout
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.