Gathering detailed insights and metrics for react-native-modal
Gathering detailed insights and metrics for react-native-modal
Gathering detailed insights and metrics for react-native-modal
Gathering detailed insights and metrics for react-native-modal
An enhanced, animated, customizable Modal for React Native.
npm install react-native-modal
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5,488 Stars
537 Commits
620 Forks
41 Watching
26 Branches
86 Contributors
Updated on 25 Nov 2024
TypeScript (73.73%)
JavaScript (7.4%)
Objective-C (6.71%)
Ruby (4.53%)
Java (4.52%)
Starlark (3.11%)
Cumulative downloads
Total Downloads
Last day
4.8%
71,376
Compared to previous day
Last week
3.8%
354,765
Compared to previous week
Last month
2.6%
1,532,545
Compared to previous month
Last year
9.2%
16,307,757
Compared to previous year
2
2
25
react-native-modal
. See #597If 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.
This library is available on npm, install it with: npm i react-native-modal
or yarn add react-native-modal
.
Since react-native-modal
is an extension of the original React Native modal, it works in a similar fashion.
react-native-modal
:1import Modal from "react-native-modal";
<Modal>
component and nest its content inside of it:1function WrapperComponent() { 2 return ( 3 <View> 4 <Modal> 5 <View style={{ flex: 1 }}> 6 <Text>I am the modal content!</Text> 7 </View> 8 </Modal> 9 </View> 10 ); 11}
isVisible
prop to true
:1function WrapperComponent() { 2 return ( 3 <View> 4 <Modal isVisible={true}> 5 <View style={{ flex: 1 }}> 6 <Text>I am the modal content!</Text> 7 </View> 8 </Modal> 9 </View> 10 ); 11}
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.
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.
1import React, { useState } from "react"; 2import { Button, Text, View } from "react-native"; 3import Modal from "react-native-modal"; 4 5function ModalTester() { 6 const [isModalVisible, setModalVisible] = useState(false); 7 8 const toggleModal = () => { 9 setModalVisible(!isModalVisible); 10 }; 11 12 return ( 13 <View style={{ flex: 1 }}> 14 <Button title="Show modal" onPress={toggleModal} /> 15 16 <Modal isVisible={isModalVisible}> 17 <View style={{ flex: 1 }}> 18 <Text>Hello!</Text> 19 20 <Button title="Hide modal" onPress={toggleModal} /> 21 </View> 22 </Modal> 23 </View> 24 ); 25} 26 27export default ModalTester;
For a more complex example take a look at the /example
directory.
Name | Type | Default | Description |
---|---|---|---|
animationIn | string or object | "slideInUp" | Modal show animation |
animationInTiming | number | 300 | Timing for the modal show animation (in ms) |
animationOut | string or object | "slideOutDown" | Modal hide animation |
animationOutTiming | number | 300 | Timing for the modal hide animation (in ms) |
avoidKeyboard | bool | false | Move the modal up if the keyboard is open |
coverScreen | bool | true | Will use RN Modal component to cover the entire screen wherever the modal is mounted in the component hierarchy |
hasBackdrop | bool | true | Render the backdrop |
backdropColor | string | "black" | The backdrop background color |
backdropOpacity | number | 0.70 | The backdrop opacity when the modal is visible |
backdropTransitionInTiming | number | 300 | The backdrop show timing (in ms) |
backdropTransitionOutTiming | number | 300 | The backdrop hide timing (in ms) |
customBackdrop | node | null | The custom backdrop element |
children | node | REQUIRED | The modal content |
deviceHeight | number | null | Device height (useful on devices that can hide the navigation bar) |
deviceWidth | number | null | Device width (useful on devices that can hide the navigation bar) |
isVisible | bool | REQUIRED | Show the modal? |
onBackButtonPress | func | () => null | Called when the Android back button is pressed |
onBackdropPress | func | () => null | Called when the backdrop is pressed |
onModalWillHide | func | () => null | Called before the modal hide animation begins |
onModalHide | func | () => null | Called when the modal is completely hidden |
onModalWillShow | func | () => null | Called before the modal show animation begins |
onModalShow | func | () => null | Called when the modal is completely visible |
onSwipeStart | func | () => null | Called when the swipe action started |
onSwipeMove | func | (percentageShown) => null | Called on each swipe event |
onSwipeComplete | func | ({ swipingDirection }) => null | Called when the swipeThreshold has been reached |
onSwipeCancel | func | () => null | Called when the swipeThreshold has not been reached |
panResponderThreshold | number | 4 | The threshold for when the panResponder should pick up swipe events |
scrollOffset | number | 0 | When > 0, disables swipe-to-close, in order to implement scrollable content |
scrollOffsetMax | number | 0 | Used to implement overscroll feel when content is scrollable. See /example directory |
scrollTo | func | null | Used to implement scrollable modal. See /example directory for reference on how to use it |
scrollHorizontal | bool | false | Set to true if your scrollView is horizontal (for a correct scroll handling) |
swipeThreshold | number | 100 | Swiping threshold that when reached calls onSwipeComplete |
swipeDirection | string or array | null | Defines the direction where the modal can be swiped. Can be 'up', 'down', 'left, or 'right', or a combination of them like ['up','down'] |
useNativeDriver | bool | false | Defines if animations should use native driver |
useNativeDriverForBackdrop | bool | null | Defines if animations for backdrop should use native driver (to avoid flashing on android) |
hideModalContentWhileAnimating | bool | false | Enhances the performance by hiding the modal content until the animations complete |
propagateSwipe | bool or func | false | Allows swipe events to propagate to children components (eg a ScrollView inside a modal) |
style | any | null | Style applied to the modal |
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.
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:
1const deviceWidth = Dimensions.get("window").width; 2const deviceHeight = 3 Platform.OS === "ios" 4 ? Dimensions.get("window").height 5 : require("react-native-extra-dimensions-android").get( 6 "REAL_WINDOW_HEIGHT" 7 ); 8 9function WrapperComponent() { 10 const [isModalVisible, setModalVisible] = useState(true); 11 12 return ( 13 <Modal 14 isVisible={isModalVisible} 15 deviceWidth={deviceWidth} 16 deviceHeight={deviceHeight} 17 > 18 <View style={{ flex: 1 }}> 19 <Text>I am the modal content!</Text> 20 </View> 21 </Modal> 22 ); 23}
The prop onBackdropPress
allows you to handle this situation:
1<Modal 2 isVisible={isModalVisible} 3 onBackdropPress={() => setModalVisible(false)} 4> 5 <View style={{ flex: 1 }}> 6 <Text>I am the modal content!</Text> 7 </View> 8</Modal>
The prop onSwipeComplete
allows you to handle this situation (remember to set swipeDirection
too!):
1<Modal 2 isVisible={isModalVisible} 3 onSwipeComplete={() => setModalVisible(false)} 4 swipeDirection="left" 5> 6 <View style={{ flex: 1 }}> 7 <Text>I am the modal content!</Text> 8 </View> 9</Modal>
Note that when using useNativeDriver={true}
the modal won't drag correctly. This is a known issue.
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.
Are you sure you named the isVisible
prop correctly? Make sure it is spelled correctly: isVisible
, not visible
.
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.
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.
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).
This issue has been discussed here.
The TLDR is: it's a know React-Native issue with the Modal component 😞
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:
1<Modal style={{ margin: 0 }}>...</Modal>
Enable propagateSwipe to allow your child components to receive swipe events:
1<Modal propagateSwipe>...</Modal>
Please notice that this is still a WIP fix and might not fix your issue yet, see issue #236.
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.
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:
1<Modal isVisible={isModalVisible} customBackdrop={<View style={{ flex: 1 }} />}> 2 <View style={{ flex: 1 }}> 3 <Text>I am the modal content!</Text> 4 </View> 5</Modal>
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.
1<Modal 2 isVisible={isModalVisible} 3 customBackdrop={ 4 <TouchableWithoutFeedback onPress={dismissModalHandler}> 5 <View style={{ flex: 1 }} /> 6 </TouchableWithoutFeedback> 7 } 8/>
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.
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!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
binaries present in source code
Details
Reason
Found 10/24 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
92 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More