mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
b58e176af0
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/28058 I'm taking the first step towards supporting iOS 13 UIScene APIs and modernizing React Native not to assume an app only has a single window. See discussion here: https://github.com/facebook/react-native/issues/25181#issuecomment-505612941 The approach I'm taking is to take advantage of `RootTagContext` and passing it to NativeModules so that they can identify correctly which window they refer to. Here I'm just laying groundwork. - [x] `Alert` and `ActionSheetIOS` take an optional `rootTag` argument that will cause them to appear on the correct window - [x] `StatusBar` methods also have `rootTag` argument added, but it's not fully hooked up on the native side — this turns out to require some more work, see: https://github.com/facebook/react-native/issues/25181#issuecomment-506690818 - [x] `setNetworkActivityIndicatorVisible` is deprecated in iOS 13 - [x] `RCTPerfMonitor`, `RCTProfile` no longer assume `UIApplicationDelegate` has a `window` property (no longer the best practice) — they now just render on the key window Next steps: Add VC-based status bar management (if I get the OK on https://github.com/facebook/react-native/issues/25181#issuecomment-506690818 ), add multiple window demo to RNTester, deprecate Dimensions in favor of a layout context, consider adding hook-based APIs for native modules such as Alert that automatically know which rootTag to pass ## Changelog [Internal] [Changed] - Modernize Modal to use RootTagContext [iOS] [Changed] - `Alert`, `ActionSheetIOS`, `StatusBar` methods now take an optional `surface` argument (for future iPadOS 13 support) [iOS] [Changed] - RCTPresentedViewController now takes a nullable `window` arg [Internal] [Changed] - Do not assume `UIApplicationDelegate` has a `window` property Pull Request resolved: https://github.com/facebook/react-native/pull/25425 Test Plan: - Open RNTester and: - go to Modal and check if it still works - Alert → see if works - ACtionSheetIOS → see if it works - StatusBar → see if it works - Share → see if it works Reviewed By: PeteTheHeat Differential Revision: D16957751 Pulled By: hramos fbshipit-source-id: ae2a4478e2e7f8d2be3022c9c4861561ec244a26
271 lines
7.5 KiB
JavaScript
271 lines
7.5 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const AppContainer = require('../ReactNative/AppContainer');
|
|
const RootTagContext = require('../ReactNative/RootTagContext');
|
|
const I18nManager = require('../ReactNative/I18nManager');
|
|
const PropTypes = require('prop-types');
|
|
const React = require('react');
|
|
const ScrollView = require('../Components/ScrollView/ScrollView');
|
|
const StyleSheet = require('../StyleSheet/StyleSheet');
|
|
const View = require('../Components/View/View');
|
|
|
|
import type {ViewProps} from '../Components/View/ViewPropTypes';
|
|
import type {DirectEventHandler} from '../Types/CodegenTypes';
|
|
import type EmitterSubscription from '../vendor/emitter/EmitterSubscription';
|
|
import RCTModalHostView from './RCTModalHostViewNativeComponent';
|
|
/**
|
|
* The Modal component is a simple way to present content above an enclosing view.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html
|
|
*/
|
|
|
|
// 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.
|
|
let uniqueModalIdentifier = 0;
|
|
|
|
type OrientationChangeEvent = $ReadOnly<{|
|
|
orientation: 'portrait' | 'landscape',
|
|
|}>;
|
|
|
|
export type Props = $ReadOnly<{|
|
|
...ViewProps,
|
|
|
|
/**
|
|
* The `animationType` prop controls how the modal animates.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#animationtype
|
|
*/
|
|
animationType?: ?('none' | 'slide' | 'fade'),
|
|
|
|
/**
|
|
* The `presentationStyle` prop controls how the modal appears.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#presentationstyle
|
|
*/
|
|
presentationStyle?: ?(
|
|
| 'fullScreen'
|
|
| 'pageSheet'
|
|
| 'formSheet'
|
|
| 'overFullScreen'
|
|
),
|
|
|
|
/**
|
|
* The `transparent` prop determines whether your modal will fill the
|
|
* entire view.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#transparent
|
|
*/
|
|
transparent?: ?boolean,
|
|
|
|
/**
|
|
* The `statusBarTranslucent` prop determines whether your modal should go under
|
|
* the system statusbar.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#transparent
|
|
*/
|
|
statusBarTranslucent?: ?boolean,
|
|
|
|
/**
|
|
* The `hardwareAccelerated` prop controls whether to force hardware
|
|
* acceleration for the underlying window.
|
|
*
|
|
* This prop works inly on Android.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#hardwareaccelerated
|
|
*/
|
|
hardwareAccelerated?: ?boolean,
|
|
|
|
/**
|
|
* The `visible` prop determines whether your modal is visible.
|
|
*
|
|
* See https://reactnative.dev/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://reactnative.dev/docs/modal.html#onrequestclose
|
|
*/
|
|
onRequestClose?: ?DirectEventHandler<null>,
|
|
|
|
/**
|
|
* The `onShow` prop allows passing a function that will be called once the
|
|
* modal has been shown.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#onshow
|
|
*/
|
|
onShow?: ?DirectEventHandler<null>,
|
|
|
|
/**
|
|
* The `onDismiss` prop allows passing a function that will be called once
|
|
* the modal has been dismissed.
|
|
*
|
|
* See https://reactnative.dev/docs/modal.html#ondismiss
|
|
*/
|
|
onDismiss?: ?() => mixed,
|
|
|
|
/**
|
|
* The `supportedOrientations` prop allows the modal to be rotated to any of the specified orientations.
|
|
*
|
|
* See https://reactnative.dev/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://reactnative.dev/docs/modal.html#onorientationchange
|
|
*/
|
|
onOrientationChange?: ?DirectEventHandler<OrientationChangeEvent>,
|
|
|}>;
|
|
|
|
class Modal extends React.Component<Props> {
|
|
static defaultProps: {|hardwareAccelerated: boolean, visible: boolean|} = {
|
|
visible: true,
|
|
hardwareAccelerated: false,
|
|
};
|
|
|
|
_identifier: number;
|
|
_eventSubscription: ?EmitterSubscription;
|
|
|
|
constructor(props: Props) {
|
|
super(props);
|
|
Modal._confirmProps(props);
|
|
this._identifier = uniqueModalIdentifier++;
|
|
}
|
|
|
|
static childContextTypes:
|
|
| any
|
|
| {|virtualizedList: React$PropType$Primitive<any>|} = {
|
|
virtualizedList: PropTypes.object,
|
|
};
|
|
|
|
getChildContext(): {|virtualizedList: null|} {
|
|
// Reset the context so VirtualizedList doesn't get confused by nesting
|
|
// in the React tree that doesn't reflect the native component hierarchy.
|
|
return {
|
|
virtualizedList: null,
|
|
};
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (this.props.onDismiss != null) {
|
|
this.props.onDismiss();
|
|
}
|
|
}
|
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
|
Modal._confirmProps(nextProps);
|
|
}
|
|
|
|
static _confirmProps(props: Props) {
|
|
if (
|
|
props.presentationStyle &&
|
|
props.presentationStyle !== 'overFullScreen' &&
|
|
props.transparent
|
|
) {
|
|
console.warn(
|
|
`Modal with '${
|
|
props.presentationStyle
|
|
}' presentation style and 'transparent' value is not supported.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
render(): React.Node {
|
|
if (this.props.visible !== true) {
|
|
return null;
|
|
}
|
|
|
|
const containerStyles = {
|
|
backgroundColor: this.props.transparent ? 'transparent' : 'white',
|
|
};
|
|
|
|
let animationType = this.props.animationType || 'none';
|
|
|
|
let presentationStyle = this.props.presentationStyle;
|
|
if (!presentationStyle) {
|
|
presentationStyle = 'fullScreen';
|
|
if (this.props.transparent) {
|
|
presentationStyle = 'overFullScreen';
|
|
}
|
|
}
|
|
|
|
const innerChildren = __DEV__ ? (
|
|
<RootTagContext.Consumer>
|
|
{rootTag => (
|
|
<AppContainer rootTag={rootTag}>{this.props.children}</AppContainer>
|
|
)}
|
|
</RootTagContext.Consumer>
|
|
) : (
|
|
this.props.children
|
|
);
|
|
|
|
return (
|
|
<RCTModalHostView
|
|
animationType={animationType}
|
|
presentationStyle={presentationStyle}
|
|
transparent={this.props.transparent}
|
|
hardwareAccelerated={this.props.hardwareAccelerated}
|
|
onRequestClose={this.props.onRequestClose}
|
|
onShow={this.props.onShow}
|
|
statusBarTranslucent={this.props.statusBarTranslucent}
|
|
identifier={this._identifier}
|
|
style={styles.modal}
|
|
onStartShouldSetResponder={this._shouldSetResponder}
|
|
supportedOrientations={this.props.supportedOrientations}
|
|
onOrientationChange={this.props.onOrientationChange}>
|
|
<ScrollView.Context.Provider value={null}>
|
|
<View style={[styles.container, containerStyles]}>
|
|
{innerChildren}
|
|
</View>
|
|
</ScrollView.Context.Provider>
|
|
</RCTModalHostView>
|
|
);
|
|
}
|
|
|
|
// We don't want any responder events bubbling out of the modal.
|
|
_shouldSetResponder(): boolean {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
const side = I18nManager.getConstants().isRTL ? 'right' : 'left';
|
|
const styles = StyleSheet.create({
|
|
modal: {
|
|
position: 'absolute',
|
|
},
|
|
container: {
|
|
/* $FlowFixMe(>=0.111.0 site=react_native_fb) This comment suppresses an
|
|
* error found when Flow v0.111 was deployed. To see the error, delete this
|
|
* comment and run Flow. */
|
|
[side]: 0,
|
|
top: 0,
|
|
flex: 1,
|
|
},
|
|
});
|
|
|
|
module.exports = Modal;
|