Skip to content
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

fix: improve when back button is shown in nested native stacks #10761

Merged
merged 1 commit into from
Aug 22, 2022

Conversation

satya164
Copy link
Member

@satya164 satya164 commented Aug 17, 2022

Currently when a Native Stack is nested in another Stack, it doesn't respect the
parent navigation's back stack. This change makes a few adjustments so that if
the parent has a back stack:

  • If the user specifies a custom back button, the canGoBack and label
    parameters will now receive the proper data so they can render a back button in
    nested stack.
  • If JS stack is nested inside a Native stack, it'll now respect the parent's
    back stack and show a back button.
  • On Web, Native stack nested inside Native or JS stack will show a back
    button.
  • On Android, Native Stack nested inside Native stack will show a back button -
    since the back button is handled natively, we can't make it show when Native
    Stack is nested inside JS Stack
  • On iOS, JS stack nested inside Native stack will show a back button - since the
    back button is handled natively, we can't make it show when Native Stack is
    nested inside JS or Native Stack (see
    Header back button missing when navigating from root to a nested stack where root-stack has headerShown set to false software-mansion/react-native-screens#1460)

Before

Screen.Recording.2022-08-17.at.03.29.14.mov

After

Screen.Recording.2022-08-17.at.03.30.20.mov

Currently when a Native Stack is nested in another Stack, it doesn't respect the
parent navigation's back stack. This change makes a few adjustments so that if
the parent has a back stack:

- If the user specifies a custom back button, the `canGoBack` and `label`
parameters will now receive the proper data so they can render a back button in
nested stack.
- If JS stack is nested inside a Native stack, it'll now respect the parent's
back stack and show a back button.
- On Web, Native stack nested inside or Native or JS stack will show a back
button.
- On Android, Native Stack nested inside Native stack will show a back button -
since the back button is handled natively, we can't make it show when Native
Stack is nested inside JS Stack
- On iOS, JS stack nested inside Native stack will show a back button - since the
back button is handled natively, we can't make it show when Native Stack is
nested inside JS or Native Stack (see
software-mansion/react-native-screens#1460)

starting # with '#' will be ignored, and an empty message aborts the commit.
@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for react-navigation-example failed.

Name Link
🔨 Latest commit 655640c
🔍 Latest deploy log https://app.netlify.com/sites/react-navigation-example/deploys/62fc533aaaf167000912b286

@github-actions
Copy link

The Expo app for the example from this branch is ready!

expo.dev/@react-navigation/react-navigation-example?release-channel=pr-10761

@satya164 satya164 merged commit 05dab7d into main Aug 22, 2022
@satya164 satya164 deleted the @satya164/native-stack-nested-back branch August 22, 2022 01:36
DrOverbuild added a commit to DrOverbuild/react-navigation that referenced this pull request Sep 15, 2022
Unfortunately the changes from react-navigation#10761 caused an issue I reported on the
react-native-screens repo. (See software-mansion/react-native-screens#1589)

Essentially, the documentation states that per native behavior, the back button
on iOS will automatically use the title of the previous screen or "Back" if
there's not enough space. However, with the latest version of react-navigation,
the back button was the title of the previous screen no matter what. In some
cases, this caused the title of the current screen to be completely pushed off
the screen.

In UIKit this happens when the title of the back button is deliberately set, or
when a custom UIBarButtonItem is set as the back button. `react-native-screens`
creates a custom UIBarButtonItem when the header config component has a value
defined for `headerBackTitle`. So by leaving headerBackTitle undefined here, we
let the system automatically determine what to set as the back button title. In
my testing, system will choose the correct value even if the previous screen's
`title` or `headerTitle` is set differently from the route name.
satya164 pushed a commit that referenced this pull request Sep 15, 2022
Unfortunately the changes from #10761 caused an issue I reported on the
react-native-screens repo. (See software-mansion/react-native-screens#1589)

Essentially, the documentation states that per native behavior, the back button
on iOS will automatically use the title of the previous screen or "Back" if
there's not enough space. However, with the latest version of react-navigation,
the back button was the title of the previous screen no matter what. In some
cases, this caused the title of the current screen to be completely pushed off
the screen.

In UIKit this happens when the title of the back button is deliberately set, or
when a custom UIBarButtonItem is set as the back button. `react-native-screens`
creates a custom UIBarButtonItem when the header config component has a value
defined for `headerBackTitle`. So by leaving headerBackTitle undefined here, we
let the system automatically determine what to set as the back button title. In
my testing, system will choose the correct value even if the previous screen's
`title` or `headerTitle` is set differently from the route name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants