-
-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Android): Request layout manually for CustomToolbar below Android…
… API 29 (#2332) ## Description On Android API 29, while using `windowSoftInputMode` with `adjustPan` option, requestLayout is not being called while subviews are being added. That's because while ScreenStackHeaderConfig adds view to the toolbar, onMeasure is being called and even if we're calling `requestLayout` on parent, Android is returning from requesting the layout, as there's somehow ongoing layout. This is not the case for Android API 30 and higher. The solution is to request another layout via ReactChoreographer (same as in ScreenContainer) to call our own layout callback on the next frame. ## Changes - Request layout via ReactChoreographer on `requestLayout` call in CustomToolbar class ## Screenshots / GIFs <details><summary>BEFORE</summary> ![CleanShot 2024-09-03 at 17 59 13](https://github.com/user-attachments/assets/3f7952a5-6430-4b25-b587-4690fac236d3) </details> <details><summary>AFTER</summary> ![CleanShot 2024-09-03 at 17 51 14](https://github.com/user-attachments/assets/f2551b98-5de1-4021-8c72-0e4718aaaf45) </details> ## Test code and steps to reproduce Use `Test2332.tsx` test case to check whether this PR works properly. ## Checklist - [x] Included code example that can be used to test this change - [ ] Ensured that CI passes --------- Co-authored-by: Kacper Kafara <kacper.kafara@swmansion.com>
- Loading branch information
Showing
3 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,86 @@ | ||
/** | ||
* | ||
* IMPORTANT! READ BEFORE TESTING! | ||
* Remember to switch windowSoftInputMode to `adjustPan` in AndroidManifest.xml file. | ||
* | ||
*/ | ||
|
||
import React, { useLayoutEffect } from 'react'; | ||
import { NavigationContainer } from '@react-navigation/native'; | ||
import { createNativeStackNavigator } from '@react-navigation/native-stack'; | ||
import { useNavigation } from '@react-navigation/native'; | ||
import { Button, Text, TextInput, View } from 'react-native'; | ||
import { NativeStackNavigationProp } from '@react-navigation/native-stack'; | ||
import { HeaderButton } from '@react-navigation/elements'; | ||
type RootStackNavigatorParamsList = { | ||
Home: undefined; | ||
Details: undefined; | ||
}; | ||
const Stack = createNativeStackNavigator(); | ||
const HomeScreen = () => { | ||
const navigation = | ||
useNavigation<NativeStackNavigationProp<RootStackNavigatorParamsList>>(); | ||
const onHandlePress = () => { | ||
navigation.navigate('Details'); | ||
}; | ||
return ( | ||
<View> | ||
<Text>HomeScreen</Text> | ||
<View> | ||
<Button title="Go to Details" onPress={onHandlePress} /> | ||
</View> | ||
</View> | ||
); | ||
}; | ||
const DetailsScreen = () => { | ||
const [text, setText] = React.useState(''); | ||
const navigation = | ||
useNavigation<NativeStackNavigationProp<RootStackNavigatorParamsList>>(); | ||
useLayoutEffect(() => { | ||
navigation.setOptions({ | ||
headerTitle: 'Detail Screen', | ||
headerRight: () => { | ||
if (text.length === 0) { | ||
return null; | ||
} | ||
return ( | ||
<HeaderButton> | ||
<Text>X</Text> | ||
</HeaderButton> | ||
); | ||
}, | ||
}); | ||
}, [navigation, text]); | ||
const onHandlePress = () => { | ||
navigation.goBack(); | ||
}; | ||
|
||
return ( | ||
<View> | ||
<Text>RegisterScreen</Text> | ||
<View> | ||
<TextInput | ||
style={{ backgroundColor: 'grey', height: 40, borderColor: 'black' }} | ||
placeholder="Enter text" | ||
value={text} | ||
onChangeText={text => { | ||
setText(text); | ||
}} | ||
/> | ||
<Button title="Go to Home" onPress={onHandlePress} /> | ||
<Button title="Go to Details" onPress={onHandlePress} /> | ||
</View> | ||
</View> | ||
); | ||
}; | ||
function App() { | ||
return ( | ||
<NavigationContainer> | ||
<Stack.Navigator> | ||
<Stack.Screen name="Home" component={HomeScreen} /> | ||
<Stack.Screen name="Details" component={DetailsScreen} /> | ||
</Stack.Navigator> | ||
</NavigationContainer> | ||
); | ||
} | ||
export default App; |
This file contains 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