Convert Figma logo to code with AI

reactjs logoreact-modal

Accessible modal dialog component for React

7,364
808
7,364
198

Top Related Projects

20,694

A React-based UI toolkit for the web

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.

Bootstrap components built with React

An enterprise-class UI design language and React UI library

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.

Quick Overview

React-Modal is a popular React component library for creating accessible modal dialogs. It provides a flexible and customizable way to display modal content in React applications, with built-in accessibility features and support for various styling options.

Pros

  • Easy to implement and integrate into existing React projects
  • Highly customizable with support for custom styling and animations
  • Built-in accessibility features, including proper focus management and keyboard navigation
  • Actively maintained with regular updates and bug fixes

Cons

  • Can be overkill for simple modal requirements
  • Some users report issues with z-index management in complex layouts
  • Limited built-in styling options, requiring additional CSS for advanced designs
  • May require additional configuration for server-side rendering

Code Examples

  1. Basic usage of React-Modal:
import React, { useState } from 'react';
import Modal from 'react-modal';

function App() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setIsOpen(true)}>Open Modal</button>
      <Modal isOpen={isOpen} onRequestClose={() => setIsOpen(false)}>
        <h2>Modal Content</h2>
        <p>This is the modal body.</p>
        <button onClick={() => setIsOpen(false)}>Close Modal</button>
      </Modal>
    </div>
  );
}
  1. Customizing modal styles:
import React, { useState } from 'react';
import Modal from 'react-modal';

const customStyles = {
  content: {
    top: '50%',
    left: '50%',
    right: 'auto',
    bottom: 'auto',
    marginRight: '-50%',
    transform: 'translate(-50%, -50%)',
    backgroundColor: '#f0f0f0',
    borderRadius: '8px',
  },
};

function App() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setIsOpen(true)}>Open Modal</button>
      <Modal
        isOpen={isOpen}
        onRequestClose={() => setIsOpen(false)}
        style={customStyles}
      >
        <h2>Styled Modal</h2>
        <p>This modal has custom styles applied.</p>
        <button onClick={() => setIsOpen(false)}>Close</button>
      </Modal>
    </div>
  );
}
  1. Using React-Modal with custom overlay:
import React, { useState } from 'react';
import Modal from 'react-modal';

function App() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setIsOpen(true)}>Open Modal</button>
      <Modal
        isOpen={isOpen}
        onRequestClose={() => setIsOpen(false)}
        overlayClassName="custom-overlay"
        className="custom-modal"
      >
        <h2>Custom Overlay Modal</h2>
        <p>This modal uses custom classes for styling the overlay and content.</p>
        <button onClick={() => setIsOpen(false)}>Close</button>
      </Modal>
    </div>
  );
}

Getting Started

To use React-Modal in your project:

  1. Install the package:

    npm install react-modal
    
  2. Import and use the Modal component in your React application:

    import Modal from 'react-modal';
    
    // Set the app element for accessibility
    Modal.setAppElement('#root');
    
    function App() {
      // ... (modal implementation as shown in the examples above)
    }
    
  3. Customize the modal as needed using props, styles, or custom classes.

Competitor Comparisons

20,694

A React-based UI toolkit for the web

Pros of Blueprint

  • Comprehensive UI toolkit with a wide range of components
  • Consistent design system for building complex applications
  • Extensive documentation and examples

Cons of Blueprint

  • Steeper learning curve due to its extensive feature set
  • Larger bundle size compared to more focused libraries
  • May be overkill for simpler projects or single-component needs

Code Comparison

React Modal:

import ReactModal from 'react-modal';

<ReactModal
  isOpen={modalIsOpen}
  onRequestClose={closeModal}
  contentLabel="Example Modal"
>
  <h2>Modal Content</h2>
</ReactModal>

Blueprint:

import { Dialog, Classes } from "@blueprintjs/core";

<Dialog
  isOpen={isOpen}
  onClose={handleClose}
  title="Dialog Title"
>
  <div className={Classes.DIALOG_BODY}>
    <p>Dialog content</p>
  </div>
</Dialog>

React Modal is a focused library for creating modal dialogs, while Blueprint provides a comprehensive set of UI components, including modals. React Modal offers simplicity and ease of use for projects that only need modal functionality. Blueprint, on the other hand, provides a consistent design system and a wide range of components, making it suitable for larger, more complex applications. However, Blueprint's extensive feature set may result in a larger bundle size and a steeper learning curve compared to React Modal's more focused approach.

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.

Pros of Material-UI

  • Comprehensive UI component library with a wide range of pre-built components
  • Consistent design language following Material Design principles
  • Extensive customization options and theming support

Cons of Material-UI

  • Larger bundle size due to the extensive component library
  • Steeper learning curve for developers new to Material Design concepts
  • More opinionated styling approach, which may not suit all project requirements

Code Comparison

Material-UI modal implementation:

import { Modal, Box } from '@mui/material';

<Modal open={open} onClose={handleClose}>
  <Box sx={modalStyle}>
    <h2>Modal Title</h2>
    <p>Modal content goes here</p>
  </Box>
</Modal>

React Modal implementation:

import Modal from 'react-modal';

<Modal isOpen={open} onRequestClose={handleClose}>
  <h2>Modal Title</h2>
  <p>Modal content goes here</p>
</Modal>

Summary

