They called Modal.
You can check the react-modal library.
Here is a simple example of react-modal being used in an app with some custom styles and focusable input elements within the modal content:
import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';
const customStyles = {
content : {
top : '50%',
left : '50%',
right : 'auto',
bottom : 'auto',
marginRight : '-50%',
transform : 'translate(-50%, -50%)'
}
};
// Make sure to bind modal to your appElement (http://reactcommunity.org/react-modal/accessibility/)
Modal.setAppElement('#yourAppElement')
class App extends React.Component {
constructor() {
super();
this.state = {
modalIsOpen: false
};
this.openModal = this.openModal.bind(this);
this.afterOpenModal = this.afterOpenModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
openModal() {
this.setState({modalIsOpen: true});
}
afterOpenModal() {
// references are now sync'd and can be accessed.
this.subtitle.style.color = '#f00';
}
closeModal() {
this.setState({modalIsOpen: false});
}
render() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
style={customStyles}
contentLabel="Example Modal"
>
<h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
<button onClick={this.closeModal}>close</button>
<div>I am a modal</div>
<form>
<input />
<button>tab navigation</button>
<button>stays</button>
<button>inside</button>
<button>the modal</button>
</form>
</Modal>
</div>
);
}
}
ReactDOM.render(<App />, appElement);
You can see more examples here
Answer from mdegis on Stack Overflow
» npm install react-popout
Videos
Can I create a React popup with TypeScript?
What's the right way to handle popup state in a large React application?
How do I test React popups effectively?
They called Modal.
You can check the react-modal library.
Here is a simple example of react-modal being used in an app with some custom styles and focusable input elements within the modal content:
import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';
const customStyles = {
content : {
top : '50%',
left : '50%',
right : 'auto',
bottom : 'auto',
marginRight : '-50%',
transform : 'translate(-50%, -50%)'
}
};
// Make sure to bind modal to your appElement (http://reactcommunity.org/react-modal/accessibility/)
Modal.setAppElement('#yourAppElement')
class App extends React.Component {
constructor() {
super();
this.state = {
modalIsOpen: false
};
this.openModal = this.openModal.bind(this);
this.afterOpenModal = this.afterOpenModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
openModal() {
this.setState({modalIsOpen: true});
}
afterOpenModal() {
// references are now sync'd and can be accessed.
this.subtitle.style.color = '#f00';
}
closeModal() {
this.setState({modalIsOpen: false});
}
render() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
style={customStyles}
contentLabel="Example Modal"
>
<h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
<button onClick={this.closeModal}>close</button>
<div>I am a modal</div>
<form>
<input />
<button>tab navigation</button>
<button>stays</button>
<button>inside</button>
<button>the modal</button>
</form>
</Modal>
</div>
);
}
}
ReactDOM.render(<App />, appElement);
You can see more examples here
The popup is called a modal. There are multiple libraries that support modals. React bootstrap provides a neat modal. Or if you want to use modal alone there are libraries such as react-modal and react-responsive-modal
» npm install react-new-window
Here is modern way to achieve this using React Hooks
import React, { useState } from "react";
const PopUp = ({ idMessage }) => {
// create state `open` with default as false
const [open, setOpen] = useState(false);
return (
<>
{/* click of button toggles `open` value therefore visibility */}
<button
onClick={() => setOpen(!open)}
type="button"
className="btn btn-primary"
data-toggle="modal"
data-target={`#${idMessage}`}
>
{idMessage}
</button>
{/* If open is true show your <div /> */}
{open && (
<div
className="modal fade"
id={idMessage}
tabIndex="-1"
role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
content
</div>
)}
</>
);
};
export default PopUp;
You could consider creating the popup as a re-usable component, that just renders the props.message.
Suppose you have the button in App.js, here is how you can add the click listener on it.
class App extends Component {
state = {showPopup: false};
openPopupHandler = () => {
this.setState({showPopup: true});
}
closePopupHandler = () => {
this.setState({showPopup: false});
}
render() {
let popup = null;
if(this.state.showPopup) {
popup = (<Popup message='the text you need to display' closeMe={this.closePopupHandler}/>);
}
return(
<div>
<button onClick={this.clicked}>Click me </button>
{popup}
</div>
);
}
}
And you can define the popup component as given below.
Popup.js
const popup = (props) => {
return (
<div>
<p>{props.message}</p>
<button onClick={props.closeMe}>Close Popup</button>
</div>
);
}
Also, style the popup component with the size as per your requirement and with an z-index greater than that of the parent component.
» npm install reactjs-popup
isOpen is not a property of this, but of state.
You have to write:
{this.state.isOpen && <Popup...
Here is the documentation in case you want to read about it yourself
You should call state values proper way. See examples here. And also you should fix the togglePopup function:
togglePopup = () => {
this.setState(
{ isOpen: !this.state.isOpen}
)
}
So it'll be changed to false when you call it in the handleClose function.