Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ - (instancetype)initWithFrame:(CGRect)frame
// and keeps it as an opt-in behavior.
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

// We intentionally force `UIScrollView`s `semanticContentAttribute` to `LTR` here
// because this attribute affects a position of vertical scrollbar; we don't want this
// scrollbar flip because we also flip it with whole `UIScrollView` flip.
// Left-to-right is the default. `semanticContentAttribute` decides on which side the vertical
// scroll indicator sits; RCTScrollViewComponentView keeps it left-to-right only while it mirrors
// the whole `UIScrollView` (a horizontal view in RTL) and switches it to right-to-left otherwise in RTL.
self.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;

__weak __typeof(self) weakSelf = self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,29 @@ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
oldLayoutMetrics:(const LayoutMetrics &)oldLayoutMetrics
{
[super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
if (layoutMetrics.layoutDirection != oldLayoutMetrics.layoutDirection) {
CGAffineTransform transform = (layoutMetrics.layoutDirection == LayoutDirection::LeftToRight)
? CGAffineTransformIdentity
: CGAffineTransformMakeScale(-1, 1);
[self _updateRightToLeftTransform];
}

// In RTL, a horizontal scroll view is mirrored (with its content view mirrored back) so it starts at the right edge.
// A vertical one is left as is: mirroring it also mirrors the views UIKit adds to the scroll view outside the
// content view, such as the navigation bar's large title on iOS 26.
- (BOOL)_isMirrored
{
const auto &scrollViewProps = static_cast<const ScrollViewProps &>(*_props);
return _layoutMetrics.layoutDirection == LayoutDirection::RightToLeft && scrollViewProps.horizontal;
}

- (void)_updateRightToLeftTransform
{
BOOL rtl = _layoutMetrics.layoutDirection == LayoutDirection::RightToLeft;
BOOL mirrored = [self _isMirrored];
CGAffineTransform transform = mirrored ? CGAffineTransformMakeScale(-1, 1) : CGAffineTransformIdentity;
if (!CGAffineTransformEqualToTransform(_scrollView.transform, transform)) {
_containerView.transform = transform;
_scrollView.transform = transform;
}
_scrollView.semanticContentAttribute = (rtl && !mirrored) ? UISemanticContentAttributeForceRightToLeft
: UISemanticContentAttributeForceLeftToRight;
}

- (bool)isInverted
Expand Down Expand Up @@ -463,6 +478,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
}

[super updateProps:props oldProps:oldProps];
[self _updateRightToLeftTransform];

// Must run after `super updateProps:` so it is based on the latest calculated view.
((RCTEnhancedScrollView *)_scrollView).inverted = [self isInverted];
Expand Down Expand Up @@ -614,7 +630,7 @@ - (BOOL)_shouldDisableScrollInteraction
metrics.timestamp = CACurrentMediaTime();
metrics.responderIgnoreScroll = !_isUserTriggeredScrolling;

if (_layoutMetrics.layoutDirection == LayoutDirection::RightToLeft) {
if ([self _isMirrored]) {
metrics.contentOffset.x = metrics.contentSize.width - metrics.containerSize.width - metrics.contentOffset.x;
}

Expand Down Expand Up @@ -705,7 +721,7 @@ - (void)prepareForRecycle

const auto &props = static_cast<const ScrollViewProps &>(*_props);
// Reset zoom scale to default. This must happen before resetting contentOffset:
// in RTL the mirroring transform makes UIKit report zoomScale == -1, and changing
// in horizontal RTL the mirroring transform makes UIKit report zoomScale == -1, and changing
// it to 1 makes UIKit move contentOffset.
_scrollView.zoomScale = 1.0;
_scrollView.contentOffset = RCTCGPointFromPoint(props.contentOffset);
Expand Down Expand Up @@ -1092,7 +1108,7 @@ - (void)scrollToOffset:(CGPoint)offset

- (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
{
if (_layoutMetrics.layoutDirection == LayoutDirection::RightToLeft) {
if ([self _isMirrored]) {
// Adjusting offset.x in right to left layout direction.
offset.x = self.contentSize.width - _scrollView.frame.size.width - offset.x;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
5C60EB1C226440DB0018C04F /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C60EB1B226440DB0018C04F /* AppDelegate.mm */; };
602160B9478DAFF2BAB56595 /* RCTUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D5D13FB1445C5C19639921CC /* RCTUtilsTests.m */; };
79B29C2E2E607A99007612A5 /* SceneDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 79B29C2D2E607A99007612A5 /* SceneDelegate.mm */; };
A1B2C3D4E5F60718293A4B5C /* RCTScrollViewComponentViewRTLTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D4E5F60718293A4B5D /* RCTScrollViewComponentViewRTLTests.mm */; };
7F806D4D4D97AD6403558B35 /* RCTStatusBarAppearanceTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */; };
A8717727467541429C262C63 /* RCTTimingClockChangeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */; };
8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */; };
Expand Down Expand Up @@ -89,6 +90,7 @@
4C706D402EE4AF9BE838CBA9 /* libPods-RNTester.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNTester.a"; sourceTree = BUILT_PRODUCTS_DIR; };
51BC9297B6C3163C14532020 /* Pods-RNTester.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTester.release.xcconfig"; path = "Target Support Files/Pods-RNTester/Pods-RNTester.release.xcconfig"; sourceTree = "<group>"; };
5C60EB1B226440DB0018C04F /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = RNTester/AppDelegate.mm; sourceTree = "<group>"; };
A1B2C3D4E5F60718293A4B5D /* RCTScrollViewComponentViewRTLTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTScrollViewComponentViewRTLTests.mm; sourceTree = "<group>"; };
67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTStatusBarAppearanceTests.mm; sourceTree = "<group>"; };
A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTTimingClockChangeTests.mm; sourceTree = "<group>"; };
79B29C2C2E607A99007612A5 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SceneDelegate.h; path = RNTester/SceneDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -319,6 +321,7 @@
E7DB20D022B2BAA5005AC45F /* RCTURLUtilsTests.m */,
D5D13FB1445C5C19639921CC /* RCTUtilsTests.m */,
E7DB20B322B2BAA4005AC45F /* RNTesterUnitTestsBundle.js */,
A1B2C3D4E5F60718293A4B5D /* RCTScrollViewComponentViewRTLTests.mm */,
67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */,
A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */,
);
Expand Down Expand Up @@ -639,6 +642,7 @@
E7DB20EB22B2BAA6005AC45F /* RCTConvert_YGValueTests.m in Sources */,
E7DB20D822B2BAA6005AC45F /* RCTJSONTests.m in Sources */,
602160B9478DAFF2BAB56595 /* RCTUtilsTests.m in Sources */,
A1B2C3D4E5F60718293A4B5C /* RCTScrollViewComponentViewRTLTests.mm in Sources */,
7F806D4D4D97AD6403558B35 /* RCTStatusBarAppearanceTests.mm in Sources */,
A8717727467541429C262C63 /* RCTTimingClockChangeTests.mm in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* 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.
*/

