Skip to content

Commit a352df5

Browse files
shubh73claude
andcommitted
Clear a Switch's stored ripple color when thumbColor is removed
setThumbColor only stored non-null colors, so after thumbColor was removed the next background color still built its ripple with the old thumb color. Also drop the mutate() call: each RippleDrawable created from a resource copies its RippleState, and setColor only writes to that copy, so recoloring it can't affect other drawables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0049973 commit a352df5

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.kt‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ internal class ReactSwitch(context: Context) : SwitchCompat(context) {
7575
val ripple =
7676
(currentBackground as? CompositeBackgroundDrawable)?.originalBackground as? RippleDrawable
7777
?: currentBackground as? RippleDrawable
78-
if (color != null) {
79-
rippleColor = color
80-
// The theme's ripple may share its state with other drawables loaded from the same resource
81-
ripple?.mutate()
82-
ripple?.setColor(createRippleDrawableColorStateList(color))
78+
rippleColor = color
79+
if (color != null && ripple != null) {
80+
ripple.setColor(createRippleDrawableColorStateList(color))
8381
}
8482
}
8583

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/switchview/ReactSwitchManagerTest.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ class ReactSwitchManagerTest {
110110
assertThat(rippleColor()).isEqualTo(Color.WHITE)
111111
}
112112

113+
@Test
114+
fun backgroundColorDoesNotUseClearedThumbColor() {
115+
manager.setThumbColor(view, Color.GREEN)
116+
manager.setThumbColor(view, null)
117+
manager.setBackgroundColor(view, Color.WHITE)
118+
119+
val pressed = intArrayOf(android.R.attr.state_pressed)
120+
assertThat(rippleEffectColor().getColorForState(pressed, 0)).isEqualTo(Color.WHITE)
121+
}
122+
113123
private fun ripple(): RippleDrawable {
114124
val background = view.background
115125
val drawable =

0 commit comments

Comments
 (0)