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 1353b88019b..a37301656aa 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 02574182c30..d10f781ee7d 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 3ffc91da343..4f8b2b6f87b 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 68662f06d59..5513137194a 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 bcdb72deacd..b5def68f3eb 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 36e5f64f4b1..b59c2f3a45e 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 eb9f977dca6..8e27313c6f0 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 5fb34f12379..af23827515b 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 c191f3f7f57..9e47dd0a547 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 9f298743190..6e1b41696c7 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 1bfb2638d89..67cf92970d7 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);