react-native-modal vs react-native-modal-datetime-picker vs react-native-modalbox
React Native 模态框库
react-native-modalreact-native-modal-datetime-pickerreact-native-modalbox类似的npm包:

React Native 模态框库

在 React Native 开发中,模态框是用于显示临时内容的常用组件,通常用于获取用户输入或显示重要信息。这些库提供了不同的模态框实现,帮助开发者以一致的方式处理用户交互。选择合适的模态框库可以提高用户体验,并简化开发过程。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
react-native-modal395,9015,65157.7 kB1002 年前MIT
react-native-modal-datetime-picker03,05143.9 kB582 年前MIT
react-native-modalbox02,91621.4 kB161-MIT

功能对比: react-native-modal vs react-native-modal-datetime-picker vs react-native-modalbox

功能特性

  • react-native-modal:

    提供多种动画效果,支持自定义模态框的样式和内容,能够处理复杂的用户交互。

  • react-native-modal-datetime-picker:

    专注于日期和时间选择,提供直观的用户界面,支持多种日期格式和本地化选项。

  • react-native-modalbox:

    提供基本的模态框功能,易于使用,适合快速实现简单的模态框需求。

可定制性

  • react-native-modal:

    高度可定制,允许开发者根据需求调整模态框的外观和行为,支持自定义动画和样式。

  • react-native-modal-datetime-picker:

    可定制性较低,主要集中在日期和时间选择的功能上,样式和行为相对固定。

  • react-native-modalbox:

    提供基本的定制选项,但功能相对简单,适合不需要复杂配置的场景。

学习曲线

  • react-native-modal:

    相对较高的学习曲线,特别是对于需要深入定制的开发者,可能需要更多的时间来理解其 API 和功能。

  • react-native-modal-datetime-picker:

    学习曲线较低,易于上手,适合快速集成日期和时间选择功能。

  • react-native-modalbox:

    学习曲线非常平缓,文档清晰,适合初学者和快速开发。

性能

  • react-native-modal:

    性能良好,支持复杂的动画和内容,但在处理大量模态框时可能会影响性能。

  • react-native-modal-datetime-picker:

    性能优化良好,专注于日期和时间选择,能够快速响应用户操作。

  • react-native-modalbox:

    性能较好,适合简单的模态框需求,但在复杂场景下可能会受到限制。

社区支持

  • react-native-modal:

    拥有活跃的社区支持,文档完善,开发者可以找到丰富的示例和解决方案。

  • react-native-modal-datetime-picker:

    社区支持良好,常见问题和用法都有详细的文档和示例。

  • react-native-modalbox:

    社区支持相对较小,但仍有基本的文档和示例可供参考。

如何选择: react-native-modal vs react-native-modal-datetime-picker vs react-native-modalbox

  • react-native-modal:

    选择 react-native-modal 如果你需要一个高度可定制的模态框,支持多种动画效果和复杂的内容布局。它适合需要灵活性和可扩展性的项目。

  • react-native-modal-datetime-picker:

    选择 react-native-modal-datetime-picker 如果你的应用需要日期和时间选择功能。这个库提供了一个用户友好的界面,专门用于选择日期和时间,简化了用户输入的过程。

  • react-native-modalbox:

    选择 react-native-modalbox 如果你需要一个简单且轻量级的模态框解决方案,适合快速实现基本的模态框功能。它易于使用且集成简单,适合小型项目或快速原型开发。

react-native-modal的README

Announcements

  • 📣 We're looking for maintainers and contributors! See #598
  • 🙏 If you have a question, please start a new discussion instead of opening a new issue.

react-native-modal

npm version styled with prettier

If you're new to the React Native world, please notice that React Native itself offers a component that works out-of-the-box.

An enhanced, animated, customizable React Native modal.

The goal of react-native-modal is expanding the original React Native <Modal> component by adding animations, style customization options, and new features, while still providing a simple API.

Features

  • Smooth enter/exit animations
  • Plain simple and flexible APIs
  • Customizable backdrop opacity, color and timing
  • Listeners for the modal animations ending
  • Resize itself correctly on device rotation
  • Swipeable
  • Scrollable

Setup

This library is available on npm, install it with: npm i react-native-modal or yarn add react-native-modal.

Usage

Since react-native-modal is an extension of the original React Native modal, it works in a similar fashion.

  1. Import react-native-modal:
import Modal from 'react-native-modal';
  1. Create a <Modal> component and nest its content inside of it:
function WrapperComponent() {
  return (
    <View>
      <Modal>
        <View style={{flex: 1}}>
          <Text>I am the modal content!</Text>
        </View>
      </Modal>
    </View>
  );
}
  1. Then, show the modal by setting the isVisible prop to true:
