diff --git a/docs/6.x/docs/guides/rtl.md b/docs/6.x/docs/guides/rtl.md index d00bb4368e..2d6b0b5b27 100644 --- a/docs/6.x/docs/guides/rtl.md +++ b/docs/6.x/docs/guides/rtl.md @@ -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 @@ -60,13 +60,17 @@ import { LocaleProvider } from 'react-native-paper'; export default function ArabicSection({ children }) { return ( - {/* Components here will use RTL layout */} + {/* Paper's components here use the RTL direction */} {children} ); } ``` +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: diff --git a/src/components/Portal/Portal.tsx b/src/components/Portal/Portal.tsx index 52330942ea..8e8fb0a863 100644 --- a/src/components/Portal/Portal.tsx +++ b/src/components/Portal/Portal.tsx @@ -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, @@ -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); @@ -59,11 +64,11 @@ const Portal = ({ children, modal, theme: themeOverrides }: Props) => { - + {children} - + diff --git a/src/core/PaperProvider.tsx b/src/core/PaperProvider.tsx index fa7913edb5..96ea2ea7bb 100644 --- a/src/core/PaperProvider.tsx +++ b/src/core/PaperProvider.tsx @@ -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'; @@ -68,11 +72,11 @@ const PaperProvider = (props: Props) => { - + {children} - + diff --git a/src/core/__tests__/LocaleProvider.test.tsx b/src/core/__tests__/LocaleProvider.test.tsx new file mode 100644 index 0000000000..4c2f07ace5 --- /dev/null +++ b/src/core/__tests__/LocaleProvider.test.tsx @@ -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( + + + + + + + + + ); + +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( + + + + + + + ); + await screen.findByTestId('portal-content'); + + expect(parentOf('content')).not.toHaveProp('dir'); + expect(parentOf('portal-content')).not.toHaveProp('dir'); + }); +}); diff --git a/src/core/locale.tsx b/src/core/locale.tsx index 11433f736f..c81b8828d3 100644 --- a/src/core/locale.tsx +++ b/src/core/locale.tsx @@ -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. @@ -15,18 +15,66 @@ export const LocaleContext = React.createContext( 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 (``). + */ + 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 ( - {children} + + + {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. + + {children} + + ) : ( + children + )} + + + ); +} + +/** + * 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 ( + + {children} + ); } @@ -43,3 +91,9 @@ export function useLocale(): LocaleContextValue { export const getDefaultDirection = (): Direction => I18nManager.getConstants?.().isRTL ? 'rtl' : 'ltr'; + +const styles = StyleSheet.create({ + contents: { + display: 'contents', + }, +}); diff --git a/src/test-utils.tsx b/src/test-utils.tsx index 2eb7a2500c..f29e15b801 100644 --- a/src/test-utils.tsx +++ b/src/test-utils.tsx @@ -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'; @@ -12,9 +12,9 @@ const Wrapper = ({ children }: { children: React.ReactNode }) => ( - + {children} - + );