react-modal
Accessible modal dialog component for React
A React component that opens an accessible pop-up dialog, automatically trapping keyboard focus inside and working correctly with screen readers.
react-modal is a component for React applications that displays a pop-up dialog box on top of the rest of the page. When a user clicks a button or triggers some action, the modal appears in the center of the screen while the content behind it is still visible but inactive. Closing the modal returns the user to the normal page.
The main focus of this library is accessibility, meaning it is built to work properly with screen readers and keyboard navigation. A screen reader is software that reads on-screen content aloud for people with visual impairments. react-modal ensures that when a dialog opens, keyboard focus moves into it and stays there until the dialog closes, which is a requirement for accessible web applications.
Installing it takes one command via npm or yarn, the standard tools for adding code packages to a JavaScript project. Once added, you import the Modal component into your React code, pass it a true/false value to control whether it is open, and supply optional callbacks for when it opens, closes, or receives a close request from the user. Styling can be applied inline through a style prop or through CSS class names, giving developers control over the visual appearance without any constraints from the library itself.
The README includes a working code example showing how to open and close the modal with a button, how to run custom logic right after the modal opens, and how form elements inside the modal keep keyboard tab focus contained within the dialog. Additional live demos on CodePen show specific features like overlay click handling, inline styles, and CSS class styling.
Where it fits
- Add a confirmation dialog to a React app that traps keyboard focus and works with screen readers out of the box.
- Build an accessible image lightbox or form modal that opens on button click and closes when the user presses Escape.
- Create a modal with a form where the Tab key cycles only through the form fields, not the page behind it.