Skip to content

Commit 3c17e10

Browse files
zoontekfacebook-github-bot
authored andcommitted
Add UiModeUtils.isDarkMode helper (facebook#51925)
Summary: This PR is part of the [edge-to-edge core implementation effort](facebook#47554). It adds a helper to `ContextUtils`, called `isDarkMode` in order to be reused accross the Android codebase. ## Changelog: - [Internal] [Added] - Add `UiModeUtils.isDarkMode` helper Pull Request resolved: facebook#51925 Test Plan: <img width="300" alt="Screenshot 2025-06-10 at 17 50 19" src="https://github.com/user-attachments/assets/48795406-e852-486a-bae8-54507ad769ee" /> <img width="300" alt="Screenshot 2025-06-10 at 17 50 42" src="https://github.com/user-attachments/assets/0dbdece9-04f7-487b-ace8-4786c8da381d" /> Rollback Plan: Reviewed By: cortinico Differential Revision: D76352849 Pulled By: alanleedev fbshipit-source-id: fbbc772d1dfa3e30e62a71a711de11b85cdec446
1 parent 8174d02 commit 3c17e10

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
package com.facebook.react.modules.appearance
99

1010
import android.content.Context
11-
import android.content.res.Configuration
1211
import androidx.appcompat.app.AppCompatDelegate
1312
import com.facebook.fbreact.specs.NativeAppearanceSpec
1413
import com.facebook.react.bridge.ReactApplicationContext
1514
import com.facebook.react.bridge.UiThreadUtil
1615
import com.facebook.react.bridge.buildReadableMap
1716
import com.facebook.react.module.annotations.ReactModule
17+
import com.facebook.react.views.common.UiModeUtils
1818

1919
/** Module that exposes the user's preferred color scheme. */
2020
@ReactModule(name = NativeAppearanceSpec.NAME)
@@ -41,13 +41,7 @@ constructor(
4141
return overrideColorScheme.getScheme()
4242
}
4343

44-
val currentNightMode =
45-
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
46-
return when (currentNightMode) {
47-
Configuration.UI_MODE_NIGHT_NO -> "light"
48-
Configuration.UI_MODE_NIGHT_YES -> "dark"
49-
else -> "light"
50-
}
44+
return if (UiModeUtils.isDarkMode(context)) "dark" else "light"
5145
}
5246

5347
public override fun getColorScheme(): String {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.views.common
9+
10+
import android.content.Context
11+
import android.content.res.Configuration
12+
13+
/** Utility object providing static methods for working with UI mode properties from Context. */
14+
internal object UiModeUtils {
15+
16+
/**
17+
* Determines whether the current UI mode is dark mode
18+
*
19+
* @param context The context to check the UI mode from
20+
* @return true if the current UI mode is dark mode, false otherwise
21+
*/
22+
@JvmStatic
23+
fun isDarkMode(context: Context): Boolean =
24+
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK ==
25+
Configuration.UI_MODE_NIGHT_YES
26+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/WindowUtil.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
package com.facebook.react.views.view
99

10-
import android.content.res.Configuration
1110
import android.graphics.Color
1211
import android.os.Build
1312
import android.view.Window
1413
import android.view.WindowManager
1514
import androidx.core.view.ViewCompat
1615
import androidx.core.view.WindowCompat
1716
import androidx.core.view.WindowInsetsControllerCompat
17+
import com.facebook.react.views.common.UiModeUtils
1818

1919
@Suppress("DEPRECATION")
2020
internal fun Window.setStatusBarTranslucency(isTranslucent: Boolean) {
@@ -71,9 +71,7 @@ internal fun Window.setSystemBarsTranslucency(isTranslucent: Boolean) {
7171
WindowCompat.setDecorFitsSystemWindows(this, !isTranslucent)
7272

7373
if (isTranslucent) {
74-
val isDarkMode =
75-
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK ==
76-
Configuration.UI_MODE_NIGHT_YES
74+
val isDarkMode = UiModeUtils.isDarkMode(context)
7775

7876
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
7977
isStatusBarContrastEnforced = false

0 commit comments

Comments
 (0)