Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ type ModalBasePropsCore = {
onShow?: ?DirectEventHandler<null>,

/**
* The backdrop color of the modal's container. Defaults to `white` if
* `transparent` is `false`. Ignored if `transparent` is `true`.
* The backdrop color of the modal's container. Ignored if `transparent` is
* `true`. On Android, this overrides the transparent background implied by
* an explicit `dimAmount`.
*
* @deprecated Prefer `style={{backgroundColor: ...}}` for the container
* color. On Android, keep using `backdropColor` if you need a custom color
* while setting `dimAmount`.
*/
backdropColor?: ColorValue,

Expand Down Expand Up @@ -155,6 +160,17 @@ export type ModalPropsIOS = {
};

export type ModalPropsAndroid = {
/**
* Amount of system dimming behind the modal, between 0 and 1.
* Ignored if `transparent` is `true`. When set, the modal's container
* defaults to a transparent background instead of `white`, unless
* `backdropColor` is set.
*
* @default `0.5`
* @platform android
*/
dimAmount?: ?number,

/**
* Controls whether to force hardware acceleration for the underlying window.
*
Expand Down Expand Up @@ -285,15 +301,17 @@ class Modal extends React.Component<ModalProps, ModalState> {
return null;
}

// Only override backgroundColor when transparent or backdropColor are
// explicitly set, so that these Modal-specific props take precedence
// over the generic style prop. The default backgroundColor ('white')
// is defined in styles.container below.
// Only override backgroundColor when transparent, backdropColor or
// dimAmount are explicitly set, so that these Modal-specific props take
// precedence over the generic style prop. The default backgroundColor
// ('white') is defined in styles.container below.
const containerStyles: {backgroundColor?: ColorValue} = {};
if (this.props.transparent === true) {
containerStyles.backgroundColor = 'transparent';
} else if (this.props.backdropColor != null) {
containerStyles.backgroundColor = this.props.backdropColor;
} else if (Platform.OS === 'android' && this.props.dimAmount != null) {
containerStyles.backgroundColor = 'transparent';
}

let animationType = this.props.animationType || 'none';
Expand Down Expand Up @@ -330,6 +348,7 @@ class Modal extends React.Component<ModalProps, ModalState> {
animationType={animationType}
presentationStyle={presentationStyle}
transparent={this.props.transparent}
dimAmount={this.props.dimAmount}
hardwareAccelerated={this.props.hardwareAccelerated}
onRequestClose={this.props.onRequestClose}
onShow={this.props.onShow}
Expand Down
53 changes: 53 additions & 0 deletions packages/react-native/Libraries/Modal/__tests__/Modal-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,59 @@ describe('<Modal>', () => {
</rn-modalHostView>,
);
});

describe('dimAmount', () => {
it('forwards a custom dim amount and resets to the default when removed', () => {
Fantom.runTask(() => {
root.render(<Modal dimAmount={0.6} />);
});

expect(root.getRenderedOutput({props: ['dimAmount']}).toJSX()).toEqual(
<rn-modalHostView dimAmount="0.6">
<rn-view />
</rn-modalHostView>,
);

Fantom.runTask(() => {
root.render(<Modal />);
});

expect(root.getRenderedOutput({props: ['dimAmount']}).toJSX()).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});

it('defaults the container to a transparent background', () => {
Fantom.runTask(() => {
root.render(<Modal dimAmount={0.6} />);
});

expect(
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view />
</rn-modalHostView>,
);
});

it('keeps an explicit backdropColor', () => {
Fantom.runTask(() => {
root.render(<Modal dimAmount={0.6} backdropColor="red" />);
});

expect(
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
).toEqual(
<rn-modalHostView>
<rn-view backgroundColor="rgba(255, 0, 0, 1)" />
</rn-modalHostView>,
);
});
});

describe('animationType', () => {
it('renders a Modal with animationType="none" by default', () => {
Fantom.runTask(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -5128,6 +5128,7 @@ public class com/facebook/react/viewmanagers/ModalHostViewManagerDelegate : com/
public abstract interface class com/facebook/react/viewmanagers/ModalHostViewManagerInterface : com/facebook/react/uimanager/ViewManagerWithGeneratedInterface {
public abstract fun setAllowSwipeDismissal (Landroid/view/View;Z)V
public abstract fun setAnimationType (Landroid/view/View;Ljava/lang/String;)V
public abstract fun setDimAmount (Landroid/view/View;F)V
public abstract fun setHardwareAccelerated (Landroid/view/View;Z)V
public abstract fun setIdentifier (Landroid/view/View;I)V
public abstract fun setNavigationBarTranslucent (Landroid/view/View;Z)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ internal class ReactModalHostManager :
view.transparent = value
}

@ReactProp(name = "dimAmount", defaultFloat = 0.5f)
override fun setDimAmount(view: ReactModalHostView, value: Float) {
view.dimAmount = value
}

@ReactProp(name = "statusBarTranslucent")
override fun setStatusBarTranslucent(view: ReactModalHostView, value: Boolean) {
view.statusBarTranslucent = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class ReactModalHostView(context: ThemedReactContext) :
private set

public var transparent: Boolean = false
internal var dimAmount: Float = 0.5f
public var onShowListener: DialogInterface.OnShowListener? = null
public var onRequestCloseListener: OnRequestCloseListener? = null

Expand Down Expand Up @@ -409,14 +410,12 @@ public class ReactModalHostView(context: ThemedReactContext) :
dialogWindow.setStatusBarTranslucency(statusBarTranslucent)
}

// Transparent modals are never dimmed.
if (transparent) {
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
} else {
dialogWindow.setDimAmount(0.5f)
dialogWindow.setFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND,
)
dialogWindow.setDimAmount(dimAmount.coerceIn(0f, 1f))
dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}
} catch (e: IllegalArgumentException) {
// This is to prevent a crash from the following error, without a clear repro steps:
Expand Down
9 changes: 5 additions & 4 deletions packages/react-native/ReactNativeApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<08c63097104ab811ba2be19ded117820>>
* @generated SignedSource<<494610a65ade37564afbf09ad75c140e>>
*
* This file was generated by scripts/js-api/build-types/index.js.
*/
Expand Down Expand Up @@ -3134,6 +3134,7 @@ declare type ModalProps = ModalBaseProps &
ModalPropsAndroid &
ViewProps
declare type ModalPropsAndroid = {
dimAmount?: number
hardwareAccelerated?: boolean
navigationBarTranslucent?: boolean
statusBarTranslucent?: boolean
Expand Down Expand Up @@ -6027,11 +6028,11 @@ export {
MeasureInWindowOnSuccessCallback, // a285f598
MeasureLayoutOnSuccessCallback, // 3592502a
MeasureOnSuccessCallback, // 82824e59
Modal, // 7910201f
Modal, // e3d1be07
ModalBaseProps, // 18ba4525
ModalInstance, // c47ff608
ModalProps, // 9d78b278
ModalPropsAndroid, // 515fb173
ModalProps, // 13cd075a
ModalPropsAndroid, // 7368c6ab
ModalPropsIOS, // 7943cbd7
ModeChangeEvent, // 9e9a28c4
MouseEvent, // 420b6a25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {ViewProps} from '../../../../../Libraries/Components/View/ViewPropTypes';
import type {
DirectEventHandler,
Float,
Int32,
WithDefault,
} from '../../../../../Libraries/Types/CodegenTypes';
Expand Down Expand Up @@ -50,6 +51,9 @@ type RCTModalHostViewNativeProps = Readonly<{
*/
transparent?: WithDefault<boolean, false>,

/** Amount of Android system dimming behind a non-transparent modal. */
dimAmount?: WithDefault<Float, 0.5>,

/**
* The `statusBarTranslucent` prop determines whether your modal should go under
* the system statusbar.
Expand Down
19 changes: 17 additions & 2 deletions packages/react-native/types_DEPRECATED/Libraries/Modal/Modal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ export interface ModalBaseProps {
onShow?: ((event: NativeSyntheticEvent<any>) => void) | undefined;

/**
* The `backdropColor` props sets the background color of the modal's container.
* Defaults to `white` if not provided and transparent is `false`. Ignored if `transparent` is `true`.
* The backdrop color of the modal's container. Ignored if `transparent` is
* `true`. On Android, this overrides the transparent background implied by
* an explicit `dimAmount`.
*
* @deprecated Prefer `style={{backgroundColor: ...}}` for the container
* color. On Android, keep using `backdropColor` if you need a custom color
* while setting `dimAmount`.
*/
backdropColor?: ColorValue | undefined;

Expand Down Expand Up @@ -94,6 +99,16 @@ export interface ModalPropsIOS {
}

export interface ModalPropsAndroid {
/**
* Amount of system dimming behind the modal, between 0 and 1.
* Ignored if `transparent` is `true`. When set, the modal's container
* defaults to a transparent background instead of `white`, unless
* `backdropColor` is set.
* @default 0.5
* @platform android
*/
dimAmount?: number | undefined;

/**
* Controls whether to force hardware acceleration for the underlying window.
*/
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2759,6 +2759,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4119,6 +4119,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4955,6 +4955,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4111,6 +4111,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4118,6 +4118,7 @@ class facebook::react::ModalHostViewProps : public facebook::react::HostPlatform
public bool statusBarTranslucent;
public bool transparent;
public bool visible;
public facebook::react::Float dimAmount;
public facebook::react::ModalHostViewAnimationType animationType;
public facebook::react::ModalHostViewPresentationStyle presentationStyle;
public facebook::react::ModalHostViewSupportedOrientationsMask supportedOrientations;
Expand Down
Loading