Skip to content
Merged
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
8 changes: 6 additions & 2 deletions docs/6.x/docs/guides/rtl.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ On the Web, the RTL value is not set globally, unlike native platforms. `I18nMan
Then, let `react-native-paper` know about it by using the `direction` prop on `PaperProvider` or the `LocaleProvider` component to match the writing direction in your app.

:::note
The `direction` prop informs React Native Paper about the text direction in the app i.e. it doesn't change the text direction by itself. If you intend to support RTL languages, it's important to set this prop to the correct value that's configured in the app. If it doesn't match the actual text direction, the layout might be incorrect.
The `direction` prop of `PaperProvider` informs React Native Paper about the text direction in the app i.e. it doesn't change the text direction by itself. If you intend to support RTL languages, it's important to set this prop to the correct value that's configured in the app. If it doesn't match the actual text direction, the layout might be incorrect.
:::

## Setting direction for the whole app
Expand Down Expand Up @@ -60,13 +60,17 @@ import { LocaleProvider } from 'react-native-paper';
export default function ArabicSection({ children }) {
return (
<LocaleProvider direction="rtl">
{/* Components here will use RTL layout */}
{/* Paper's components here use the RTL direction */}
{children}
</LocaleProvider>
);
}
```

On the Web, `LocaleProvider` also lays out its part of the tree in that direction, including menus, dialogs and other content it opens in a `Portal`. It renders a wrapper with the `dir` attribute that doesn't take part in the layout, so you don't need to set `dir` yourself.

On Android and iOS, `I18nManager` sets the layout direction for the whole app, and `LocaleProvider` doesn't change it. It only changes the direction that Paper's components read, so rows and `start`/`end` styles in the subtree keep the app's direction.

## Reading the current direction

The direction is available in your own components via the `useLocale` hook:
Expand Down
11 changes: 8 additions & 3 deletions src/components/Portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as React from 'react';

