Skip to content

Commit

Permalink
fix: fix headerBackTitleVisible in native-stack (#11423)
Browse files Browse the repository at this point in the history
## Motivation

Changes made in
software-mansion/react-native-screens#1646 in
react-native-screens v3.19.0 introduced a bug making customizing
`headerBackTitle` impossible.

This got fixed in
software-mansion/react-native-screens#1726 and
released in `react-native-screens` **v3.21.0** with a change of
`headerBackTitleVisible` logic.

It requires a small adjustment in the `HeaderConfig.tsx` of the
`@react-navigation/native-stack` as the empty string passed to
`backTitle` results in a default back button instead of hiding it.

Detailed explaination can be found in
software-mansion/react-native-screens#1726

Fixes #11303,
#11337 and
#11375

## Code example

<details>
<summary>Code example</summary>


```jsx
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;
```

</details>

## Screen recordings

#### `react-native-screens` v3.21.1 with fix in
`@react-navigation/native-stack`:

On the second screen header back title is hidden as it should and back
button menu working ✅



https://github.com/react-navigation/react-navigation/assets/39658211/cc710687-3a64-4746-963e-a211a1b36455



#### `react-native-screens` v3.21.1 without the fix in
`@react-navigation/native-stack`:

On the second screen you can see the default header back title is shown
instead of being hidden ❌





https://github.com/react-navigation/react-navigation/assets/39658211/2380dc4a-58c2-4376-a32d-a4249e3d8cc0




#### `react-native-screens` v3.20.0:

Here the back button is all over the place ❌ 



https://github.com/react-navigation/react-navigation/assets/39658211/429c451a-3775-4d3f-bc08-6e33a2ac4af6

#### `react-native-screens` v3.18.2:

Backward compatibility is kept in place (but back button menu doesn't
work as expected in v3.18) ✅



https://github.com/react-navigation/react-navigation/assets/39658211/15665b45-1797-412e-9a13-540320dd27ed
  • Loading branch information
kacperkapusciak committed Jun 22, 2023
1 parent 42f32d4 commit 596c633
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/native-stack/src/views/HeaderConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
View,
} from 'react-native';
import {
// @ts-expect-error: Available since react-native-screens v3.21
isNewBackTitleImplementation,
isSearchBarAvailableForCurrentPlatform,
ScreenStackHeaderBackButtonImage,
ScreenStackHeaderCenterView,
Expand Down Expand Up @@ -168,7 +170,13 @@ export default function HeaderConfig({
? 'transparent'
: colors.card)
}
backTitle={headerBackTitleVisible ? headerBackTitle : ' '}
backTitle={
isNewBackTitleImplementation || headerBackTitleVisible
? headerBackTitle
: ' '
}
// @ts-expect-error: Available since react-native-screens v3.21
backTitleVisible={headerBackTitleVisible}
backTitleFontFamily={backTitleFontFamily}
backTitleFontSize={headerBackTitleStyleFlattened.fontSize}
blurEffect={headerBlurEffect}
Expand Down

0 comments on commit 596c633

Please sign in to comment.