Skip to content

Fix inverted flat list - #44168

Closed
kosmydel wants to merge 6 commits into
react:mainfrom
kosmydel:@kosmydel/fix-inverted-flat-list
Closed

Fix inverted flat list#44168
kosmydel wants to merge 6 commits into
react:mainfrom
kosmydel:@kosmydel/fix-inverted-flat-list

Conversation

@kosmydel

@kosmydelkosmydel commented Apr 19, 2024

Copy link
Copy Markdown
Contributor

Summary:

This PR solves this issue.
Inverted FlatList doesn't work (elements cannot be clicked) when the list is scrolled.

Changelog:

[GENERAL] [FIXED] - Fix clicking items on the inverted FlatList on the new architecture

Test Plan:

  1. Use this reproducer: https://github.com/WoLewicki/reproducer-react-native/tree/%40wolewicki/flatlist-inverted
  2. Apply changes from this PR & build the app.
  3. Scroll a bit the list, so it changes the position.
  4. The onPress should be fired when the button is clicked.
  5. Do the following tests:
  6. Add a horizontal prop to the FlatList - verify everything works.
  7. Remove a inverted prop - verify everything works.
  8. Remove a inverted prop and add a horizontal prop - verify everything works.
  9. Test different combinations of transforms of the FlatList, example:
<FlatListinvertedhorizontalstyle={{transform: [{scaleY: -1},{scaleY: -2},{scaleY: -0.5},{translateY: 20},{translateY: -10},{skewX: '10deg'},{rotateX: '10deg'},],}}/>
Reproducrer
react-native-flat-list.mov
RN tester
android-rn-tester.mov

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Hi @kosmydel!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@cipolleschi

Copy link
Copy Markdown
Contributor

/rebase - this comment rebase the PR on top of main automatically

@cipolleschicipolleschi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The code looks good to me. I assume that inverting the list actually apply a transformation that is not applied to the content origin offset, right?

I wonder how many other instance of similar issues we have sprinkled around. 🤔

@github-actions
github-actionsBotforce-pushed the @kosmydel/fix-inverted-flat-list branch from 4e59e0e to 8fc31f6CompareApril 19, 2024 16:38
@kosmydel

Copy link
Copy Markdown
ContributorAuthor

The code looks good to me. I assume that inverting the list actually apply a transformation that is not applied to the content origin offset, right?

That's right.

I still need to test it before opening it for review, as for now I tested it only in the @WoLewicki reproducer.

@roryabrahamroryabraham left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We tested this in a release build of the Expensify app and verified that it solves the problem for us 👍🏼

@facebook-github-botfacebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 19, 2024
@facebook-github-bot

Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@cipolleschi

Copy link
Copy Markdown
Contributor

/rebase - this comment rebase the PR on top of main automatically

@github-actions
github-actionsBotforce-pushed the @kosmydel/fix-inverted-flat-list branch from 8fc31f6 to 520ddb4CompareApril 20, 2024 15:51

@cipolleschicipolleschi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

CI is red with an invalid operation. I suspect that this is usually a warning, but we have a "treat warnings as errors" build flag, so it fails in CI.

@NickGerlemanNickGerleman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See above

@kosmydel

Copy link
Copy Markdown
ContributorAuthor

I've addressed the review and solved the mentioned issues

@analysis-bot

analysis-bot commented Apr 22, 2024

Copy link
Copy Markdown
PlatformEngineArchSize (bytes)Diff
androidhermesarm64-v8a20,364,276-142
androidhermesarmeabi-v7an/a--
androidhermesx86n/a--
androidhermesx86_64n/a--
androidjscarm64-v8a23,569,540+1
androidjscarmeabi-v7an/a--
androidjscx86n/a--
androidjscx86_64n/a--

Base commit: 6a3a305
Branch: main

@kosmydel
kosmydel marked this pull request as ready for review April 22, 2024 11:51
@facebook-github-botfacebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 22, 2024
@NickGerleman

NickGerleman commented Apr 22, 2024

Copy link
Copy Markdown
Contributor

I think this maybe should be adjusted for transformOrigin now that we have that (@intergalacticspacehighway do you know the math we should be doing here?) Otherwise this lgtm

This might end up interacting with #43192 more broadly I think, since we need to after resolve transforms relative to view size.

return {-contentOffset.x, -contentOffset.y + stateData.scrollAwayPaddingTop};

const auto& props = getConcreteProps();
auto result = props.transform * Vector{-contentOffset.x, -contentOffset.y, 0, 1};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto result = props.transform * Vector{-contentOffset.x, -contentOffset.y, 0, 1};
auto result = props.resolveTransform(layoutMetrics_) * Vector{-contentOffset.x, -contentOffset.y, 0, 1};

cc - @NickGerleman this should take the transformOrigin into consideration

@intergalacticspacehighway

Copy link
Copy Markdown
Contributor

This might end up interacting with #43192 more broadly I think, since we need to after resolve transforms relative to view size.

I think as long as we use resolveTransform and layout metrics are correct, we should be good.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@NickGerlemanNickGerleman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I took a closer look at the function where we are doing this, and found a couple more potential issues:

  1. Making getContentOriginOffset() return transformed offset breaks the contract assumed by DOM code
  2. This is called by getRelativeLayoutMetrics() which can be configured whether or not to use a transform. So we can't assume all callers there want a transform either

I.e. we're doing this transform at the wrong layer I think

@kosmydel

Copy link
Copy Markdown
ContributorAuthor

Hey, today I won't have time to investigate it.

we're doing this transform at the wrong layer I think

Could you provide more details on where the change should be made? This would be helpful.

@NickGerleman

Copy link
Copy Markdown
Contributor

