This project is an example of a modal (dialog) component made with React. It's made to be simple and accessible. It purposely doesn't use the dialog's showModal and hideModal methods because they handle all the heavy lifting for accessibility and functionality.
It's not necesssarily complete. It's just a quick example. For example, it doesn't have tests for all props. Also, I haven't thoroughly tested its accessibility with screen readers.
The main component is the Modal component. I tried to make it the "react way" where the parent either renders the modal or doesn't. This contrasts the HTML way where the modal is always open and its open
attribute is toggled either directly or through its methods.
Alternative methods would be to:
- Let HTML handle the heavy lifting. Use the dialog component along with the
showModal
andhideModal
methods. They handle the majority of accessibility issues including closing the modal when Escape is pressed, focus trapping and changing the initial focus. - Allow the modal stay rendered and instead close / open with by passing an
open
prop from the parent.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Make sure Docker and docker compose are installed and that you're able to run containers. Please see Docker installation instructions.
- Clone the repo
git clone https://github.com/sargalias/react-modal
- Open a terminal and
cd
into the react-modal folder. - Run
docker compose up
- Visit http://localhost:3000 in your browser.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- NPM
Installation requires NPM which is included with Node. You can install Node by downloading the installer from the website.
To update NPM to the latest version:
npm install -g npm
- Clone the repo
git clone https://github.com/sargalias/react-modal
- Install NPM packages
npm install
npm start
- Start the project on a local server:
npm start
- Build the project for production:
npm run build
- React
- typescript
- focus-trap-react - For focus trapping inside the modal
- create-react-app - Uses the typescript template.
- docker and docker compose
- Node Docker image
This project is licensed under the MIT License - see the LICENSE.md file for details.