function WrapperComponent() {
  return (
    <View>
      <Modal isVisible={true}>
        <View style={{flex: 1}}>
          <Text>I am the modal content!</Text>
        </View>
      </Modal>
    </View>
  );
}

The isVisible prop is the only prop you'll really need to make the modal work: you should control this prop value by saving it in your wrapper component state and setting it to true or false when needed.

A complete example

The following example consists in a component (ModalTester) with a button and a modal. The modal is controlled by the isModalVisible state variable and it is initially hidden, since its value is false.
Pressing the button sets isModalVisible to true, making the modal visible.
Inside the modal there is another button that, when pressed, sets isModalVisible to false, hiding the modal.

import React, {useState} from 'react';
import {Button, Text, View} from 'react-native';
import Modal from 'react-native-modal';

function ModalTester() {
  const [isModalVisible, setModalVisible] = useState(false);

  const toggleModal = () => {
    setModalVisible(!isModalVisible);
  };

  return (
    <View style={{flex: 1}}>
      <Button title="Show modal" onPress={toggleModal} />

      <Modal isVisible={isModalVisible}>
        <View style={{flex: 1}}>
          <Text>Hello!</Text>

          <Button title="Hide modal" onPress={toggleModal} />
        </View>
      </Modal>
    </View>
  );
}

export default ModalTester;

For a more complex example take a look at the /example directory.

Available props

NameTypeDefaultDescription
animationInstring or object"slideInUp"Modal show animation
animationInTimingnumber300Timing for the modal show animation (in ms)
animationOutstring or object"slideOutDown"Modal hide animation
animationOutTimingnumber300Timing for the modal hide animation (in ms)
avoidKeyboardboolfalseMove the modal up if the keyboard is open
coverScreenbooltrueWill use RN Modal component to cover the entire screen wherever the modal is mounted in the component hierarchy
hasBackdropbooltrueRender the backdrop
backdropColorstring"black"The backdrop background color
backdropOpacitynumber0.70The backdrop opacity when the modal is visible
backdropTransitionInTimingnumber300The backdrop show timing (in ms)
backdropTransitionOutTimingnumber300The backdrop hide timing (in ms)
customBackdropnodenullThe custom backdrop element
childrennodeREQUIREDThe modal content
deviceHeightnumbernullDevice height (useful on devices that can hide the navigation bar)
deviceWidthnumbernullDevice width (useful on devices that can hide the navigation bar)
isVisibleboolREQUIREDShow the modal?
onBackButtonPressfunc() => nullCalled when the Android back button is pressed
onBackdropPressfunc() => nullCalled when the backdrop is pressed
onModalWillHidefunc() => nullCalled before the modal hide animation begins
onModalHidefunc() => nullCalled when the modal is completely hidden
onModalWillShowfunc() => nullCalled before the modal show animation begins
onModalShowfunc() => nullCalled when the modal is completely visible
onSwipeStartfunc() => nullCalled when the swipe action started
onSwipeMovefunc(percentageShown) => nullCalled on each swipe event
onSwipeCompletefunc({ swipingDirection }) => nullCalled when the swipeThreshold has been reached
onSwipeCancelfunc() => nullCalled when the swipeThreshold has not been reached
panResponderThresholdnumber4The threshold for when the panResponder should pick up swipe events
scrollOffsetnumber0When > 0, disables swipe-to-close, in order to implement scrollable content
scrollOffsetMaxnumber0Used to implement overscroll feel when content is scrollable. See /example directory
scrollTofuncnullUsed to implement scrollable modal. See /example directory for reference on how to use it
scrollHorizontalboolfalseSet to true if your scrollView is horizontal (for a correct scroll handling)
swipeThresholdnumber100Swiping threshold that when reached calls onSwipeComplete
swipeDirectionstring or arraynullDefines the direction where the modal can be swiped. Can be 'up', 'down', 'left, or 'right', or a combination of them like ['up','down']
useNativeDriverboolfalseDefines if animations should use native driver
useNativeDriverForBackdropboolnullDefines if animations for backdrop should use native driver (to avoid flashing on android)
hideModalContentWhileAnimatingboolfalseEnhances the performance by hiding the modal content until the animations complete
propagateSwipebool or funcfalseAllows swipe events to propagate to children components (eg a ScrollView inside a modal)
styleanynullStyle applied to the modal

Frequently Asked Questions

The component is not working as expected

Under the hood react-native-modal uses react-native original Modal component.
Before reporting a bug, try swapping react-native-modal with react-native original Modal component and, if the issue persists, check if it has already been reported as a react-native issue.

The backdrop is not completely filled/covered on some Android devices (Galaxy, for one)

React-Native has a few issues detecting the correct device width/height of some devices.
If you're experiencing this issue, you'll need to install react-native-extra-dimensions-android.
Then, provide the real window height (obtained from react-native-extra-dimensions-android) to the modal:

