-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add direction prop to PaperProvider and useLocale hook for RTL support #4921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0cfb39b
feat: add useLocale hook and direction prop for RTL support
0de5711
Merge branch 'v6' of https://github.com/callstack/react-native-paper …
21ff89d
fix: memoize locale context value in PaperProvider
6c03baa
docs: add RTL guide and improve locale API
d1934c8
fix: null default for locale and wrap test renders
c5805bd
Merge branch 'v6' of https://github.com/callstack/react-native-paper …
eddaaa6
fix: exclude test-utils from build and restore node types in tsconfig
fc1290e
fix: exclude test-utils from typescript build config
3cb9b1c
Update docs/docs/guides/10-rtl.md
hristototov b094aa2
Merge branch 'v6' of https://github.com/callstack/react-native-paper …
c3620d6
fix: wrap html snippet in code fence in RTL guide
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| --- | ||
| title: RTL Support | ||
| --- | ||
|
|
||
| # RTL Support | ||
|
|
||
| React Native Paper supports right-to-left (RTL) layouts for languages such as Arabic and Hebrew. | ||
|
|
||
| ## How it works | ||
|
|
||
| On React Native, the writing direction is normally controlled by `I18nManager.forceRTL`. React Native Paper reads this automatically i.e. no configuration is needed for native apps that already set up RTL via `I18nManager`. | ||
|
|
||
| However, `I18nManager` is a no-op on **React Native Web**, which means RTL layouts break silently in web apps. The `direction` prop on `PaperProvider` (and the `LocaleProvider` component) lets you explicitly control the writing direction so Paper behaves correctly on all platforms. | ||
|
|
||
| :::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. | ||
| ::: | ||
|
|
||
| ## Setting direction for the whole app | ||
|
|
||
| Pass the `direction` prop to `PaperProvider`. Defaults to `'rtl'` when `I18nManager.getConstants().isRTL` returns `true`, otherwise `'ltr'`. | ||
|
|
||
| Supported values: | ||
|
|
||
| - `'ltr'`: Left-to-right text direction for languages like English, French etc. | ||
| - `'rtl'`: Right-to-left text direction for languages like Arabic, Hebrew etc. | ||
|
|
||
| ```js | ||
| import * as React from 'react'; | ||
| import { PaperProvider } from 'react-native-paper'; | ||
| import App from './src/App'; | ||
|
|
||
| export default function Main() { | ||
| return ( | ||
| <PaperProvider direction="rtl"> | ||
| <App /> | ||
| </PaperProvider> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Overriding direction for a subtree | ||
|
|
||
| Use `LocaleProvider` to override the direction for a specific part of the tree without affecting the rest of the app: | ||
|
|
||
| ```js | ||
| import * as React from 'react'; | ||
| import { LocaleProvider } from 'react-native-paper'; | ||
|
|
||
| export default function ArabicSection() { | ||
| return ( | ||
| <LocaleProvider direction="rtl"> | ||
| {/* Components here will use RTL layout */} | ||
| </LocaleProvider> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Reading the current direction | ||
|
|
||
| The direction is available in your own components via the `useLocale` hook: | ||
|
|
||
| ```js | ||
| import * as React from 'react'; | ||
| import { useLocale } from 'react-native-paper'; | ||
|
|
||
| function MyComponent() { | ||
| const { direction } = useLocale(); | ||
|
|
||
| // Use the direction | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.