#import <XCTest/XCTest.h>

#import <React/RCTScrollViewComponentView.h>
#import <react/renderer/components/scrollview/ScrollViewEventEmitter.h>
#import <react/renderer/components/scrollview/ScrollViewProps.h>

using namespace facebook::react;

@interface RCTScrollViewComponentView (RCTScrollViewComponentViewRTLTests)
- (ScrollViewEventEmitter::Metrics)_scrollViewMetrics;
@end

static const CGSize kViewSize = {300, 60};
static const CGSize kContentSize = {900, 60};

static Props::Shared scrollViewProps(bool horizontal)
{
auto props = std::make_shared<ScrollViewProps>();
props->horizontal = horizontal;
return props;
}

static LayoutMetrics layoutMetrics(LayoutDirection direction)
{
LayoutMetrics metrics;
metrics.frame = facebook::react::Rect{
facebook::react::Point{0, 0}, facebook::react::Size{kViewSize.width, kViewSize.height}};
metrics.layoutDirection = direction;
return metrics;
}

@interface RCTScrollViewComponentViewRTLTests : XCTestCase
@end

@implementation RCTScrollViewComponentViewRTLTests

- (RCTScrollViewComponentView *)scrollViewWithHorizontal:(bool)horizontal direction:(LayoutDirection)direction
{
RCTScrollViewComponentView *view =
[[RCTScrollViewComponentView alloc] initWithFrame:CGRect{CGPointZero, kViewSize}];
[view updateProps:scrollViewProps(horizontal) oldProps:view.props];
[view updateLayoutMetrics:layoutMetrics(direction) oldLayoutMetrics:EmptyLayoutMetrics];
[self setContentSize:kContentSize ofView:view];
return view;
}

- (void)setContentSize:(CGSize)contentSize ofView:(RCTScrollViewComponentView *)view
{
// The content size normally comes from the ScrollView state.
[view setValue:[NSValue valueWithCGSize:contentSize] forKey:@"contentSize"];
view.scrollView.contentSize = contentSize;
}

- (void)assertMirrored:(RCTScrollViewComponentView *)view
{
XCTAssertTrue(CGAffineTransformEqualToTransform(view.scrollView.transform, CGAffineTransformMakeScale(-1, 1)));
XCTAssertTrue(CGAffineTransformEqualToTransform(view.containerView.transform, CGAffineTransformMakeScale(-1, 1)));
XCTAssertEqual(view.scrollView.semanticContentAttribute, UISemanticContentAttributeForceLeftToRight);
}

