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

Add support for viewOffset and viewPosition to maintainVisibleContentPosition #44863

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ryanlntn
Copy link
Contributor

@ryanlntn ryanlntn commented Jun 10, 2024

Summary:

When building a chat like application with FlatList maintainVisibleContentPosition works great but if you happen to get a particularly lengthy message (i.e. longer than the list container) it will scroll to the end instead of the top of the new message. This can be worked around with scrollToIndex({ animated: true, index: 0, viewPosition: 1 }) but requires an extra round trip that can make coordination tricky. This adds support for viewOffset and viewPosition to maintainVisibleContentPosition giving it the same level of control as scrollToIndex.

Changelog:

[GENERAL] [ADDED] - Add support for viewOffset and viewPosition to maintainVisibleContentPosition

Test Plan:

Here's a quick demo:

Simulator.Screen.Recording.-.iPhone.15.Pro.Max.-.2024-06-10.at.15.47.44.mp4

maintainVisibleContentPosition can now be modified with viewOffset and viewPosition just as in scrollToIndex.

<FlatList
  data={data ?? []}
  inverted
  maintainVisibleContentPosition={{
    autoscrollToTopThreshold: 9999,
    minIndexForVisible: 0,
    viewOffset: -16,
    viewPosition: 1,
  }}
  renderItem={({item, index}) => (<Item item={item} isFirst={index === 0} />)}
/>

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Infinite Red Partner: Infinite Red Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Jun 10, 2024
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 20,249,343 +3
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 23,454,886 -6
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: e686b43
Branch: main

@liylmn
Copy link

liylmn commented Jul 12, 2024

After adding mainVisibleContentPosition, adding data to the bottom of the invested flatlist iOS will cause shaking. Is there any good way to solve this problem? thanks

@ryanlntn
Copy link
Contributor Author

After adding mainVisibleContentPosition, adding data to the bottom of the invested flatlist iOS will cause shaking. Is there any good way to solve this problem? thanks

Are you resizing or reordering the existing items? That's typically when I saw glitchy behavior like that and is a known limitation of the current implementation of maintainVisibleContentPosition.

@liylmn
Copy link

liylmn commented Jul 12, 2024

@ryanlntn Thank you for your answer. My data is indeed reordered. I saw the official document has a solution. Can you share it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Infinite Red Partner: Infinite Red Partner Shared with Meta Applied 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.

None yet

4 participants