-
-
Notifications
You must be signed in to change notification settings - Fork 643
fix(Android): RTL layout for center-aligned custom header title #3896
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -138,30 +138,40 @@ class ScreenStackHeaderConfig( | |||||
| } | ||||||
|
|
||||||
| val isBackButtonDisplayed = toolbar.navigationIcon != null | ||||||
| val isRtl = toolbar.layoutDirection == LAYOUT_DIRECTION_RTL | ||||||
|
|
||||||
| val contentInsetStartEstimation = | ||||||
| val startInsetEstimation = | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary renaming. This value name should stay untouched.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also applies to other names.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point dug into it and Yoga does handle logical edges correctly, but the existing setPadding() call maps paddingStart/paddingEnd to physical Edge::Left/Edge::Right, so the values never reach Yoga as logical. Pushed a commit that routes them through Edge::Start/Edge::End instead; tested in FabricExample and my production app, renders correctly in LTR and RTL. |
||||||
| if (isBackButtonDisplayed) { | ||||||
| toolbar.currentContentInsetStart + toolbar.paddingStart | ||||||
| } else { | ||||||
| max(toolbar.currentContentInsetStart, toolbar.paddingStart) | ||||||
| } | ||||||
|
|
||||||
| // Assuming that there is nothing to the left of back button here, the content | ||||||
| // offset we're interested in in ShadowTree is the `left` of the subview left. | ||||||
| // In case it is not available we fallback to approximation. | ||||||
| val contentInsetStart = | ||||||
| configSubviews.firstOrNull { it.type === ScreenStackHeaderSubview.Type.LEFT }?.left | ||||||
| ?: contentInsetStartEstimation | ||||||
|
|
||||||
| val contentInsetEnd = toolbar.currentContentInsetEnd + toolbar.paddingEnd | ||||||
| val endInset = toolbar.currentContentInsetEnd + toolbar.paddingEnd | ||||||
| val leftSubview = | ||||||
| configSubviews.firstOrNull { it.type === ScreenStackHeaderSubview.Type.LEFT } | ||||||
|
|
||||||
| // The shadow tree expects physical left/right padding, but the toolbar APIs | ||||||
| // return logical start/end values which swap sides in RTL. | ||||||
| val paddingLeft: Int | ||||||
| val paddingRight: Int | ||||||
|
|
||||||
| if (!isRtl) { | ||||||
| paddingLeft = leftSubview?.left ?: startInsetEstimation | ||||||
| paddingRight = endInset | ||||||
| } else { | ||||||
| paddingLeft = endInset | ||||||
| paddingRight = | ||||||
| if (leftSubview != null) toolbar.width - leftSubview.left else startInsetEstimation | ||||||
|
||||||
| if (leftSubview != null) toolbar.width - leftSubview.left else startInsetEstimation | |
| if (leftSubview != null) toolbar.width - leftSubview.right else startInsetEstimation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import React from 'react'; | ||
| import { View, Text, StyleSheet, I18nManager } from 'react-native'; | ||
| import { NavigationContainer } from '@react-navigation/native'; | ||
| import { createNativeStackNavigator } from '@react-navigation/native-stack'; | ||
|
|
||
| const Stack = createNativeStackNavigator(); | ||
|
|
||
| function HomeScreen() { | ||
| return ( | ||
| <View style={styles.container}> | ||
| <Text>Issue #3438</Text> | ||
| <Text>headerTitleAlign: center + headerTitle as function</Text> | ||
| <Text>RTL mode: {I18nManager.isRTL ? 'YES' : 'NO'}</Text> | ||
| <Text style={styles.note}> | ||
| The header title above should say "Custom Title". | ||
| {'\n'}If it's missing or there's extra space, the bug is present. | ||
| </Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function App() { | ||
| return ( | ||
| <NavigationContainer> | ||
| <Stack.Navigator> | ||
| <Stack.Screen | ||
| name="Home" | ||
| component={HomeScreen} | ||
| options={{ | ||
| headerTitleAlign: 'center', | ||
| headerTitle: () => ( | ||
| <Text style={{ fontSize: 18, fontWeight: 'bold' }}> | ||
| Custom Title | ||
| </Text> | ||
| ), | ||
| }} | ||
| /> | ||
| </Stack.Navigator> | ||
|
Comment on lines
+46
to
+58
|
||
| </NavigationContainer> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| padding: 20, | ||
| gap: 8, | ||
| }, | ||
| note: { | ||
| textAlign: 'center', | ||
| marginTop: 20, | ||
| color: 'gray', | ||
| }, | ||
| }); | ||
|
|
||
| export default App; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LAYOUT_DIRECTION_RTL/LAYOUT_DIRECTION_LTRare used unqualified in this file, but there is no import for these constants. This won’t compile unless they’re referenced viaView.LAYOUT_DIRECTION_*or imported explicitly.