From 973b31ba57b7d79207beea6a60503908a709ff9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20B=C5=82oniarz?= Date: Wed, 21 Oct 2026 10:26:56 -0700 Subject: [PATCH] Fix Text and ScrollView payloads that Props 2.0 sends wrong or drops (#59080) Summary: 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 Pull Request resolved: https://github.com/react/react-native/pull/59080 Test Plan: - New `ParagraphPropsDiffTest` and `ScrollViewPropsDiffTest`, using the payload helper from https://github.com/react/react-native/issues/59077. Each test fails on main. Built with the NDK and run on an Android emulator (there is no open-source gtest target). - RNTester on an Android emulator with the Props 2.0 flags forced on: the Playground screen below. **Remove `letterSpacing: 8`**
BeforeAfter
https://github.com/user-attachments/assets/235c115d-f73d-49e5-8eb0-367d3bbabb33 https://github.com/user-attachments/assets/285709c7-3d62-423d-acb6-81387fbae7cc
**Focus an input below the fold with `scrollsChildToFocus={false}`**
BeforeAfter
https://github.com/user-attachments/assets/89002966-e9f1-45bb-81e9-1c2ee296fea9 https://github.com/user-attachments/assets/e13ad50f-da40-44bd-9997-405fa2c84575
**Paging ScrollView with `snapToAlignment="start"`**
BeforeAfter
https://github.com/user-attachments/assets/154fcd54-bb89-44da-ab3d-efa69cad5ec1 https://github.com/user-attachments/assets/97d42c24-4713-4e2d-9dd4-2cba7305d0cd
**Remove `snapToAlignment="center"`**
BeforeAfter
https://github.com/user-attachments/assets/267b1100-c9dd-4f0d-a698-0ebbf634117b https://github.com/user-attachments/assets/838ea3f5-1fd2-409c-8a05-9af0feb60468
**One fling with `decelerationRate={0.998}`**
BeforeAfter
https://github.com/user-attachments/assets/bb0ddc8c-129f-4c66-b813-09e08bb81d68 https://github.com/user-attachments/assets/5ccf4582-d0ef-41d0-a8cb-272572d96739
**Remove `nestedScrollEnabled` from the inner ScrollView**
BeforeAfter
https://github.com/user-attachments/assets/068e48b9-943a-4370-9c0f-79160b5b5a02 https://github.com/user-attachments/assets/d1c9721c-2cd1-44b0-9178-0a117c714358
**Remove `sendMomentumEvents`**
BeforeAfter
https://github.com/user-attachments/assets/1aaea9ef-2865-46ee-8f87-80aef21cd28c https://github.com/user-attachments/assets/3c623dca-15d5-49ac-86b6-e3ed4ff04864
RNTesterPlayground.js: paste into packages/rn-tester/js/examples/Playground/RNTesterPlayground.js and open Playground. ```js /** * 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. * * flow strict-local * format */ import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; import * as React from 'react'; import {useRef, useState} from 'react'; import { Button, ScrollView, StyleSheet, Text, TextInput, View, } from 'react-native'; import ScrollViewNativeComponent from 'react-native/Libraries/Components/ScrollView/ScrollViewNativeComponent'; function log(demo: string, data: {...}) { console.log('P49 ' + JSON.stringify({demo, ...data})); } const COLORS = ['#ffcdd2', '#c8e6c9', '#bbdefb', '#fff9c4', '#e1bee7']; function Items({count, height}: {count: number, height: number}) { return Array.from({length: count}, (_, i) => ( item {i}, top {i * height} )); } // pagingEnabled, items 180 tall in a 400 tall ScrollView. A slow drag of // ~290 then release snaps to the nearest: item top (start: 360), item centred // (center: 250) or a viewport page (disabled: 400). function Snap({initial, next}: {initial: ?string, next: ?string}) { const ref = useRef>(null); const [alignment, setAlignment] = useState(initial); const [y, setY] = useState(0); const [endY, setEndY] = useState(null); const name = 'snap-' + String(initial); return (
Reviewed By: javache Differential Revision: D125438443 Pulled By: bartlomiejbloniarz fbshipit-source-id: b08abf57a6bffe66929cdd7b31294dcfd5d8ff2c --- .../ScrollView/RCTScrollViewComponentView.mm | 3 +- .../scrollview/BaseScrollViewProps.cpp | 7 +- .../scrollview/BaseScrollViewProps.h | 10 ++- .../HostPlatformScrollViewProps.cpp | 33 +++++---- .../scrollview/HostPlatformScrollViewProps.h | 1 + .../tests/ScrollViewPropsDiffTest.cpp | 70 +++++++++++++++++++ .../components/text/BaseTextProps.cpp | 6 +- .../text/tests/ParagraphPropsDiffTest.cpp | 30 ++++++++ .../ReactAndroidFrameworksCxx.api | 4 +- .../api-snapshots/ReactAppleFrameworksCxx.api | 3 +- .../ReactCommonFrameworksCxx.api | 3 +- 11 files changed, 145 insertions(+), 25 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollViewPropsDiffTest.cpp create mode 100644 packages/react-native/ReactCommon/react/renderer/components/text/tests/ParagraphPropsDiffTest.cpp diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index d9e3b901824..6f84e8e24a4 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -413,7 +413,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; diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.cpp index cb5407ea853..5b6f2863435 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.cpp @@ -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, diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.h index 3a3ebbe84b9..4e4b5d44785 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.h +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/BaseScrollViewProps.h @@ -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 snapToAlignment{}; bool disableIntervalMomentum{false}; std::vector snapToOffsets{}; bool snapToStart{true}; diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.cpp index a9a83ef67db..2aa02d73854 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.cpp @@ -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; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.h index 7ac321ec89f..49fcc1cb7f7 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.h +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/platform/android/react/renderer/components/scrollview/HostPlatformScrollViewProps.h @@ -38,6 +38,7 @@ class HostPlatformScrollViewProps : public BaseScrollViewProps { folly::dynamic fadingEdgeLength{}; std::string overScrollMode{"auto"}; SharedColor endFillColor{clearColor()}; + bool scrollsChildToFocus{true}; #pragma mark - DebugStringConvertible diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollViewPropsDiffTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollViewPropsDiffTest.cpp new file mode 100644 index 00000000000..8c46c2b2db8 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollViewPropsDiffTest.cpp @@ -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 + +#include +#include + +#ifdef RN_SERIALIZABLE_STATE + +namespace facebook::react { + +namespace { + +using folly::dynamic; + +void expectPayloads(const std::vector& commits) { + expectPayloadsMatchRawProps(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 diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp index 135463aa2d0..407445b5a6b 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/text/BaseTextProps.cpp @@ -13,6 +13,8 @@ #include #include +#include + 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) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/tests/ParagraphPropsDiffTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/tests/ParagraphPropsDiffTest.cpp new file mode 100644 index 00000000000..96ede0ca8e1 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/text/tests/ParagraphPropsDiffTest.cpp @@ -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 + +#include +#include + +#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({ + {dynamic::object("letterSpacing", 4.0)}, + {dynamic::object("letterSpacing", nullptr)}, + }); +} + +} // namespace facebook::react + +#endif diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index 02764f509d5..18ddfbbbca4 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -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 maintainVisibleContentPosition; + public std::optional snapToAlignment; public std::vector 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; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index 535f7407c0a..aebfecbad2b 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -4026,8 +4026,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 maintainVisibleContentPosition; + public std::optional snapToAlignment; public std::vector snapToOffsets; public void setProp(const facebook::react::PropsParserContext& context, facebook::react::RawPropsPropNameHash hash, const char* propName, const facebook::react::RawValue& value); } diff --git a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api index e85fe6f6fe5..7837bbde743 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api @@ -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 maintainVisibleContentPosition; + public std::optional snapToAlignment; public std::vector snapToOffsets; public void setProp(const facebook::react::PropsParserContext& context, facebook::react::RawPropsPropNameHash hash, const char* propName, const facebook::react::RawValue& value); }