Reland: [Android] Fix Text and ScrollView payloads that Props 2.0 sends wrong or drops (#59388)

Summary:

Reland of https://github.com/react/react-native/pull/59080, which was reverted together with https://github.com/react/react-native/pull/59079. Code outside this repository that compiles against these headers also read `snapToAlignment` as a non-optional value.

With Props 2.0 on Android (`enableExclusivePropsUpdateAndroid` and the related flags), the Text and ScrollView payloads come from `getDiffProps`. Some props come out wrong:

- A removed Text `letterSpacing`: sent as `NaN`, which Java ignores, so the old spacing stays.
- `scrollsChildToFocus`: never sent.
- `snapToAlignment`: an explicit `"start"` is not sent on mount, and removing another alignment sends `"start"`.
- `decelerationRate={0.998}`: not sent, because the C++ default is the iOS rate, 0.998. Android ScrollViews start at 0.985.
- A removed `nestedScrollEnabled` or `sendMomentumEvents`: stays on.

This PR sends `null` for a removed `letterSpacing`, adds `scrollsChildToFocus` on Android, makes `snapToAlignment` optional, defaults `decelerationRate` to 0.985 on Android, and resets the two booleans to `false`. iOS keeps its defaults. Nothing changes on Android with the flags off.

One difference from the flags-off path is intended. A removed `decelerationRate` now restores 0.985. With the flags off it reaches Java as `null`, which the setter reads as 0, so a fling stops at once.

## Changelog:

[ANDROID] [FIXED] - Fix Text letterSpacing and ScrollView scrollsChildToFocus, snapToAlignment, decelerationRate, nestedScrollEnabled and sendMomentumEvents with Props 2.0

Differential Revision: D126922845

Pull Request resolved: https://github.com/react/react-native/pull/59388
This commit is contained in:
Bartłomiej Błoniarz authored and GitHub committed 2026-10-27 17:26:21 +00:00
1 parent 546d014914
commit f14a251800
11 files changed
+145 -25

No files matched your search

@@ -412,7 +412,8 @@ static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCu
auto *scrollView = (RCTEnhancedScrollView *)_scrollView;
if (oldScrollViewProps.snapToAlignment != newScrollViewProps.snapToAlignment) {
scrollView.snapToAlignment = RCTNSStringFromString(toString(newScrollViewProps.snapToAlignment));
scrollView.snapToAlignment =
RCTNSStringFromString(toString(newScrollViewProps.snapToAlignment.value_or(ScrollViewSnapToAlignment::Start)));
}
scrollView.snapToStart = newScrollViewProps.snapToStart;
@@ -80,7 +80,7 @@ BaseScrollViewProps::BaseScrollViewProps(
rawProps,
"decelerationRate",
sourceProps.decelerationRate,
(Float)0.998)),
kDefaultDecelerationRate)),
endDraggingSensitivityMultiplier(convertRawProp(
context,
rawProps,
@@ -427,10 +427,7 @@ SharedDebugStringConvertibleList BaseScrollViewProps::getDebugProps() const {
"snapToInterval",
snapToInterval,
defaultScrollViewProps.snapToInterval),
debugStringConvertibleItem(
"snapToAlignment",
snapToAlignment,
defaultScrollViewProps.snapToAlignment),
debugStringConvertibleItem("snapToAlignment", snapToAlignment),
debugStringConvertibleItem(
"disableIntervalMomentum",
disableIntervalMomentum,
@@ -41,7 +41,13 @@ class BaseScrollViewProps : public ViewProps {
bool automaticallyAdjustContentInsets{};
bool automaticallyAdjustsScrollIndicatorInsets{true};
bool automaticallyAdjustKeyboardInsets{false};
Float decelerationRate{0.998f};
#ifdef ANDROID
// ReactScrollView's initial rate.
static constexpr Float kDefaultDecelerationRate = 0.985f;
#else
static constexpr Float kDefaultDecelerationRate = 0.998f;
#endif
Float decelerationRate{kDefaultDecelerationRate};
Float endDraggingSensitivityMultiplier{1};
bool directionalLockEnabled{};
ScrollViewIndicatorStyle indicatorStyle{};
@@ -63,7 +69,7 @@ class BaseScrollViewProps : public ViewProps {
Point contentOffset{};
EdgeInsets scrollIndicatorInsets{};
Float snapToInterval{};
ScrollViewSnapToAlignment snapToAlignment{};
std::optional<ScrollViewSnapToAlignment> snapToAlignment{};
bool disableIntervalMomentum{false};
std::vector<Float> snapToOffsets{};
bool snapToStart{true};
@@ -49,7 +49,13 @@ HostPlatformScrollViewProps::HostPlatformScrollViewProps(
rawProps,
"endFillColor",
sourceProps.endFillColor,
clearColor())) {}
clearColor())),
scrollsChildToFocus(convertRawProp(
context,
rawProps,
"scrollsChildToFocus",
sourceProps.scrollsChildToFocus,
true)) {}
void HostPlatformScrollViewProps::setProp(
const PropsParserContext& context,
@@ -69,6 +75,7 @@ void HostPlatformScrollViewProps::setProp(
RAW_SET_PROP_SWITCH_CASE_BASIC(fadingEdgeLength);
RAW_SET_PROP_SWITCH_CASE_BASIC(overScrollMode);
RAW_SET_PROP_SWITCH_CASE_BASIC(endFillColor);
RAW_SET_PROP_SWITCH_CASE_BASIC(scrollsChildToFocus);
}
}
@@ -96,7 +103,11 @@ SharedDebugStringConvertibleList HostPlatformScrollViewProps::getDebugProps()
debugStringConvertibleItem(
"endFillColor",
endFillColor,
defaultScrollViewProps.endFillColor)};
defaultScrollViewProps.endFillColor),
debugStringConvertibleItem(
"scrollsChildToFocus",
scrollsChildToFocus,
defaultScrollViewProps.scrollsChildToFocus)};
}
#endif
@@ -301,17 +312,9 @@ folly::dynamic HostPlatformScrollViewProps::getDiffProps(
}
if (snapToAlignment != oldProps->snapToAlignment) {
switch (snapToAlignment) {
case ScrollViewSnapToAlignment::Start:
result["snapToAlignment"] = "start";
break;
case ScrollViewSnapToAlignment::Center:
result["snapToAlignment"] = "center";
break;
case ScrollViewSnapToAlignment::End:
result["snapToAlignment"] = "end";
break;
}
result["snapToAlignment"] = snapToAlignment.has_value()
? toString(*snapToAlignment)
: folly::dynamic(nullptr);
}
if (disableIntervalMomentum != oldProps->disableIntervalMomentum) {
@@ -380,6 +383,10 @@ folly::dynamic HostPlatformScrollViewProps::getDiffProps(
result["endFillColor"] = *endFillColor;
}
if (scrollsChildToFocus != oldProps->scrollsChildToFocus) {
result["scrollsChildToFocus"] = scrollsChildToFocus;
}
return result;
}
@@ -38,6 +38,7 @@ class HostPlatformScrollViewProps : public BaseScrollViewProps {
folly::dynamic fadingEdgeLength{};
std::string overScrollMode{"auto"};
SharedColor endFillColor{clearColor()};
bool scrollsChildToFocus{true};
#pragma mark - DebugStringConvertible
@@ -0,0 +1,70 @@
/*
* 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.
*/
#include <gtest/gtest.h>
#include <react/renderer/components/scrollview/ScrollViewProps.h>
#include <react/test_utils/PropsPayloads.h>
#ifdef RN_SERIALIZABLE_STATE
namespace facebook::react {
namespace {
using folly::dynamic;
void expectPayloads(const std::vector<PropsCommit>& commits) {
expectPayloadsMatchRawProps<ScrollViewProps>(commits);
}
} // namespace
// ReactScrollViewManager has a setter for it; with
// enableExclusivePropsUpdateAndroid there is no rawProps fallback.
TEST(ScrollViewPropsDiffTest, emitsScrollsChildToFocus) {
expectPayloads({{dynamic::object("scrollsChildToFocus", false)}});
}
// Android ScrollViews start with snapping alignment disabled, and
// setSnapToAlignment disables it for null.
TEST(
ScrollViewPropsDiffTest,
keepsAnExplicitSnapToAlignmentStartApartFromUnset) {
expectPayloads({
{dynamic::object("snapToAlignment", "start")},
{dynamic::object("snapToAlignment", "center")},
{dynamic::object("snapToAlignment", nullptr)},
});
}
// Android ScrollViews start decelerating at 0.985 (ReactScrollViewHelper).
// A removed rate restores it; Props 1.5 sends null, which the setter reads as
// 0.
TEST(ScrollViewPropsDiffTest, emitsAnExplicitDecelerationRate) {
expectPayloads({
{dynamic::object("decelerationRate", 0.998)},
{dynamic::object("decelerationRate", nullptr),
dynamic::object("decelerationRate", 0.985)},
});
}
// The ScrollView managers turn both off for null.
TEST(ScrollViewPropsDiffTest, removingNestedScrollAndMomentumEventsEmitsFalse) {
expectPayloads({
{dynamic::object("nestedScrollEnabled", true)(
"sendMomentumEvents", true)},
{dynamic::object("nestedScrollEnabled", nullptr)(
"sendMomentumEvents", nullptr),
dynamic::object("nestedScrollEnabled", false)(
"sendMomentumEvents", false)},
});
}
} // namespace facebook::react
#endif
@@ -13,6 +13,8 @@
#include <react/renderer/debug/DebugStringConvertibleItem.h>
#include <react/utils/FloatComparison.h>
#include <cmath>
namespace facebook::react {
static TextAttributes convertRawProp(
@@ -436,7 +438,9 @@ void BaseTextProps::appendTextAttributesProps(
if (!floatEquality(
textAttributes.letterSpacing,
oldProps->textAttributes.letterSpacing)) {
result["letterSpacing"] = textAttributes.letterSpacing;
result["letterSpacing"] = std::isnan(textAttributes.letterSpacing)
? folly::dynamic(nullptr)
: textAttributes.letterSpacing;
}
if (textAttributes.textTransform != oldProps->textAttributes.textTransform) {
@@ -0,0 +1,30 @@
/*
* 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.
*/
#include <gtest/gtest.h>
#include <react/renderer/components/text/ParagraphProps.h>
#include <react/test_utils/PropsPayloads.h>
#ifdef RN_SERIALIZABLE_STATE
namespace facebook::react {
using folly::dynamic;
// ReactTextViewManager resets letterSpacing to 0 for null;
// ReactTextView.setLetterSpacing ignores NaN.
TEST(ParagraphPropsDiffTest, removingLetterSpacingEmitsNull) {
expectPayloadsMatchRawProps<ParagraphProps>({
{dynamic::object("letterSpacing", 4.0)},
{dynamic::object("letterSpacing", nullptr)},
});
}
} // namespace facebook::react
#endif
@@ -1439,8 +1439,9 @@ class facebook::react::BaseScrollViewProps : public facebook::react::HostPlatfor
public facebook::react::Point contentOffset;
public facebook::react::ScrollViewIndicatorStyle indicatorStyle;
public facebook::react::ScrollViewKeyboardDismissMode keyboardDismissMode;
public facebook::react::ScrollViewSnapToAlignment snapToAlignment;
public static constexpr facebook::react::Float kDefaultDecelerationRate;
public std::optional<facebook::react::ScrollViewMaintainVisibleContentPosition> maintainVisibleContentPosition;
public std::optional<facebook::react::ScrollViewSnapToAlignment> snapToAlignment;
public std::vector<facebook::react::Float> snapToOffsets;
public void setProp(const facebook::react::PropsParserContext& context, facebook::react::RawPropsPropNameHash hash, const char* propName, const facebook::react::RawValue& value);
}
@@ -2049,6 +2050,7 @@ class facebook::react::HostPlatformScrollViewProps : public facebook::react::Bas
public HostPlatformScrollViewProps() = default;
public HostPlatformScrollViewProps(const facebook::react::PropsParserContext& context, const facebook::react::HostPlatformScrollViewProps& sourceProps, const facebook::react::RawProps& rawProps);
public bool nestedScrollEnabled;
public bool scrollsChildToFocus;
public bool sendMomentumEvents;
public facebook::react::SharedColor endFillColor;
public folly::dynamic fadingEdgeLength;
@@ -4003,8 +4003,9 @@ class facebook::react::BaseScrollViewProps : public facebook::react::HostPlatfor
public facebook::react::Point contentOffset;
public facebook::react::ScrollViewIndicatorStyle indicatorStyle;
public facebook::react::ScrollViewKeyboardDismissMode keyboardDismissMode;
public facebook::react::ScrollViewSnapToAlignment snapToAlignment;
public static constexpr facebook::react::Float kDefaultDecelerationRate;
public std::optional<facebook::react::ScrollViewMaintainVisibleContentPosition> maintainVisibleContentPosition;
public std::optional<facebook::react::ScrollViewSnapToAlignment> snapToAlignment;
public std::vector<facebook::react::Float> snapToOffsets;
public void setProp(const facebook::react::PropsParserContext& context, facebook::react::RawPropsPropNameHash hash, const char* propName, const facebook::react::RawValue& value);
}
@@ -780,8 +780,9 @@ class facebook::react::BaseScrollViewProps : public facebook::react::HostPlatfor
public facebook::react::Point contentOffset;
public facebook::react::ScrollViewIndicatorStyle indicatorStyle;
public facebook::react::ScrollViewKeyboardDismissMode keyboardDismissMode;
public facebook::react::ScrollViewSnapToAlignment snapToAlignment;
public static constexpr Float kDefaultDecelerationRate;
public std::optional<facebook::react::ScrollViewMaintainVisibleContentPosition> maintainVisibleContentPosition;
public std::optional<facebook::react::ScrollViewSnapToAlignment> snapToAlignment;
public std::vector<Float> snapToOffsets;
public void setProp(const facebook::react::PropsParserContext& context, facebook::react::RawPropsPropNameHash hash, const char* propName, const facebook::react::RawValue& value);
}