Skip to content

Commit e43dac7

Browse files
Fix measuring views inside a scrolled RTL ScrollView on iOS
In RTL, RCTScrollViewComponentView mirrors both the UIScrollView and its content view, and ScrollViewShadowNode moves the content container to x = 0. ScrollViewState::contentOffset holds the raw UIScrollView offset, which is measured from the opposite edge of the content in that layout, but getContentOriginOffset subtracted it as if it were an LTR offset. measure, measureInWindow, getBoundingClientRect and hit testing of views inside a horizontal RTL ScrollView were off by (contentWidth - viewportWidth) - 2 * contentOffset.x. getContentOriginOffset now converts the offset the same way the scroll events already do (contentWidth - viewportWidth - contentOffset.x) when the scroll view is RTL on iOS. CullingContext flipped the culling frame to work around the raw offset, so that flip is removed. In Fantom, scrollTo now stores the raw offset like iOS does, and the image renderer applies the same conversion, so events, measurement and screenshots agree.
1 parent 6144835 commit e43dac7

6 files changed

Lines changed: 101 additions & 15 deletions

File tree

‎packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,35 @@ describe('onScroll', () => {
261261
});
262262
});
263263
});
264+
265+
describe('horizontal ScrollView in RTL', () => {
266+
it('agrees on the scroll offset in onScroll, scrollLeft and child positions', () => {
267+
const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 100});
268+
const scrollViewRef = createRef<HostInstance>();
269+
const childRef = createRef<HostInstance>();
270+
const onScroll = jest.fn();
271+
272+
Fantom.runTask(() => {
273+
root.render(
274+
<ScrollView
275+
horizontal={true}
276+
onScroll={event => {
277+
onScroll(event.nativeEvent);
278+
}}
279+
ref={scrollViewRef}
280+
style={{direction: 'rtl', height: 100, width: 100}}>
281+
<View style={{height: 100, width: 100}} />
282+
<View ref={childRef} style={{height: 100, width: 100}} />
283+
<View style={{height: 100, width: 100}} />
284+
</ScrollView>,
285+
);
286+
});
287+
288+
Fantom.scrollTo(scrollViewRef, {x: 50, y: 0});
289+
290+
const [entry] = onScroll.mock.lastCall;
291+
expect(entry.contentOffset).toEqual({x: 50, y: 0});
292+
expect(nullthrows(scrollViewRef.current).scrollLeft).toBe(50);
293+
expect(nullthrows(childRef.current).getBoundingClientRect().x).toBe(50);
294+
});
295+
});

