shutu
shutu

Reputation: 149

Popup window disappearing after unclicking the button

I am trying to create a Popup window that is displayed just after the right arrow is pressed that is in KYC.jsx. I created but it is displayed as a window also but goes away after I stop clicking the button. Did I do something wrong? My code for KYC.jsx, Popup.jsx and KYC.css and Popup.css are as below: KYC.jsx

import React, { useState } from "react";
import "./KYC.css";
import { Link } from "react-router-dom";
import Popup from "./Popup";

const KYC = () => {
  const [isOpen, setIsOpen] = useState(false);

  const togglePopup = () => {
    setIsOpen(!isOpen);
  };

  return (
    <div className="KYC">
      <div className="page">
        <div className="content">
          <h1 className="flex">
            <i className="fas fa-praying-hands"></i>Welcome
          </h1>
          <div className="flex-list">
            <p className="flex-item">
              <i className="far fa-handshake"></i>
              <h2>Welcome to our Digital Account Opening platform.</h2>
            </p>
            <p className="flex-item">
              <i className="far fa-thumbs-up"></i>
              <h2>Thank you for going green with us and adopting paperless way of money.</h2>
            </p>
            <p className="flex-item">
              <i className="far fa-clock"></i>
              <h2>
                This service also help to save time so that you have more time to do what is more important to you.{" "}
              </h2>
            </p>
            <div className="Info">
              <h1>KYC Form Information</h1>
            </div>
            <hr />
            <div className="account">
              <p>Enter Your Account Number for KYC information:</p>
            </div>
            <form className="form">
              <input type="text" placeholder="Account Number" />
              <button className="right" onClick={togglePopup}>
                <i class="fas fa-arrow-right"></i>
              </button>
              {isOpen && (
                <Popup
                  content={
                    <div>
                      <h1>Sorry!!</h1>
                    </div>
                  }
                />
              )}
            </form>

            <Link to="/">
              <button className="back">
                <i class="fas fa-angle-double-left"></i>
              </button>
            </Link>
          </div>
        </div>
      </div>
    </div>
  );
};

export default KYC;

Popup.jsx


    import React from "react";
    import './Popup.css'
    
    const Popup = (props) => {
      return (
        <div className="popup-box">
          <div className="box">{props.content}</div>
        </div>
      );
    };
    
    export default Popup;

and KYC.css


.KYC {
    background-image: url("../../Assets/customer.png");
    background-repeat: no-repeat;
    background-position: right;
    background-size: auto 90%;

}

.page::before {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    width: 30%;
    height: 100%;
    background: #f939;
    clip-path: path('M0 0C35 96 106 177 195 210 315 249 362 290 433 361 567 491 493 597 635 800V0Z')
}

.page::after {
    content: '';
    position: absolute;

    right: 0px;
    top: 0px;
    width: 30%;
    height: 100%;
    background: #ee7600;
    clip-path: path('M0 0C35 70 180 197 253 211c126 28 145 77 194 167C542 506 493 597 635 800V0Z')
}

.flex {
    color: #ee7600;
    font-size: 50px;
    margin-left: 300px;
    display: flex;
    gap: 15px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

}


.flex-item {
    display: flex;
    align-items: center;
    gap: 6px;
    color: #585858;
    margin-left: 100px;
    font-weight: lighter;

}

.Info {
    color: #585858;
    margin-left: 150px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-weight: lighter;
    margin-top: 90px;
}

hr {
    width: 400px;
    border: 2px solid #ee7600;
    margin-top: -40px;
    margin-left: 27%;
    margin-bottom: 60px;


}

input {


    padding: 12px 20px;
    margin: 8px 5px;
    box-sizing: border-box;
    border: 2px solid #ee7600;
    border-radius: 4px;
    margin-left: 150px;
    width: 20%
}

.right {
    background-color: #ee7600;
    cursor: pointer;
    margin-left: 10px;
    color: white;
    font-size: 30px;
    border: 6px solid #ee7600;
    border-radius: 5px;
    padding: 12px 20px
}

.back {
    color: white;
    background-color: #ee7600;
    float: right;
    margin-right: 100px;
    font-size: 35px;
    border: 6px solid #ee7600;
    border-radius: 5px;
    cursor: pointer;
    padding: 15px 25px
}

and Popup.css

.popup-box {
    position: fixed;
    background: #00000050;
    width: 100%;
    height: 100vh;
    top: 0;
    left: 0;
  }
   
  .box {
    position: relative;
    width: 70%;
    margin: 0 auto;
    height: auto;
    max-height: 70vh;
    margin-top: calc(100vh - 85vh - 20px);
    background: #fff;
    border-radius: 4px;
    padding: 20px;
    border: 1px solid #999;
    overflow: auto;
  }

Though the window is displayed now but it disappears after I unclick the button? What should be done so that the window remains there until we want.

Upvotes: 2

Views: 578

Answers (2)

Youssouf Oumar
Youssouf Oumar

Reputation: 46311

The popup is showing right below the button because as I see you didn't set any CSS on it that should make it behave as a modal. You could use these lines for example:

.popup-box{
  position :fixed;
  width : 15rem;
  min-height: 20rem;
  top : 50%;
  left : 50%;
  transform:translate(-50%, -50%);
  z-index:5;
}

Upvotes: 0

Saief El Gebali
Saief El Gebali

Reputation: 116

Use React Portals to Create Popups

Your problem is that you are trying to render the Popup as a child element of the form.

<form className='form'>
    ...
    {isOpen && (
        <Popup/>
    )}
</form>;

This will not lead to your desired results.

If you want to render a React component outside of the DOM hierarchy of its parent component (Render the Popup outside of the form), you want to use React Portals.

Upvotes: 3

Related Questions