const deviceWidth = Dimensions.get('window').width;
const deviceHeight =
  Platform.OS === 'ios'
    ? Dimensions.get('window').height
    : require('react-native-extra-dimensions-android').get(
        'REAL_WINDOW_HEIGHT',
      );

function WrapperComponent() {
  const [isModalVisible, setModalVisible] = useState(true);

  return (
    <Modal
      isVisible={isModalVisible}
      deviceWidth={deviceWidth}
      deviceHeight={deviceHeight}>
      <View style={{flex: 1}}>
        <Text>I am the modal content!</Text>
      </View>
    </Modal>
  );
}

How can I hide the modal by pressing outside of its content?

The prop onBackdropPress allows you to handle this situation:

<Modal
  isVisible={isModalVisible}
  onBackdropPress={() => setModalVisible(false)}>
  <View style={{flex: 1}}>
    <Text>I am the modal content!</Text>
  </View>
</Modal>

How can I hide the modal by swiping it?

The prop onSwipeComplete allows you to handle this situation (remember to set swipeDirection too!):

<Modal
  isVisible={isModalVisible}
  onSwipeComplete={() => setModalVisible(false)}
  swipeDirection="left">
  <View style={{flex: 1}}>
    <Text>I am the modal content!</Text>
  </View>
</Modal>

Note that when using useNativeDriver={true} the modal won't drag correctly. This is a known issue.

The modal flashes in a weird way when animating

Unfortunately this is a known issue that happens when useNativeDriver=true and must still be solved.
In the meanwhile as a workaround you can set the hideModalContentWhileAnimating prop to true: this seems to solve the issue. Also, do not assign a backgroundColor property directly to the Modal. Prefer to set it on the child container.

The modal background doesn't animate properly

Are you sure you named the isVisible prop correctly? Make sure it is spelled correctly: isVisible, not visible.

The modal doesn't change orientation

Add a supportedOrientations={['portrait', 'landscape']} prop to the component, as described in the React Native documentation.

Also, if you're providing the deviceHeight and deviceWidth props you'll have to manually update them when the layout changes.

I can't show multiple modals one after another

Unfortunately right now react-native doesn't allow multiple modals to be displayed at the same time. This means that, in react-native-modal, if you want to immediately show a new modal after closing one you must first make sure that the modal that your closing has completed its hiding animation by using the onModalHide prop.

I can't show multiple modals at the same time

See the question above. Showing multiple modals (or even alerts/dialogs) at the same time is not doable because of a react-native bug. That said, I would strongly advice against using multiple modals at the same time because, most often than not, this leads to a bad UX, especially on mobile (just my opinion).

The StatusBar style changes when the modal shows up

This issue has been discussed here.
The TLDR is: it's a know React-Native issue with the Modal component 😞

The modal is not covering the entire screen

The modal style applied by default has a small margin.
If you want the modal to cover the entire screen you can easily override it this way:

<Modal style={{margin: 0}}>...</Modal>

I can't scroll my ScrollView inside of the modal

Enable propagateSwipe to allow your child components to receive swipe events:

<Modal propagateSwipe>...</Modal>

Please notice that this is still a WIP fix and might not fix your issue yet, see issue #236.

The modal enter/exit animation flickers

Make sure your animationIn and animationOut are set correctly.
We noticed that, for example, using fadeIn as an exit animation makes the modal flicker (it should be fadeOut!). Also, some users have noticed that setting backdropTransitionOutTiming={0} can fix the flicker without affecting the animation.

The custom backdrop doesn't fill the entire screen

You need to specify the size of your custom backdrop component. You can also make it expand to fill the entire screen by adding a flex: 1 to its style:

<Modal isVisible={isModalVisible} customBackdrop={<View style={{flex: 1}} />}>
  <View style={{flex: 1}}>
    <Text>I am the modal content!</Text>
  </View>
</Modal>

The custom backdrop doesn't dismiss the modal on press

You can provide an event handler to the custom backdrop element to dismiss the modal. The prop onBackdropPress is not supported for a custom backdrop.

<Modal
  isVisible={isModalVisible}
  customBackdrop={
    <TouchableWithoutFeedback onPress={dismissModalHandler}>
      <View style={{flex: 1}} />
    </TouchableWithoutFeedback>
  }
/>

Available animations

Take a look at react-native-animatable to see the dozens of animations available out-of-the-box. You can also pass in custom animation definitions and have them automatically register with react-native-animatable. For more information on creating custom animations, see the react-native-animatable animation definition schema.

Alternatives

Acknowledgements

Thanks @oblador for react-native-animatable, @brentvatne for the npm namespace and to anyone who contributed to this library!

Pull requests, feedbacks and suggestions are welcome!