How did we find this function as the original culprit? I.e. how do we call it for hit testing in the issue case? That would be a clue.

@WoLewicki

Copy link
Copy Markdown
Contributor

How do we call it for hit testing in the issue case:

We call it when adding offset for elements that are inside the ScrollView: https://github.com/facebook/react-native/blob/c884d19a501081ffbc4b353d751ecc3e32d6f624/packages/react-native/ReactCommon/react/renderer/core/LayoutableShadowNode.cpp#L224

@NickGerleman

Copy link
Copy Markdown
Contributor

@sammy-SC wrote this code it looks like, but I can see that calculateTransformedFrames() already has some logic for transforming origin based on transform (though, seems special cased instead of using the matrix)? So it seems like there is an assumption there as well, that the returned origin is not yet transformed, but that caller should transform it.

We could maybe pass through policy for whether to include transform to this API, like some others. Because some code calling this (e.g. scroll offset) which rely on it not being transformed, but some other code calling it which already seems to try to apply transforms (and it sounds like one case, where we are forgetting to do this).

@NickGerleman

NickGerleman commented Apr 23, 2024

Copy link
Copy Markdown
Contributor

I also went to experiment on DOM behavior of scrollTop API the above is using, by adding a translation to scrollable content. Note that this transform is applied to the content inside of the scrolling container.

image

The expectation is that the offset is really just how much has been scrolled, in the coordinate space of the scrolling layer. This means that e.g. translating the origin of the content down would not effect reported scrollTop start, but that content transforms can still indirectly effect scrollTop etc when they create more scrollable area.

Transforms against scrollview outer view, like in test plan, do not effect returned results.

@kosmydel

Copy link
Copy Markdown
ContributorAuthor

Hello, I made changes last week and applied a transform only where necessary. I would appreciate it if someone could review and confirm that these changes are what we want. Thank you.

@NickGerleman

Copy link
Copy Markdown
Contributor

@realsoelynn you were digging into this, right? Could you take a look at current revision?

@realsoelynn

Copy link
Copy Markdown
Member

Sorry about my late reply. Was busy with ReactConf and internal summit. I am still doing some more investigation.

@realsoelynn

Copy link
Copy Markdown
Member

Like @NickGerleman mentioned below, scrollAwayPaddingTop should be excluded for transform.

Calculation for transform for ScrollViewNode need to be overridden specifically as done in the commit b25d6fe071e77dc64e6ce1030e97fe504ba96433. However, we will need to avoid break to DOM endpoints like @NickGerlemanmentioned, i introduced includeTransform params in this PR #44822

Code Suggestion

Point ScrollViewShadowNode::getContentOriginOffset(
bool includeTransform) const {
auto stateData = getStateData();
auto contentOffset = stateData.contentOffset;
auto transform = includeTransform ? getTransform() : Transform::Identity();
auto result = transform * Vector{-contentOffset.x, -contentOffset.y, 0, 1};
return {result.x, result.y + stateData.scrollAwayPaddingTop};
}

I also went to experiment on DOM behavior of scrollTop API the above is using, by adding a translation to scrollable content. Note that this transform is applied to the content inside of the scrolling container.

image The expectation is that the offset is really just how much has been scrolled, in the coordinate space of the scrolling layer. This means that e.g. translating the origin of the content down would not effect reported `scrollTop` start, but that content transforms can still indirectly effect scrollTop etc when they create more scrollable area.

Transforms against scrollview outer view, like in test plan, do not effect returned results.

@realsoelynn

Copy link
Copy Markdown
Member

@kosmydel Please address this code suggestion which is based on your original changes

Point ScrollViewShadowNode::getContentOriginOffset(
bool includeTransform) const {
auto stateData = getStateData();
auto contentOffset = stateData.contentOffset;
auto transform = includeTransform ? getTransform() : Transform::Identity();
auto result = transform * Vector{-contentOffset.x, -contentOffset.y, 0, 1};
return {result.x, result.y + stateData.scrollAwayPaddingTop};
}

@kosmydel
kosmydelforce-pushed the @kosmydel/fix-inverted-flat-list branch 2 times, most recently from 41d97fb to 59fe663CompareJune 11, 2024 18:30
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@realsoelynn has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@realsoelynn

Copy link
Copy Markdown
Member

/rebase - this comment rebase the PR on top of main automatically

@realsoelynn

Copy link
Copy Markdown
Member

/rebase

@realsoelynn

Copy link
Copy Markdown
Member

@kosmydel Please help us rebase this PR so that we can do intake. Thanks in advance

realsoelynnand others added 6 commits June 12, 2024 08:53
…sform or not (react#44822)
Summary:
Pull Request resolved: react#44822
Changelog: [Internal]
This is to make `getContentOriginOffset` to have `includeTransform` information passed during Layout computation.
Differential Revision: D58223380
@kosmydel
kosmydelforce-pushed the @kosmydel/fix-inverted-flat-list branch from 7c4d603 to 0094773CompareJune 12, 2024 07:27
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@realsoelynn has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-botfacebook-github-bot added the Merged This PR has been merged. label Jun 12, 2024
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@realsoelynn merged this pull request in 3753b7a.

@github-actions

Copy link
Copy Markdown

This pull request was successfully merged by @kosmydel in 3753b7a.

When will my fix make it into a release? | How to file a pick request?

@realsoelynn

Copy link
Copy Markdown
Member

Thank you everyone for contributing to fix Inverted FlatList 🥳

@roryabrahamroryabraham mentioned this pull request Aug 27, 2024
50 tasks
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.MergedThis PR has been merged.Shared with MetaApplied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@kosmydel@facebook-github-bot@cipolleschi@analysis-bot@NickGerleman@intergalacticspacehighway@WoLewicki@realsoelynn@roryabraham