react-modal

Accessible modal dialog component for React

README

react-modal


NOTE


Need feedback to push a new version of react-modal forward. See issue #881.


Accessible modal dialog component for React.JS
Build Status Coverage Status
gzip size Join the chat at https://gitter.im/react-modal/Lobby

Table of Contents



Installation


To install, you can use npm or yarn:


    $ npm install --save react-modal
    $ yarn add react-modal
    
To install react-modal in React CDN app:

   - Add this CDN script tag after React CDN scripts and before your JS files (for example from cdnjs):

            <script src="https://cdnjs.cloudflare.com/ajax/libs/react-modal/3.14.3/react-modal.min.js"
            integrity="sha512-MY2jfK3DBnVzdS2V8MXo5lRtr0mNRroUI9hoLVv2/yL3vrJTam3VzASuKQ96fLEpyYIT4a8o7YgtUs5lPjiLVQ=="
            crossorigin="anonymous"
referrerpolicy="no-referrer">

- Use `` tag inside your React CDN app.


API documentation


The primary documentation for react-modal is the
reference book, which describes the API
and gives examples of its usage.

Examples


Here is a simple example of react-modal being used in an app with some custom
styles and focusable input elements within the modal content:

  1. ``` js
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import Modal from 'react-modal';

  5. const customStyles = {
  6.   content: {
  7.     top: '50%',
  8.     left: '50%',
  9.     right: 'auto',
  10.     bottom: 'auto',
  11.     marginRight: '-50%',
  12.     transform: 'translate(-50%, -50%)',
  13.   },
  14. };

  15. // Make sure to bind modal to your appElement (https://reactcommunity.org/react-modal/accessibility/)
  16. Modal.setAppElement('#yourAppElement');

  17. function App() {
  18.   let subtitle;
  19.   const [modalIsOpen, setIsOpen] = React.useState(false);

  20.   function openModal() {
  21.     setIsOpen(true);
  22.   }

  23.   function afterOpenModal() {
  24.     // references are now sync'd and can be accessed.
  25.     subtitle.style.color = '#f00';
  26.   }

  27.   function closeModal() {
  28.     setIsOpen(false);
  29.   }

  30.   return (
  31.     <div>
  32.       <button onClick={openModal}>Open Modal</button>
  33.       <Modal
  34.         isOpen={modalIsOpen}
  35.         onAfterOpen={afterOpenModal}
  36.         onRequestClose={closeModal}
  37.         style={customStyles}
  38.         contentLabel="Example Modal"
  39.       >
  40.         <h2 ref={(_subtitle) => (subtitle = _subtitle)}>Hello</h2>
  41.         <button onClick={closeModal}>close</button>
  42.         <div>I am a modal</div>
  43.         <form>
  44.           <input />
  45.           <button>tab navigation</button>
  46.           <button>stays</button>
  47.           <button>inside</button>
  48.           <button>the modal</button>
  49.         </form>
  50.       </Modal>
  51.     </div>
  52.   );
  53. }

  54. ReactDOM.render(<App />, appElement);
  55. ```

You can find more examples in the examples directory, which you can run in a
local development server using npm start or yarn run start.

Demos


There are several demos hosted on CodePen which
demonstrate various features of react-modal: