Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a modal in ReactJS

Understanding Modals

Let's picture modals as pop-up books. You're reading along and suddenly, you open a page and a 3D figure pops out at you. In programming, a modal is a similar concept. It's a pop-up window that suddenly appears (or "pops out") when triggered by a user action.

Modals are ideal for moments when you need to grab the user's attention. For example, it could be used to display a form or show some important information that needs the user's immediate action.

In this blog post, we will look at how to create a modal in ReactJS, a popular JavaScript library for building user interfaces.

The Fundamentals of a Modal

Before we dive into the code, let's grasp the basic structure of a modal. A modal typically consists of three main parts:

Overlay: This is a semi-transparent layer that covers the entire page when the modal is displayed. Its purpose is to give focus to the modal while visually indicating that the rest of the page is temporarily inactive.

Content: This is the main part of the modal where user interactions happen. It can hold various types of content like text, images, forms, etc.

Close Button: This allows the user to close the modal and return to the regular page content.

Creating a Basic Modal in ReactJS

We will start by creating a simple modal. Below is the code example:

import React, { useState } from 'react';

const App = () => {
  const [modalVisible, setModalVisible] = useState(false);

  return (
    <>
      <button onClick={() => setModalVisible(true)}>Open Modal</button>
      {modalVisible && (
        <div className="modal">
          <div className="modal-content">
            <h2>Hello, I'm a Modal</h2>
            <button onClick={() => setModalVisible(false)}>Close Modal</button>
          </div>
        </div>
      )}
    </>
  );
};

export default App;

In this code snippet, we're using the useState hook from React to handle the visibility of our modal. Initially, the modal is not visible (modalVisible is false). When the "Open Modal" button is clicked, we set modalVisible to true which triggers the modal to pop up.

The modal is a div element with the className of "modal". Inside this div, we have another div for the modal's content, and a button that sets modalVisible back to false when clicked, effectively closing the modal.

Styling the Modal

A modal wouldn't be a modal without its characteristic look and feel. Let's add some CSS to give our modal an overlay, center it on the page, and give it some basic styling:

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 4px;
  width: 80%;
  max-width: 500px;
}

Making the Modal Reusable

Most applications need more than one modal. To avoid repeating code, we can make our modal component reusable:

import React from 'react';

const Modal = ({ children, visible, setVisible }) => {
  if (!visible) return null;

  return (
    <div className="modal">
      <div className="modal-content">
        {children}
        <button onClick={() => setVisible(false)}>Close Modal</button>
      </div>
    </div>
  );
};

export default Modal;

In this new version of our Modal component, we're passing in children (the content we want to display), visible (whether the modal is currently visible), and setVisible (a function to change the visibility of the modal).

Conclusion

Creating modals in ReactJS is similar to building a pop-up book. You start with a trigger (like a button click) that opens the modal. The modal itself is a simple component that can be turned on and off with state, and can be styled to look like a typical modal. And just like a pop-up book, your modals can be filled with any content you need.

Mastering modals is a valuable skill in ReactJS development. They are a simple and effective way to engage your users and create dynamic and interactive user interfaces. Keep practicing and soon you'll be popping modals like a pro!