diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt index 99a753dbd362..5062222431f4 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.kt @@ -55,6 +55,7 @@ import com.facebook.react.uimanager.ViewDefaults import com.facebook.react.uimanager.ViewProps import com.facebook.react.uimanager.annotations.ReactProp import com.facebook.react.uimanager.annotations.ReactPropGroup +import com.facebook.react.uimanager.drawable.CompositeBackgroundDrawable import com.facebook.react.uimanager.events.BlurEvent import com.facebook.react.uimanager.events.EventDispatcher import com.facebook.react.uimanager.events.FocusEvent @@ -532,15 +533,16 @@ public open class ReactTextInputManager public constructor() : @ReactProp(name = "underlineColorAndroid", customType = "Color") public fun setUnderlineColor(view: ReactEditText, underlineColor: Int?) { + // The underline is the EditText's own background. Once BackgroundStyleApplicator wraps it, tint + // only that drawable, so the filter does not also tint box shadows and outlines. + val background = + view.background.let { if (it is CompositeBackgroundDrawable) it.originalBackground else it } + ?: return + // Drawable.mutate() can sometimes crash due to an AOSP bug: // See https://code.google.com/p/android/issues/detail?id=191754 for more info - val background = view.background var drawableToMutate = background - if (background == null) { - return - } - if (background.constantState != null) { try { drawableToMutate = checkNotNull(background.mutate()) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputUnderlineColorTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputUnderlineColorTest.kt new file mode 100644 index 000000000000..caa32befd562 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactTextInputUnderlineColorTest.kt @@ -0,0 +1,110 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.textinput + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.graphics.drawable.GradientDrawable +import android.os.Build +import android.util.DisplayMetrics +import androidx.core.content.res.ResourcesCompat.ID_NULL +import androidx.core.graphics.createBitmap +import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.BackgroundStyleApplicator +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.uimanager.style.BoxShadow +import com.facebook.testutils.shadows.ShadowSoLoader +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +/** Tests that `underlineColorAndroid` tints only the underline, not the other background layers. */ +@RunWith(RobolectricTestRunner::class) +@Config(shadows = [ShadowSoLoader::class], sdk = [Build.VERSION_CODES.UPSIDE_DOWN_CAKE]) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class ReactTextInputUnderlineColorTest { + + private lateinit var manager: ReactTextInputManager + private lateinit var view: ReactEditText + private lateinit var underline: GradientDrawable + + @Before + fun setUp() { + ReactNativeFeatureFlagsForTests.setUp() + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics().apply { density = 1f }) + val themedContext = + ThemedReactContext( + createTestReactApplicationContext(), + RuntimeEnvironment.getApplication(), + null, + ID_NULL, + ) + manager = ReactTextInputManager() + view = manager.createViewInstance(themedContext) + underline = GradientDrawable() + view.background = underline + } + + @After + fun tearDown() { + DisplayMetricsHolder.setScreenDisplayMetrics(null) + ReactNativeFeatureFlags.dangerouslyReset() + } + + @Test + fun underlineColorSetAfterBoxShadowKeepsTheShadow() { + setBoxShadow() + manager.setUnderlineColor(view, Color.TRANSPARENT) + + assertThat(underline.colorFilter) + .isEqualTo(PorterDuffColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN)) + assertThat(drawBackground().getPixel(15, 50)).isEqualTo(Color.BLACK) + } + + @Test + fun underlineColorSetBeforeBoxShadowStaysOnTheUnderline() { + manager.setUnderlineColor(view, Color.RED) + setBoxShadow() + assertThat(underline.colorFilter) + .isEqualTo(PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN)) + + manager.setUnderlineColor(view, null) + + assertThat(underline.colorFilter).isNull() + assertThat(drawBackground().getPixel(15, 50)).isEqualTo(Color.BLACK) + } + + /** Gives the view a black 10px shadow on every side, without blur. */ + private fun setBoxShadow() { + BackgroundStyleApplicator.setBoxShadow( + view, + listOf(BoxShadow(offsetX = 0f, offsetY = 0f, color = Color.BLACK, spreadDistance = 10f)), + ) + } + + /** Draws the background into a 100px square, inset by 20px so the 10px shadow spread fits. */ + private fun drawBackground(): Bitmap { + val bitmap = createBitmap(100, 100, Bitmap.Config.ARGB_8888) + val background = checkNotNull(view.background) + background.setBounds(20, 20, 80, 80) + background.draw(Canvas(bitmap)) + return bitmap + } +} diff --git a/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js b/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js index b4a060635c09..039211bb0283 100644 --- a/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js +++ b/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js @@ -20,7 +20,7 @@ import ExampleTextInput from './ExampleTextInput'; import TextInputSharedExamples from './TextInputSharedExamples'; import * as React from 'react'; import {useState} from 'react'; -import {StyleSheet, Text, View} from 'react-native'; +import {Pressable, StyleSheet, Text, View} from 'react-native'; const ToggleDefaultPaddingExample = (): React.Node => { const [hasPadding, setHasPadding] = useState(false); @@ -35,6 +35,35 @@ const ToggleDefaultPaddingExample = (): React.Node => { ); }; +const BoxShadowUnderlineColorExample = (): React.Node => { + const [underlineColor, setUnderlineColor] = useState<'transparent' | 'red'>( + 'transparent', + ); + + return ( + + + setUnderlineColor(color => (color === 'red' ? 'transparent' : 'red')) + }> + + {`underlineColorAndroid="${underlineColor}" (tap to change)`} + + + + + + + ); +}; + const styles = StyleSheet.create({ multiline: { height: 60, @@ -49,6 +78,18 @@ const styles = StyleSheet.create({ wrappedText: { maxWidth: 300, }, + boxShadowContainer: { + gap: 10, + }, + boxShadowBackdrop: { + backgroundColor: '#e5e5e5', + padding: 20, + }, + boxShadow: { + backgroundColor: 'white', + borderRadius: 8, + boxShadow: '0px 6px 10px 0px rgba(0, 0, 0, 0.4)', + }, }); const examples: Array = [ @@ -490,6 +531,15 @@ const examples: Array = [ ); }, }, + { + title: 'boxShadow and underlineColorAndroid', + name: 'boxShadowUnderlineColor', + description: + 'Changing underlineColorAndroid tints the underline, not the box shadow', + render: function (): React.Node { + return ; + }, + }, { title: 'Toggle Default Padding', render: function (): React.Node { diff --git a/packages/rn-tester/visual-tests/flows/text-input.yml b/packages/rn-tester/visual-tests/flows/text-input.yml index 95955b0aa6e4..152d46edc825 100644 --- a/packages/rn-tester/visual-tests/flows/text-input.yml +++ b/packages/rn-tester/visual-tests/flows/text-input.yml @@ -110,3 +110,37 @@ appId: ${APP_ID} path: ${VISUAL_OUTPUT_DIR}/text-input/text-styles/cycle-6 cropOn: id: 'text-styles' +- runFlow: + when: + platform: Android + commands: + - runFlow: + file: ../helpers/open-example.yml + env: + EXAMPLE_URL: rntester://example/TextInputExample/boxShadowUnderlineColor + - extendedWaitUntil: + visible: + id: 'textinput-box-shadow-underline' + timeout: 15000 + - tapOn: + id: 'textinput-box-shadow-underline-toggle' + - assertVisible: + id: 'textinput-box-shadow-underline-label' + text: 'underlineColorAndroid="red" (tap to change)' + - waitForAnimationToEnd: + timeout: 5000 + - takeScreenshot: + path: ${VISUAL_OUTPUT_DIR}/text-input/box-shadow-underline-color/red + cropOn: + id: 'textinput-box-shadow-underline' + - tapOn: + id: 'textinput-box-shadow-underline-toggle' + - assertVisible: + id: 'textinput-box-shadow-underline-label' + text: 'underlineColorAndroid="transparent" (tap to change)' + - waitForAnimationToEnd: + timeout: 5000 + - takeScreenshot: + path: ${VISUAL_OUTPUT_DIR}/text-input/box-shadow-underline-color/transparent + cropOn: + id: 'textinput-box-shadow-underline' diff --git a/packages/rn-tester/visual-tests/suites/text-input.md b/packages/rn-tester/visual-tests/suites/text-input.md index 946610e149c8..cc21d04f73be 100644 --- a/packages/rn-tester/visual-tests/suites/text-input.md +++ b/packages/rn-tester/visual-tests/suites/text-input.md @@ -5,5 +5,6 @@ | `clipping` | Initial rendering | | `placeholderEllipsizing` | Single-line and multiline placeholders | | `textStyles` | Cycles 1–6 of nested single-line and multiline styles | +| `boxShadowUnderlineColor` | `underlineColorAndroid` changed to `red`, then back to `transparent`: the box shadow stays black (Android only) | Screenshots use the existing RNTester example crops. Each example starts from a fresh app launch; captures wait for the target and for animations to settle.