From db36716f738a5c8e4a391feaea75630bdc830169 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:41:18 -0800 Subject: [PATCH] Let iOS scroll views scroll inside a non-blocking JS responder On iOS a scroll view does not scroll while one of its ancestors is the JavaScript responder: `RCTScrollViewComponentView` answers NO from `touchesShouldCancelInContentView:`, and a `UIScrollView` that may not cancel its content's touches does not begin dragging. This is meant for a gesture that JavaScript owns, such as a `PanResponder` around a list. `setIsJSResponder` also says whether the responder blocks native responders, which is the value `onResponderGrant` returns. `PanResponder` blocks by default; `Pressability` blocks only when `blockNativeResponder` is set. `RCTMountingManager` dropped that argument, so every responder blocked. A scroll view inside a `Pressable` or a `Touchable` could not be dragged from content that does not itself take the responder, such as plain text, because the press had made the ancestor the responder. On Android the same tree scrolls. Pass the argument on to the component view as `blocksNativeResponder`, and have the scroll view stop only for an ancestor that blocks. The scroll then begins, the `ScrollView` takes the responder, and the press is terminated, as it is for a `Pressable` inside a scroll view. Co-Authored-By: Claude Fable 5.2 --- .../ScrollView/RCTScrollViewComponentView.mm | 8 ++--- .../View/RCTViewComponentView.mm | 12 +++++++ .../Mounting/RCTComponentViewProtocol.h | 8 +++++ .../Fabric/Mounting/RCTMountingManager.mm | 1 + .../Mounting/UIView+ComponentViewProtocol.h | 2 ++ .../Mounting/UIView+ComponentViewProtocol.mm | 11 ++++++ .../RCTScrollViewComponentViewTests.mm | 35 +++++++++++++++++++ .../api-snapshots/ReactAppleDebugCxx.api | 3 ++ .../api-snapshots/ReactAppleFrameworksCxx.api | 3 ++ .../api-snapshots/ReactAppleNewarchCxx.api | 3 ++ .../api-snapshots/ReactAppleReleaseCxx.api | 3 ++ 11 files changed, 85 insertions(+), 4 deletions(-) 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 1353b88019b8..a37301656aaa 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -620,16 +620,16 @@ - (void)unmountChildComponentView:(UIView *)childCompo /* * Returns whether or not the scroll view interaction should be blocked because - * JavaScript was found to be the responder. + * an ancestor is the JavaScript responder and blocks native responders. */ - (BOOL)_shouldDisableScrollInteraction { UIView *ancestorView = self.superview; while (ancestorView) { - if ([ancestorView respondsToSelector:@selector(isJSResponder)]) { - BOOL isJSResponder = ((UIView *)ancestorView).isJSResponder; - if (isJSResponder) { + if ([ancestorView respondsToSelector:@selector(blocksNativeResponder)]) { + BOOL blocksNativeResponder = ((UIView *)ancestorView).blocksNativeResponder; + if (blocksNativeResponder) { return YES; } } diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 02574182c301..d10f781ee7d2 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -116,6 +116,7 @@ @implementation RCTViewComponentView { NSMutableArray *_backgroundImageLayers; BOOL _needsInvalidateLayer; BOOL _isJSResponder; + BOOL _blocksNativeResponder; BOOL _removeClippedSubviews; NSMutableArray *_reactSubviews; NSSet *_Nullable _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN; @@ -847,6 +848,16 @@ - (void)setIsJSResponder:(BOOL)isJSResponder _isJSResponder = isJSResponder; } +- (BOOL)blocksNativeResponder +{ + return _blocksNativeResponder; +} + +- (void)setBlocksNativeResponder:(BOOL)blocksNativeResponder +{ + _blocksNativeResponder = blocksNativeResponder; +} + - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask { [super finalizeUpdates:updateMask]; @@ -896,6 +907,7 @@ - (void)prepareForRecycle _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil; _eventEmitter.reset(); _isJSResponder = NO; + _blocksNativeResponder = NO; _removeClippedSubviews = NO; _reactSubviews = [NSMutableArray new]; _layoutMetrics = EmptyLayoutMetrics; diff --git a/packages/react-native/React/Fabric/Mounting/RCTComponentViewProtocol.h b/packages/react-native/React/Fabric/Mounting/RCTComponentViewProtocol.h index 3ffc91da343a..4f8b2b6f87b9 100644 --- a/packages/react-native/React/Fabric/Mounting/RCTComponentViewProtocol.h +++ b/packages/react-native/React/Fabric/Mounting/RCTComponentViewProtocol.h @@ -122,6 +122,14 @@ typedef NS_OPTIONS(NSInteger, RNComponentViewUpdateMask) { - (BOOL)isJSResponder; - (void)setIsJSResponder:(BOOL)isJSResponder; +/* + * Whether the view is the JavaScript responder and asked to block native + * responders, such as an enclosing scroll view, when it was granted the + * responder (the `blockNativeResponder` argument of `setIsJSResponder`). + */ +- (BOOL)blocksNativeResponder; +- (void)setBlocksNativeResponder:(BOOL)blocksNativeResponder; + /* * This is broken. Do not use. */ diff --git a/packages/react-native/React/Fabric/Mounting/RCTMountingManager.mm b/packages/react-native/React/Fabric/Mounting/RCTMountingManager.mm index 68662f06d59b..5513137194a8 100644 --- a/packages/react-native/React/Fabric/Mounting/RCTMountingManager.mm +++ b/packages/react-native/React/Fabric/Mounting/RCTMountingManager.mm @@ -279,6 +279,7 @@ - (void)setIsJSResponder:(BOOL)isJSResponder RCTExecuteOnMainQueue(^{ UIView *componentView = [self->_componentViewRegistry findComponentViewWithTag:reactTag]; [componentView setIsJSResponder:isJSResponder]; + [componentView setBlocksNativeResponder:isJSResponder && blockNativeResponder]; }); } diff --git a/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.h b/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.h index bcdb72deacd8..b5def68f3ebb 100644 --- a/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.h +++ b/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.h @@ -43,6 +43,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)setIsJSResponder:(BOOL)isJSResponder; +- (void)setBlocksNativeResponder:(BOOL)blocksNativeResponder; + - (void)setPropKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN:(nullable NSSet *)props; - (nullable NSSet *)propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN; diff --git a/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm b/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm index 36e5f64f4b19..b59c2f3a45e4 100644 --- a/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm +++ b/packages/react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm @@ -151,6 +151,17 @@ - (void)setIsJSResponder:(BOOL)isJSResponder // Default implementation does nothing. } +- (BOOL)blocksNativeResponder +{ + // Default implementation always returns `NO`. + return NO; +} + +- (void)setBlocksNativeResponder:(BOOL)blocksNativeResponder +{ + // Default implementation does nothing. +} + - (void)setPropKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN:(nullable NSSet *)propKeys { // Default implementation does nothing. diff --git a/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm b/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm index eb9f977dca63..8e27313c6f0a 100644 --- a/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm @@ -5,9 +5,13 @@ * LICENSE file in the root directory of this source tree. */ +#import +#import #import +#import #import #import +#import #import #import #import @@ -19,6 +23,7 @@ #import #import #import +#import #import #import @@ -228,6 +233,36 @@ - (void)testAutomaticallyAdjustKeyboardInsetsAcrossRecycling XCTAssertEqual(view.scrollView.contentInset.bottom, 50); } +- (void)testAncestorJSResponderDisablesScrollingOnlyWhileItBlocksNativeResponder +{ + RCTMountingManager *mountingManager = [RCTMountingManager new]; + RCTComponentViewRegistry *registry = mountingManager.componentViewRegistry; + [registry.componentViewFactory registerComponentViewClass:[RCTViewComponentView class]]; + + facebook::react::ShadowView ancestorShadowView{}; + ancestorShadowView.tag = 2; + UIView *ancestor = + [registry dequeueComponentViewWithComponentHandle:[RCTViewComponentView componentDescriptorProvider].handle + tag:ancestorShadowView.tag] + .view; + RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; + [ancestor addSubview:view]; + UIView *contentView = [UIView new]; + + [mountingManager setIsJSResponder:YES blockNativeResponder:NO forShadowView:ancestorShadowView]; + XCTAssertTrue([view.scrollView touchesShouldCancelInContentView:contentView]); + + [mountingManager setIsJSResponder:YES blockNativeResponder:YES forShadowView:ancestorShadowView]; + XCTAssertFalse([view.scrollView touchesShouldCancelInContentView:contentView]); + + [mountingManager setIsJSResponder:NO blockNativeResponder:YES forShadowView:ancestorShadowView]; + XCTAssertTrue([view.scrollView touchesShouldCancelInContentView:contentView]); + + [mountingManager setIsJSResponder:YES blockNativeResponder:YES forShadowView:ancestorShadowView]; + [ancestor prepareForRecycle]; + XCTAssertTrue([view.scrollView touchesShouldCancelInContentView:contentView]); +} + // XCTest assertion macros use variadic reporting and catch framework exceptions by value. // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference) - (void)testSafeAreaCorrectionPreservesPullToRefreshInset diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 5fb34f123790..af23827515b0 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -240,6 +240,7 @@ category UIView(ComponentViewProtocol) { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateClippedSubviewsWithClipRect:relativeToView:(CGRect clipRect, UIView* clipView); @@ -2575,6 +2576,7 @@ protocol RCTComponentViewFactoryComponentProvider : public NSObject { } protocol RCTComponentViewProtocol : public NSObject { + public virtual BOOL blocksNativeResponder(); public virtual BOOL isJSResponder(); public virtual facebook::react::Props::Shared props(); public virtual static facebook::react::ComponentDescriptorProvider componentDescriptorProvider(); @@ -2584,6 +2586,7 @@ protocol RCTComponentViewProtocol : public NSObject { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateEventEmitter:(const facebook::react::EventEmitter::Shared& eventEmitter); diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index c191f3f7f57e..9e47dd0a5477 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -240,6 +240,7 @@ category UIView(ComponentViewProtocol) { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateClippedSubviewsWithClipRect:relativeToView:(CGRect clipRect, UIView* clipView); @@ -2633,6 +2634,7 @@ protocol RCTComponentViewFactoryComponentProvider : public NSObject { } protocol RCTComponentViewProtocol : public NSObject { + public virtual BOOL blocksNativeResponder(); public virtual BOOL isJSResponder(); public virtual facebook::react::Props::Shared props(); public virtual static facebook::react::ComponentDescriptorProvider componentDescriptorProvider(); @@ -2642,6 +2644,7 @@ protocol RCTComponentViewProtocol : public NSObject { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateEventEmitter:(const facebook::react::EventEmitter::Shared& eventEmitter); diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 9f2987431905..6e1b41696c7b 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -240,6 +240,7 @@ category UIView(ComponentViewProtocol) { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateClippedSubviewsWithClipRect:relativeToView:(CGRect clipRect, UIView* clipView); @@ -2568,6 +2569,7 @@ protocol RCTComponentViewFactoryComponentProvider : public NSObject { } protocol RCTComponentViewProtocol : public NSObject { + public virtual BOOL blocksNativeResponder(); public virtual BOOL isJSResponder(); public virtual facebook::react::Props::Shared props(); public virtual static facebook::react::ComponentDescriptorProvider componentDescriptorProvider(); @@ -2577,6 +2579,7 @@ protocol RCTComponentViewProtocol : public NSObject { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateEventEmitter:(const facebook::react::EventEmitter::Shared& eventEmitter); diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 1bfb2638d899..67cf92970d75 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -240,6 +240,7 @@ category UIView(ComponentViewProtocol) { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateClippedSubviewsWithClipRect:relativeToView:(CGRect clipRect, UIView* clipView); @@ -2575,6 +2576,7 @@ protocol RCTComponentViewFactoryComponentProvider : public NSObject { } protocol RCTComponentViewProtocol : public NSObject { + public virtual BOOL blocksNativeResponder(); public virtual BOOL isJSResponder(); public virtual facebook::react::Props::Shared props(); public virtual static facebook::react::ComponentDescriptorProvider componentDescriptorProvider(); @@ -2584,6 +2586,7 @@ protocol RCTComponentViewProtocol : public NSObject { public virtual void invalidate(); public virtual void mountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void prepareForRecycle(); + public virtual void setBlocksNativeResponder:(BOOL blocksNativeResponder); public virtual void setIsJSResponder:(BOOL isJSResponder); public virtual void unmountChildComponentView:index:(UIView* childComponentView, NSInteger index); public virtual void updateEventEmitter:(const facebook::react::EventEmitter::Shared& eventEmitter);