Skip to content

Use a stable sort for MVCP anchor ordering on iOS - #59562

Open
Abbondanzo wants to merge 2 commits into
react:mainfrom
Abbondanzo:export-D127875487
Open

Abbondanzo wants to merge 2 commits into
react:mainfrom
Abbondanzo:export-D127875487

Conversation

@Abbondanzo

Copy link
Copy Markdown
Contributor

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

hsource and others added 2 commits October 27, 2026 12:01
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
@meta-cla meta-cla 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 Oct 27, 2026
@meta-codesync

meta-codesync Bot commented Oct 27, 2026

Copy link
Copy Markdown

@Abbondanzo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D127875487.

@github-actions

Copy link
Copy Markdown

Visual regression

1 visual difference(s) at 576088d264c5. Visual differences need maintainer approval. Review the report, then apply visual-change-approved.

android: no capture

ios button/default-styling/default: Changed, 38223 px (1.533%)

diff

Expected and Captured
Expected Captured
expected captured

This branch has not been deployed

No deployments
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. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants