Skip to content

Commit b51bc8f

Browse files
alanleedevfacebook-github-bot
authored andcommitted
Add listener API to OverrideColorScheme for dynamic dark mode updates
Summary: This change adds a listener mechanism to the `OverrideColorScheme` interface to enable dynamic updates when the user's dark mode preference changes via `OverrideColorScheme`. Previously, `AppearanceModule` would only reflect the initial color scheme state when instantiated. If a user toggled dark mode through an `OverrideColorScheme` implementation, React Native JavaScript would not be notified of the change, causing UI inconsistencies. This implementation adds: 1. An optional `addListener()` method to the `OverrideColorScheme` interface with a default no-op implementation for backward compatibility 2. Automatic listener registration in `AppearanceModule`'s init block that triggers `onConfigurationChanged()` when the color scheme changes Changelog: [Android][Added] - Add `addListener()` function to `OverrideColorScheme` interface to support dynamic appearance updates via override Differential Revision: D88427482
1 parent 1a28d9e commit b51bc8f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2475,9 +2475,14 @@ public final class com/facebook/react/modules/appearance/AppearanceModule$Compan
24752475
}
24762476

24772477
public abstract interface class com/facebook/react/modules/appearance/AppearanceModule$OverrideColorScheme {
2478+
public abstract fun addListener (Lkotlin/jvm/functions/Function0;)V
24782479
public abstract fun getScheme ()Ljava/lang/String;
24792480
}
24802481

2482+
public final class com/facebook/react/modules/appearance/AppearanceModule$OverrideColorScheme$DefaultImpls {
2483+
public static fun addListener (Lcom/facebook/react/modules/appearance/AppearanceModule$OverrideColorScheme;Lkotlin/jvm/functions/Function0;)V
2484+
}
2485+
24812486
public abstract interface class com/facebook/react/modules/appregistry/AppRegistry : com/facebook/react/bridge/JavaScriptModule {
24822487
public abstract fun runApplication (Ljava/lang/String;Lcom/facebook/react/bridge/WritableMap;)V
24832488
public abstract fun startHeadlessTask (ILjava/lang/String;Lcom/facebook/react/bridge/WritableMap;)V
@@ -6764,4 +6769,3 @@ public final class com/facebook/react/views/virtual/viewexperimental/VirtualView
67646769
public fun <init> (IILcom/facebook/react/uimanager/events/EventDispatcher;)V
67656770
public fun emitModeChange (Lcom/facebook/react/views/virtual/VirtualViewMode;Landroid/graphics/Rect;Landroid/graphics/Rect;Z)V
67666771
}
6767-

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,32 @@ constructor(
2828

2929
private var lastEmittedColorScheme: String? = null
3030

31+
init {
32+
// Register as a listener for color scheme changes if override is provided
33+
overrideColorScheme?.addListener {
34+
val activity = reactApplicationContext.getCurrentActivity()
35+
onConfigurationChanged(activity ?: reactApplicationContext)
36+
}
37+
}
38+
3139
/** Optional override to the current color scheme */
32-
public fun interface OverrideColorScheme {
40+
public interface OverrideColorScheme {
3341
/**
3442
* Color scheme will use the return value instead of the current system configuration. Available
3543
* scheme: {light, dark}
3644
*/
3745
public fun getScheme(): String
46+
47+
/**
48+
* Register a listener to be notified when the color scheme changes. The listener will be
49+
* invoked whenever the underlying theme preference changes.
50+
*
51+
* Default implementation does nothing. Override this method if you want to support dynamic
52+
* color scheme updates.
53+
*/
54+
public fun addListener(listener: () -> Unit) {
55+
// no-op
56+
}
3857
}
3958

4059
private fun colorSchemeForCurrentConfiguration(context: Context): String {

0 commit comments

Comments
 (0)