From e59a1d252c9e69958989ef41d88418f194348983 Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Wed, 12 Aug 2026 12:37:41 -0700 Subject: [PATCH] fix(virtualized-lists): invalidate content length on orientation change (#57871) Summary: When a list changes between vertical and horizontal layouts, `ListMetricsAggregator` resets its cell measurements but keeps `_contentLength`. That value belongs to the old scrolling axis. In horizontal RTL mode, it can then be used to calculate a cell offset before the new content layout is observed. This clears `_contentLength` during horizontal orientation changes so RTL calculations wait for the new content length. The reset was present in the original orientation invalidation logic and was removed when bottom-up layout support was removed. ## Changelog: [GENERAL] [FIXED] - Invalidate list content length when orientation changes. Pull Request resolved: https://github.com/react/react-native/pull/57871 Test Plan: - Added regression coverage for vertical to horizontal content length invalidation. - Verified horizontal RTL metrics throw until new content layout is observed. - Ran `yarn jest packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js --runInBand`. - Ran Prettier and targeted ESLint checks. Reviewed By: christophpurrer Differential Revision: D115586644 Pulled By: fabriziocucci fbshipit-source-id: 6c0f1b435575c080444b72381f7ce56ae3f69a15 --- .../Lists/ListMetricsAggregator.js | 1 + .../__tests__/ListMetricsAggregator-test.js | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/virtualized-lists/Lists/ListMetricsAggregator.js b/packages/virtualized-lists/Lists/ListMetricsAggregator.js index 56d57e20fc0..b0516e6f7af 100644 --- a/packages/virtualized-lists/Lists/ListMetricsAggregator.js +++ b/packages/virtualized-lists/Lists/ListMetricsAggregator.js @@ -319,6 +319,7 @@ export default class ListMetricsAggregator { if (orientation.horizontal !== this._orientation.horizontal) { this._cellMetrics.clear(); this._averageCellLength = 0; + this._contentLength = null; this._highestMeasuredCellIndex = 0; this._measuredCellsLength = 0; this._measuredCellsCount = 0; diff --git a/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js b/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js index 7b5e416d419..36b50e88e86 100644 --- a/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js +++ b/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js @@ -986,6 +986,43 @@ describe('ListMetricsAggregator', () => { expect(listMetrics.getContentLength()).toBe(25); }); + it('invalidates content length when list orientation changes', () => { + const listMetrics = new ListMetricsAggregator(); + const verticalOrientation = {horizontal: false, rtl: false}; + const horizontalOrientation = {horizontal: true, rtl: false}; + const horizontalRtlOrientation = {horizontal: true, rtl: true}; + const cellLayout = {height: 50, width: 100, x: 0, y: 0}; + + listMetrics.notifyListContentLayout({ + layout: {height: 800, width: 400}, + orientation: verticalOrientation, + }); + expect(listMetrics.hasContentLength()).toBe(true); + + listMetrics.notifyCellLayout({ + cellIndex: 0, + cellKey: '0', + orientation: horizontalOrientation, + layout: cellLayout, + }); + expect(listMetrics.hasContentLength()).toBe(false); + + expect(() => + listMetrics.notifyCellLayout({ + cellIndex: 0, + cellKey: '0', + orientation: horizontalRtlOrientation, + layout: cellLayout, + }), + ).toThrow(); + + listMetrics.notifyListContentLayout({ + layout: {height: 50, width: 500}, + orientation: horizontalRtlOrientation, + }); + expect(listMetrics.getContentLength()).toBe(500); + }); + it('requires contentLength to resolve RTL metrics', () => { const listMetrics = new ListMetricsAggregator(); const orientation = {horizontal: true, rtl: true};