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.
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.
- Create a
Modal.jsxfile. - Ensure the overlay dims the background (use
position: fixedandbackground: rgba(0,0,0,0.7)in your CSS). - Implement the
onCloselogic 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-overlayhas a highz-indexvalue. - 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.
Work with me

Headless WordPress + Next.js Frontend Development
Keep WordPress for content, get a lightning-fast Next.js frontend. The best of both worlds — familiar editing, modern speed.

React & Next.js Dashboard / Admin UI Development
A clean, data-rich dashboard UI in React or Next.js — charts, tables, and real-time data that your users will actually enjoy using.