# react-modal2 **Repository Path**: mirrors_cloudflare/react-modal2 ## Basic Information - **Project Name**: react-modal2 - **Description**: :thought_balloon: Simple modal component for React. - **Primary Language**: Unknown - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2026-02-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # react-modal2 > Simple modal component for React. - Unopionated - Stateless (dumb component) - Accessible - Universal/Isomorphic - Built via [reusable](https://github.com/cloudflare/react-gateway) [collection](https://github.com/cloudflare/a11y-focus-scope) of [modules](https://github.com/cloudflare/a11y-focus-store) ## Installation ```js $ npm install --save react-modal2 ``` ## Usage ReactModal2 tries to be as minimal as possible. This means it requires a little bit of setup, but gives you complete flexibility to do what you want. Let's start off with the actual API of ReactModal2: ```js ... ``` If we use it like this it will simply render those two elements in the dom like this: ```html
...
``` However, you likely want to render the modal somewhere else in the DOM (in most cases at the end of the `document.body`. For this there is a separate library called [React Gateway](https://github.com/cloudflare/react-gateway). You can use it like this: ```js import { Gateway, GatewayDest, GatewayProvider } from 'react-gateway'; import ReactModal2 from 'react-modal2'; class Application extends React.Component { render() { return (

My Application

...
); } } ``` Which will render as: ```html

My Application

``` Now this might seem like a lot to do every time you want to render a modal, but this is by design. You are meant to wrap ReactModal2 with your own component that you use everywhere. Your component can add it's own DOM, styles, animations, and behavior. ```js import React from 'react'; import {Gateway} from 'react-gateway'; import ReactModal2 from 'react-modal2'; export default class MyCustomModal extends React.Component { static propTypes = { onClose: React.PropTypes.func.isRequired, closeOnEsc: React.PropTypes.bool, closeOnBackdropClick: React.PropTypes.bool }; getDefaultProps() { return { closeOnEsc: true, closeOnBackdropClick: true }; } render() { return ( {this.props.children} ); } } ``` Then simply setup your application once: ```js import { GatewayDest, GatewayProvider } from 'react-gateway'; export default class Application extends React.Component { render() { return (
...
); } } ``` Then you have your own ideal API for working with modals in any of your components. ```js import MyCustomModal from './my-custom-modal'; export default class MyComponent extends React.Component { state = { isModalOpen: false }; handleOpen() { this.setState({ isModalOpen: true }); } handleClose() { this.setState({ isModalOpen: false }); } render() { return (
{this.state.isModalOpen && (

Hello from Modal

)}
); } } ``` ## Props | Name | Type | Description | | --- | --- | --- | | `onClose` | `Function` | **Required.** A callback to handle an event that is attempting to close the modal. | | `closeOnEsc` | `Boolean` | Should this modal call `onClose` when the `esc` key is pressed? | | `closeOnBackdropClick` | `Boolean` | Should this modal call `onClose` when the backdrop is clicked? | | `backdropClassName` | `String` | An optional `className` for the backdrop element. | | `modalClassName` | `String` | An optional `className` for the modal element. | | `backdropStyles` | `Object` | Optional `style` for the backdrop element. | | `modalStyles` | `Object` | Optional `style` for the modal element. | ## Accessibility One of ReactModal2's opinions is that modals should be as accessible as possible. It does much of the work for you, but there's one little thing you need to help it with. In order to "hide" your application from screenreaders while a modal is open you need to let ReactModal2 what the root element for your application is. > **Note:** The root element should not contain the `GatewayDest` or whereever > the modal is getting rendered. This will break all the things. ```js import ReactModal2 from 'react-modal2'; ReactModal2.getApplicationElement = () => document.getElementById('application'); ``` ## FAQ #### How do I close the modal? ReactModal2 is designed to have no state, if you put it in the DOM then it will render. So if you don't want to show it then simply do not render it in your parent component. For this reason there is no `isOpen` property to pass.