diff --git a/packages/react-native/Libraries/Modal/Modal.js b/packages/react-native/Libraries/Modal/Modal.js index dc855add3d06..b8d931a7b7dd 100644 --- a/packages/react-native/Libraries/Modal/Modal.js +++ b/packages/react-native/Libraries/Modal/Modal.js @@ -80,8 +80,13 @@ type ModalBasePropsCore = { onShow?: ?DirectEventHandler, /** - * 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, @@ -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. * @@ -285,15 +301,17 @@ class Modal extends React.Component { 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'; @@ -330,6 +348,7 @@ class Modal extends React.Component { animationType={animationType} presentationStyle={presentationStyle} transparent={this.props.transparent} + dimAmount={this.props.dimAmount} hardwareAccelerated={this.props.hardwareAccelerated} onRequestClose={this.props.onRequestClose} onShow={this.props.onShow} diff --git a/packages/react-native/Libraries/Modal/__tests__/Modal-itest.js b/packages/react-native/Libraries/Modal/__tests__/Modal-itest.js index 34e268d2442a..8d7cbe322f1a 100644 --- a/packages/react-native/Libraries/Modal/__tests__/Modal-itest.js +++ b/packages/react-native/Libraries/Modal/__tests__/Modal-itest.js @@ -49,6 +49,59 @@ describe('', () => { , ); }); + + describe('dimAmount', () => { + it('forwards a custom dim amount and resets to the default when removed', () => { + Fantom.runTask(() => { + root.render(); + }); + + expect(root.getRenderedOutput({props: ['dimAmount']}).toJSX()).toEqual( + + + , + ); + + Fantom.runTask(() => { + root.render(); + }); + + expect(root.getRenderedOutput({props: ['dimAmount']}).toJSX()).toEqual( + + + , + ); + }); + + it('defaults the container to a transparent background', () => { + Fantom.runTask(() => { + root.render(); + }); + + expect( + root.getRenderedOutput({props: ['backgroundColor']}).toJSX(), + ).toEqual( + + + , + ); + }); + + it('keeps an explicit backdropColor', () => { + Fantom.runTask(() => { + root.render(); + }); + + expect( + root.getRenderedOutput({props: ['backgroundColor']}).toJSX(), + ).toEqual( + + + , + ); + }); + }); + describe('animationType', () => { it('renders a Modal with animationType="none" by default', () => { Fantom.runTask(() => { diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index e5609d383c48..6cd8ab8bbfb2 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -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 diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt index a1beb0b6922b..503934cf7d74 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt @@ -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 diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt index 0cd7342ccb7e..9092b43bc8c8 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt @@ -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 @@ -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: diff --git a/packages/react-native/ReactNativeApi.d.ts b/packages/react-native/ReactNativeApi.d.ts index b5127f242a2f..696c1380dff8 100644 --- a/packages/react-native/ReactNativeApi.d.ts +++ b/packages/react-native/ReactNativeApi.d.ts @@ -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. */ @@ -3134,6 +3134,7 @@ declare type ModalProps = ModalBaseProps & ModalPropsAndroid & ViewProps declare type ModalPropsAndroid = { + dimAmount?: number hardwareAccelerated?: boolean navigationBarTranslucent?: boolean statusBarTranslucent?: boolean @@ -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 diff --git a/packages/react-native/src/private/components/modal/specs/RCTModalHostViewNativeComponent.js b/packages/react-native/src/private/components/modal/specs/RCTModalHostViewNativeComponent.js index c8f274c82332..48b2e984045d 100644 --- a/packages/react-native/src/private/components/modal/specs/RCTModalHostViewNativeComponent.js +++ b/packages/react-native/src/private/components/modal/specs/RCTModalHostViewNativeComponent.js @@ -11,6 +11,7 @@ import type {ViewProps} from '../../../../../Libraries/Components/View/ViewPropTypes'; import type { DirectEventHandler, + Float, Int32, WithDefault, } from '../../../../../Libraries/Types/CodegenTypes'; @@ -50,6 +51,9 @@ type RCTModalHostViewNativeProps = Readonly<{ */ transparent?: WithDefault, + /** Amount of Android system dimming behind a non-transparent modal. */ + dimAmount?: WithDefault, + /** * The `statusBarTranslucent` prop determines whether your modal should go under * the system statusbar. diff --git a/packages/react-native/types_DEPRECATED/Libraries/Modal/Modal.d.ts b/packages/react-native/types_DEPRECATED/Libraries/Modal/Modal.d.ts index e60f63145fa5..7632fec1c323 100644 --- a/packages/react-native/types_DEPRECATED/Libraries/Modal/Modal.d.ts +++ b/packages/react-native/types_DEPRECATED/Libraries/Modal/Modal.d.ts @@ -42,8 +42,13 @@ export interface ModalBaseProps { onShow?: ((event: NativeSyntheticEvent) => 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; @@ -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. */ diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index f214d60a07cd..27a692f319dc 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index f1811b25c34a..77fd3b7a9302 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index 2ef1680b9010..fe075f054b3d 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 2ef1680b9010..fe075f054b3d 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 5fb34f123790..1cac38b82487 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index c191f3f7f57e..ff91991273d6 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 9f2987431905..cb4985e7ffd8 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -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; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 1bfb2638d899..91b1549bab44 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -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;