Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

react-native-modal

Package Overview
Dependencies
19
Maintainers
2
Versions
104
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-modal

An enhanced React-Native modal


Version published
Maintainers
2
Created

Package description

What is react-native-modal?

react-native-modal is a customizable modal component for React Native applications. It provides a variety of features to create modals with different animations, styles, and behaviors.

What are react-native-modal's main functionalities?

Basic Modal

This code demonstrates a basic modal that can be toggled on and off with a button. The modal contains a simple text and a button to hide the modal.

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

const BasicModalExample = () => {
  const [isModalVisible, setModalVisible] = useState(false);

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

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Show Modal" onPress={toggleModal} />
      <Modal isVisible={isModalVisible}>
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Hello!</Text>
          <Button title="Hide Modal" onPress={toggleModal} />
        </View>
      </Modal>
    </View>
  );
};

export default BasicModalExample;

Custom Animation

This code demonstrates a modal with custom animations. The modal slides in from the left and slides out to the right.

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

const CustomAnimationModalExample = () => {
  const [isModalVisible, setModalVisible] = useState(false);

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

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Show Modal" onPress={toggleModal} />
      <Modal isVisible={isModalVisible} animationIn="slideInLeft" animationOut="slideOutRight">
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Custom Animation!</Text>
          <Button title="Hide Modal" onPress={toggleModal} />
        </View>
      </Modal>
    </View>
  );
};

export default CustomAnimationModalExample;

Backdrop Customization

This code demonstrates a modal with a customized backdrop. The backdrop color is set to red with an opacity of 0.8.

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

const BackdropCustomizationModalExample = () => {
  const [isModalVisible, setModalVisible] = useState(false);

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

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Show Modal" onPress={toggleModal} />
      <Modal isVisible={isModalVisible} backdropColor="red" backdropOpacity={0.8}>
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Custom Backdrop!</Text>
          <Button title="Hide Modal" onPress={toggleModal} />
        </View>
      </Modal>
    </View>
  );
};

export default BackdropCustomizationModalExample;

Other packages similar to react-native-modal

Readme

Source

react-native-modal

npm version styled with prettier

An enhanced, animated and customizable react-native modal.

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

Demo

Setup

This library is available on npm, install it with: npm install --save 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 react-native original modal.

  1. Import react-native-modal:
import Modal from "react-native-modal";
  1. Create a modal and nest its content inside of it:
render () {
    return (
      <View>
        <Modal>
          <View style={{ flex: 1 }}>
            <Text>I am the modal content!</Text>
          </View>
        </Modal>
      </View>
    )
  }
  1. Then simply show it by setting the isVisible prop to true:
render () {
    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 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, { Component } from "react";
import { Text, TouchableOpacity, View } from "react-native";
import Modal from "react-native-modal";

export default class ModalTester extends Component {
  state = {
    isModalVisible: false
  };

  _toggleModal = () =>
    this.setState({ isModalVisible: !this.state.isModalVisible });

  render() {
    return (
      <View style={{ flex: 1 }}>
        <TouchableOpacity onPress={this._toggleModal}>
          <Text>Show Modal</Text>
        </TouchableOpacity>
        <Modal isVisible={this.state.isModalVisible}>
          <View style={{ flex: 1 }}>
            <Text>Hello!</Text>
            <TouchableOpacity onPress={this._toggleModal}>
              <Text>Hide me!</Text>
            </TouchableOpacity>
          </View>
        </Modal>
      </View>
    );
  }
}

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
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)
childrennodeREQUIREDThe modal content
isVisibleboolREQUIREDShow the modal?
onBackButtonPressfunc() => nullCalled when the Android back button is pressed
onBackdropPressfunc() => nullCalled when the backdrop is pressed
onModalHidefunc() => nullCalled when the modal is completely hidden
onModalShowfunc() => nullCalled when the modal is completely visible
onSwipefuncnullCalled when the swipeThreshold has been reached
swipeThresholdnumber100Swiping threshold that when reached calls onSwipe
swipeDirectionstringnullDefines the direction where the modal can be swiped (can be 'up', 'down', 'left, or 'right')
useNativeDriverboolfalseDefines if animations should use native driver
hideModalContentWhileAnimatingboolfalseEnhances the performance by hiding the modal content until the animations complete
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.

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

The prop onBackdropPress allows you to handle this situation:

<Modal
  isVisible={this.state.isVisible}
  onBackdropPress={() => this.setState({ isVisible: 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 onSwipe allows you to handle this situation (remember to set swipeDirection too!):

<Modal
  isVisible={this.state.isVisible}
  onSwipe={() => this.setState({ isVisible: false })}
  swipeDirection="left"
>
  <View style={{ flex: 1 }}>
    <Text>I am the modal content!</Text>
  </View>
</Modal>

The modal flashes in a weird way when animating

Unfortunately this is a know 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.

The modal doesn't change orientation

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

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.

Pull requests, feedbacks and suggestions are welcome!

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

Keywords

FAQs

Last updated on 24 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc