Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Fabric): back gesture activates Pressable elements #2131

Merged
merged 7 commits into from
May 15, 2024

Conversation

kkafar
Copy link
Member

@kkafar kkafar commented May 13, 2024

Description

When cancelling touches we previously relied on touchHandler instance being exposed by some react managed view above us (screen stack) in the view hierarchy (on Paper it was RCTRootContentView). However this changed with some recent Fabric version and no view exposes such property any longer, despite the fact that touch handler is obviously still utilised by these views even on Fabric, but the field has been removed from public API.

Currently on Fabric (as of version 0.74.1) RCTSurfaceTouchHandler is owned by instance of RCTFabricSurface owned by instance of RCTSurfaceView which we can expect to live above us in the view hierarchy (RN mounts it in the very beginning of application runtime).

To get access to the private touchHandler (which is a UIGestureRecognizer) we observe that it is attached to the RCTSurfaceView and thus it is retained in its gestureRecognizer array (native API).

Tip

We could potentially cache the instance of RCTSurfaceView, but for now I've come to a conclusion that this is an minor minor computation (there is only one gesture recognizer in the array & the parent lookup is O(log n) (I know this is not a balanced tree, but you know what I mean). However this is always an option for future.

Note

I thought of adding warning in case of RCTSurfaceView being not present above screen stack in view hierarchy but this might be the case with modal stack presentation.

Closes #2118

Changes

  • Added utility extensions on RCTTouchHandler & RCTSurfaceTouchHandler classes to make the code more expressive
  • Splited RNSScreenStackView.cancelTouchesInParent implementation into two separate for Paper & Fabric
  • In case of Fabric added lookup for RCTSurfaceView & got access to its touch handler.

Test code and steps to reproduce

Test2118

See #2118 for issue description.

Checklist

  • Included code example that can be used to test this change
  • Ensured that CI passes

@kkafar kkafar requested review from WoLewicki and tboba May 14, 2024 04:48
Copy link
Member

@tboba tboba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great change, thanks! Just left a few comments

FabricTestExample/src/Test654.js Outdated Show resolved Hide resolved
FabricTestExample/src/Test831.tsx Outdated Show resolved Hide resolved
TestsExample/App.js Show resolved Hide resolved
ios/RNSScreenStack.mm Outdated Show resolved Hide resolved
ios/utils/RCTTouchHandler+RNSUtility.mm Show resolved Hide resolved
ios/RNSScreenStack.mm Outdated Show resolved Hide resolved
@kkafar kkafar requested review from WoLewicki and tboba May 15, 2024 10:25
Copy link
Member

@tboba tboba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then

@kkafar kkafar merged commit fb0ce28 into main May 15, 2024
7 checks passed
@kkafar kkafar deleted the @kkafar/fix-pressable-issue-2118 branch May 15, 2024 13:08
kkafar added a commit that referenced this pull request Jul 2, 2024
…sted stack (#2223)

## Description

In #2193 I've made a mistake - on Paper, when there is no touch handler
held in `_touchHandler` field I've started
lookup for touch handler in ancestor chain from `self` -> which leads to
infinite loop when swiping back in nested-stack navigation scenario.


## Changes

* Started lookup from superview.


## Test code and steps to reproduce

`Test2223`

1. Navigate to NestedStack.
2. Navigate to NestedStack details screen.
3. Use swipe gesture to go-back


W/o this PR the application will crash hitting infinite loop.

Also test that this behaviour remains fixed:

* #2131

## Checklist

- [x] Included code example that can be used to test this change
- [x] Ensured that CI passes
ja1ns pushed a commit to WiseOwlTech/react-native-screens that referenced this pull request Oct 9, 2024
…nsion#2131)

## Description

When cancelling touches we previously relied on `touchHandler` instance
being exposed by some react managed view above us (screen stack) in the
view hierarchy (on Paper it was `RCTRootContentView`). However this
changed with some recent Fabric version and no view exposes such
property any longer, despite the fact that touch handler is obviously
still utilised by these views even on Fabric, but the field has been
removed from public API.

Currently on Fabric (as of version `0.74.1`) `RCTSurfaceTouchHandler` is
owned by instance of `RCTFabricSurface` owned by instance of
`RCTSurfaceView` which we can expect to live above us in the view
hierarchy (RN mounts it in the very beginning of application runtime).

To get access to the private touchHandler (which is a
`UIGestureRecognizer`) we observe that it is attached to the
`RCTSurfaceView` and thus it is retained in its `gestureRecognizer`
array (native API).

> [!tip]
> We could potentially cache the instance of `RCTSurfaceView`, but for
now I've come to a conclusion that this is an minor minor computation
(there is only one gesture recognizer in the array & the parent lookup
is O(log n) (I know this is not a balanced tree, but you know what I
mean). However this is always an option for future.

> [!note]
> I thought of adding warning in case of `RCTSurfaceView` being not
present above screen stack in view hierarchy but this might be the case
with `modal` stack presentation.

Closes software-mansion#2118

## Changes

* Added utility extensions on `RCTTouchHandler` &
`RCTSurfaceTouchHandler` classes to make the code more expressive
* Splited `RNSScreenStackView.cancelTouchesInParent` implementation into
two separate for Paper & Fabric
* In case of Fabric added lookup for `RCTSurfaceView` & got access to
its touch handler.


## Test code and steps to reproduce

`Test2118`

See software-mansion#2118 for issue description.

## Checklist

- [x] Included code example that can be used to test this change
- [x] Ensured that CI passes
ja1ns pushed a commit to WiseOwlTech/react-native-screens that referenced this pull request Oct 9, 2024
…sted stack (software-mansion#2223)

## Description

In software-mansion#2193 I've made a mistake - on Paper, when there is no touch handler
held in `_touchHandler` field I've started
lookup for touch handler in ancestor chain from `self` -> which leads to
infinite loop when swiping back in nested-stack navigation scenario.


## Changes

* Started lookup from superview.


## Test code and steps to reproduce

`Test2223`

1. Navigate to NestedStack.
2. Navigate to NestedStack details screen.
3. Use swipe gesture to go-back


W/o this PR the application will crash hitting infinite loop.

Also test that this behaviour remains fixed:

* software-mansion#2131

## Checklist

- [x] Included code example that can be used to test this change
- [x] Ensured that CI passes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[iOS] back gesture activates Pressable element
3 participants