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
18 changes: 18 additions & 0 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ export type ModalPropsAndroid = {
* @platform android
*/
navigationBarTranslucent?: ?boolean,

/**
* Blurs the content behind the modal, in dp, with the system window blur
* (`WindowManager.LayoutParams#setBlurBehindRadius`). Use it with
* `transparent`, otherwise the modal's own background covers the blur.
*
* When the device does not support cross-window blur, or the user turned it
* off (battery saver, "Allow window-level blurs"), the window behind is
* dimmed instead.
*
* Requires Android 12 (API 31) or newer; ignored on older versions.
*
* @default 0
*
* @platform android
*/
blurBehindRadius?: ?number,
};

export type ModalProps = {
Expand Down Expand Up @@ -338,6 +355,7 @@ class Modal extends React.Component<ModalProps, ModalState> {
visible={this.props.visible}
statusBarTranslucent={this.props.statusBarTranslucent}
navigationBarTranslucent={this.props.navigationBarTranslucent}
blurBehindRadius={this.props.blurBehindRadius}
identifier={this._identifier}
style={styles.modal}
/* $FlowFixMe[incompatible-type] Error exposed after fixing this typing
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -5125,6 +5125,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 setBlurBehindRadius (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 Expand Up @@ -5411,6 +5412,7 @@ public final class com/facebook/react/views/modal/ReactModalHostView : android/v
public fun dispatchPopulateAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)Z
public fun dispatchProvideStructure (Landroid/view/ViewStructure;)V
public final fun getAnimationType ()Ljava/lang/String;
public final fun getBlurBehindRadius ()F
public fun getChildAt (I)Landroid/view/View;
public fun getChildCount ()I
public final fun getEventDispatcher ()Lcom/facebook/react/uimanager/events/EventDispatcher;
Expand All @@ -5428,6 +5430,7 @@ public final class com/facebook/react/views/modal/ReactModalHostView : android/v
public fun removeView (Landroid/view/View;)V
public fun removeViewAt (I)V
public final fun setAnimationType (Ljava/lang/String;)V
public final fun setBlurBehindRadius (F)V
public final fun setDialogRootViewGroupTestId (Ljava/lang/String;)V
public final fun setEventDispatcher (Lcom/facebook/react/uimanager/events/EventDispatcher;)V
public final fun setHardwareAccelerated (Z)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ internal class ReactModalHostManager :
view.navigationBarTranslucent = value
}

@ReactProp(name = "blurBehindRadius", defaultFloat = 0f)
override fun setBlurBehindRadius(view: ReactModalHostView, value: Float) {
view.blurBehindRadius = value
}

@ReactProp(name = "hardwareAccelerated")
override fun setHardwareAccelerated(view: ReactModalHostView, value: Boolean) {
view.hardwareAccelerated = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewStructure
import android.view.Window
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.FrameLayout
import androidx.activity.ComponentDialog
import androidx.activity.OnBackPressedCallback
import androidx.annotation.RequiresApi
import androidx.annotation.UiThread
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
Expand All @@ -43,6 +45,7 @@ import com.facebook.react.common.build.ReactBuildConfig
import com.facebook.react.config.ReactFeatureFlags
import com.facebook.react.uimanager.JSPointerDispatcher
import com.facebook.react.uimanager.JSTouchDispatcher
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.facebook.react.uimanager.PixelUtil.pxToDp
import com.facebook.react.uimanager.RootView
import com.facebook.react.uimanager.StateWrapper
Expand All @@ -55,6 +58,8 @@ import com.facebook.react.views.view.disableEdgeToEdge
import com.facebook.react.views.view.enableEdgeToEdge
import com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn
import com.facebook.react.views.view.setStatusBarTranslucency
import java.util.function.Consumer
import kotlin.math.roundToInt

/**
* ReactModalHostView is a view that sits in the view hierarchy representing a Modal view.
Expand Down Expand Up @@ -106,6 +111,13 @@ public class ReactModalHostView(context: ThemedReactContext) :
createNewDialog = true
}

/**
* Radius, in dp, of the system blur applied to the windows behind the modal. Applied with
* [WindowManager.LayoutParams.FLAG_BLUR_BEHIND] on Android 12 (API 31) and newer. `0` disables
* it. Changing it does not recreate the dialog.
*/
public var blurBehindRadius: Float = 0f

public var stateWrapper: StateWrapper?
get() = dialogRootViewGroup.stateWrapper
public set(stateWrapper) {
Expand All @@ -126,6 +138,11 @@ public class ReactModalHostView(context: ThemedReactContext) :
// but transparency does not since we can access the window to update the property.
private var createNewDialog = false

// Listens for the system turning cross-window blur on or off (battery saver, the "Allow
// window-level blurs" setting, tunnel mode) while a blurred modal is showing.
private var crossWindowBlurListener: Consumer<Boolean>? = null
private var crossWindowBlurWindowManager: WindowManager? = null

init {
dialogRootViewGroup = DialogRootViewGroup(context)
}
Expand Down Expand Up @@ -197,6 +214,7 @@ public class ReactModalHostView(context: ThemedReactContext) :
private fun dismiss() {
UiThreadUtil.assertOnUiThread()

unregisterCrossWindowBlurListener()
dialog?.let { nonNullDialog ->
if (nonNullDialog.isShowing) {
nonNullDialog.window?.let { window ->
Expand Down Expand Up @@ -409,7 +427,11 @@ public class ReactModalHostView(context: ThemedReactContext) :
dialogWindow.setStatusBarTranslucency(statusBarTranslucent)
}

if (transparent) {
// When a blur was asked for but the system can't draw it, dim the windows behind instead so
// the modal is still set apart from the content it covers.
val blurBehindApplied = updateBlurBehind(dialogWindow)
val dimForMissingBlur = blurBehindRadius > 0f && !blurBehindApplied
if (transparent && !dimForMissingBlur) {
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
} else {
dialogWindow.setDimAmount(0.5f)
Expand All @@ -426,6 +448,64 @@ public class ReactModalHostView(context: ThemedReactContext) :
}
}

/**
* Applies [blurBehindRadius] to the dialog window. Returns whether the system will draw the blur:
* false below Android 12, when the radius is 0, and when cross-window blur is currently disabled.
*/
private fun updateBlurBehind(dialogWindow: Window): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
return false
}
val radiusPx = blurBehindRadius.dpToPx().roundToInt()
if (radiusPx <= 0) {
unregisterCrossWindowBlurListener()
setWindowBlurBehind(dialogWindow, 0)
return false
}
registerCrossWindowBlurListener(dialogWindow.windowManager)
val blurEnabled = dialogWindow.windowManager.isCrossWindowBlurEnabled
setWindowBlurBehind(dialogWindow, if (blurEnabled) radiusPx else 0)
return blurEnabled
}

@RequiresApi(Build.VERSION_CODES.S)
private fun setWindowBlurBehind(dialogWindow: Window, radiusPx: Int) {
// Only touch the window when something changes: every attribute update relayouts the dialog
// window, and most modals never use the blur.
val attributes = dialogWindow.attributes
val blurFlag = if (radiusPx > 0) WindowManager.LayoutParams.FLAG_BLUR_BEHIND else 0
val flagUnchanged =
(attributes.flags and WindowManager.LayoutParams.FLAG_BLUR_BEHIND) == blurFlag
if (flagUnchanged && attributes.blurBehindRadius == radiusPx) {
return
}
attributes.flags =
(attributes.flags and WindowManager.LayoutParams.FLAG_BLUR_BEHIND.inv()) or blurFlag
attributes.blurBehindRadius = radiusPx
dialogWindow.attributes = attributes
}

@RequiresApi(Build.VERSION_CODES.S)
private fun registerCrossWindowBlurListener(windowManager: WindowManager) {
if (crossWindowBlurListener != null) {
return
}
// The listener is also called right away with the current state; it runs on the main thread.
val listener = Consumer<Boolean> { if (dialog != null) updateProperties() }
crossWindowBlurListener = listener
crossWindowBlurWindowManager = windowManager
windowManager.addCrossWindowBlurEnabledListener(context.mainExecutor, listener)
}

private fun unregisterCrossWindowBlurListener() {
val listener = crossWindowBlurListener ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
crossWindowBlurWindowManager?.removeCrossWindowBlurEnabledListener(listener)
}
crossWindowBlurListener = null
crossWindowBlurWindowManager = null
}

/**
* Updates the system appearance of the dialog to match the activity that it is being displayed
* on.
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/ReactNativeApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3118,6 +3118,7 @@ declare type ModalProps = ModalBaseProps &
ModalPropsAndroid &
ViewProps
declare type ModalPropsAndroid = {
blurBehindRadius?: number
hardwareAccelerated?: boolean
navigationBarTranslucent?: boolean
statusBarTranslucent?: boolean
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 @@ -74,6 +75,14 @@ type RCTModalHostViewNativeProps = Readonly<{
*/
hardwareAccelerated?: WithDefault<boolean, false>,

/**
* The `blurBehindRadius` prop blurs the content behind the modal window, in
* dp, using the system window blur. Android 12+ only.
*
* See https://reactnative.dev/docs/modal#blurbehindradius-android
*/
blurBehindRadius?: WithDefault<Float, 0>,

/**
* The `onRequestClose` callback is called when the user taps the hardware
* back button on Android or the menu button on Apple TV.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ export interface ModalPropsAndroid {
* Determines whether your modal should go under the system navigationbar.
*/
navigationBarTranslucent?: boolean | undefined;

/**
* Blurs the content behind the modal, in dp, with the system window blur.
* Use it with `transparent`. Falls back to a dim when cross-window blur is
* unavailable. Android 12 (API 31)+ only.
*/
blurBehindRadius?: number | undefined;
}

export type ModalProps = ModalBaseProps &
Expand Down
Loading