Skip to content

Commit

Permalink
fix: add internal isNewBackTitleImplementation const (#1791)
Browse files Browse the repository at this point in the history
## Description

Because of a bug introduced in
#1646
`react-native-screens` v3.21 changed how header's backTitle handles
whitespace strings in
#1726

To allow for backwards compatibility in @react-navigation/native-stack
we need have a way to check if this version or newer is used

See react-navigation/react-navigation#11423 for
more context.

## Changes

Added new `isNewBackTitleImplementation` internal constant that can be
used in `@react-navigation/native-stack`.

## Screenshots / GIFs

#### This change &
react-navigation/react-navigation#11423 applied:


https://github.com/software-mansion/react-native-screens/assets/39658211/e2409b46-0725-473d-962b-1acc9deaa469


#### Without this change and
react-navigation/react-navigation#11423:


https://github.com/software-mansion/react-native-screens/assets/39658211/ec65fd5d-f8e9-4d88-b442-6bfc68e9ee9c


## Test code and steps to reproduce

Test1791.tsx

You need to apply changes introduced in
react-navigation/react-navigation#11423 to
`@react-navigation/native-stack` to test these canges.

## Checklist

- [x] Included code example that can be used to test this change
  • Loading branch information
kacperkapusciak authored Jun 19, 2023
1 parent 7c2801f commit d481b3e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import Test1646 from './src/Test1646';
import Test1649 from './src/Test1649';
import Test1683 from './src/Test1683';
import Test1726 from './src/Test1726';
import Test1791 from './src/Test1791';

enableFreeze(true);

Expand Down
63 changes: 63 additions & 0 deletions TestsExample/src/Test1791.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {NavigationContainer} from '@react-navigation/native';

import {createNativeStackNavigator} from '@react-navigation/native-stack';

import React from 'react';
import {Button, View} from 'react-native';

const Stack = createNativeStackNavigator();

const Screen1 = ({navigation}) => (
<View style={{flex: 1}}>
<Button onPress={() => navigation.navigate('Screen2')} title="Next" />
</View>
);

const Screen2 = ({navigation}) => (
<View style={{flex: 1}}>
<Button onPress={() => navigation.navigate('Screen3')} title="Next" />
</View>
);

const Screen3 = ({navigation}) => (
<View style={{flex: 1}}>
<Button onPress={() => navigation.navigate('Screen4')} title="Next" />
</View>
);

const Screen4 = () => <View style={{flex: 1}} />;

const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen component={Screen1} name="Screen1" />
<Stack.Screen
component={Screen2}
name="Screen2"
options={{
headerBackTitleVisible: false,
headerBackTitle: 'Custom title in back button menu',
}}
/>
<Stack.Screen
component={Screen3}
name="Screen3"
options={{
headerBackTitle: 'Small title',
headerBackTitleStyle: {fontSize: 8},
}}
/>
<Stack.Screen
component={Screen4}
name="Screen4"
options={{
headerBackTitle: 'Custom title',
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};

export default App;
2 changes: 2 additions & 0 deletions src/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from './types';
import {
isSearchBarAvailableForCurrentPlatform,
isNewBackTitleImplementation,
executeNativeBackPress,
} from './utils';

Expand Down Expand Up @@ -592,5 +593,6 @@ module.exports = {
useTransitionProgress,

isSearchBarAvailableForCurrentPlatform,
isNewBackTitleImplementation,
executeNativeBackPress,
};
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './types';
export { default as useTransitionProgress } from './useTransitionProgress';
export {
isSearchBarAvailableForCurrentPlatform,
isNewBackTitleImplementation,
executeNativeBackPress,
} from './utils';

Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ export function executeNativeBackPress() {
BackHandler.exitApp();
return true;
}

// Because of a bug introduced in https://github.com/software-mansion/react-native-screens/pull/1646
// react-native-screens v3.21 changed how header's backTitle handles whitespace strings in https://github.com/software-mansion/react-native-screens/pull/1726
// To allow for backwards compatibility in @react-navigation/native-stack we need a way to check if this version or newer is used.
// See https://github.com/react-navigation/react-navigation/pull/11423 for more context.
export const isNewBackTitleImplementation = true;

0 comments on commit d481b3e

Please sign in to comment.