Right-to-Left (RTL) Styling Best Practices
RTL (Right-to-Left) is a text direction that is used in languages such as Arabic, Hebrew, and Persian. In RTL languages, the text is written from right to left. When designing an app for RTL languages, it is important to consider the following best practices:
Styling Components for RTL and LTR Layouts
Certain UI elements need to be mirrored when switching between RTL and LTR. To handle this:
Direction-Aware Flexbox Layouts
Flexbox automatically handles direction-based layouts, but you should be mindful of flexDirection
, alignItems
, and justifyContent
properties.
For example:
const styles = StyleSheet.create({
container: {
flexDirection: I18nManager.isRTL ? "row-reverse" : "row",
},
});
Padding and Margin Adjustments
While setting padding and margin, use directional properties such as paddingStart
, paddingEnd
, marginStart
, and marginEnd
instead of absolute values like paddingLeft
and paddingRight
.
const styles = StyleSheet.create({
container: {
paddingStart: 10, // Automatically adjusts for RTL
paddingEnd: 10,
},
});
Text Alignment
For text alignment, use textAlign: 'auto'
which automatically handles alignment based on the language direction. Alternatively:
textAlign: I18nManager.isRTL ? "right" : "left";
TextInput Component
For TextInput
, RTL text should automatically be supported. However, you may need to handle placeholders and input text alignment:
<TextInput
style={{ textAlign: I18nManager.isRTL ? "right" : "left" }}
placeholder="Enter text"
/>
Also, consider setting textAlignVertical
if the input spans multiple lines.
Horizontal Scrolling in RTL Mode
By default, when
FlatList
is used with horizontal scrolling (horizontal={true}
) and the app is in RTL mode, the list automatically starts from the right.The content will align accordingly, and the list scrolls from right to left, which is the expected behavior for RTL layouts.
Inverting the Scroll Behavior:
<FlatList
horizontal={true}
inverted={I18nManager.isRTL} // Inverts the scroll behavior
data={data}
renderItem={renderItem}
/>
Below is a detailed table of RTL-supported style properties in React Native. These properties automatically adjust their behavior based on the layout direction (LTR or RTL).
Icons and Images
When using icons and images, ensure that they are mirrored correctly for RTL layouts. Some icons and images might need to be mirrored in RTL mode. React Native supports this with the transform
property:
<Image
source={yourImageSource}
style={{
transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }],
}}
/>
Table of RTL-Supported Styles
Style Property | Description | Equivalent LTR Property | Equivalent RTL Property |
---|---|---|---|
paddingStart | Sets padding at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL). | paddingLeft | paddingRight |
paddingEnd | Sets padding at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL). | paddingRight | paddingLeft |
marginStart | Sets margin at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL). | marginLeft | marginRight |
marginEnd | Sets margin at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL). | marginRight | marginLeft |
start | Sets position from the leading edge (start) for absolute/relative positioning. Adjusts based on layout direction (LTR/RTL). | left | right |
end | Sets position from the trailing edge (end) for absolute/relative positioning. Adjusts based on layout direction (LTR/RTL). | right | left |
textAlign | Adjusts text alignment based on the layout direction. Use 'auto' for automatic alignment based on LTR/RTL. | left , center , right | Same as LTR |
flexDirection | Defines the primary axis of layout. Use 'row-reverse' for RTL-specific adjustments. | row , column | row-reverse , column |
borderStartWidth | Sets border width at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL). | borderLeftWidth | borderRightWidth |
borderEndWidth | Sets border width at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL). | borderRightWidth | borderLeftWidth |
borderStartColor | Sets border color at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL). | borderLeftColor | borderRightColor |
borderEndColor | Sets border color at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL). | borderRightColor | borderLeftColor |
Key Notes:
- Padding and Margin:
paddingStart
,paddingEnd
,marginStart
, andmarginEnd
adjust padding and margin relative to the layout direction (LTR or RTL). - Text Alignment: Use
textAlign: 'auto'
to automatically align text based on the current language direction. - Flexbox Layout: The
flexDirection
property handles row direction changes. Usingrow-reverse
is particularly useful in RTL layouts. - Border and Positioning: Border and positional properties (like
start
,end
,borderStartWidth
, etc.) ensure correct positioning and borders in both LTR and RTL.
By leveraging these RTL-supported style properties, your React Native app can automatically handle both left-to-right and right-to-left languages without the need for hardcoding directional adjustments.