content logo

React Popups:

Fly in Popup Effect using React JS

In this post, by clicking on the red button in the middle of the page, you can see the blue pop-up. This pop-up has a lighter color than the background of the page. With the pop-up display, the text is displayed from the left. Below the text is a red button that can be clicked to close the pop-up.

#

React Popup Code

#

Popup Fly in Effect

#

Animating Popup Code

#

CSS React Popup Effect

<!-- This script got from frontendfreecode.com -->
<div class="root"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
.root {
  position: relative;
  display: flex;
  background-color: #138C95;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}
.body--open {
  width: 100%;
  transform: scale(0.9);
  transition: transform 0.5s;
  background: rgba(68, 179, 194,0.8);
}
.button {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px 40px;
  border: 0;
  outline: none;
  background: #D81F4B;
  color: #fff;
  cursor: pointer;
  opacity: 1;
  transistion: opacity 0.3s ease;
}
.button--open {
  opacity: 0;
}
.modal-window {
  display: flex;
  flex-direction: column;
  position: absolute;
  width: 50%;
  height: 50vh;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #F2EDD8;
  padding: 40px;
  opacity: 1;
  transition: opacity 0.3s, transform 0.5s ease-in-out;
}
.modal-window-enter {
  opacity: 0;
  transform: translate(250%, -50%);
}
.modal-window button {
  margin-top: auto;
  background: #D81F4B;
  padding: 20px 30px;
  border: 0;
  outline: none;
  color: #fff;
  cursor: pointer;
}
const { CSSTransition } = ReactTransitionGroup;
class PopUp extends React.Component {
  constructor() {
    super();
    this.state = {
      modalOpen: false };
    this.handleToggleOpen = this.handleToggleOpen.bind(this);
    this.handleToggleClose = this.handleToggleClose.bind(this);
  }
  handleToggleOpen() {
    this.setState({
      modalOpen: true });
  }
  handleToggleClose() {
    this.setState({
      modalOpen: false });
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { className:
        this.state.modalOpen ?
        'body body--open' :
        'body' }, /*#__PURE__*/
      React.createElement("button", { className:
        this.state.modalOpen ?
        'button button--open' :
        'button',
        onClick: this.handleToggleOpen }, "Click Me!"), /*#__PURE__*/
      React.createElement(CSSTransition, {
        in: this.state.modalOpen,
        timeout: 300,
        classNames: "modal-window",
        unmountOnExit: true }, /*#__PURE__*/
      React.createElement("div", { className: 'modal-window' }, /*#__PURE__*/
      React.createElement("p", null, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin tincidunt tortor non eros vestibulum rutrum. Duis consequat purus sem, at aliquet arcu rutrum ut. Phasellus commodo, massa at congue commodo, sem massa egestas arcu, venenatis vehicula est lorem sed urna. Proin faucibus, nisl a sagittis condimentum."), /*#__PURE__*/
      React.createElement("button", { onClick: this.handleToggleClose }, "Close")))));
  }}
ReactDOM.render( /*#__PURE__*/React.createElement(PopUp, null), document.querySelector('.root'));
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.7.0/umd/react.production.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.7.0/umd/react-dom.production.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-transition-group/2.5.3/react-transition-group.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.7.0/umd/react.production.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.7.0/umd/react-dom.production.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-transition-group/2.5.3/react-transition-group.js'></script>
<style>
.root {
  position: relative;
  display: flex;
  background-color: #138C95;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}
.body--open {
  width: 100%;
  transform: scale(0.9);
  transition: transform 0.5s;
  background: rgba(68, 179, 194,0.8);
}
.button {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px 40px;
  border: 0;
  outline: none;
  background: #D81F4B;
  color: #fff;
  cursor: pointer;
  opacity: 1;
  transistion: opacity 0.3s ease;
}
.button--open {
  opacity: 0;
}
.modal-window {
  display: flex;
  flex-direction: column;
  position: absolute;
  width: 50%;
  height: 50vh;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #F2EDD8;
  padding: 40px;
  opacity: 1;
  transition: opacity 0.3s, transform 0.5s ease-in-out;
}
.modal-window-enter {
  opacity: 0;
  transform: translate(250%, -50%);
}
.modal-window button {
  margin-top: auto;
  background: #D81F4B;
  padding: 20px 30px;
  border: 0;
  outline: none;
  color: #fff;
  cursor: pointer;
}
</style>

</head>
<body>
<div class="root"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
const { CSSTransition } = ReactTransitionGroup;
class PopUp extends React.Component {
  constructor() {
    super();
    this.state = {
      modalOpen: false };
    this.handleToggleOpen = this.handleToggleOpen.bind(this);
    this.handleToggleClose = this.handleToggleClose.bind(this);
  }
  handleToggleOpen() {
    this.setState({
      modalOpen: true });
  }
  handleToggleClose() {
    this.setState({
      modalOpen: false });
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { className:
        this.state.modalOpen ?
        'body body--open' :
        'body' }, /*#__PURE__*/
      React.createElement("button", { className:
        this.state.modalOpen ?
        'button button--open' :
        'button',
        onClick: this.handleToggleOpen }, "Click Me!"), /*#__PURE__*/
      React.createElement(CSSTransition, {
        in: this.state.modalOpen,
        timeout: 300,
        classNames: "modal-window",
        unmountOnExit: true }, /*#__PURE__*/
      React.createElement("div", { className: 'modal-window' }, /*#__PURE__*/
      React.createElement("p", null, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin tincidunt tortor non eros vestibulum rutrum. Duis consequat purus sem, at aliquet arcu rutrum ut. Phasellus commodo, massa at congue commodo, sem massa egestas arcu, venenatis vehicula est lorem sed urna. Proin faucibus, nisl a sagittis condimentum."), /*#__PURE__*/
      React.createElement("button", { onClick: this.handleToggleClose }, "Close")))));
  }}
ReactDOM.render( /*#__PURE__*/React.createElement(PopUp, null), document.querySelector('.root'));
</script>

</body>
</html>
Preview