Skip to content

Commit 0655837

Browse files
tomekzawclaude
andcommitted
[Android] Commit TextInput native state updates synchronously
A multiline TextInput re-flows and repaints its text on the keystroke, but its frame only changed once the state update sent from the text watcher was flushed asynchronously (next Choreographer tick, commit and layout on the JS thread, mount on the following UI frame). Every change that adds or removes a line therefore drew one frame with the new text and the old height, which showed up as a content jump. Dispatch that state update with UpdateMode.unstable_Immediate so the commit, layout and mount happen inside onTextChanged, before the frame is drawn, and refresh the cached spannable before notifying the text watchers so the synchronous measure sees the new text. Fixes #46813 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 5dc1d78 commit 0655837

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,12 +1350,14 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
13501350
FLog.e(TAG, "onTextChanged[$id]: $s $start $before $count")
13511351
}
13521352

1353+
// The cached spannable must be up to date before the listeners run: a listener may commit a
1354+
// state update synchronously, which measures this view through the cached spannable.
1355+
updateCachedSpannable()
1356+
13531357
if (!isSettingTextFromJS) {
13541358
listeners?.forEach { listener -> listener.onTextChanged(s, start, before, count) }
13551359
}
13561360

1357-
updateCachedSpannable()
1358-
13591361
onContentSizeChange()
13601362
}
13611363

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ 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.uimanager.StateWrapper
1517
import com.facebook.react.uimanager.UIManagerHelper
1618
import com.facebook.react.uimanager.events.EventDispatcher
1719

20+
@OptIn(UnstableReactNativeAPI::class)
1821
internal class ReactTextInputTextWatcher(
1922
reactContext: ReactContext,
2023
private val editText: ReactEditText,
@@ -52,7 +55,10 @@ internal class ReactTextInputTextWatcher(
5255
val newStateData: WritableMap = WritableNativeMap()
5356
newStateData.putInt("mostRecentEventCount", editText.incrementAndGetEventCounter())
5457
newStateData.putInt("opaqueCacheId", editText.id)
55-
stateWrapper.updateState(newStateData)
58+
// Committing the state synchronously re-measures and re-lays out the input (and anything
59+
// depending on its size) before the current frame is drawn, so the view's size and its
60+
// text never disagree for a frame (e.g. when a multiline input gains or loses a line).
61+
stateWrapper.updateState(newStateData, StateWrapper.UpdateMode.unstable_Immediate)
5662
}
5763

5864
// The event that contains the event counter and updates it must be sent first.

0 commit comments

Comments
 (0)