react-modal vs react-modal-promise
React 模态框库
react-modalreact-modal-promise类似的npm包:

React 模态框库

在 React 应用程序中,模态框是用于显示重要信息或获取用户输入的对话框。它们通常会覆盖屏幕的其余部分,以便用户集中注意力。react-modalreact-modal-promise 是两个流行的 React 模态框库,分别提供了不同的功能和使用场景。react-modal 提供了基本的模态框功能,允许开发者创建可自定义的模态框,而 react-modal-promise 则在此基础上引入了 Promise 机制,使得模态框的使用更加灵活,尤其是在需要处理用户输入或确认操作时。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
react-modal07,418188 kB2091 年前MIT
react-modal-promise06858 kB17-MIT

功能对比: react-modal vs react-modal-promise

功能

  • react-modal:

    react-modal 提供了基本的模态框功能,包括打开、关闭、动画效果和可自定义的样式。它允许开发者通过 props 控制模态框的可见性,并提供了对键盘事件的支持。

  • react-modal-promise:

    react-modal-promisereact-modal 的基础上,增加了 Promise 支持,使得模态框的调用可以返回一个 Promise 对象,便于处理用户的确认或输入结果。

如何选择: react-modal vs react-modal-promise

  • react-modal:

    选择 react-modal 如果你需要一个简单且可定制的模态框解决方案,适合于基本的模态框需求,且不需要复杂的用户交互。它提供了丰富的 API 以支持样式和行为的自定义。

  • react-modal-promise:

    选择 react-modal-promise 如果你的应用需要处理用户确认或输入,并希望通过 Promise 机制来简化异步操作的处理。这对于需要在模态框中执行确认操作的场景非常有用,比如删除确认或表单提交。

react-modal的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: