Following is my created react native Modal and still couldn't find how to dim the background and transparent around pop-up modal.I am not using any external libraries and trying to find solution without libraries.Is it possible to do with on this way?
render() { let modal = this.state.modalType=='skill'? <SkillModal /> : <TrialModal />; return ( <View style={styles.container}> <Modal animationType='slide' onRequestClose={() => console.log('no warning')} presentationStyle="FormSheet" transparent visible={this.state.showModal} > <View> <View style={styles.modalView} > <TouchableOpacity onPress={this.closeModalFunc} > <Text style={styles.closeText}>X</Text> </TouchableOpacity> <View> {modal} </View> </View> </View> </Modal> </View> ); }
import {StyleSheet} from 'react-native'; import colors from '../../../theme/colors'; import metrics from '../../../theme/metrics'; import {container} from '../../../theme/base'; const styles = StyleSheet.create({ container: { backgroundColor: colors.background.gray, }, modalView: { backgroundColor: colors.background.white, margin: 40, }, closeText: { backgroundColor: colors.background.purpleishBlue, color: colors.background.white, borderRadius: metrics.avatar.small, width: 32, padding: 6, alignSelf: 'flex-end', textAlign: 'center', borderWidth: 1, borderColor: colors.background.white, }, }); export default styles;
Transparent is used to make the modal transparent. For example if we pass transparent={true} prop to the modal then it will make the Modal transparent. Because by default modal has a White background on it and not shows the back of application.
To set Alpha of an image or view component in React Native based application, style's property opacity is used. Developers can make the shape or image background transparent according to his requirement in a fixed manner; in a fixed percentage, the view is made transparent by setting alpha.
In React Native we can use backgroundColor property of stylesheet to change the screen color to white, black, yellow etc. React Native beginners makes mistake by using background property instead of backgroundColor . This works in React and HTML but not in React native. The hex code for white color is #FFFFFF or #FFF.
I solve this one by create my own component MyPopup like below
class MyPopup extends React.PureComponent { render() { return ( <Modal animationType="fade" transparent={true} visible={this.props.visible} > <View style={{flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: 'rgba(0,0,0,0.5)'}}> {this.props.children} </View> </Modal> ) }}
Then I use it like
<MyPopup visible={this.state.modalVisible}> <View style={{ width: '90%', height: 50, borderColor: '#ccc', borderWidth: 1, borderStyle: 'solid', backgroundColor: 'white', elevation: 20, padding: 10, borderRadius: 4, }}> <Text>Hello, This is my model with dim background color</Text> </View>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With