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
2 changes: 2 additions & 0 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/6.x/docs/guides/rtl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`.
Expand Down
13 changes: 4 additions & 9 deletions src/components/Typography/AnimatedText.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand All @@ -73,7 +74,7 @@ function AnimatedText({
? [font, style]
: [
font,
styles.text,
alignStyle,
{ writingDirection, color: theme.colors.onSurface },
style,
]
Expand All @@ -93,7 +94,7 @@ function AnimatedText({
ref={ref}
{...rest}
style={[
styles.text,
alignStyle,
textStyle,
{
writingDirection,
Expand All @@ -107,12 +108,6 @@ function AnimatedText({
return markChildrenAsNested(element, rest.children);
}

const styles = StyleSheet.create({
text: {
textAlign: 'left',
},
});

export const customAnimatedText = <T,>() =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
AnimatedText as (props: Props<T>) => ReactNode;
Expand Down
14 changes: 5 additions & 9 deletions src/components/Typography/Text.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -90,6 +91,7 @@ const Text = ({
const root = React.useRef<React.ComponentRef<typeof NativeText> | 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;

Expand Down Expand Up @@ -157,7 +159,7 @@ const Text = ({
inheritsStyle
? textStyle
: [
styles.text,
alignStyle,
{ writingDirection, color: theme.colors.onSurface },
textStyle,
]
Expand All @@ -177,20 +179,14 @@ const Text = ({
<NativeText
{...rest}
ref={root}
style={[styles.text, textStyle, { writingDirection }, style]}
style={[alignStyle, textStyle, { writingDirection }, style]}
/>
);
}

return markChildrenAsNested(element, rest.children);
};

const styles = StyleSheet.create({
text: {
textAlign: 'left',
},
});

type TextComponent<T> = (props: Props<T>) => ReactNode;

const Component = Text as TextComponent<never>;
Expand Down
13 changes: 13 additions & 0 deletions src/components/Typography/utils.ts
Original file line number Diff line number Diff line change
@@ -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' },
});
93 changes: 90 additions & 3 deletions src/components/__tests__/Typography/Text.test.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
<LocaleProvider direction={direction}>
<Text testID="text" style={style}>
{content}
</Text>
<Text testID="variant-text" variant="bodyLarge" style={style}>
{content}
</Text>
<AnimatedText testID="animated-text" style={style}>
{content}
</AnimatedText>
<AnimatedText
testID="animated-variant-text"
variant="bodyLarge"
style={style}
>
{content}
</AnimatedText>
</LocaleProvider>
);

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(() => {});

Expand Down
Loading