From 4b41f3f6f0219a59a71da5c08404edf0c9aa7adf Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Mon, 30 Nov 2026 07:35:10 -0800 Subject: [PATCH] Apply safe area insets RTL-aware in RNTester and helloworld (#59645) Summary: Pull Request resolved: https://github.com/react/react-native/pull/59645 RNTester's platform test results view and the helloworld app pad themselves from `experimental_onSafeAreaInsetsChange`, mapping `insets.left`/`insets.right` straight to `paddingLeft`/`paddingRight`. Insets are physical edges, but Yoga treats `paddingLeft`/`paddingRight` as start/end when `swapLeftAndRightInRTL` is on, so in RTL a right-edge cutout was left unpadded while the left edge got its padding. Both callers now swap the left and right insets in that case, as the internal `SafeAreaView` already does. Changelog: [Internal] Reviewed By: andrewdacenko Differential Revision: D128337176 --- .../RNTesterPlatformTestResultView.js | 22 ++++++++++++----- private/helloworld/App.tsx | 8 +++++-- private/helloworld/__tests__/App.test.tsx | 24 +++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/packages/rn-tester/js/examples/Experimental/PlatformTest/RNTesterPlatformTestResultView.js b/packages/rn-tester/js/examples/Experimental/PlatformTest/RNTesterPlatformTestResultView.js index 5e0dc37146b5..337563b1d7dd 100644 --- a/packages/rn-tester/js/examples/Experimental/PlatformTest/RNTesterPlatformTestResultView.js +++ b/packages/rn-tester/js/examples/Experimental/PlatformTest/RNTesterPlatformTestResultView.js @@ -26,6 +26,7 @@ import {memo, useCallback, useMemo, useState} from 'react'; import { Button, FlatList, + I18nManager, KeyboardAvoidingView, Modal, Platform, @@ -241,6 +242,20 @@ export default function RNTesterPlatformTestResultView( }, [], ); + const safeAreaPaddingStyle = useMemo(() => { + if (safeAreaInsets == null) { + return null; + } + // Insets are physical edges, but Yoga treats paddingLeft/paddingRight as + // start/end when swapLeftAndRightInRTL is on. + const swap = I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL; + return { + paddingTop: safeAreaInsets.top, + paddingRight: swap ? safeAreaInsets.left : safeAreaInsets.right, + paddingBottom: safeAreaInsets.bottom, + paddingLeft: swap ? safeAreaInsets.right : safeAreaInsets.left, + }; + }, [safeAreaInsets]); const handleReset = useCallback(() => { setFilterFailStatus(false); @@ -285,12 +300,7 @@ export default function RNTesterPlatformTestResultView( height: '100%', flexDirection: 'column', }, - safeAreaInsets != null && { - paddingTop: safeAreaInsets.top, - paddingRight: safeAreaInsets.right, - paddingBottom: safeAreaInsets.bottom, - paddingLeft: safeAreaInsets.left, - }, + safeAreaPaddingStyle, ]}> diff --git a/private/helloworld/App.tsx b/private/helloworld/App.tsx index 19c0a8358ff8..0aa51a55b775 100644 --- a/private/helloworld/App.tsx +++ b/private/helloworld/App.tsx @@ -10,6 +10,7 @@ import * as React from 'react'; import {useState} from 'react'; import { + I18nManager, type SafeAreaInsets, ScrollView, StatusBar, @@ -22,15 +23,18 @@ import { function App(): React.ReactNode { const isDarkMode = useColorScheme() === 'dark'; const [insets, setInsets] = useState(null); + // Insets are physical edges, but Yoga treats paddingLeft/paddingRight as + // start/end when swapLeftAndRightInRTL is on. + const swap = I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL; return ( diff --git a/private/helloworld/__tests__/App.test.tsx b/private/helloworld/__tests__/App.test.tsx index 399fc98c4b41..0f2e3604b18d 100644 --- a/private/helloworld/__tests__/App.test.tsx +++ b/private/helloworld/__tests__/App.test.tsx @@ -9,6 +9,7 @@ import App from '../App'; import React from 'react'; +import {I18nManager, View} from 'react-native'; import ReactTestRenderer from 'react-test-renderer'; test('renders correctly', async () => { @@ -16,3 +17,26 @@ test('renders correctly', async () => { ReactTestRenderer.create(); }); }); + +test('pads the physical edges with the safe area insets in RTL', async () => { + jest.replaceProperty(I18nManager, 'isRTL', true); + + let renderer: ReactTestRenderer.ReactTestRenderer | undefined; + await ReactTestRenderer.act(() => { + renderer = ReactTestRenderer.create(); + }); + const getRootView = () => renderer!.root.findAllByType(View)[0]; + + await ReactTestRenderer.act(() => { + getRootView().props.experimental_onSafeAreaInsetsChange({ + nativeEvent: {insets: {top: 44, right: 0, bottom: 34, left: 59}}, + }); + }); + + expect(getRootView().props.style).toEqual({ + paddingTop: 44, + paddingRight: 59, + paddingBottom: 34, + paddingLeft: 0, + }); +});