From 7ca8a48318b99b64fdfedfe8a98074d3d0be2e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 26 Oct 2026 09:59:43 -0700 Subject: [PATCH] Warn only when Fantom root creation changes global dimensions Summary: Avoid repeated warnings when a new root matches the current global dimensions, even if older roots have different metrics. Continue warning when another root is alive and the new root changes the current metrics. Changelog: [Internal] Compare Fantom root dimensions against current global metrics. Differential Revision: D127606984 --- .../react-native-fantom/__docs__/README.md | 6 ++--- .../src/__tests__/FantomDimensions-itest.js | 7 ++++-- private/react-native-fantom/src/index.js | 25 +++++++++---------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/private/react-native-fantom/__docs__/README.md b/private/react-native-fantom/__docs__/README.md index bb93983ac021..0a6d73d07e5d 100644 --- a/private/react-native-fantom/__docs__/README.md +++ b/private/react-native-fantom/__docs__/README.md @@ -95,9 +95,9 @@ describe('My feature', () => { Creating a root sets global `Dimensions` for both `window` and `screen` from its `viewportWidth`, `viewportHeight`, `devicePixelRatio`, and optional `fontScale` (default `1`). `useWindowDimensions()` and `PixelRatio` report the new root's -values. If another root is still alive with different display metrics, Fantom -applies the new values and emits a warning. Call `root.destroy()` when a root is -no longer needed. +values. If another root is still alive and the new metrics differ from the +current global dimensions, Fantom applies the new values and emits a warning. +Call `root.destroy()` when a root is no longer needed. For a full API reference, please see the [inline documentation](../src/index.js) defined for the methods in the `@react-native/fantom` [module](../src/index.js). diff --git a/private/react-native-fantom/src/__tests__/FantomDimensions-itest.js b/private/react-native-fantom/src/__tests__/FantomDimensions-itest.js index dc287128aff4..edb818f9aac6 100644 --- a/private/react-native-fantom/src/__tests__/FantomDimensions-itest.js +++ b/private/react-native-fantom/src/__tests__/FantomDimensions-itest.js @@ -116,12 +116,15 @@ for (const [name, config] of conflictingConfigs) { }); } -it('checks all active roots, including one that differs from the latest root', () => { +it('compares new metrics only with the current global dimensions', () => { createRoot(); createRoot({viewportWidth: 640}); createRoot({viewportWidth: 640}); - expect(warn).toHaveBeenCalledTimes(2); + expect(warn).toHaveBeenCalledTimes(1); expect(Dimensions.get('window').width).toBe(640); + createRoot(); + expect(warn).toHaveBeenCalledTimes(2); + expect(Dimensions.get('window').width).toBe(390); }); it('does not warn for a destroyed root', () => { diff --git a/private/react-native-fantom/src/index.js b/private/react-native-fantom/src/index.js index af451e6c2ab9..98937065dc6c 100644 --- a/private/react-native-fantom/src/index.js +++ b/private/react-native-fantom/src/index.js @@ -84,21 +84,20 @@ function pushDimensions( surfaceId: RootTag, displayMetrics: DisplayMetrics, ): void { + const previousDisplayMetrics = Dimensions.get('window'); activeRootDisplayMetrics.set(surfaceId, displayMetrics); Dimensions.set({window: displayMetrics, screen: displayMetrics}); - for (const metrics of activeRootDisplayMetrics.values()) { - if ( - metrics.width !== displayMetrics.width || - metrics.height !== displayMetrics.height || - metrics.scale !== displayMetrics.scale || - metrics.fontScale !== displayMetrics.fontScale - ) { - console.warn( - 'Fantom.createRoot() updated global Dimensions while another active root has different display metrics. Dimensions and PixelRatio now reflect the new root.', - {existingRoot: metrics, newRoot: displayMetrics}, - ); - break; - } + if ( + activeRootDisplayMetrics.size > 1 && + (previousDisplayMetrics.width !== displayMetrics.width || + previousDisplayMetrics.height !== displayMetrics.height || + previousDisplayMetrics.scale !== displayMetrics.scale || + previousDisplayMetrics.fontScale !== displayMetrics.fontScale) + ) { + console.warn( + 'Fantom.createRoot() updated global Dimensions while another active root has different display metrics. Dimensions and PixelRatio now reflect the new root.', + {existingRoot: previousDisplayMetrics, newRoot: displayMetrics}, + ); } }