Skip to content
Open
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
5 changes: 1 addition & 4 deletions packages/jest-preset/jest/mocks/NativeModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,9 @@ const NativeModules = {
removeListeners: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
},
I18nManager: {
allowRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
forceRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
swapLeftAndRightInRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
forceRTL: jest.fn(() => false) as JestMockFn<$FlowFixMe, $FlowFixMe>,
getConstants: (): $FlowFixMe => ({
isRTL: false,
doLeftAndRightSwapInRTL: true,
}),
},
};
Expand Down
7 changes: 1 addition & 6 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import * as React from 'react';
const ScrollView = require('../Components/ScrollView/ScrollView').default;
const View = require('../Components/View/View').default;
const AppContainer = require('../ReactNative/AppContainer').default;
const I18nManager = require('../ReactNative/I18nManager').default;
const {RootTagContext} = require('../ReactNative/RootTag');
const StyleSheet = require('../StyleSheet/StyleSheet').default;

Expand Down Expand Up @@ -367,17 +366,13 @@ class Modal extends React.Component<ModalProps, ModalState> {
}
}

const side = I18nManager.getConstants().isRTL ? 'right' : 'left';
const styles = StyleSheet.create({
modal: {
position: 'absolute',
},
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
* https://fburl.com/workplace/6291gfvu */
container: {
// $FlowFixMe[incompatible-type]
[side]: 0,
top: 0,
start: 0,
flex: 1,
backgroundColor: 'white',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {createRef} from 'react';
import {Modal} from 'react-native';

const DEFAULT_MODAL_CHILD_VIEW = (
<rn-view backgroundColor="rgba(255, 255, 255, 1)" flex="1" left="0" top="0" />
<rn-view
backgroundColor="rgba(255, 255, 255, 1)"
flex="1"
start="0"
top="0"
/>
);

describe('<Modal>', () => {
Expand Down
94 changes: 66 additions & 28 deletions packages/react-native/Libraries/ReactNative/I18nManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@
* @format
*/

import type {I18nManagerConstants} from './NativeI18nManager';

import warnOnce from '../Utilities/warnOnce';
import NativeI18nManager from './NativeI18nManager';

const i18nConstants: I18nManagerConstants = getI18nManagerConstants();
type I18nManagerConstants = {
/**
* @deprecated `left` and `right` style properties are no longer swapped in
* RTL. Use `start` and `end` style properties, such as `marginStart`,
* instead.
*/
doLeftAndRightSwapInRTL: boolean,
isRTL: boolean,
localeIdentifier?: ?string,
};

let i18nConstants: I18nManagerConstants = getI18nManagerConstants();

function getI18nManagerConstants(): I18nManagerConstants {
if (NativeI18nManager) {
const {isRTL, doLeftAndRightSwapInRTL, localeIdentifier} =
NativeI18nManager.getConstants();
return {isRTL, doLeftAndRightSwapInRTL, localeIdentifier};
return {
isRTL,
// Older native binaries still swap left and right styles in RTL
doLeftAndRightSwapInRTL: doLeftAndRightSwapInRTL ?? false,
localeIdentifier,
};
}

return {
isRTL: false,
doLeftAndRightSwapInRTL: true,
doLeftAndRightSwapInRTL: false,
};
}

Expand All @@ -38,45 +53,68 @@ export default {
},

/**
* Allows the app to opt in to RTL layout behavior. Should be called early
* in app startup. Takes effect on the next app reload.
* @deprecated Has no effect. Set `RCTSupportsRTL` to `true` in Info.plist on
* iOS, or `android:supportsRtl="true"` in AndroidManifest.xml on Android,
* instead.
*/
allowRTL: (shouldAllow: boolean) => {
if (!NativeI18nManager) {
return;
}

NativeI18nManager.allowRTL(shouldAllow);
allowRTL: (shouldAllow: boolean): void => {
warnOnce(
'I18nManager.allowRTL',
'I18nManager.allowRTL() is deprecated and has no effect. Set ' +
'`RCTSupportsRTL` to `true` in Info.plist on iOS, or ' +
'`android:supportsRtl="true"` in AndroidManifest.xml on Android, ' +
'instead.',
);
},

/**
* Forces the app into RTL layout mode, regardless of the device locale.
* Takes effect on the next app reload.
*/
forceRTL: (shouldForce: boolean) => {
forceRTL: (shouldForce: boolean): void => {
if (!NativeI18nManager) {
return;
}

NativeI18nManager.forceRTL(shouldForce);
const isRTL = NativeI18nManager.forceRTL(shouldForce);

// Older native binaries return nothing, and apply the change on the next reload
if (typeof isRTL === 'boolean') {
i18nConstants = {...i18nConstants, isRTL};
}
},

/**
* Controls whether `left`/`right` style properties are automatically swapped
* in RTL layouts. When enabled, `left` becomes `right` and vice versa.
* @deprecated Has no effect. Use `start` and `end` style properties, such as
* `marginStart`, instead.
*/
swapLeftAndRightInRTL: (flipStyles: boolean) => {
if (!NativeI18nManager) {
return;
}

NativeI18nManager.swapLeftAndRightInRTL(flipStyles);
swapLeftAndRightInRTL: (flipStyles: boolean): void => {
warnOnce(
'I18nManager.swapLeftAndRightInRTL',
'I18nManager.swapLeftAndRightInRTL() is deprecated and has no effect. ' +
'Use `start` and `end` style properties, such as `marginStart`, ' +
'instead.',
);
},

/** Whether the current layout direction is Right-to-Left. */
isRTL: i18nConstants.isRTL as I18nManagerConstants['isRTL'],
// $FlowFixMe[unsafe-getters-setters]
get isRTL(): I18nManagerConstants['isRTL'] {
return i18nConstants.isRTL;
},

/** Whether left and right style properties are swapped in RTL mode. */
doLeftAndRightSwapInRTL:
i18nConstants.doLeftAndRightSwapInRTL as I18nManagerConstants['doLeftAndRightSwapInRTL'],
/**
* @deprecated `left` and `right` style properties are no longer swapped in
* RTL. Use `start` and `end` style properties, such as `marginStart`,
* instead.
*/
// $FlowFixMe[unsafe-getters-setters]
get doLeftAndRightSwapInRTL(): I18nManagerConstants['doLeftAndRightSwapInRTL'] {
warnOnce(
'I18nManager.doLeftAndRightSwapInRTL',
'I18nManager.doLeftAndRightSwapInRTL is deprecated. `left` and `right` ' +
'style properties are no longer swapped in RTL. Use `start` and ' +
'`end` style properties, such as `marginStart`, instead.',
);
return i18nConstants.doLeftAndRightSwapInRTL;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {I18nManagerConstants} from '../NativeI18nManager';

const mockGetConstants: JestMockFn<[], I18nManagerConstants> = jest.fn();
const mockForceRTL: JestMockFn<[boolean], ?boolean> = jest.fn();

jest.mock('../NativeI18nManager', () => ({
__esModule: true,
default: {
getConstants: () => mockGetConstants(),
forceRTL: (forceRTL: boolean) => mockForceRTL(forceRTL),
},
}));

describe('I18nManager', () => {
beforeEach(() => {
jest.resetModules();
mockGetConstants.mockReset();
mockForceRTL.mockReset();
});

it('does not swap left and right styles when native does not send the constant', () => {
mockGetConstants.mockReturnValue({isRTL: true});
const I18nManager = require('../I18nManager').default;

expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(false);
});

it('reads doLeftAndRightSwapInRTL from older native binaries', () => {
mockGetConstants.mockReturnValue({
isRTL: true,
doLeftAndRightSwapInRTL: true,
});
const I18nManager = require('../I18nManager').default;

expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(true);
});

it('updates isRTL when forceRTL returns a boolean', () => {
mockGetConstants.mockReturnValue({isRTL: false});
mockForceRTL.mockImplementation(forceRTL => forceRTL);
const I18nManager = require('../I18nManager').default;

I18nManager.forceRTL(true);

expect(mockForceRTL).toHaveBeenCalledWith(true);
expect(mockGetConstants).toHaveBeenCalledTimes(1);
expect(I18nManager.isRTL).toBe(true);
expect(I18nManager.getConstants()).toEqual({
doLeftAndRightSwapInRTL: false,
isRTL: true,
});
});

it('keeps isRTL when forceRTL returns nothing on older native binaries', () => {
mockGetConstants.mockReturnValue({isRTL: false});
mockForceRTL.mockReturnValue(undefined);
const I18nManager = require('../I18nManager').default;

I18nManager.forceRTL(true);

expect(mockForceRTL).toHaveBeenCalledWith(true);
expect(I18nManager.isRTL).toBe(false);
});
});
27 changes: 18 additions & 9 deletions packages/react-native/React/CoreModules/RCTI18nManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <FBReactNativeSpec/FBReactNativeSpec.h>

#import <React/RCTI18nUtil.h>
#import <React/RCTSurfacePresenterStub.h>
#import "RCTI18nManager.h"

#import "CoreModulesPlugins.h"
Expand All @@ -17,7 +18,9 @@
@interface RCTI18nManager () <NativeI18nManagerSpec>
@end

@implementation RCTI18nManager
@implementation RCTI18nManager {
__weak id<RCTSurfacePresenterStub> _surfacePresenter;
}

RCT_EXPORT_MODULE()

Expand All @@ -26,19 +29,26 @@ + (BOOL)requiresMainQueueSetup
return NO;
}

- (void)allowRTL:(BOOL)value
- (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
{
[[RCTI18nUtil sharedInstance] allowRTL:value];
_surfacePresenter = surfacePresenter;
}

- (void)forceRTL:(BOOL)value
- (NSNumber *)forceRTL:(BOOL)value
{
BOOL wasRTL = [[RCTI18nUtil sharedInstance] isRTL];
[[RCTI18nUtil sharedInstance] forceRTL:value];
}
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];

- (void)swapLeftAndRightInRTL:(BOOL)value
{
[[RCTI18nUtil sharedInstance] swapLeftAndRightInRTL:value];
if (isRTL != wasRTL) {
// Returns before the relayout, so JS reads the new value before any layout event
id<RCTSurfacePresenterStub> surfacePresenter = _surfacePresenter;
dispatch_async(dispatch_get_main_queue(), ^{
[surfacePresenter updateLayoutDirectionOfSurfaces];
});
}

return @(isRTL);
}

- (NSDictionary *)constantsToExport
Expand All @@ -50,7 +60,6 @@ - (NSDictionary *)getConstants
{
return @{
@"isRTL" : @([[RCTI18nUtil sharedInstance] isRTL]),
@"doLeftAndRightSwapInRTL" : @([[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL])
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#import "RCTScrollContentViewComponentView.h"

#import <React/RCTI18nUtil.h>
#import <React/RCTUtils.h>
#import <react/featureflags/ReactNativeFeatureFlags.h>
#import <react/renderer/components/scrollview/ScrollContentViewComponentDescriptor.h>
Expand Down Expand Up @@ -53,11 +52,9 @@ - (void)_updateStateIfNecessary
return;
}

RCTI18nUtil *i18nUtil = [RCTI18nUtil sharedInstance];
ScrollContentViewState newData;
newData.safeAreaPadding = RCTRoundedEdgeInsets(_safeAreaPadding);
newData.contentInsetAdjustment = RCTRoundedEdgeInsets(_contentInsetAdjustment);
newData.swapLeftAndRightInRTL = i18nUtil.isRTL && i18nUtil.doLeftAndRightSwapInRTL;
if (newData == _lastStateData) {
return;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native/React/Fabric/RCTSurfacePresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag;

/*
* Lays out all surfaces again with the current layout direction. Must be called on the main thread.
*/
- (void)updateLayoutDirectionOfSurfaces;

- (void)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props;
- (void)schedulerDidSynchronouslyUpdateViewOnUIThread:(ReactTag)tag props:(folly::dynamic)props;

Expand Down
10 changes: 10 additions & 0 deletions packages/react-native/React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ - (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag
return [_surfaceRegistry surfaceForRootTag:rootTag];
}

- (void)updateLayoutDirectionOfSurfaces
{
RCTAssertMainQueue();
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
for (RCTFabricSurface *surface in enumerator) {
[surface updateLayoutContext];
}
}];
}

- (id<RCTSurfaceProtocol>)createFabricSurfaceForModuleName:(NSString *)moduleName
initialProperties:(NSDictionary *)initialProperties
{
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/React/Fabric/Surface/RCTFabricSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ NS_ASSUME_NONNULL_BEGIN

- (const facebook::react::SurfaceHandler &)surfaceHandler;

/**
* Lays out the Surface again with the current layout direction, screen scale
* and font size multiplier. Must be called on the main thread.
*/
- (void)updateLayoutContext;

@end

NS_ASSUME_NONNULL_END
Loading
Loading