Repository navigation
Let iOS scroll views scroll inside a ScrollView JS responder - #60367
Conversation
db36716 to
1d6d309
Compare
chrfalch
left a comment
There was a problem hiding this comment.
This looks good to me: passing blockNativeResponder through to touchesShouldCancelInContentView fixes the root cause, and the new test drives the real mounting path. (Commenting rather than approving since it's still a draft.)
One request for the description: the change also affects nested scroll views. ScrollView can itself become the JS responder (e.g. via its keyboard-tap handling), and its grant handler doesn't return true, so it never blocks native responders. Before this PR, an inner scroll view would refuse to start scrolling while an outer ScrollView held the JS responder; after it, the inner one scrolls. Think of a horizontal carousel inside a vertical list, or a scrollable TextInput in a form.
That's probably the better behavior, and it matches Android, but it's a second behavior change beyond the Pressable case. Could you mention it in the description, and ideally check a nested scroll view on device?
javache
left a comment
There was a problem hiding this comment.
Thanks for fixing this. Let's call out that this brings API parity with Android, which already behaved this way (https://github.com/react/react-native/blob/main/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java#L1441)
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D135059323. |
zeyap
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
@javache Added to the description, with a link to The internal "Builds & Tests" check fails on this PR, and the internal linter needs action. Neither is visible outside Meta; could you tell me what they report? |
|
@chrfalch The description now has a paragraph on nested scroll views: a horizontal list inside a vertical one, and a scrollable multiline |
1d6d309 to
fb1f29d
Compare
|
@javache @chrfalch I checked this on a simulator and an Android emulator, and two things I told you were wrong. The branch and the description are updated, so please look again: the approval was for a different claim, and the change needs a new import. The first revision did not cancel the press. With a This is not parity with Android for a So for a @chrfalch The nested case you asked about is in that table, and the keyboard case changes as you described. A touch that lands while the outer list decelerates does not let the inner one scroll, before or after this change, so I removed that claim. I also removed the multiline |
chrfalch
left a comment
There was a problem hiding this comment.
Thanks for checking this on both platforms and for correcting the description. Your analysis holds up: when the target is inside the current responder, the responder system only offers the responder to views above it, so the ScrollView is never asked; and on Android JSResponderHandler intercepts for the responder's view regardless of blockNativeResponder.
I'd suggest narrowing this PR to the nested scroll view case. For a View/Pressable responder, this version makes iOS scroll and cancel where Android doesn't scroll and ends in a press. Scrolling may well be the better behavior, but changing it on one platform only creates a new iOS/Android difference that apps would have to handle. That deserves its own PR, ideally changing both platforms together. Narrowing looks small: in _shouldDisableScrollInteraction, keep blocking when the responder isn't an RCTScrollViewComponentView, and only cancel when the responder is a scroll view. Then both platforms agree in both cases.
Whichever way that goes, three things:
- Keep
cancelTouchesinternal.-[RCTSurfaceTouchHandler cancelTouches]is added to the public header, but its only caller is in the same module. A private header or category avoids committing to a public method. - It cancels every touch on the surface. A finger on an unrelated
PanResponder(say, a slider dragged with a second finger) is terminated too, without a termination request, and so is aPressablewithcancelable={false}. Can this be limited to the touches inside the scroll view? - Test the real cancel.
RCTCancelCountingTouchHandlerreplacescancelToucheswith a counter, so nothing exercises the actualreset: that JS getstouchcanceland that the next touch still works. A test that runs the real handler would cover the part most likely to break.
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` or a `Pressable` around a list. A `ScrollView` also takes the responder itself, for instance when a touch should dismiss the keyboard, and then the scroll views inside it could not be dragged: with the keyboard up, a drag on a horizontal list inside a vertical one did nothing, and the release dismissed the keyboard. On Android the inner list scrolls. Only a view group that is the responder takes its descendants' touches there (`JSResponderHandler`), and a scroll view is not one. Stop a scroll view only for an ancestor responder that is not a scroll view. A scroll view inside a `Pressable` or a `PanResponder` is unchanged, as on Android. When a drag or a pinch begins on a scroll view under a scroll view that is the responder, cancel the touches that began inside the inner scroll view, through the `RCTSurfaceTouchHandler` above the responder. The responder is then terminated, so the outer `ScrollView` does not dismiss the keyboard when the drag ends. The responder system does not do this: for an event that comes from inside the responder it offers the responder only to the views above it, so the inner `ScrollView` never gets `onScrollShouldSetResponder`. Android cancels at the same moment, in `NativeGestureUtil.notifyNativeGestureStarted`, and cancels every touch on the surface. Here a finger outside the inner scroll view keeps its touch. `cancelTouchesInView:` is declared in `RCTSurfaceTouchHandler+Internal.h` and is not public API. UIKit keeps reporting a touch until it ends, whatever the handler told JavaScript, so the touch and pointer handlers drop the later events of the touches they cancelled. Until those touches have ended they also keep from JavaScript a touch that begins inside the same view, which is part of the gesture the view took.
fb1f29d to
ed87f8e
Compare
chrfalch
left a comment
There was a problem hiding this comment.
Thanks for the rework, this addresses everything from my last comment:
- Only a scroll-view responder is cancelled; any other JS responder still blocks the nested scroll view.
cancelTouchesInView:now lives inRCTSurfaceTouchHandler+Internal.h, and the public header is unchanged.- Only touches that started inside the inner scroll view are cancelled, so an unrelated PanResponder or slider elsewhere on the surface keeps its gesture.
- The tests drive the real
RCTSurfaceTouchHandlerand assert on the dispatched events instead of a counting subclass.
The pinch check and the non-scroll-view responder case are covered too.
Two optional follow-ups, not blocking:
- The cancelled-touch bookkeeping (
_cancelledTouches,_cancelledTouchesView,_touchesWithoutTouchesBeganInCancelledView:,_touchesWithoutCancelledTouches:forgettingThem:) is duplicated betweenRCTSurfaceTouchHandler.mmandRCTSurfacePointerHandler.mm. A small shared helper would keep the two from drifting apart. - The pointer handler's cancel path has no test, only the simulator check. A test like the touch-handler one would protect it, since that handler asserts on touches it doesn't know about.
Looks good to me.
Visual regressionNo visual differences at android: no capture |
|
@chrfalch Thanks. I'll keep this PR as reviewed and take both follow-ups in a separate PR once it lands: a shared helper for the cancelled-touch bookkeeping, and a test of the pointer handler's cancel path. @javache The PR has changed since your import. It is narrowed to nested scroll views and no longer changes a public header, so it needs a new import. |
|
This pull request was successfully merged by @ide in 2c52501 When will my fix make it into a release? | How to file a pick request? |
Summary: react#60367 lets a scroll view inside a `ScrollView` that is the JavaScript responder cancel the touches that began inside it. `RCTSurfaceTouchHandler` and `RCTSurfacePointerHandler` each kept their own copy of the bookkeeping for those cancelled touches, and only the touch handler's cancel had a test. chrfalch's review of react#60367 suggested sharing the bookkeeping and testing the pointer handler (react#60367 (review)). This change moves the bookkeeping into `RCTCancelledTouches`, a C++ class both handlers hold. It is declared in `RCTCancelledTouches+Internal.h`, which the C++ API snapshots leave out. Behavior doesn't change. Changelog: [Internal] - Share the cancelled-touch bookkeeping of the iOS touch and pointer handlers, and test the pointer handler's cancel Test Plan: `RCTScrollViewComponentViewTests` gains `testDragUnderScrollViewJSResponderCancelsOnlyThePointersInsideTheScrollView`. It checks that a drag cancels only the pointers inside the inner scroll view, that the cancelled touch's later events are dropped, that a touch beginning inside waits until the cancelled one ends, and that `reset` clears the state. It and the existing touch-handler test pass before and after the move, and breaking each part of the bookkeeping makes one of them fail. I didn't run the change in RNTester. ```sh xcodebuild test -workspace packages/rn-tester/RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests \ -destination 'platform=iOS Simulator,name=iPhone 18 Pro' ``` RNTester's Podfile doesn't install the `React-RCTFabric` `Tests` spec, so open-source CI doesn't run these tests. `yarn format-check-cpp` passes. Signed-off-by: James Ide <ide@expo.dev>
Summary:
On iOS a scroll view does not scroll while one of its ancestors is the JavaScript responder:
RCTScrollViewComponentViewanswers NO fromtouchesShouldCancelInContentView:, and aUIScrollViewthat gets NO from that method does not begin dragging. This is meant for a gesture that JavaScript owns, such as aPanResponderor aPressablearound a list.A
ScrollViewalso takes the responder itself, for instance when a touch should dismiss the keyboard (with the defaultkeyboardShouldPersistTaps, or with"handled"when nothing inside handled the touch) or while it is animating (_handleStartShouldSetResponderCapture,_handleStartShouldSetResponder). The scroll views inside it then could not be dragged. With the keyboard up, a drag on a horizontal list inside a vertical one did nothing, and the release dismissed the keyboard. On Android the inner list scrolls and the keyboard stays.This change has two parts.
A scroll view stops only for an ancestor responder that is not a scroll view. That is Android's rule:
JSResponderHandler.onInterceptTouchEventgives the responder's view its descendants' touches, and only aReactViewGroupasks it; a scroll view does not. A scroll view inside aPressable, aTouchableor aPanResponderbehaves as before on both platforms: it does not scroll, and aPressablepresses on release.When a drag or a pinch begins on a scroll view under a scroll view that is the responder, the inner scroll view cancels the touches that began inside it. JavaScript receives
touchcancel(andpointercancelwhen W3C pointer events are on) for those touches, and the responder is terminated, so the outerScrollViewdoes not dismiss the keyboard when the drag ends. The responder system does not do this. For an event that comes from inside the responder, it offers the responder only to the views above the responder, so the innerScrollViewnever getsonScrollShouldSetResponder. Android cancels at the same moment, inNativeGestureUtil.notifyNativeGestureStarted.The cancel is narrower than Android's in one way. Android cancels every touch on the surface and sends JavaScript nothing more until all fingers lift (
JSTouchDispatcher.onChildStartedNativeGesture). Here only the inner scroll view's touches are affected. The touches that began inside it are cancelled, and until they have ended a finger that lands inside it is not reported to JavaScript either. A finger resting elsewhere keeps its touch and gets itstouchend. The responder is still terminated, because the responder system terminates it on anytouchcancel.How it is built:
-[RCTSurfaceTouchHandler cancelTouchesInView:]and-[RCTSurfacePointerHandler cancelPointersInView:]are new and internal, declared inRCTSurfaceTouchHandler+Internal.handRCTSurfacePointerHandler+Internal.h. No public header and no API snapshot changes.RCTSurfaceTouchHandlerlogs "Inconsistency between local and UIKit touch registries" for such a touch, andRCTSurfacePointerHandlerasserts. They add to that set, without reporting it, a touch that begins inside the same view while one of them is still down. Without that, the new finger makes the outerScrollViewtake the responder again and dismiss the keyboard.RCTSurfaceTouchHandler: the surface's view, aModal's view, or anInputAccessoryView's content view. The nearest one is the one that sent the responder its touches.zoomToRect) also reachesscrollViewWillBeginZooming:withView:. It takes no gesture, so the cancel there requires the pinch recognizer to be active.Not changed, and why:
setIsJSResponder'sblockNativeResponderargument is still dropped on iOS. On Android it keeps the responder's ancestors from intercepting (JSResponderHandler.setJSResponder); the iOS counterpart would stop the scroll views that enclose a blocking responder, which is a different change. It plays no part in what a responder does to the scroll views inside it, on either platform.ScrollViewinside aPressablestill does not scroll. Letting it scroll and cancelling the press may be the better behavior, but it would have to change on both platforms together, in a separate PR.Changelog:
[IOS] [FIXED] - A ScrollView inside a ScrollView that took the JS responder to dismiss the keyboard can be scrolled, as on Android, and the scroll cancels the JS touch
Test Plan:
Two tests in
RCTScrollViewComponentViewTestsare new. Both run the realRCTSurfaceTouchHandler. XCTest cannot create aUITouchthat carries a view and a phase, so the tests pass the handler'stouchesBegan:,touchesMoved:,touchesEnded:andtouchesCancelled:aUITouchsubclass that overrides the few properties the handler reads, and they read the events from anEventDispatcherlistener on each view'sTouchEventEmitter.testDragUnderScrollViewJSResponderCancelsOnlyTheTouchesInsideTheScrollView: with one touch inside the inner scroll view and one outside it,scrollViewWillBeginDragging:sendstopTouchCancelfor the first only; a touch that then begins inside sends nothing; the outside touch goes on totopTouchMoveandtopTouchEnd; the handler logs no warning for the later events of the touches it dropped; the next touch inside starts and ends as usual; and a zoom callback without a pinch cancels nothing.testViewJSResponderDisablesScrollingAndIsNotCancelled: under aViewresponder the scroll view still refuses to scroll and cancels nothing.Each of these makes a test fail: blocking under every responder, as on
main; removing the call inscrollViewWillBeginDragging:; cancelling every touch; not dropping the cancelled touch's later events, intouchesMoved:or intouchesCancelled:; reporting a touch that begins inside during the gesture; cancelling for a zoom without a pinch; cancelling under aViewresponder. The tests do not coverRCTSurfacePointerHandleror the cancel for a real pinch; the simulator check below does. Run with Xcode 27.1 on an iOS 27.0 simulator; RNTester builds:RNTester's Podfile does not install the
React-RCTFabricTeststest spec. To run it (locally, not in this change): add:testspecs => ['Tests']to that pod inscripts/cocoapods/fabric.rb, narrow the test spec'ssource_filesto this test file (RCTSelectableParagraphComponentViewTests.mmdoes not compile in open source), and add an empty Swift file to it so the bundle links the Swift runtimeRCTSwiftUIneeds.Checked on an iPhone 18 Pro simulator with iOS 27.0, on
mainand on this change, and on an Android 16 emulator at this change, which touches no Android code. A UI test (adb shell inputon Android) makes the gesture and reads counters that a page in RNTester's Playground shows: the inner list'scontentOffset,onPress, and thetouchcancelandtouchendevents that reach chosen views. No device was used. The nested case is a horizontalScrollViewinside a vertical one (defaultkeyboardShouldPersistTaps), with aTextInputfocused and the software keyboard up.The first row on the simulator, with
mainon the left and this change on the right. On the left the list stays put and the keyboard goes down on release; on the right the list scrolls and the keyboard stays:maintouchcancel; keyboard staystouchcancel; keyboard staystouchcancel, for the dragging finger; the resting finger gets itstouchend; keyboard staystouchcancel; keyboard staystouchcancel; keyboard staysscrollTotouchcanceltouchcancelfor some dragsScrollViewofTextrows inside aPressable: drag a rowViewwithPanResponder.create({onStartShouldSetPanResponder: () => true}).panHandlers: dragPressablerows inside aScrollView, nothing above it: dragtouchcanceltouchcancelIn the deceleration case the touch landed while the outer list was still moving (the page counts it), and the inner list did not scroll before or after, so that case does not change. In the animated
scrollTocase each run made three drags and the page kept the largest inner offset, not one per drag. Onmainit was 191 in one run and 382 in the other, so the list scrolled there. On this change it was 382 in both runs, and one drag in one run and two in the other sent atouchcancel: those began while the outerScrollViewheld the responder. Whethermainlet those particular drags scroll was not measured. With W3C pointer events turned on in a local build, the drag in the first two rows sends onepointercanceland the pinch sends two, and the pointer handler's assertions stay quiet.