- (void)assertNotMirroredWithRTLSemantics:(RCTScrollViewComponentView *)view
{
XCTAssertTrue(CGAffineTransformIsIdentity(view.scrollView.transform));
XCTAssertTrue(CGAffineTransformIsIdentity(view.containerView.transform));
XCTAssertEqual(view.scrollView.semanticContentAttribute, UISemanticContentAttributeForceRightToLeft);
}

- (void)testRTLHorizontalScrollViewIsMirrored
{
[self assertMirrored:[self scrollViewWithHorizontal:true direction:LayoutDirection::RightToLeft]];
}

- (void)testRTLVerticalScrollViewIsNotMirroredAndHasRTLSemantics
{
[self assertNotMirroredWithRTLSemantics:[self scrollViewWithHorizontal:false direction:LayoutDirection::RightToLeft]];
}

- (void)testLTRScrollViewsAreNotMirrored
{
for (bool horizontal : {true, false}) {
RCTScrollViewComponentView *view = [self scrollViewWithHorizontal:horizontal
direction:LayoutDirection::LeftToRight];
XCTAssertTrue(CGAffineTransformIsIdentity(view.scrollView.transform));
XCTAssertEqual(view.scrollView.semanticContentAttribute, UISemanticContentAttributeForceLeftToRight);
}
}

- (void)testRTLHorizontalScrollToRoundTrips
{
RCTScrollViewComponentView *view = [self scrollViewWithHorizontal:true direction:LayoutDirection::RightToLeft];

[view scrollToOffset:CGPointMake(0, 0) animated:NO];
// x = 0 is the right edge of the mirrored content.
XCTAssertEqual(view.scrollView.contentOffset.x, kContentSize.width - kViewSize.width);
XCTAssertEqual([view _scrollViewMetrics].contentOffset.x, 0);

[view scrollToOffset:CGPointMake(200, 0) animated:NO];
XCTAssertEqual(view.scrollView.contentOffset.x, kContentSize.width - kViewSize.width - 200);
XCTAssertEqual([view _scrollViewMetrics].contentOffset.x, 200);
}

- (void)testRTLVerticalScrollToRoundTrips
{
RCTScrollViewComponentView *view = [self scrollViewWithHorizontal:false direction:LayoutDirection::RightToLeft];

[view scrollToOffset:CGPointMake(200, 0) animated:NO];
XCTAssertEqual(view.scrollView.contentOffset.x, 200);
XCTAssertEqual([view _scrollViewMetrics].contentOffset.x, 200);

[view scrollToOffset:CGPointMake(0, 0) animated:NO];
XCTAssertEqual(view.scrollView.contentOffset.x, 0);
XCTAssertEqual([view _scrollViewMetrics].contentOffset.x, 0);
}

- (void)testRecycledScrollViewFollowsChangedHorizontal
{
RCTScrollViewComponentView *view = [self scrollViewWithHorizontal:true direction:LayoutDirection::RightToLeft];
[self assertMirrored:view];

[view prepareForRecycle];
[view updateProps:scrollViewProps(false) oldProps:view.props];
[view updateLayoutMetrics:layoutMetrics(LayoutDirection::RightToLeft) oldLayoutMetrics:EmptyLayoutMetrics];
[self setContentSize:kContentSize ofView:view];
[self assertNotMirroredWithRTLSemantics:view];
[view scrollToOffset:CGPointMake(200, 0) animated:NO];
XCTAssertEqual([view _scrollViewMetrics].contentOffset.x, 200);

[view prepareForRecycle];
[view updateProps:scrollViewProps(false) oldProps:view.props];
[view updateLayoutMetrics:layoutMetrics(LayoutDirection::LeftToRight) oldLayoutMetrics:EmptyLayoutMetrics];
XCTAssertTrue(CGAffineTransformIsIdentity(view.scrollView.transform));
XCTAssertEqual(view.scrollView.semanticContentAttribute, UISemanticContentAttributeForceLeftToRight);

[view prepareForRecycle];
[view updateProps:scrollViewProps(true) oldProps:view.props];
[view updateLayoutMetrics:layoutMetrics(LayoutDirection::RightToLeft) oldLayoutMetrics:EmptyLayoutMetrics];
[self setContentSize:kContentSize ofView:view];
[self assertMirrored:view];
[view scrollToOffset:CGPointMake(200, 0) animated:NO];
XCTAssertEqual(view.scrollView.contentOffset.x, kContentSize.width - kViewSize.width - 200);
XCTAssertEqual([view _scrollViewMetrics].contentOffset.x, 200);
}

@end
Loading