2015-07-28 17:31:26 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-07-28 17:31:26 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-07-28 17:31:26 +03:00
|
|
|
*
|
2018-05-11 05:06:46 +03:00
|
|
|
* @format
|
2015-07-28 17:31:26 +03:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
|
2015-07-28 17:31:26 +03:00
|
|
|
'use strict';
|
|
|
|
|
2019-05-08 18:44:25 +03:00
|
|
|
const AppContainer = require('../ReactNative/AppContainer');
|
|
|
|
const I18nManager = require('../ReactNative/I18nManager');
|
|
|
|
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
|
2019-05-29 23:01:45 +03:00
|
|
|
import NativeModalManager from './NativeModalManager';
|
2019-05-08 18:44:25 +03:00
|
|
|
const Platform = require('../Utilities/Platform');
|
|
|
|
const React = require('react');
|
2017-04-13 02:09:48 +03:00
|
|
|
const PropTypes = require('prop-types');
|
2019-07-10 22:39:15 +03:00
|
|
|
const ScrollView = require('../Components/ScrollView/ScrollView');
|
2019-05-08 18:44:25 +03:00
|
|
|
const StyleSheet = require('../StyleSheet/StyleSheet');
|
|
|
|
const View = require('../Components/View/View');
|
2015-07-28 17:31:26 +03:00
|
|
|
|
2019-06-23 20:00:28 +03:00
|
|
|
import RCTModalHostView from './RCTModalHostViewNativeComponent';
|
2018-05-11 05:06:46 +03:00
|
|
|
const ModalEventEmitter =
|
2019-05-29 23:01:45 +03:00
|
|
|
Platform.OS === 'ios' && NativeModalManager != null
|
|
|
|
? new NativeEventEmitter(NativeModalManager)
|
2018-05-11 05:06:46 +03:00
|
|
|
: null;
|
2017-09-22 00:46:42 +03:00
|
|
|
|
2019-05-08 18:44:25 +03:00
|
|
|
import type EmitterSubscription from '../vendor/emitter/EmitterSubscription';
|
|
|
|
import type {ViewProps} from '../Components/View/ViewPropTypes';
|
|
|
|
import type {SyntheticEvent} from '../Types/CoreEventTypes';
|
2019-07-10 15:37:03 +03:00
|
|
|
import type {DirectEventHandler} from '../Types/CodegenTypes';
|
2015-09-28 09:46:00 +03:00
|
|
|
/**
|
2016-06-24 04:32:21 +03:00
|
|
|
* The Modal component is a simple way to present content above an enclosing view.
|
2018-02-22 18:04:35 +03:00
|
|
|
*
|
2018-01-30 03:10:49 +03:00
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html
|
2015-09-28 09:46:00 +03:00
|
|
|
*/
|
2017-06-21 05:02:27 +03:00
|
|
|
|
2017-09-22 00:46:42 +03: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.
|
2018-05-11 01:44:52 +03:00
|
|
|
let uniqueModalIdentifier = 0;
|
2017-09-22 00:46:42 +03:00
|
|
|
|
2019-07-10 15:37:03 +03:00
|
|
|
type OrientationChangeEvent = $ReadOnly<{|
|
|
|
|
orientation: 'portrait' | 'landscape',
|
|
|
|
|}>;
|
2018-09-25 01:14:04 +03:00
|
|
|
|
2019-07-10 12:10:06 +03:00
|
|
|
export type Props = $ReadOnly<{|
|
2018-09-25 01:14:04 +03:00
|
|
|
...ViewProps,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `animationType` prop controls how the modal animates.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#animationtype
|
|
|
|
*/
|
|
|
|
animationType?: ?('none' | 'slide' | 'fade'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `presentationStyle` prop controls how the modal appears.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#presentationstyle
|
|
|
|
*/
|
|
|
|
presentationStyle?: ?(
|
|
|
|
| 'fullScreen'
|
|
|
|
| 'pageSheet'
|
|
|
|
| 'formSheet'
|
|
|
|
| 'overFullScreen'
|
|
|
|
),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `transparent` prop determines whether your modal will fill the
|
|
|
|
* entire view.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#transparent
|
|
|
|
*/
|
|
|
|
transparent?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `hardwareAccelerated` prop controls whether to force hardware
|
|
|
|
* acceleration for the underlying window.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#hardwareaccelerated
|
|
|
|
*/
|
|
|
|
hardwareAccelerated?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `visible` prop determines whether your modal is visible.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#visible
|
|
|
|
*/
|
|
|
|
visible?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `onRequestClose` callback is called when the user taps the hardware
|
|
|
|
* back button on Android or the menu button on Apple TV.
|
|
|
|
*
|
|
|
|
* This is required on Apple TV and Android.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#onrequestclose
|
|
|
|
*/
|
2019-07-10 15:37:03 +03:00
|
|
|
onRequestClose?: ?DirectEventHandler<null>,
|
2018-09-25 01:14:04 +03: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
|
|
|
|
*/
|
2019-07-10 15:37:03 +03:00
|
|
|
onShow?: ?DirectEventHandler<null>,
|
2018-09-25 01:14:04 +03: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
|
|
|
|
*/
|
|
|
|
onDismiss?: ?() => mixed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated. Use the `animationType` prop instead.
|
|
|
|
*/
|
|
|
|
animated?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `supportedOrientations` prop allows the modal to be rotated to any of the specified orientations.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#supportedorientations
|
|
|
|
*/
|
|
|
|
supportedOrientations?: ?$ReadOnlyArray<
|
|
|
|
| 'portrait'
|
|
|
|
| 'portrait-upside-down'
|
|
|
|
| 'landscape'
|
|
|
|
| 'landscape-left'
|
|
|
|
| 'landscape-right',
|
|
|
|
>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `onOrientationChange` callback is called when the orientation changes while the modal is being displayed.
|
|
|
|
*
|
|
|
|
* See https://facebook.github.io/react-native/docs/modal.html#onorientationchange
|
|
|
|
*/
|
2019-07-10 15:37:03 +03:00
|
|
|
onOrientationChange?: ?DirectEventHandler<OrientationChangeEvent>,
|
2018-09-25 01:14:04 +03:00
|
|
|
|}>;
|
|
|
|
|
|
|
|
class Modal extends React.Component<Props> {
|
2016-04-22 05:46:36 +03:00
|
|
|
static defaultProps = {
|
|
|
|
visible: true,
|
2016-12-14 21:25:57 +03:00
|
|
|
hardwareAccelerated: false,
|
2016-04-22 05:46:36 +03:00
|
|
|
};
|
|
|
|
|
2016-11-18 12:31:14 +03:00
|
|
|
static contextTypes = {
|
2017-04-13 02:09:48 +03:00
|
|
|
rootTag: PropTypes.number,
|
2016-11-18 12:31:14 +03:00
|
|
|
};
|
|
|
|
|
2017-09-22 00:46:42 +03:00
|
|
|
_identifier: number;
|
|
|
|
_eventSubscription: ?EmitterSubscription;
|
|
|
|
|
2018-09-25 01:14:04 +03:00
|
|
|
constructor(props: Props) {
|
2017-06-21 05:02:27 +03:00
|
|
|
super(props);
|
|
|
|
Modal._confirmProps(props);
|
2017-09-22 00:46:42 +03:00
|
|
|
this._identifier = uniqueModalIdentifier++;
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:09:44 +03:00
|
|
|
static childContextTypes = {
|
|
|
|
virtualizedList: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
// Reset the context so VirtualizedList doesn't get confused by nesting
|
2019-07-23 13:19:41 +03:00
|
|
|
// in the React tree that doesn't reflect the native component hierarchy.
|
2018-05-09 23:09:44 +03:00
|
|
|
return {
|
|
|
|
virtualizedList: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:46:42 +03:00
|
|
|
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 05:02:27 +03:00
|
|
|
}
|
|
|
|
|
2018-09-25 01:14:04 +03:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2017-06-21 05:02:27 +03:00
|
|
|
Modal._confirmProps(nextProps);
|
|
|
|
}
|
|
|
|
|
2018-09-25 01:14:04 +03:00
|
|
|
static _confirmProps(props: Props) {
|
2018-05-11 05:06:46 +03:00
|
|
|
if (
|
|
|
|
props.presentationStyle &&
|
|
|
|
props.presentationStyle !== 'overFullScreen' &&
|
|
|
|
props.transparent
|
|
|
|
) {
|
|
|
|
console.warn(
|
|
|
|
`Modal with '${
|
|
|
|
props.presentationStyle
|
|
|
|
}' presentation style and 'transparent' value is not supported.`,
|
|
|
|
);
|
2017-06-21 05:02:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 04:36:54 +03:00
|
|
|
render(): React.Node {
|
2018-11-04 04:36:46 +03:00
|
|
|
if (this.props.visible !== true) {
|
2015-07-28 17:31:26 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:07:39 +03:00
|
|
|
const containerStyles = {
|
2016-04-21 18:56:46 +03:00
|
|
|
backgroundColor: this.props.transparent ? 'transparent' : 'white',
|
|
|
|
};
|
2015-08-13 19:09:10 +03:00
|
|
|
|
2016-04-29 01:59:11 +03: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 05:02:27 +03:00
|
|
|
let presentationStyle = this.props.presentationStyle;
|
|
|
|
if (!presentationStyle) {
|
|
|
|
presentationStyle = 'fullScreen';
|
|
|
|
if (this.props.transparent) {
|
|
|
|
presentationStyle = 'overFullScreen';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:06:46 +03:00
|
|
|
const innerChildren = __DEV__ ? (
|
|
|
|
<AppContainer rootTag={this.context.rootTag}>
|
|
|
|
{this.props.children}
|
|
|
|
</AppContainer>
|
|
|
|
) : (
|
|
|
|
this.props.children
|
|
|
|
);
|
2016-09-29 14:46:17 +03:00
|
|
|
|
2015-07-28 17:31:26 +03:00
|
|
|
return (
|
2015-08-13 19:09:10 +03:00
|
|
|
<RCTModalHostView
|
2016-04-29 01:59:11 +03:00
|
|
|
animationType={animationType}
|
2017-06-21 05:02:27 +03:00
|
|
|
presentationStyle={presentationStyle}
|
2015-08-13 19:09:10 +03:00
|
|
|
transparent={this.props.transparent}
|
2016-12-14 21:25:57 +03:00
|
|
|
hardwareAccelerated={this.props.hardwareAccelerated}
|
2016-03-17 18:51:58 +03:00
|
|
|
onRequestClose={this.props.onRequestClose}
|
2016-03-03 23:42:41 +03:00
|
|
|
onShow={this.props.onShow}
|
2017-09-22 00:46:42 +03:00
|
|
|
identifier={this._identifier}
|
2016-04-20 10:58:46 +03:00
|
|
|
style={styles.modal}
|
|
|
|
onStartShouldSetResponder={this._shouldSetResponder}
|
2016-09-07 16:06:12 +03:00
|
|
|
supportedOrientations={this.props.supportedOrientations}
|
2018-05-11 05:06:46 +03:00
|
|
|
onOrientationChange={this.props.onOrientationChange}>
|
2019-07-10 22:39:15 +03:00
|
|
|
<ScrollView.Context.Provider value={null}>
|
|
|
|
<View style={[styles.container, containerStyles]}>
|
|
|
|
{innerChildren}
|
|
|
|
</View>
|
|
|
|
</ScrollView.Context.Provider>
|
2015-07-28 17:31:26 +03:00
|
|
|
</RCTModalHostView>
|
|
|
|
);
|
|
|
|
}
|
2016-04-20 10:58:46 +03:00
|
|
|
|
|
|
|
// We don't want any responder events bubbling out of the modal.
|
|
|
|
_shouldSetResponder(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-28 17:31:26 +03:00
|
|
|
}
|
|
|
|
|
2019-06-01 05:53:21 +03:00
|
|
|
const side = I18nManager.getConstants().isRTL ? 'right' : 'left';
|
2016-04-21 18:56:46 +03:00
|
|
|
const styles = StyleSheet.create({
|
2015-07-28 17:31:26 +03:00
|
|
|
modal: {
|
|
|
|
position: 'absolute',
|
|
|
|
},
|
|
|
|
container: {
|
2018-05-11 05:06:46 +03:00
|
|
|
[side]: 0,
|
2015-07-28 17:31:26 +03:00
|
|
|
top: 0,
|
2019-05-01 01:01:37 +03:00
|
|
|
flex: 1,
|
2018-05-11 05:06:46 +03:00
|
|
|
},
|
2015-07-28 17:31:26 +03:00
|
|
|
});
|
|
|
|
|
2016-05-09 17:07:39 +03:00
|
|
|
module.exports = Modal;
|