2015-07-28 14:31:26 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule Modal
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-21 15:56:46 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const PropTypes = require('ReactPropTypes');
|
|
|
|
const React = require('React');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
2016-05-09 14:07:39 +00:00
|
|
|
const UIManager = require('UIManager');
|
2016-04-21 15:56:46 +00:00
|
|
|
const View = require('View');
|
2016-04-28 22:59:11 +00:00
|
|
|
const deprecatedPropType = require('deprecatedPropType');
|
2015-07-28 14:31:26 +00:00
|
|
|
|
2016-04-21 15:56:46 +00:00
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
|
|
|
const RCTModalHostView = requireNativeComponent('RCTModalHostView', null);
|
2015-07-28 14:31:26 +00:00
|
|
|
|
2015-09-28 06:46:00 +00:00
|
|
|
/**
|
|
|
|
* A Modal component covers the native view (e.g. UIViewController, Activity)
|
|
|
|
* that contains the React Native root.
|
|
|
|
*
|
|
|
|
* Use Modal in hybrid apps that embed React Native; Modal allows the portion of
|
|
|
|
* your app written in React Native to present content above the enclosing
|
|
|
|
* native view hierarchy.
|
|
|
|
*
|
|
|
|
* In apps written with React Native from the root view down, you should use
|
|
|
|
* Navigator instead of Modal. With a top-level Navigator, you have more control
|
2015-10-21 13:58:28 +00:00
|
|
|
* over how to present the modal scene over the rest of your app by using the
|
|
|
|
* configureScene property.
|
2015-09-28 06:46:00 +00:00
|
|
|
*/
|
2015-07-28 14:31:26 +00:00
|
|
|
class Modal extends React.Component {
|
2016-04-22 02:46:36 +00:00
|
|
|
static propTypes = {
|
2016-04-28 22:59:11 +00:00
|
|
|
animated: deprecatedPropType(
|
|
|
|
PropTypes.bool,
|
|
|
|
'Use the `animationType` prop instead.'
|
|
|
|
),
|
|
|
|
animationType: PropTypes.oneOf(['none', 'slide', 'fade']),
|
2016-04-22 02:46:36 +00:00
|
|
|
transparent: PropTypes.bool,
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
onRequestClose: Platform.OS === 'android' ? PropTypes.func.isRequired : PropTypes.func,
|
|
|
|
onShow: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
visible: true,
|
|
|
|
};
|
|
|
|
|
2016-05-25 01:20:12 +00:00
|
|
|
render(): ?ReactElement<any> {
|
2015-07-28 14:31:26 +00:00
|
|
|
if (this.props.visible === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:07:39 +00:00
|
|
|
const containerStyles = {
|
2016-04-21 15:56:46 +00:00
|
|
|
backgroundColor: this.props.transparent ? 'transparent' : 'white',
|
2016-05-09 14:07:39 +00:00
|
|
|
top: Platform.OS === 'android' && Platform.Version >= 19 ? UIManager.RCTModalHostView.Constants.StatusBarHeight : 0,
|
2016-04-21 15:56:46 +00:00
|
|
|
};
|
2015-08-13 16:09:10 +00:00
|
|
|
|
2016-04-28 22:59:11 +00:00
|
|
|
let animationType = this.props.animationType;
|
|
|
|
if (!animationType) {
|
|
|
|
// manually setting default prop here to keep support for the deprecated 'animated' prop
|
|
|
|
animationType = 'none';
|
|
|
|
if (this.props.animated) {
|
|
|
|
animationType = 'slide';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:31:26 +00:00
|
|
|
return (
|
2015-08-13 16:09:10 +00:00
|
|
|
<RCTModalHostView
|
2016-04-28 22:59:11 +00:00
|
|
|
animationType={animationType}
|
2015-08-13 16:09:10 +00:00
|
|
|
transparent={this.props.transparent}
|
2016-03-17 15:51:58 +00:00
|
|
|
onRequestClose={this.props.onRequestClose}
|
2016-03-03 20:42:41 +00:00
|
|
|
onShow={this.props.onShow}
|
2016-04-20 07:58:46 +00:00
|
|
|
style={styles.modal}
|
|
|
|
onStartShouldSetResponder={this._shouldSetResponder}
|
|
|
|
>
|
2016-05-09 14:07:39 +00:00
|
|
|
<View style={[styles.container, containerStyles]}>
|
2015-07-28 14:31:26 +00:00
|
|
|
{this.props.children}
|
|
|
|
</View>
|
|
|
|
</RCTModalHostView>
|
|
|
|
);
|
|
|
|
}
|
2016-04-20 07:58:46 +00:00
|
|
|
|
|
|
|
// We don't want any responder events bubbling out of the modal.
|
|
|
|
_shouldSetResponder(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 15:56:46 +00:00
|
|
|
const styles = StyleSheet.create({
|
2015-07-28 14:31:26 +00:00
|
|
|
modal: {
|
|
|
|
position: 'absolute',
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
position: 'absolute',
|
2016-03-17 15:51:58 +00:00
|
|
|
left: 0,
|
2015-07-28 14:31:26 +00:00
|
|
|
top: 0,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-09 14:07:39 +00:00
|
|
|
module.exports = Modal;
|