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-09-29 11:46:17 +00:00
|
|
|
const AppContainer = require('AppContainer');
|
2016-07-19 17:37:08 +00:00
|
|
|
const I18nManager = require('I18nManager');
|
2017-09-21 21:46:42 +00:00
|
|
|
const NativeEventEmitter = require('NativeEventEmitter');
|
|
|
|
const NativeModules = require('NativeModules');
|
2016-04-21 15:56:46 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
const PropTypes = require('prop-types');
|
2016-04-21 15:56:46 +00:00
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const View = require('View');
|
2015-07-28 14:31:26 +00:00
|
|
|
|
BREAKING: Fix modal resizing on keyboard show
Summary:
This changes modal behavior to resize when the keyboard appears/disappears.
Previously, the modal would not react in any way, or it would pan above to bring the
TextInput into view. Resizing is the correct behavior for android.
This is not trivial, as in, setting the flag, because of the combination of
react native laying out all views and the system reacting to the keyboard
appearance in a weird way. Namely:
- if `windowTranslucentStatus` is not set, the system will just call
`onSizeChanged` on the dialog's content view, and everything works nicely
- with `windowTranslucentStatus` set, the system will consider the dialog as a
full screen view that doesn't resize. In order for it to resize, the base
view of the layout needs to have
`setFitsSystemWindows(true)` called on it. This is needed, so that the system
can call layout on that base view with the new value of `paddingBottom` that
coincides with the height of the keyboard. Neat.
We fix this by wrapping our existing content view (mHostView) in a simple
FrameLayout that has `setFitsSystemWindows` set. That way, `mHostView` will have
`onSizeChanged` called on itself with the correct new size of the dialog.
This has the fortunate consequence of our layout now also getting `paddingTop` as the size of the
status bar, which means that we can remove the JS `top` hack in Modal, which
was necessary for no view getting drawn under the status bar.
This behavior is set as default, since that is the default correct Android behavior.
Reviewed By: astreet
Differential Revision: D3913784
fbshipit-source-id: 4378ada21f466dc7ac6e357abeca10b88009ca3f
2016-09-28 09:53:36 +00:00
|
|
|
const deprecatedPropType = require('deprecatedPropType');
|
2016-04-21 15:56:46 +00:00
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
|
|
|
const RCTModalHostView = requireNativeComponent('RCTModalHostView', null);
|
2017-09-21 21:46:42 +00:00
|
|
|
const ModalEventEmitter = Platform.OS === 'ios' && NativeModules.ModalManager ?
|
|
|
|
new NativeEventEmitter(NativeModules.ModalManager) : null;
|
|
|
|
|
|
|
|
import type EmitterSubscription from 'EmitterSubscription';
|
2015-07-28 14:31:26 +00:00
|
|
|
|
2015-09-28 06:46:00 +00:00
|
|
|
/**
|
2016-06-24 01:32:21 +00:00
|
|
|
* The Modal component is a simple way to present content above an enclosing view.
|
2018-01-30 00:10:49 +00:00
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html
|
2015-09-28 06:46:00 +00:00
|
|
|
*/
|
2017-06-21 02:02:27 +00:00
|
|
|
|
2017-09-21 21:46:42 +00:00
|
|
|
// In order to route onDismiss callbacks, we need to uniquely identifier each
|
|
|
|
// <Modal> on screen. There can be different ones, either nested or as siblings.
|
|
|
|
// We cannot pass the onDismiss callback to native as the view will be
|
|
|
|
// destroyed before the callback is fired.
|
|
|
|
var uniqueModalIdentifier = 0;
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class Modal extends React.Component<Object> {
|
2016-04-22 02:46:36 +00:00
|
|
|
static propTypes = {
|
2016-06-24 01:32:21 +00:00
|
|
|
/**
|
|
|
|
* The `animationType` prop controls how the modal animates.
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#animationtype
|
2016-06-24 01:32:21 +00:00
|
|
|
*/
|
2016-04-28 22:59:11 +00:00
|
|
|
animationType: PropTypes.oneOf(['none', 'slide', 'fade']),
|
2017-06-21 02:02:27 +00:00
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* The `presentationStyle` prop controls how the modal appears.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#presentationstyle
|
2017-06-21 02:02:27 +00:00
|
|
|
*/
|
|
|
|
presentationStyle: PropTypes.oneOf(['fullScreen', 'pageSheet', 'formSheet', 'overFullScreen']),
|
2016-06-24 01:32:21 +00:00
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* The `transparent` prop determines whether your modal will fill the
|
|
|
|
* entire view.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#transparent
|
2016-06-24 01:32:21 +00:00
|
|
|
*/
|
2016-04-22 02:46:36 +00:00
|
|
|
transparent: PropTypes.bool,
|
2016-12-14 18:25:57 +00:00
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* The `hardwareAccelerated` prop controls whether to force hardware
|
|
|
|
* acceleration for the underlying window.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#hardwareaccelerated
|
2016-12-14 18:25:57 +00:00
|
|
|
*/
|
|
|
|
hardwareAccelerated: PropTypes.bool,
|
2016-06-24 01:32:21 +00:00
|
|
|
/**
|
|
|
|
* The `visible` prop determines whether your modal is visible.
|
2018-01-30 00:10:49 +00:00
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#visible
|
2016-06-24 01:32:21 +00:00
|
|
|
*/
|
2016-04-22 02:46:36 +00:00
|
|
|
visible: PropTypes.bool,
|
2016-06-24 01:32:21 +00:00
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* The `onRequestClose` callback is called when the user taps the hardware
|
|
|
|
* back button on Android or the menu button on Apple TV.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#onrequestclose
|
2016-06-24 01:32:21 +00:00
|
|
|
*/
|
2017-08-17 22:05:26 +00:00
|
|
|
onRequestClose: (Platform.isTVOS || Platform.OS === 'android') ? PropTypes.func.isRequired : PropTypes.func,
|
2016-06-24 01:32:21 +00:00
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* The `onShow` prop allows passing a function that will be called once the
|
|
|
|
* modal has been shown.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#onshow
|
2016-06-24 01:32:21 +00:00
|
|
|
*/
|
2016-04-22 02:46:36 +00:00
|
|
|
onShow: PropTypes.func,
|
2017-09-21 21:46:42 +00:00
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* The `onDismiss` prop allows passing a function that will be called once
|
|
|
|
* the modal has been dismissed.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#ondismiss
|
2017-09-21 21:46:42 +00:00
|
|
|
*/
|
|
|
|
onDismiss: PropTypes.func,
|
2016-06-24 01:32:21 +00:00
|
|
|
animated: deprecatedPropType(
|
|
|
|
PropTypes.bool,
|
|
|
|
'Use the `animationType` prop instead.'
|
|
|
|
),
|
2016-09-07 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* The `supportedOrientations` prop allows the modal to be rotated to any of the specified orientations.
|
2018-01-30 00:10:49 +00:00
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#supportedorientations
|
2016-09-07 13:06:12 +00:00
|
|
|
*/
|
|
|
|
supportedOrientations: PropTypes.arrayOf(PropTypes.oneOf(['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right'])),
|
|
|
|
/**
|
|
|
|
* The `onOrientationChange` callback is called when the orientation changes while the modal is being displayed.
|
2018-01-30 00:10:49 +00:00
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#onorientationchange
|
2016-09-07 13:06:12 +00:00
|
|
|
*/
|
|
|
|
onOrientationChange: PropTypes.func,
|
2016-04-22 02:46:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
visible: true,
|
2016-12-14 18:25:57 +00:00
|
|
|
hardwareAccelerated: false,
|
2016-04-22 02:46:36 +00:00
|
|
|
};
|
|
|
|
|
2016-11-18 09:31:14 +00:00
|
|
|
static contextTypes = {
|
2017-04-12 23:09:48 +00:00
|
|
|
rootTag: PropTypes.number,
|
2016-11-18 09:31:14 +00:00
|
|
|
};
|
|
|
|
|
2017-09-21 21:46:42 +00:00
|
|
|
_identifier: number;
|
|
|
|
_eventSubscription: ?EmitterSubscription;
|
|
|
|
|
2017-06-21 02:02:27 +00:00
|
|
|
constructor(props: Object) {
|
|
|
|
super(props);
|
|
|
|
Modal._confirmProps(props);
|
2017-09-21 21:46:42 +00:00
|
|
|
this._identifier = uniqueModalIdentifier++;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (ModalEventEmitter) {
|
|
|
|
this._eventSubscription = ModalEventEmitter.addListener(
|
|
|
|
'modalDismissed',
|
|
|
|
event => {
|
|
|
|
if (event.modalID === this._identifier && this.props.onDismiss) {
|
|
|
|
this.props.onDismiss();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this._eventSubscription) {
|
|
|
|
this._eventSubscription.remove();
|
|
|
|
}
|
2017-06-21 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps: Object) {
|
|
|
|
Modal._confirmProps(nextProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static _confirmProps(props: Object) {
|
|
|
|
if (props.presentationStyle && props.presentationStyle !== 'overFullScreen' && props.transparent) {
|
|
|
|
console.warn(`Modal with '${props.presentationStyle}' presentation style and 'transparent' value is not supported.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
render(): React.Node {
|
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',
|
|
|
|
};
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 02:02:27 +00:00
|
|
|
let presentationStyle = this.props.presentationStyle;
|
|
|
|
if (!presentationStyle) {
|
|
|
|
presentationStyle = 'fullScreen';
|
|
|
|
if (this.props.transparent) {
|
|
|
|
presentationStyle = 'overFullScreen';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:46:17 +00:00
|
|
|
const innerChildren = __DEV__ ?
|
2016-11-18 09:31:14 +00:00
|
|
|
( <AppContainer rootTag={this.context.rootTag}>
|
2016-09-29 11:46:17 +00:00
|
|
|
{this.props.children}
|
|
|
|
</AppContainer>) :
|
|
|
|
this.props.children;
|
|
|
|
|
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}
|
2017-06-21 02:02:27 +00:00
|
|
|
presentationStyle={presentationStyle}
|
2015-08-13 16:09:10 +00:00
|
|
|
transparent={this.props.transparent}
|
2016-12-14 18:25:57 +00:00
|
|
|
hardwareAccelerated={this.props.hardwareAccelerated}
|
2016-03-17 15:51:58 +00:00
|
|
|
onRequestClose={this.props.onRequestClose}
|
2016-03-03 20:42:41 +00:00
|
|
|
onShow={this.props.onShow}
|
2017-09-21 21:46:42 +00:00
|
|
|
identifier={this._identifier}
|
2016-04-20 07:58:46 +00:00
|
|
|
style={styles.modal}
|
|
|
|
onStartShouldSetResponder={this._shouldSetResponder}
|
2016-09-07 13:06:12 +00:00
|
|
|
supportedOrientations={this.props.supportedOrientations}
|
|
|
|
onOrientationChange={this.props.onOrientationChange}
|
2016-04-20 07:58:46 +00:00
|
|
|
>
|
2016-05-09 14:07:39 +00:00
|
|
|
<View style={[styles.container, containerStyles]}>
|
2016-09-29 11:46:17 +00:00
|
|
|
{innerChildren}
|
2015-07-28 14:31:26 +00:00
|
|
|
</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-07-19 17:37:08 +00:00
|
|
|
const side = I18nManager.isRTL ? 'right' : 'left';
|
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-07-19 17:37:08 +00:00
|
|
|
[side] : 0,
|
2015-07-28 14:31:26 +00:00
|
|
|
top: 0,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-09 14:07:39 +00:00
|
|
|
module.exports = Modal;
|