This is the official react-native modal documentation and this is a live example for iOS and Android.
How can I add a clickable backdrop overlay which is over my view and under the modal?
This is the official react-native modal documentation and this is a live example for iOS and Android.
How can I add a clickable backdrop overlay which is over my view and under the modal?
Share Improve this question asked May 27, 2020 at 8:53 Dimitri KopriwaDimitri Kopriwa 14.5k33 gold badges117 silver badges232 bronze badges4 Answers
Reset to default 3You can wrap the Modal content in a Touchable Opacity and style it with a background. I edited the sample given in the documentation.
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<TouchableOpacity
style={{ backgroundColor: 'black', flex: 1,justifyContent:'center' }}
onPress={() => setModalVisible(!modalVisible)}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<TouchableHighlight
style={{ ...styles.openButton, backgroundColor: '#2196F3' }}
onPress={() => {
setModalVisible(!modalVisible);
}}>
<Text style={styles.textStyle}>Hide Modal</Text>
</TouchableHighlight>
</View>
</TouchableOpacity>
</Modal>
React Native Elements has a fantastic ponent called Overlay
that would solve your problem. It saved me earlier this week. The only issue is that it is part of a larger library.
https://react-native-elements.github.io/react-native-elements/docs/overlay.html
I think if the separation between the backdrop and your modal is not crucial to your project, you can handle it like this:
<Modal
animationType="slide"
transparent={true}
style={{ width: '100%', alignSelf: 'center', height: '100%', justifyContent: 'flex-start' }}
visible={this.state.modalVisible}
onRequestClose={() => {
// Do smth
}}>
<TouchableWithoutFeedback style={{ flex: 1 }} onPress={() => {
// Do the backdrop action
}}>
<View style={{ backgroundColor: '#fff', flex: 1, width: '80%', height: '50%' }} >
<Text> Your content </Text>
</View>
</TouchableWithoutFeedback>
</Modal>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
// Touchable Opacity
<TouchableOpacity
style={{ position: 'absolute' , flex: 1 }}
onPress={() => setModalVisible(!modalVisible)} />
<View style={styles.modalView}>
// Other..
<View>
design
</View>
</Modal>