‎packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewShadowNode.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ Point ScrollViewShadowNode::getContentOriginOffset(
6868
bool includeTransform) const {
6969
auto stateData = getStateData();
7070
auto contentOffset = stateData.contentOffset;
71+
#ifndef ANDROID
72+
if (getLayoutMetrics().layoutDirection == LayoutDirection::RightToLeft) {
73+
// iOS mirrors both `scrollView` and `contentView` in RTL, so the native
74+
// offset is measured from the opposite edge of the content.
75+
contentOffset.x = stateData.getContentSize().width -
76+
getLayoutMetrics().frame.size.width - contentOffset.x;
77+
}
78+
#endif
7179
auto transform = includeTransform ? getTransform() : Transform::Identity();
7280
auto result = transform *
7381
Vector{

‎packages/react-native/ReactCommon/react/renderer/core/tests/LayoutableShadowNodeTest.cpp‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,52 @@ TEST(LayoutableShadowNodeTest, contentOriginOffset) {
262262
EXPECT_EQ(relativeLayoutMetrics.frame.origin.y, 20);
263263
}
264264

265+
#ifndef ANDROID
266+
/*
267+
* Horizontal RTL <ScrollView> 100 wide with 300 of content, scrolled 50 from
268+
* the left edge of the content in UIKit coordinates. The last <View> sits at
269+
* x=200 in the content and shows at x=50 on screen.
270+
*/
271+
TEST(LayoutableShadowNodeTest, contentOriginOffsetInRightToLeftScrollView) {
272+
auto builder = simpleComponentBuilder();
273+
auto childShadowNode = std::shared_ptr<ViewShadowNode>{};
274+
// clang-format off
275+
auto element =
276+
Element<ScrollViewShadowNode>()
277+
.finalize([](ScrollViewShadowNode &shadowNode){
278+
auto layoutMetrics = EmptyLayoutMetrics;
279+
layoutMetrics.frame.size = {.width=100, .height=100};
280+
layoutMetrics.layoutDirection = LayoutDirection::RightToLeft;
281+
shadowNode.setLayoutMetrics(layoutMetrics);
282+
})
283+
.stateData([](ScrollViewState &data) {
284+
data.contentOffset = {.x=50, .y=0};
285+
data.contentBoundingRect = {.size={.width=300, .height=100}};
286+
})
287+
.children({
288+
Element<ViewShadowNode>()
289+
.finalize([](ViewShadowNode &shadowNode){
290+
auto layoutMetrics = EmptyLayoutMetrics;
291+
layoutMetrics.frame.origin = {.x=200, .y=0};
292+
layoutMetrics.frame.size = {.width=100, .height=100};
293+
shadowNode.setLayoutMetrics(layoutMetrics);
294+
})
295+
.reference(childShadowNode)
296+
});
297+
// clang-format on
298+
299+
auto parentShadowNode = builder.build(element);
300+
301+
auto relativeLayoutMetrics =
302+
LayoutableShadowNode::computeRelativeLayoutMetrics(
303+
childShadowNode->getFamily(),
304+
*parentShadowNode,
305+
{.includeTransform = true});
306+
307+
EXPECT_EQ(relativeLayoutMetrics.frame.origin.x, 50);
308+
}
309+
#endif
310+
265311
/*
266312
* ┌────────────────────────┐
267313
* │<View> │

‎packages/react-native/ReactCommon/react/renderer/mounting/internal/CullingContext.cpp‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ CullingContext CullingContext::adjustCullingContextIfNeeded(
2626
if (scrollViewShadowNode->getConcreteProps().yogaStyle.overflow() !=
2727
yoga::Overflow::Visible &&
2828
!scrollViewShadowNode->getStateData().disableViewCulling) {
29-
auto layoutMetrics = scrollViewShadowNode->getLayoutMetrics();
3029
cullingContext.frame.origin =
3130
-scrollViewShadowNode->getContentOriginOffset(
3231
/* includeTransform */ true);
@@ -47,17 +46,6 @@ CullingContext CullingContext::adjustCullingContextIfNeeded(
4746
}
4847

4948
cullingContext.transform = Transform::Identity();
50-
51-
if (layoutMetrics.layoutDirection == LayoutDirection::RightToLeft) {
52-
// In RTL, content offset is flipped horizontally.
53-
// We need to flip the culling context frame to match.
54-
// See:
55-
// https://github.com/facebook/react-native/blob/c2f39cfdd87c32b9a59efe8a788b8a03f02b0ea0/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm#L579
56-
auto stateData = scrollViewShadowNode->getStateData();
57-
cullingContext.frame.origin.x =
58-
stateData.contentBoundingRect.size.width -
59-
layoutMetrics.frame.size.width - cullingContext.frame.origin.x;
60-
}
6149
} else {
6250
cullingContext = {};
6351
}

‎private/react-native-fantom/tester/src/NativeFantom.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,21 @@ void NativeFantom::enqueueScrollEvent(
222222

223223
scrollViewShadowNode->getConcreteEventEmitter().onScroll(scrollEvent);
224224

225+
auto contentOffset = point;
226+
if (scrollViewShadowNode->getLayoutMetrics().layoutDirection ==
227+
LayoutDirection::RightToLeft) {
228+
contentOffset.x = scrollEvent.contentSize.width -
229+
scrollEvent.containerSize.width - point.x;
230+
}
231+
225232
auto state =
226233
std::static_pointer_cast<const ScrollViewShadowNode::ConcreteState>(
227234
scrollViewShadowNode->getState());
228235
state->updateState(
229-
[point](const ScrollViewShadowNode::ConcreteState::Data& oldData)
236+
[contentOffset](const ScrollViewShadowNode::ConcreteState::Data& oldData)
230237
-> ScrollViewShadowNode::ConcreteState::SharedData {
231238
auto newData = oldData;
232-
newData.contentOffset = point;
239+
newData.contentOffset = contentOffset;
233240
return std::make_shared<
234241
const ScrollViewShadowNode::ConcreteState::Data>(newData);
235242
});

‎private/react-native-fantom/tester/src/render/ImageRenderer.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,12 @@ void paintView(
385385
const auto& state =
386386
static_cast<const ConcreteState<ScrollViewState>&>(*view.state)
387387
.getData();
388-
canvas.translate(-state.contentOffset.x, -state.contentOffset.y);
388+
auto contentOffset = state.contentOffset;
389+
if (metrics.layoutDirection == LayoutDirection::RightToLeft) {
390+
contentOffset.x = state.getContentSize().width -
391+
metrics.frame.size.width - contentOffset.x;
392+
}
393+
canvas.translate(-contentOffset.x, -contentOffset.y);
389394
}
390395
}
391396
for (const auto& child : view.children) {

0 commit comments

Comments
 (0)