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
6 changes: 2 additions & 4 deletions packages/jest-preset/jest/mocks/NativeModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,10 @@ 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,
localeIdentifier: 'en-US',
}),
},
};
Expand Down
10 changes: 1 addition & 9 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 @@ -368,20 +367,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[invalid-computed-prop] (>=0.111.0 site=react_native_fb) This
* comment suppresses an error found when Flow v0.111 was deployed. To see
* the error, delete this comment and run Flow. */
// $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
99 changes: 58 additions & 41 deletions packages/react-native/Libraries/ReactNative/I18nManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
* @format
*/

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

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

const i18nConstants: I18nManagerConstants = getI18nManagerConstants();

function getI18nManagerConstants(): I18nManagerConstants {
if (NativeI18nManager) {
const {isRTL, doLeftAndRightSwapInRTL, localeIdentifier} =
NativeI18nManager.getConstants();
return {isRTL, doLeftAndRightSwapInRTL, localeIdentifier};
}
type I18nManagerConstants = {
/**
* @deprecated Has no effect, and is always `false`. Use `start` and `end`
* style properties, such as `marginStart`, instead.
*/
doLeftAndRightSwapInRTL: boolean,
isRTL: boolean,
localeIdentifier: string,
};

return {
isRTL: false,
doLeftAndRightSwapInRTL: true,
};
}
let i18nConstants: I18nManagerConstants = {
doLeftAndRightSwapInRTL: false,
isRTL: false,
localeIdentifier: 'en-US',
...NativeI18nManager?.getConstants(),
};

/**
* Utilities for managing Right-to-Left (RTL) layout support.
Expand All @@ -38,45 +39,61 @@ 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) => {
if (!NativeI18nManager) {
return;
forceRTL: (shouldForce: boolean): void => {
if (NativeI18nManager != null) {
const isRTL = NativeI18nManager.forceRTL(shouldForce);
i18nConstants = {...i18nConstants, isRTL};
}

NativeI18nManager.forceRTL(shouldForce);
},

/**
* 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 Has no effect, and is always `false`. 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 and has no effect, it ' +
'is always false. Use `start` and `end` style properties, such as ' +
'`marginStart`, instead.',
);
return false;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 nativeConstants: I18nManagerConstants = {
isRTL: false,
localeIdentifier: 'en-US',
};

const nativeModule = {
getConstants: jest.fn(() => nativeConstants),
forceRTL: jest.fn((forceRTL: boolean) => forceRTL),
};

jest.mock('../NativeI18nManager', () => ({
__esModule: true,
default: nativeModule,
}));

describe('I18nManager', () => {
it('updates the constants when forceRTL returns', () => {
const I18nManager = require('../I18nManager').default;

I18nManager.forceRTL(true);

expect(nativeModule.forceRTL).toHaveBeenCalledWith(true);
expect(nativeModule.getConstants).toHaveBeenCalledTimes(1);
expect(I18nManager.isRTL).toBe(true);

expect(I18nManager.getConstants()).toEqual({
doLeftAndRightSwapInRTL: false,
isRTL: true,
localeIdentifier: 'en-US',
});
});
});
44 changes: 35 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,10 +60,26 @@ - (NSDictionary *)getConstants
{
return @{
@"isRTL" : @([[RCTI18nUtil sharedInstance] isRTL]),
@"doLeftAndRightSwapInRTL" : @([[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL])
@"localeIdentifier" : [self _localeIdentifier],
};
}

// BCP 47 language tag (e.g. "en-US", "zh-Hans-CN"), the same format as Android and the web
- (NSString *)_localeIdentifier
{
NSLocale *locale = [NSLocale currentLocale];
NSMutableArray<NSString *> *subtags = [NSMutableArray arrayWithObject:locale.languageCode];

if (locale.scriptCode != nil) {
[subtags addObject:locale.scriptCode];
}
if (locale.countryCode != nil) {
[subtags addObject:locale.countryCode];
}

return [subtags componentsJoinedByString:@"-"];
}

- (std::shared_ptr<TurboModule>)getTurboModule:(const ObjCTurboModule::InitParams &)params
{
return std::make_shared<NativeI18nManagerSpecJSI>(params);
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