import PortalConsumer from './PortalConsumer';
import PortalHost, { PortalContext } from './PortalHost';
import { LocaleProvider, useLocale } from '../../core/locale';
import {
DirectionProvider,
SubtreeDirectionContext,
useLocale,
} from '../../core/locale';
import {
SettingsContext,
Provider as SettingsProvider,
Expand Down Expand Up @@ -51,6 +55,7 @@ export type Props = {
const Portal = ({ children, modal, theme: themeOverrides }: Props) => {
const theme = useInternalTheme(themeOverrides);
const { direction } = useLocale();
const setsLayout = React.useContext(SubtreeDirectionContext);
const settings = React.useContext(SettingsContext);
const manager = React.useContext(PortalContext);
const reduceMotion = React.useContext(ReduceMotionContext);
Expand All @@ -59,11 +64,11 @@ const Portal = ({ children, modal, theme: themeOverrides }: Props) => {
<PortalConsumer manager={manager} modal={modal}>
<SettingsProvider value={settings}>
<ReduceMotionContext.Provider value={reduceMotion}>
<LocaleProvider direction={direction}>
<DirectionProvider direction={direction} setsLayout={setsLayout}>
<InternalThemeProvider theme={theme}>
{children}
</InternalThemeProvider>
</LocaleProvider>
</DirectionProvider>
</ReduceMotionContext.Provider>
</SettingsProvider>
</PortalConsumer>
Expand Down
10 changes: 7 additions & 3 deletions src/core/PaperProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react';

import { getDefaultDirection, LocaleProvider, type Direction } from './locale';
import {
DirectionProvider,
getDefaultDirection,
type Direction,
} from './locale';
import SafeAreaProviderCompat from './SafeAreaProviderCompat';
import { Provider as SettingsProvider } from './settings';
import type { Settings } from './settings';
Expand Down Expand Up @@ -68,11 +72,11 @@ const PaperProvider = (props: Props) => {
<PortalHost>
<SettingsProvider value={settingsValue}>
<ReduceMotionContext.Provider value={resolvedReduceMotion}>
<LocaleProvider direction={direction}>
<DirectionProvider direction={direction}>
<InternalThemeProvider theme={theme}>
{children}
</InternalThemeProvider>
</LocaleProvider>
</DirectionProvider>
</ReduceMotionContext.Provider>
</SettingsProvider>
</PortalHost>
Expand Down
76 changes: 76 additions & 0 deletions src/core/__tests__/LocaleProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Platform, View } from 'react-native';

import {
afterEach,
beforeEach,
describe,
expect,
it,
jest,
} from '@jest/globals';
import { render, screen } from '@testing-library/react-native';

import Portal from '../../components/Portal/Portal';
import { LocaleProvider } from '../locale';
import PaperProvider from '../PaperProvider';

// react-native-web renders `dir` as the attribute that lays out the element's
// children in that direction.
const parentOf = (testID: string) => screen.getByTestId(testID).parent;

const renderSubtree = () =>
render(
<PaperProvider>
<LocaleProvider direction="rtl">
<View testID="content" />
<Portal>
<View testID="portal-content" />
</Portal>
</LocaleProvider>
</PaperProvider>
);

describe.each(['web', 'ios', 'android'] as const)('on %s', (os) => {
let platformOS: { restore(): void } | undefined;

beforeEach(() => {
platformOS = jest.replaceProperty(Platform, 'OS', os);
});

afterEach(() => {
platformOS?.restore();
});

if (os === 'web') {
it('lays out a LocaleProvider subtree and its portal content in its direction', async () => {
await renderSubtree();
await screen.findByTestId('portal-content');

expect(parentOf('content')).toHaveProp('dir', 'rtl');
expect(parentOf('portal-content')).toHaveProp('dir', 'rtl');
});
} else {
it('leaves the layout direction of a LocaleProvider subtree to the app', async () => {
await renderSubtree();
await screen.findByTestId('portal-content');

expect(parentOf('content')).not.toHaveProp('dir');
expect(parentOf('portal-content')).not.toHaveProp('dir');
});
}

it('leaves the layout direction to the app with PaperProvider alone', async () => {
await render(
<PaperProvider direction="rtl">
<View testID="content" />
<Portal>
<View testID="portal-content" />
</Portal>
</PaperProvider>
);
await screen.findByTestId('portal-content');

expect(parentOf('content')).not.toHaveProp('dir');
expect(parentOf('portal-content')).not.toHaveProp('dir');
});
});
62 changes: 58 additions & 4 deletions src/core/locale.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { I18nManager } from 'react-native';
import { I18nManager, Platform, StyleSheet, View } from 'react-native';

/**
* Writing direction of the app.
Expand All @@ -15,18 +15,66 @@ export const LocaleContext = React.createContext<LocaleContextValue | null>(
null
);

/**
* Whether the nearest direction comes from a `LocaleProvider`, which also sets
* the layout direction of its subtree on web. `Portal` reads it to carry that
* layout direction to the portal host.
*/
export const SubtreeDirectionContext = React.createContext(false);

export type LocaleProviderProps = {
direction: Direction;
children: React.ReactNode;
};

type DirectionProviderProps = LocaleProviderProps & {
/**
* Whether to lay the children out in `direction` on web. `PaperProvider`
* leaves the layout direction of the whole app to the page (`<html dir>`).
*/
setsLayout?: boolean;
};

/**
* Provider component for locale configuration.
* Provides the direction to Paper's components.
*/
export function LocaleProvider({ direction, children }: LocaleProviderProps) {
export function DirectionProvider({
direction,
setsLayout = false,
children,
}: DirectionProviderProps) {
const value = React.useMemo(() => ({ direction }), [direction]);
return (
<LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>
<LocaleContext.Provider value={value}>
<SubtreeDirectionContext.Provider value={setsLayout}>
{Platform.OS === 'web' && setsLayout ? (
// react-native-web reads `dir` for `start` and `end` styles, and the
// browser for rows and text. `display: contents` keeps the wrapper
// out of the layout.
<View
// @ts-expect-error: `ViewProps` doesn't expose the web-specific `dir` prop.
dir={direction}
style={styles.contents}
>
{children}
</View>
) : (
children
)}
</SubtreeDirectionContext.Provider>
</LocaleContext.Provider>
);
}

/**
* Overrides the direction for a part of the tree. On web it also lays that
* part out in the direction, including content it opens in a `Portal`.
*/
export function LocaleProvider({ direction, children }: LocaleProviderProps) {
return (
<DirectionProvider direction={direction} setsLayout>
{children}
</DirectionProvider>
);
}

Expand All @@ -43,3 +91,9 @@ export function useLocale(): LocaleContextValue {

export const getDefaultDirection = (): Direction =>
I18nManager.getConstants?.().isRTL ? 'rtl' : 'ltr';

const styles = StyleSheet.create({
contents: {
display: 'contents',
},
});
6 changes: 3 additions & 3 deletions src/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import { render, type RenderOptions } from '@testing-library/react-native';

import MaterialCommunityIcon from './components/MaterialCommunityIcon';
import { getDefaultDirection, LocaleProvider } from './core/locale';
import { DirectionProvider, getDefaultDirection } from './core/locale';
import { Provider as SettingsProvider } from './core/settings';
import { ThemeProvider } from './core/theming';
import { LightTheme } from './theme/schemes';
Expand All @@ -12,9 +12,9 @@ const Wrapper = ({ children }: { children: React.ReactNode }) => (
<SettingsProvider
value={{ icon: MaterialCommunityIcon, rippleEffectEnabled: true }}
>
<LocaleProvider direction={getDefaultDirection()}>
<DirectionProvider direction={getDefaultDirection()}>
<ThemeProvider theme={LightTheme}>{children}</ThemeProvider>
</LocaleProvider>
</DirectionProvider>
</SettingsProvider>
);

Expand Down
Loading