Repository navigation
Use a stable sort for MVCP anchor ordering on iOS - #59562
Open
Abbondanzo wants to merge 2 commits into
Open
Abbondanzo wants to merge 2 commits into
Abbondanzo wants to merge 2 commits into
Conversation
Summary: ### Motivation At Wanderlog, we use a FlatList with negative zIndex so earlier items' bottom shadows can cover later items (by default, later items cover earlier items). When `maintainVisibleContentPosition` is enabled, the native helper selects the scroll anchor by scanning content children and picking the first one whose end position exceeds the current scroll offset. Fabric can reorder native children when views use `zIndex`, so hierarchy order no longer matches layout order. On both iOS and Android, the helper can then anchor to the wrong item, and scroll corrections keep the bottom edge of the visible content fixed instead of the top. When `minIndexForVisible > 0`, skipping early items also needs to follow layout order rather than native hierarchy order. ### Fix **iOS and Android:** Scan all eligible children and select the topmost visible anchor (the smallest end position still below the scroll offset). When `minIndexForVisible > 0`, sort children by layout position before applying the skip. Also clear the cached anchor when the helper stops listening. Ported from the approach in react#46247, adapted for the current Kotlin helper and Fabric iOS scroll view, and simplified. ## Changelog: [GENERAL] [FIXED] - Fix `maintainVisibleContentPosition` anchoring to the wrong child when z-index reorders Fabric children Pull Request resolved: react#57959 Test Plan: **Automated Android tests** ``` yarn install cd packages/rn-tester yarn start yarn android maestro test .maestro/flatlist-horizontal-inverted-maintainvisible.yml -e APP_ID=com.facebook.react.uiapp ``` **Automated Fantom tests** ``` yarn install yarn fantom ScrollView-maintainVisibleContentPosition-itest ``` **Manual test on iOS and Android using RNTester:** 1. Open **ScrollView** → **smooth bi-directional content loading** 2. Add a few items and scroll so there are items above and below the current item 3. Use **Change height at id** to change the height of an item in the middle 4. Verify the top edge of that item stays in place, rather than the bottom edge jumping ### Android | Before (maintained position relative to bottom) | After (correctly maintains position relative to top) | | --- | --- | | <img width="300" alt="Before edited" src="https://github.com/user-attachments/assets/de21122e-1fec-4937-a07d-b93253d83c17" /> | <img width="300" alt="After edited" src="https://github.com/user-attachments/assets/159cf12a-3936-459b-ba74-5e843bba14f0" /> | ### iOS | Before (maintained position relative to bottom) | After (correctly maintains position relative to top) | | --- | --- | | <img width="413" height="890" alt="MVCP before" src="https://github.com/user-attachments/assets/b3f85a72-6b96-4223-866a-6c7df3c65499" /> | <img width="413" height="890" alt="MVCP after" src="https://github.com/user-attachments/assets/c50a3c56-a49d-468f-9c8b-289639d1be50" /> | Reviewed By: andrewdacenko Differential Revision: D127603701 Pulled By: Abbondanzo
Summary: `_sortedContentSubviewsWithHorizontal:` broke ties between subviews at the same offset with `indexOfObject:`, a linear scan inside every tied comparison. Sort with `NSSortStable` instead, which keeps tied subviews in hierarchy order without the lookup. Changelog: [Internal] Differential Revision: D127875487
|
@Abbondanzo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D127875487. |
Visual regression1 visual difference(s) at android: no capture ios
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary:
_sortedContentSubviewsWithHorizontal:broke ties between subviews at the same offset withindexOfObject:, a linear scan inside every tied comparison. Sort withNSSortStableinstead, which keeps tied subviews in hierarchy order without the lookup.Changelog: [Internal]
Differential Revision: D127875487