Skip to content

Commit 05d011a

Browse files
jasozhmeta-codesync[bot]
authored andcommitted
Fix content jump for growing multiline TextInputs on Android (#59419)
Summary: Pull Request resolved: #59419 Fixes the issue described in [#46813](#46813), where multiline TextInputs on Android experience a content jump when adding and removing lines that change the input's height. ### Root Cause ReactEditText hardcodes `isLayoutRequested() = false` which causes Android to immediately scroll the caret to the newly typed character instead of waiting until the next layout. When adding a new line to an auto-expanding input, Android scrolls to the new line before Fabric's asynchronous state update triggers a new layout. This manifests as a glitchy content jump since the current line briefly scrolls to the newly added line before being reset by the incoming layout. In React Native, text is centered vertically in TextInputs by default. When removing a line from an auto-expanding input, Android immediately recenters the text in the remaining space before Fabric's asynchronous state update occurs, causing another glitchy content jump since the current line briefly shifts from its current position due to recentering before being reset by the incoming layout. One workaround for this is to set the `textAlignVertical` CSS style in the opposite direction of the growth direction, such as `textAlignVertical: 'top'` for a text field that grows downwards. ### This PR Update `ReactTextInputTextWatcher` to trigger an `unstable_Immediate` synchronous state update when a multiline text field's line count changes. This ensures that a layout arrives in the next frame, avoiding the existing race issues where Android scrolls or recenters before the next layout. Gated behind `fixMultilineContentJumpAndroid`. ### Limitations This fix depends on synchronous state updates, which could have performance concerns. This is mitigated by limiting synchronous updates only to when the input expands or shrinks, with all other updates remaining async. One potential alternative is to use scroll deferral to fix the content jump when adding new lines. By conditionally setting `isLayoutRequested() = true` in ReactEditText, the content jump can be avoided by delaying the scroll in cases when we can guarantee a layout is pending. However, this means ReactEditText needs to predict when Fabric guarantees a layout, which means hardcoding assumptions about Fabric measurement that break if the measurement code changes. Alternatively, ReactEditText would need a working `requestLayout()`, but this goes against existing architectural patterns (see `ReactViewGroup.requestLayout()`) where UIManager is meant to handle laying out children. Even then, would only fix the content jump when adding new lines and not when removing lines. Changelog: [Android]Fixed] - Fix content jump for expanding multiline TextInputs on Android Differential Revision: D127048758
1 parent 63835d3 commit 05d011a

21 files changed

Lines changed: 217 additions & 67 deletions

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<0128ee1e6f9a35114e76a548f1ccc8ab>>
7+
* @generated SignedSource<<34bdbabf546b759301a91ae428fed6ec>>
88
*/
99

1010
/**
@@ -402,6 +402,12 @@ public object ReactNativeFeatureFlags {
402402
@JvmStatic
403403
public fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean = accessor.fixMappingOfEventPrioritiesBetweenFabricAndReact()
404404

405+
/**
406+
* Flushes TextInput state updates synchronously on Android when a multiline input changes line count, so the new height mounts in the same frame instead of the content jumping. Pairs with enableFollowUpMountItemDispatchAndroid for updates that happen during mount dispatch.
407+
*/
408+
@JvmStatic
409+
public fun fixMultilineContentJumpAndroid(): Boolean = accessor.fixMultilineContentJumpAndroid()
410+
405411
/**
406412
* Dispatches `onMomentumScrollBegin` and `onMomentumScrollEnd` as an ordered pair for animated programmatic scrolls on iOS, and neither for non-animated programmatic scrolls.
407413
*/

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<eda7892bb6355bf6e239bc56948b06c5>>
7+
* @generated SignedSource<<9726e5e1cd3145fea4b07a166f85660a>>
88
*/
99

1010
/**
@@ -82,6 +82,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
8282
private var enableVirtualViewContainerStateExperimentalCache: Boolean? = null
8383
private var fixBorderlessRippleAndroidCache: Boolean? = null
8484
private var fixMappingOfEventPrioritiesBetweenFabricAndReactCache: Boolean? = null
85+
private var fixMultilineContentJumpAndroidCache: Boolean? = null
8586
private var fixProgrammaticScrollMomentumEventsIOSCache: Boolean? = null
8687
private var fixTextInlineViewLineHeightCache: Boolean? = null
8788
private var fixTextLayoutOnPixelDensityChangeCache: Boolean? = null
@@ -680,6 +681,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
680681
return cached
681682
}
682683

684+
override fun fixMultilineContentJumpAndroid(): Boolean {
685+
var cached = fixMultilineContentJumpAndroidCache
686+
if (cached == null) {
687+
cached = ReactNativeFeatureFlagsCxxInterop.fixMultilineContentJumpAndroid()
688+
fixMultilineContentJumpAndroidCache = cached
689+
}
690+
return cached
691+
}
692+
683693
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean {
684694
var cached = fixProgrammaticScrollMomentumEventsIOSCache
685695
if (cached == null) {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<8606a8e4878360df1538b4218534377a>>
7+
* @generated SignedSource<<a980a4686e0ec1da053ef504509c4314>>
88
*/
99

1010
/**
@@ -152,6 +152,8 @@ public object ReactNativeFeatureFlagsCxxInterop {
152152

153153
@DoNotStrip @JvmStatic public external fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean
154154

155+
@DoNotStrip @JvmStatic public external fun fixMultilineContentJumpAndroid(): Boolean
156+
155157
@DoNotStrip @JvmStatic public external fun fixProgrammaticScrollMomentumEventsIOS(): Boolean
156158

157159
@DoNotStrip @JvmStatic public external fun fixTextInlineViewLineHeight(): Boolean

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<49d72918b9b9df7d50b7baa9fa4aa85b>>
7+
* @generated SignedSource<<fd11ece2517442e70232346797b75cdf>>
88
*/
99

1010
/**
@@ -147,6 +147,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi
147147

148148
override fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean = false
149149

150+
override fun fixMultilineContentJumpAndroid(): Boolean = false
151+
150152
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean = false
151153

152154
override fun fixTextInlineViewLineHeight(): Boolean = false

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<0f7a0db5da3bb94bb71a0557628dcf2d>>
7+
* @generated SignedSource<<7bb012133d3e7b2f565a6cce09f2101e>>
88
*/
99

1010
/**
@@ -86,6 +86,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
8686
private var enableVirtualViewContainerStateExperimentalCache: Boolean? = null
8787
private var fixBorderlessRippleAndroidCache: Boolean? = null
8888
private var fixMappingOfEventPrioritiesBetweenFabricAndReactCache: Boolean? = null
89+
private var fixMultilineContentJumpAndroidCache: Boolean? = null
8990
private var fixProgrammaticScrollMomentumEventsIOSCache: Boolean? = null
9091
private var fixTextInlineViewLineHeightCache: Boolean? = null
9192
private var fixTextLayoutOnPixelDensityChangeCache: Boolean? = null
@@ -746,6 +747,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
746747
return cached
747748
}
748749

750+
override fun fixMultilineContentJumpAndroid(): Boolean {
751+
var cached = fixMultilineContentJumpAndroidCache
752+
if (cached == null) {
753+
cached = currentProvider.fixMultilineContentJumpAndroid()
754+
accessedFeatureFlags.add("fixMultilineContentJumpAndroid")
755+
fixMultilineContentJumpAndroidCache = cached
756+
}
757+
return cached
758+
}
759+
749760
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean {
750761
var cached = fixProgrammaticScrollMomentumEventsIOSCache
751762
if (cached == null) {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<33bd0d69031266aecf69a8b4b216936f>>
7+
* @generated SignedSource<<72dbf13d4f0e66503364e384a4d32ec9>>
88
*/
99

1010
/**
@@ -147,6 +147,8 @@ public interface ReactNativeFeatureFlagsProvider {
147147

148148
@DoNotStrip public fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean
149149

150+
@DoNotStrip public fun fixMultilineContentJumpAndroid(): Boolean
151+
150152
@DoNotStrip public fun fixProgrammaticScrollMomentumEventsIOS(): Boolean
151153

152154
@DoNotStrip public fun fixTextInlineViewLineHeight(): Boolean

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputTextWatcher.kt‎

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ import android.text.TextWatcher
1212
import com.facebook.react.bridge.ReactContext
1313
import com.facebook.react.bridge.WritableMap
1414
import com.facebook.react.bridge.WritableNativeMap
15+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
16+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
17+
import com.facebook.react.uimanager.StateWrapper.UpdateMode
1518
import com.facebook.react.uimanager.UIManagerHelper
1619
import com.facebook.react.uimanager.events.EventDispatcher
1720

21+
@OptIn(UnstableReactNativeAPI::class)
1822
internal class ReactTextInputTextWatcher(
1923
reactContext: ReactContext,
2024
private val editText: ReactEditText,
2125
) : TextWatcher {
2226
private val eventDispatcher: EventDispatcher? = UIManagerHelper.getEventDispatcher(reactContext)
2327
private val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
2428
private var previousText: String? = null
29+
private var previousLineCount: Int? = null
30+
private var pendingEventCount: Int? = null
2531

2632
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
2733
// Incoming charSequence gets mutated before onTextChanged() is invoked
2834
previousText = s.toString()
35+
previousLineCount = editText.layout?.lineCount
2936
}
3037

3138
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
@@ -46,13 +53,13 @@ internal class ReactTextInputTextWatcher(
4653
return
4754
}
4855

49-
val stateWrapper = editText.stateWrapper
50-
51-
if (stateWrapper != null) {
52-
val newStateData: WritableMap = WritableNativeMap()
53-
newStateData.putInt("mostRecentEventCount", editText.incrementAndGetEventCounter())
54-
newStateData.putInt("opaqueCacheId", editText.id)
55-
stateWrapper.updateState(newStateData)
56+
if (editText.stateWrapper != null) {
57+
val eventCount = editText.incrementAndGetEventCounter()
58+
if (shouldUpdateSynchronously) {
59+
pendingEventCount = eventCount
60+
} else {
61+
updateState(eventCount)
62+
}
5663
}
5764

5865
// The event that contains the event counter and updates it must be sent first.
@@ -68,5 +75,31 @@ internal class ReactTextInputTextWatcher(
6875
)
6976
}
7077

71-
override fun afterTextChanged(s: Editable) = Unit
78+
override fun afterTextChanged(s: Editable) {
79+
// Update here rather than in on TextChanged since ReactEditText updates the cached spannable
80+
// only after every TextWatcher has seen onTextChanged
81+
pendingEventCount?.let { eventCount ->
82+
// A synchronous update flushes all enqueued state updates. Mounting a queued text update
83+
// calls maybeSetTextFromState() which re-runs this callback, so clear pendingEventCount first
84+
pendingEventCount = null
85+
updateState(eventCount, UpdateMode.unstable_Immediate)
86+
}
87+
}
88+
89+
private fun updateState(eventCount: Int, updateMode: UpdateMode = UpdateMode.Asynchronous) {
90+
val stateWrapper = editText.stateWrapper ?: return
91+
val newStateData: WritableMap = WritableNativeMap()
92+
newStateData.putInt("mostRecentEventCount", eventCount)
93+
newStateData.putInt("opaqueCacheId", editText.id)
94+
stateWrapper.updateState(newStateData, updateMode)
95+
}
96+
97+
private val shouldUpdateSynchronously: Boolean
98+
get() {
99+
if (!ReactNativeFeatureFlags.fixMultilineContentJumpAndroid()) {
100+
return false
101+
}
102+
val lineCount = editText.layout?.lineCount
103+
return editText.isMultiline && lineCount != null && lineCount != previousLineCount
104+
}
72105
}

‎packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<5948daf54ab796ec4bc2754bd309cbc1>>
7+
* @generated SignedSource<<04ec1a82d4d8a9f78fcb804be6189176>>
88
*/
99

1010
/**
@@ -411,6 +411,12 @@ class ReactNativeFeatureFlagsJavaProvider
411411
return method(javaProvider_);
412412
}
413413

414+
bool fixMultilineContentJumpAndroid() override {
415+
static const auto method =
416+
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("fixMultilineContentJumpAndroid");
417+
return method(javaProvider_);
418+
}
419+
414420
bool fixProgrammaticScrollMomentumEventsIOS() override {
415421
static const auto method =
416422
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("fixProgrammaticScrollMomentumEventsIOS");
@@ -959,6 +965,11 @@ bool JReactNativeFeatureFlagsCxxInterop::fixMappingOfEventPrioritiesBetweenFabri
959965
return ReactNativeFeatureFlags::fixMappingOfEventPrioritiesBetweenFabricAndReact();
960966
}
961967

968+
bool JReactNativeFeatureFlagsCxxInterop::fixMultilineContentJumpAndroid(
969+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
970+
return ReactNativeFeatureFlags::fixMultilineContentJumpAndroid();
971+
}
972+
962973
bool JReactNativeFeatureFlagsCxxInterop::fixProgrammaticScrollMomentumEventsIOS(
963974
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
964975
return ReactNativeFeatureFlags::fixProgrammaticScrollMomentumEventsIOS();
@@ -1371,6 +1382,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
13711382
makeNativeMethod(
13721383
"fixMappingOfEventPrioritiesBetweenFabricAndReact",
13731384
JReactNativeFeatureFlagsCxxInterop::fixMappingOfEventPrioritiesBetweenFabricAndReact),
1385+
makeNativeMethod(
1386+
"fixMultilineContentJumpAndroid",
1387+
JReactNativeFeatureFlagsCxxInterop::fixMultilineContentJumpAndroid),
13741388
makeNativeMethod(
13751389
"fixProgrammaticScrollMomentumEventsIOS",
13761390
JReactNativeFeatureFlagsCxxInterop::fixProgrammaticScrollMomentumEventsIOS),

‎packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<5156494ed5baa3f0ee71fc46544680f0>>
7+
* @generated SignedSource<<19f054d765a345d3768c075502177952>>
88
*/
99

1010
/**
@@ -216,6 +216,9 @@ class JReactNativeFeatureFlagsCxxInterop
216216
static bool fixMappingOfEventPrioritiesBetweenFabricAndReact(
217217
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
218218

219+
static bool fixMultilineContentJumpAndroid(
220+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
221+
219222
static bool fixProgrammaticScrollMomentumEventsIOS(
220223
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
221224

‎packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<67d108d1c225a982e2b6d443d19ea02d>>
7+
* @generated SignedSource<<abe454ca84cbfeb04b9720d7bf5cbb6d>>
88
*/
99

1010
/**
@@ -274,6 +274,10 @@ bool ReactNativeFeatureFlags::fixMappingOfEventPrioritiesBetweenFabricAndReact()
274274
return getAccessor().fixMappingOfEventPrioritiesBetweenFabricAndReact();
275275
}
276276

277+
bool ReactNativeFeatureFlags::fixMultilineContentJumpAndroid() {
278+
return getAccessor().fixMultilineContentJumpAndroid();
279+
}
280+
277281
bool ReactNativeFeatureFlags::fixProgrammaticScrollMomentumEventsIOS() {
278282
return getAccessor().fixProgrammaticScrollMomentumEventsIOS();
279283
}

0 commit comments

Comments
 (0)