Skip to content

Instantly share code, notes, and snippets.

@wvengen
Last active January 13, 2016 10:19
Show Gist options
  • Save wvengen/4ce9537bbc271981afc5 to your computer and use it in GitHub Desktop.
Save wvengen/4ce9537bbc271981afc5 to your computer and use it in GitHub Desktop.
Basic Modal for react-native on Android
// Basic Android-implementation of Modal
//
// based on https://github.com/niftylettuce/react-native-loading-spinner-overlay
import React, {StyleSheet, View} from 'react-native';
const Portal = require('react-native/Libraries/Portal/Portal.js');
export default React.createClass({
propTypes: {
animated: React.PropTypes.bool, // @todo support this
onDismiss: React.PropTypes.func,
transparent: React.PropTypes.bool, // @todo support this
visible: React.PropTypes.bool
},
componentWillMount: function() {
this.tag = Portal.allocateTag();
},
componentWillUnmount: function() {
this.tag = null;
},
componentDidUpdate: function(prevProps) {
if (!prevProps.visible && this.props.visible) {
return Portal.showModal(this.tag, this.render());
} else {
Portal.closeModal(this.tag);
if (this.props.onDismiss) {
this.props.onDismiss();
}
}
},
render: function() {
return (
this.props.visible ?
<View style={styles.container}>
{this.props.children}
</View>
:
null
);
}
});
const styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0
}
});
import {Modal} from 'react-native';
export default Modal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment