Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions private/react-native-fantom/__docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
25 changes: 12 additions & 13 deletions private/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
);
}
}

Expand Down
Loading