Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close react native modal by clicking on overlay?

Is it possible to close react native modal by clicking on overlay when transparent option is true? Documentation doesn't provide anything about it. Is it possible?

like image 399
acidernt Avatar asked Nov 08 '16 09:11

acidernt


People also ask

How do you close a modal react native?

Add a close button to a React Native and close the modal by clicking beside it.

How do you close modal by clicking outside in react JS?

Simple solution. You need one touchableOpacity for clicking outside and another touchableOpacity for actual modal that will do nothing onPress. This solution was best for my purposes!

How do you stop modal close on outside Click react?

To disable outside click on a dialog modal with React Material-UI, we can set the onClose prop of the Modal to a function that has the reason as the 2nd parameter. Then we can check that the reason isn't 'backdropClick' before we close it.

How do you open modal popup on button click in react native?

Let's see an example of displaying a pop-up modal on clicking the button. Once we clicked the button, state variable isVisible sets to true and opens the Modal component. To implement the Modal component import Modal from the react-native library. onRequestClose = {() =>{ console.


2 Answers

If I understood correctly, you want to close the modal when the user clicks outside of it, right ?

If yes, I searched for this some time ago and the only solution that I remember was this one (which is the one that I've been using so far):

render() {    if (!this.state.modalVisible)     return null   return (      <View>         <Modal            animationType="fade"           transparent={true}           visible={this.state.modalVisible}           onRequestClose={() => {this.setModalVisible(false)}}         >           <TouchableOpacity              style={styles.container}              activeOpacity={1}              onPressOut={() => {this.setModalVisible(false)}}           >             <ScrollView                directionalLockEnabled={true}                contentContainerStyle={styles.scrollModal}             >               <TouchableWithoutFeedback>                 <View style={styles.modalContainer}>                   // Here you put the content of your modal.                 </View>               </TouchableWithoutFeedback>             </ScrollView>           </TouchableOpacity>            </Modal>       </View>   ) }   // Then on setModalVisible(), you do everything that you need to do when closing or opening the modal. setModalVisible(visible) {     this.setState({         modalVisible: visible,     }) } 

Explanation

This is basically using a TouchableOpacity in the whole screen to get when the user clicks to close the modal. The TouchableWithoutFeedback is to avoid the TouchableOpacity to work inside of the Modal.

If you have a better solution, please share here.

like image 66
Gui Herzog Avatar answered Sep 22 '22 13:09

Gui Herzog


Another solution:

// Modal.js import React from 'react'; import {   TouchableWithoutFeedback,   StyleSheet,   Modal,   View, } from 'react-native'; import t from 'prop-types';   class MyModal extends React.Component {   static propTypes = {     children: t.node.isRequired,     visible: t.bool.isRequired,     dismiss: t.func.isRequired,     transparent: t.bool,     animationType: t.string,   };    static defaultProps = {     animationType: 'none',     transparent: true,   };    render() {     const { props } = this;     return (       <View>         <Modal           visible={props.visible}           transparent={props.transparent}           onRequestClose={props.dismiss}           animationType={props.animationType}         >           <TouchableWithoutFeedback onPress={props.dismiss}>             <View style={styles.modalOverlay} />           </TouchableWithoutFeedback>            <View style={styles.modalContent}>             {props.children}           </View>         </Modal>       </View>     );   } }   const styles = StyleSheet.create({   modalContent: {     flex: 1,     justifyContent: 'center',     margin: '5%',   },   modalOverlay: {     position: 'absolute',     top: 0,     bottom: 0,     left: 0,     right: 0,     backgroundColor: 'rgba(0,0,0,0.5)'   }, });   export default MyModal; 

Usage example:

// SomeScreen.js import React from 'react'; import { View, Text, Button } from 'react-native';  import Modal from './Modal';   class SomeScreen extends React.Component {   state = {     isModalVisible: false,   };    showModal = () => this.setState({ isModalVisible: true });   hideModal = () => this.setState({ isModalVisible: false });    render() {     return (       <View>         <Button onPress={this.showModal} />         <Modal           visible={this.state.isModalVisible}           dismiss={this.hideModal}         >           <Text>Hello, I am modal</Text>         </Modal>       </View>     );   } } 
like image 23
sorrat Avatar answered Sep 20 '22 13:09

sorrat