Material-UI offers a comprehensive UI library with consistent design and extensive customization options, but comes with a larger bundle size and steeper learning curve. React Modal is a lightweight, focused solution for modal functionality, providing more flexibility in styling but requiring additional work for complex UI components.

Bootstrap components built with React

Pros of react-bootstrap

  • Comprehensive UI component library with a wide range of pre-built components
  • Consistent styling and theming based on Bootstrap
  • Active community and regular updates

Cons of react-bootstrap

  • Larger bundle size due to the extensive component library
  • Less flexibility for custom designs compared to individual modal solutions

Code Comparison

react-bootstrap modal example:

import { Modal, Button } from 'react-bootstrap';

function MyModal({ show, handleClose }) {
  return (
    <Modal show={show} onHide={handleClose}>
      <Modal.Header closeButton>
        <Modal.Title>Modal Title</Modal.Title>
      </Modal.Header>
      <Modal.Body>Modal content goes here</Modal.Body>
      <Modal.Footer>
        <Button variant="secondary" onClick={handleClose}>Close</Button>
      </Modal.Footer>
    </Modal>
  );
}

react-modal example:

import Modal from 'react-modal';

function MyModal({ isOpen, onRequestClose }) {
  return (
    <Modal isOpen={isOpen} onRequestClose={onRequestClose}>
      <h2>Modal Title</h2>
      <p>Modal content goes here</p>
      <button onClick={onRequestClose}>Close</button>
    </Modal>
  );
}

Summary

react-bootstrap offers a comprehensive UI library with consistent styling, while react-modal provides a focused solution for modal functionality. Choose react-bootstrap for a full-featured Bootstrap-based design system, or react-modal for a lightweight, customizable modal implementation.

An enterprise-class UI design language and React UI library

Pros of Ant Design

  • Comprehensive UI component library with a wide range of components beyond just modals
  • Consistent design language and theming capabilities
  • Active development and large community support

Cons of Ant Design

  • Larger bundle size due to its extensive component set
  • Steeper learning curve for developers new to the library
  • Less flexibility for customizing individual components

Code Comparison

React Modal:

import ReactModal from 'react-modal';

<ReactModal
  isOpen={modalIsOpen}
  onRequestClose={closeModal}
  contentLabel="Example Modal"
>
  <h2>Modal Content</h2>
  <button onClick={closeModal}>Close</button>
</ReactModal>

Ant Design:

import { Modal } from 'antd';

<Modal
  title="Modal Title"
  visible={isModalVisible}
  onOk={handleOk}
  onCancel={handleCancel}
>
  <p>Modal Content</p>
</Modal>

Summary

React Modal is a focused library for creating accessible modal dialogs in React applications. It offers simplicity and ease of use for developers who only need modal functionality.

Ant Design, on the other hand, is a comprehensive UI library that includes a modal component along with many other UI elements. It provides a consistent design system but may be overkill for projects that only require modal functionality.

Choose React Modal for lightweight, modal-specific needs, and Ant Design for larger projects requiring a complete UI toolkit with consistent design.

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.

Pros of Headless UI

  • More comprehensive UI component library, offering various accessible components beyond just modals
  • Unstyled components provide greater flexibility for custom designs
  • Seamless integration with Tailwind CSS for rapid development

Cons of Headless UI

  • Steeper learning curve due to its headless nature and more complex API
  • Requires more setup and configuration compared to React Modal's simpler implementation

Code Comparison

React Modal:

import ReactModal from 'react-modal';

<ReactModal isOpen={isOpen} onRequestClose={closeModal}>
  <h2>Modal Content</h2>
  <button onClick={closeModal}>Close</button>
</ReactModal>

Headless UI:

import { Dialog } from '@headlessui/react'

<Dialog open={isOpen} onClose={closeModal}>
  <Dialog.Overlay className="fixed inset-0 bg-black opacity-30" />
  <Dialog.Title>Modal Content</Dialog.Title>
  <button onClick={closeModal}>Close</button>
</Dialog>

Summary

Headless UI offers a more comprehensive and flexible solution for building accessible UI components, including modals. It provides greater customization options and integrates well with Tailwind CSS. However, it may require more setup and has a steeper learning curve compared to React Modal's simpler, focused approach to modal functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-modal

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"></script>
    
  • Use <ReactModal> 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:

import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';

const customStyles = {
  content: {
    top: '50%',
    left: '50%',
    right: 'auto',
    bottom: 'auto',
    marginRight: '-50%',
    transform: 'translate(-50%, -50%)',
  },
};

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

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

  function openModal() {
    setIsOpen(true);
  }

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

  function closeModal() {
    setIsOpen(false);
  }

  return (
    <div>
      <button onClick={openModal}>Open Modal</button>
      <Modal
        isOpen={modalIsOpen}
        onAfterOpen={afterOpenModal}
        onRequestClose={closeModal}
        style={customStyles}
        contentLabel="Example Modal"
      >
        <h2 ref={(_subtitle) => (subtitle = _subtitle)}>Hello</h2>
        <button onClick={closeModal}>close</button>
        <div>I am a modal</div>
        <form>
          <input />
          <button>tab navigation</button>
          <button>stays</button>
          <button>inside</button>
          <button>the modal</button>
        </form>
      </Modal>
    </div>
  );
}

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

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:

NPM DownloadsLast 30 Days