How to Create a Modal Popup in React JS

How to Create a Modal Popup in React JS

Hey everybody, let’s talk about How to create a Modal Popup in React JS from scratch. Creating modal popups in React JS is a common practice for builders. The modal is used to specify the content of the layer above the main content of the application. It can be used for a wide range of functions, corresponding to type, text, or other data. This article will walk you through the process of creating a modal popup in React JS.

Modals are temporary UI components that seem to be prime primary content content. It is often used to capture the attention of an individual or to gather direction. Modals can improve one’s knowledge by focusing on specific actions or data without leaving the current web page.

Planning Your Modal

Before diving into the code, you need to design your modal. Consider the following questions:

  • What is the purpose of the modal?

  • Will the content be revealed?

  • How will customers open and close?

  • What does it feel and look like?

If you want to create models in React, you want to configure your development environment. If you haven’t already, install Node.js and use Create React App to create a brand new React workspace. This can result in a boilerplate system in your utility.

How to Create a Modal Popup in React JS

Want to learn each step to creating a Modal Popup in React JS, you should watch the video. Inside the video tutorial, I’ve mentioned each step that will help you to understand please watch the video till to end.

Once you watch the complete tutorial I hope you’ve learned many new things and tactics that will help to improve your logic in React JS. I’m going to share the codes that I used in the video tutorial. If you face any problems during the video you can check the codes on below.

You May Also Like:

The best strategy is to create a dedicated version of your mode. This makes your code more modular and reusable. The modal part will handle its state and handle opening and closing.

App
import React from "react";
import Modal from "./compnents/Modal";
function App() {
  return (
    <div className="container">
      <Modal />
    </div>
  );
}
export default App;

The codes represent your main App.jsx file, you need to call the component on it. Then the next thing you need to create another file and write the complete codes, let’s see the codes on below.

Modal.jsx
import { useState } from "react";
import ModalImg from "../assets/bg.png";

function Modal() {
  const [isOpen, setOpen] = useState(false);
  return (
    <>
      {isOpen ? (
        <ModalContent setOpen={setOpen} />
      ) : (
        <button className="btn-modal" onClick={() => setOpen(true)}>
          Open a Modal
        </button>
      )}
    </>
  );
}
function ModalContent({ setOpen }) {
  return (
    <div className="modal_container">
      <div className="modal">
        <div className="img">
          <img src={ModalImg} alt="" />
          <p className="close" onClick={() => setOpen(false)}>
            X
          </p>
        </div>
        <div className="content">
          <h2>Signup for our newsletter</h2>
          <h3>& receive 10% off your first order</h3>
          <form>
            <input type="text" placeholder="Enter your email" />
            <button>Sign up</button>
          </form>
        </div>
      </div>
    </div>
  );
}
export default Modal;

Including Basic Styling

Styling for models is important to ensure it gives the impression of quality and ease of use. You should use CSS or CSS-in-JS post-like style components. The guarantee covers, backs up, and highlights your modal display.


@import url("https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-family: "poppins", sans-serif;
  background: url(".//assets/bg2.jpg") no-repeat center center/cover;
  position: relative;
}
body::before {
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  content: "";
  z-index: -1;
}
.btn-modal {
  outline: none;
  border: none;
  background: tomato;
  padding: 0.7rem 3rem;
  cursor: pointer;
  font-family: inherit;
  color: #fff;
  font-size: 1.2rem;
  border-radius: 10px;
}
.btn-modal:active {
  transform: scale(0.98);
}
.modal_container {
  width: 500px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 5px 0px 2px rgba(0, 0, 0, 0.5);
}
.modal_container .img {
  position: relative;
}
.modal_container .img img {
  object-fit: cover;
  width: 100%;
  display: block;
  border-radius: 10px;
}
.modal_container .close {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 1.3rem;
  cursor: pointer;
}
.modal_container .content {
  text-align: center;
  padding: 2rem 0;
}
.modal_container .content h2 {
  font-size: 2rem;
}
.modal_container form {
  margin: 2rem 0;
  position: relative;
}
.modal_container form input {
  width: 90%;
  outline: none;
  font-family: inherit;
  border: 1px solid #ddd;
  padding: 0.7rem 1rem;
  border-radius: 10px;
}
.modal_container form button {
  position: absolute;
  top: 05px;
  right: 30px;
  padding: 0.6rem 2rem;
  outline: none;
  border: none;
  border-radius: 50px;
  cursor: pointer;
  background-color: slateblue;
  color: #fff;
}

State management is necessary to control the visibility of your modal. You need to use the React useState hook to handle open and shut states. If someone makes a move that should trigger the modal, substitute the state accordingly.

It should be easy to open and close your modal. Sometimes you will get a button or hyperlink that opens the modal. The mode should be equipped with a wide button. Also, the modal may need to be released when the user clicks back or hits the ESC key.

Testing your modal

Once you have implemented your modal, it is important to fully test it. Rest assured that it actually works in completely different browser units. Observe several states, such as opening and closing the modal and the accuracy with which individual inputs are handled.

Improving efficiency

Modals should not affect the operation of your utility. Optimize your modal by lazily loading content content as needed and ensuring it is removed from the DOM when not in use.

Including Animation

Adding animation can improve one’s skills. You should use libraries like CSS animations or Framer Movement to make clean transitions as the modal opens and closes.

Behavior in compassionate identification

If your modal includes compassion, treat compassionate referrals accordingly. Be sure to welcome personal input and suggestions. You may have to deal with asynchronous operations such as API calls and specifying loading conditions.

Click Close Modal Next

Activate the closure of the modal by clicking backward to increase the individual’s awareness. This can be done by adding an opportunity listener in the background, and closing the modal when clicked.

Attention control

When the modal opens, it must assume attention, and when it closes, it must refocus on the stimulus. This can be handled using the useRef hook and focus administration methods.

If you have a number of modals in your utility, consider creating reusable modal elements. This reduces redundancy and keeps your codebase clean. You can move the contents and layouts of the objects into your modal component as props.

The modal can be used in many situations:

Types: For individual deposits, corresponding to log-in or registration type.
Notifications: To display important messages or alerts.
Confirmation dialogs: Actions that require human confirmation, such as deleting a file.
Update: To display updates when navigating away from the current web page.
Overuse of modals: Overuse of too many modals can lead to customer overwhelm. Use sparingly when necessary.
Ignore accessibility: Guarantee your modals are accessible to all customers. It has standard ARIA features and keyboard navigation.
Points of efficiency: Poorly optimized graphics can slow down your process. Guaranteed to load and unload correctly.

Conclusion

Creating a modal popup in React JS includes many steps from managing the state layout to styling and accessibility. By following best practices and optimizing assignments, you can create efficient and user-friendly models that improve the user’s knowledge of your utility. Be mindful to thoroughly analyze and consider the person’s wishes every step of the way.