A production-grade modal dialog is more than just a centered box. It requires:
document.body via createPortal.Escape key to close the modal.role="dialog", aria-modal="true").e.stopPropagation() NuanceNotice line 35: onClick={(e) => e.stopPropagation()} on .modal-content.
Because the outer .modal-backdrop has an onClick={onClose} handler, clicking anywhere inside the modal card would bubble up to the backdrop and close the modal! Invoking e.stopPropagation() on the inner container halts event bubbling, ensuring the modal only closes when the user clicks outside the card on the darkened backdrop.
e.stopPropagation() called on the inner modal card container?document.body.style.overflow = 'hidden' be set when a modal opens?useEffect cleanup function when locking body scroll?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Breaking Out of DOM Hierarchies with createPortal | Event Bubbling Through Portals Explained |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.