Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 38 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20263 min read

Building a Modal Component: Managing UI Overlays in React

Learn to build a professional modal component in React. Master state-driven visibility, overlay patterns, and click-outside event handling for your movie app.

ReactUI ComponentsEventsState ManagementModalsjavascriptfrontend

Previously in this course, we explored review of state management to understand how data flows through our application. In this lesson, we’ll take that knowledge and apply it to a specific UI challenge: displaying detailed information in a modal.

A modal is a focused overlay that interrupts the user's flow to present critical content. Because it sits on top of your existing dynamic movie cards, it requires careful management of state and events.

Understanding the Modal Pattern

At its core, a modal is just a component that conditionally renders based on a boolean state. However, a "true" modal behaves differently than a standard block element. It needs an overlay to dim the background, a container to hold the content, and a way to dismiss itself when the user clicks outside.

Managing Visibility with State

To control the modal, we lift the visibility state to the parent component that contains our movie list. When a user clicks a "Details" button, we set that state to true.

JSX
// MovieList.jsx
import { useState } from CE9178">'react';
import Modal from CE9178">'./Modal';

export default function MovieList({ movies }) {
  const [selectedMovie, setSelectedMovie] = useState(null);

  return (
    <div>
      {movies.map(movie => (
        <div key={movie.id}>
          <h3>{movie.title}</h3>
          <button onClick={() => setSelectedMovie(movie)}>View Details</button>
        </div>
      ))}

      {selectedMovie && (
        <Modal 
          onClose={() => setSelectedMovie(null)} 
          movie={selectedMovie} 
        />
      )}
    </div>
  );
}

By storing the selectedMovie object itself in state (rather than just a boolean), we effectively handle both the "open" state and the data context for the modal simultaneously.

Implementing the Modal Overlay

Now, let's build the Modal component. We want a full-screen overlay that captures clicks to close the window.

JSX
// Modal.jsx
export default function Modal({ onClose, movie }) {
  return (
    <div className="modal-overlay" onClick={onClose}>
      <div className="modal-content" onClick={(e) => e.stopPropagation()}>
        <h2>{movie.title}</h2>
        <p>{movie.description}</p>
        <button onClick={onClose}>Close</button>
      </div>
    </div>
  );
}

The Importance of Event Propagation

Notice the e.stopPropagation() call on the modal-content div. Without this, clicking on the modal content would trigger the onClick event of the modal-overlay behind it, causing the modal to close immediately. This is a classic example of handling click events where we need to stop the event from bubbling up to parent elements.

Hands-on Exercise

In your movie browser project, add a "Details" button to each movie card.

  1. Create a Modal.jsx file.
  2. Ensure the overlay dims the background (use position: fixed and background: rgba(0,0,0,0.7) in your CSS).
  3. Implement the onClose logic so that clicking the overlay or a "Close" button removes the modal from the DOM.

Common Pitfalls

  • Forgetting to stop propagation: As mentioned, if your modal closes when you click inside it, you missed e.stopPropagation() on your inner container.
  • Z-index issues: If the modal isn't appearing on top of your content, ensure the modal-overlay has a high z-index value.
  • Accessibility: In a production app, you would also want to trap focus within the modal so that users navigating via keyboard don't accidentally leave the modal while tabbing.

Recap

We’ve successfully extended our creating static components foundation to include interactive overlays. By using state to track the active movie and carefully managing click events, we’ve created a seamless viewing experience. Remember that modals are just components that exist conditionally; they don't require complex routing, just clean state management.

Up next: We will dive into PropTypes to add type safety and catch bugs in our component props before they happen.

Previous lessonReview of State ManagementNext lesson Introduction to PropTypes
Back to Blog

Similar Posts

ReactReactJune 25, 20263 min read

Building a Movie Filter Toggle in React: A Beginner's Guide

Master the art of UI state management by adding a filter toggle to your movie app. Learn to control list rendering using boolean state and checkbox inputs.

Read more
ReactReactJune 25, 2026

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 38 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min
3 min read

Fetching Data from an API: A Practical React Guide

Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.

Read more
ReactReactJune 25, 20264 min read

Rendering Lists of Data in React: A Practical Guide

Learn how to use the map method for list rendering in React. Master the key prop to keep your dynamic movie lists efficient, performant, and bug-free.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    3 min
  • 34

    Polishing the UI

    3 min
  • 35

    Finalizing the Movie Browser

    3 min
  • 36

    Review of Component Lifecycle

    4 min
  • 37

    Review of State Management

    4 min
  • 38

    Building a Modal Component

    3 min
  • 39

    Introduction to PropTypes

    3 min
  • 40

    Performance Optimization Basics

    3 min
  • 41

    Handling Browser History

    3 min
  • 42

    Working with LocalStorage

    3 min
  • 43

    Building a Favorites List

    3 min
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course