diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 5fd0c7ad10..264a338b9a 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -1395,6 +1395,8 @@ A `Text` nested in another `Text` now inherits the parent's font and color, as R On iOS and Android, nesting follows React Native: a `Text` inside React Native's own `Text` inherits from it as well, and a `Text` inside a `View` isn't nested, so it renders like a top-level `Text`. +On the web, a `Text` under `direction="rtl"` (on `PaperProvider` or `LocaleProvider`) now aligns right by default. In 5.x it stayed left. A `textAlign` in `style` still wins. + ### ToggleButton `ToggleButton`, `ToggleButton.Group` and `ToggleButton.Row` were removed. For an diff --git a/docs/6.x/docs/guides/rtl.md b/docs/6.x/docs/guides/rtl.md index d00bb4368e..4f70c549f8 100644 --- a/docs/6.x/docs/guides/rtl.md +++ b/docs/6.x/docs/guides/rtl.md @@ -26,6 +26,8 @@ Then, let `react-native-paper` know about it by using the `direction` prop on `P 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. ::: +On the web, `Text` aligns to the start of the writing direction, so it aligns right when `direction` is `rtl`. On Android and iOS, React Native already aligns `left` text to the right in a right-to-left layout. A `textAlign` in `style` still wins. + ## Setting direction for the whole app Pass the `direction` prop to `PaperProvider`. Defaults to `'rtl'` when `I18nManager.getConstants().isRTL` returns `true`, otherwise `'ltr'`. diff --git a/src/components/Typography/AnimatedText.tsx b/src/components/Typography/AnimatedText.tsx index 3749783cd8..148f6e69f2 100644 --- a/src/components/Typography/AnimatedText.tsx +++ b/src/components/Typography/AnimatedText.tsx @@ -1,11 +1,11 @@ import * as React from 'react'; import type { ReactNode } from 'react'; -import { StyleSheet } from 'react-native'; import Animated from 'react-native-reanimated'; import { useIsNestedText, markChildrenAsNested } from './NestedTextContext'; import type { VariantProp } from './types'; +import { getTextAlignStyle } from './utils'; import { useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; import type { ThemeProp } from '../../theme/types'; @@ -49,6 +49,7 @@ function AnimatedText({ const theme = useInternalTheme(themeOverrides); const { direction: writingDirection } = useLocale(); + const alignStyle = getTextAlignStyle(writingDirection); // Nested text inherits the parent's style unless it has its own theme const inheritsStyle = useIsNestedText() && !themeOverrides; @@ -73,7 +74,7 @@ function AnimatedText({ ? [font, style] : [ font, - styles.text, + alignStyle, { writingDirection, color: theme.colors.onSurface }, style, ] @@ -93,7 +94,7 @@ function AnimatedText({ ref={ref} {...rest} style={[ - styles.text, + alignStyle, textStyle, { writingDirection, @@ -107,12 +108,6 @@ function AnimatedText({ return markChildrenAsNested(element, rest.children); } -const styles = StyleSheet.create({ - text: { - textAlign: 'left', - }, -}); - export const customAnimatedText = () => // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion AnimatedText as (props: Props) => ReactNode; diff --git a/src/components/Typography/Text.tsx b/src/components/Typography/Text.tsx index 31934c3a71..6809dab381 100644 --- a/src/components/Typography/Text.tsx +++ b/src/components/Typography/Text.tsx @@ -1,11 +1,12 @@ import * as React from 'react'; import type { ReactNode } from 'react'; -import { StyleSheet, Text as NativeText } from 'react-native'; +import { Text as NativeText } from 'react-native'; import type { StyleProp, TextStyle } from 'react-native'; import AnimatedText from './AnimatedText'; import { useIsNestedText, markChildrenAsNested } from './NestedTextContext'; import type { VariantProp } from './types'; +import { getTextAlignStyle } from './utils'; import { useLocale } from '../../core/locale'; import { useInternalTheme } from '../../core/theming'; import type { ThemeProp } from '../../theme/types'; @@ -90,6 +91,7 @@ const Text = ({ const root = React.useRef | null>(null); const theme = useInternalTheme(initialTheme); const { direction: writingDirection } = useLocale(); + const alignStyle = getTextAlignStyle(writingDirection); // Nested text inherits the parent's style unless it has its own theme const inheritsStyle = useIsNestedText() && !initialTheme; @@ -157,7 +159,7 @@ const Text = ({ inheritsStyle ? textStyle : [ - styles.text, + alignStyle, { writingDirection, color: theme.colors.onSurface }, textStyle, ] @@ -177,7 +179,7 @@ const Text = ({ ); } @@ -185,12 +187,6 @@ const Text = ({ return markChildrenAsNested(element, rest.children); }; -const styles = StyleSheet.create({ - text: { - textAlign: 'left', - }, -}); - type TextComponent = (props: Props) => ReactNode; const Component = Text as TextComponent; diff --git a/src/components/Typography/utils.ts b/src/components/Typography/utils.ts new file mode 100644 index 0000000000..4548cc06e4 --- /dev/null +++ b/src/components/Typography/utils.ts @@ -0,0 +1,13 @@ +import { Platform, StyleSheet } from 'react-native'; + +import type { Direction } from '../../core/locale'; + +// A `left` alignment follows the layout direction on Android and iOS, but +// stays physical on web, so right-to-left text aligns right there. +export const getTextAlignStyle = (direction: Direction) => + Platform.OS === 'web' && direction === 'rtl' ? styles.rtlWeb : styles.start; + +const styles = StyleSheet.create({ + start: { textAlign: 'left' }, + rtlWeb: { textAlign: 'right' }, +}); diff --git a/src/components/__tests__/Typography/Text.test.tsx b/src/components/__tests__/Typography/Text.test.tsx index 7fc747bda5..daa699eed1 100644 --- a/src/components/__tests__/Typography/Text.test.tsx +++ b/src/components/__tests__/Typography/Text.test.tsx @@ -1,8 +1,9 @@ -import { View } from 'react-native'; -import type { Text as NativeText } from 'react-native'; +import { Platform, View } from 'react-native'; +import type { Text as NativeText, TextStyle } from 'react-native'; -import { expect, it, jest } from '@jest/globals'; +import { afterEach, describe, expect, it, jest } from '@jest/globals'; +import { LocaleProvider, type Direction } from '../../../core/locale'; import PaperProvider from '../../../core/PaperProvider'; import { render, screen } from '../../../test-utils'; import configureFonts from '../../../theme/fonts'; @@ -328,6 +329,92 @@ it("detects nesting like React Native's own Text and View", async () => { }); }); +describe('text alignment', () => { + let platformOS: { restore(): void } | undefined; + + afterEach(() => { + platformOS?.restore(); + platformOS = undefined; + }); + + const textIDs = [ + 'text', + 'variant-text', + 'animated-text', + 'animated-variant-text', + ]; + + const renderTexts = (direction: Direction, style?: TextStyle) => + render( + + + {content} + + + {content} + + + {content} + + + {content} + + + ); + + it('aligns right-to-left text to the right on web', async () => { + platformOS = jest.replaceProperty(Platform, 'OS', 'web'); + + await renderTexts('rtl'); + + textIDs.forEach((testID) => { + expect(screen.getByTestId(testID)).toHaveStyle({ + textAlign: 'right', + writingDirection: 'rtl', + }); + }); + }); + + it('aligns left-to-right text to the left on web', async () => { + platformOS = jest.replaceProperty(Platform, 'OS', 'web'); + + await renderTexts('ltr'); + + textIDs.forEach((testID) => { + expect(screen.getByTestId(testID)).toHaveStyle({ textAlign: 'left' }); + }); + }); + + // React Native aligns `left` text to the right when the layout is + // right-to-left, so Android and iOS keep it. + it.each(['android', 'ios'] as const)( + 'keeps the left alignment for right-to-left text on %s', + async (os) => { + platformOS = jest.replaceProperty(Platform, 'OS', os); + + await renderTexts('rtl'); + + textIDs.forEach((testID) => { + expect(screen.getByTestId(testID)).toHaveStyle({ textAlign: 'left' }); + }); + } + ); + + it('keeps an alignment set in style for right-to-left text on web', async () => { + platformOS = jest.replaceProperty(Platform, 'OS', 'web'); + + await renderTexts('rtl', { textAlign: 'center' }); + + textIDs.forEach((testID) => { + expect(screen.getByTestId(testID)).toHaveStyle({ textAlign: 'center' }); + }); + }); +}); + it('throws when custom variant not provided', async () => { jest.spyOn(console, 'error').mockImplementation(() => {});