react-modal

Accessible modal dialog component for React

Github星跟蹤圖

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 react-modal
$ yarn add react-modal

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 (http://reactcommunity.org/react-modal/accessibility/)
Modal.setAppElement('#yourAppElement')

function App(){
  var 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:

主要指標

概覽
名稱與所有者reactjs/react-modal
主編程語言JavaScript
編程語言JavaScript (語言數: 4)
平台
許可證MIT License
所有者活动
創建於2014-09-21 01:30:33
推送於2024-03-14 17:26:44
最后一次提交2024-02-11 21:29:07
發布數114
最新版本名稱v3.16.1 (發布於 2022-10-17 22:07:22)
第一版名稱v0.0.0 (發布於 )
用户参与
星數7.4k
關注者數64
派生數809
提交數576
已啟用問題?
問題數608
打開的問題數160
拉請求數269
打開的拉請求數38
關閉的拉請求數127
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?