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(angular): transition animation plays when using browser back and forward buttons #28188

Closed
wants to merge 12 commits into from

Conversation

hoi4
Copy link
Contributor

@hoi4 hoi4 commented Sep 16, 2023

Issue number: resolves #16569


This PR resolves a few issues around navigation transition animations and the navigation stack when using the browser back and forward buttons.

  1. The respective transition animation is now applied automatically (without e.g. the workaround described here)

  2. Sometimes when navigating forward with the browser button, pages in the middle of the current tab's stack were incorrectly removed. This caused an issue when toggling between pages via the browser back and forward button (especially visible when using custom transition animations).

I added some logic to use the current navigation stack as basis for the direction of the animation that will be applied.

Short demonstration video of the changes (using the ng16 test app):
https://github.com/ionic-team/ionic-framework/assets/23243256/40aba3ec-0c56-44f1-8b82-ba5a2903b414

Does this introduce a breaking change?

  • Yes
  • No

At least to my limited knowledge. The tests are still passing. I just had to increase the timeouts slightly to properly wait for the page transitions to finish.

@github-actions github-actions bot added the package: angular @ionic/angular package label Sep 16, 2023
@hoi4 hoi4 changed the title fix(angular): fix navigation stack when using browser back and forward button fix(angular): fix navigation transition animation when using browser back and forward button Sep 16, 2023
@hoi4
Copy link
Contributor Author

hoi4 commented Sep 19, 2023

@liamdebeasi FYI :)

@hoi4
Copy link
Contributor Author

hoi4 commented Oct 4, 2023

Any chance to get feedback on these changes? 🙏🏻

@liamdebeasi
Copy link
Contributor

Hey there! Sorry for the delay. Someone from the team will do a code review soon. Thanks for contributing!

Copy link
Contributor

@liamdebeasi liamdebeasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! Thanks so much for making this PR. I spoke with the team, and we've identified a simpler solution that would address #16569.

The main issue is we explicitly disable animations when using the browser back/forward buttons:

this.guessAnimation = !ev.restoredState ? this.guessDirection : undefined;

ev.restoredState is set when the user presses the back/forward buttons: https://angular.io/api/router/NavigationStart#restoredState

Something like the following should resolve the issue:

if (router) {
      router.events.subscribe((ev) => {
        if (ev instanceof NavigationStart) {
          const id = ev.restoredState ? ev.restoredState.navigationId : ev.id;
-         this.guessDirection = id < this.lastNavId ? 'back' : 'forward';
-         this.guessAnimation = !ev.restoredState ? this.guessDirection : undefined;
+         this.guessAnimation = this.guessDirection = id < this.lastNavId ? 'back' : 'forward';
           this.lastNavId = this.guessDirection === 'forward' ? ev.id : id;
        }
      });
    }

I know your PR fixes other issues, but we typically don't accept PRs that fix multiple issues at the same time (it makes PRs hard to review). We're happy to review separate PRs for the other issues you worked to resolve in this PR.

Let me know if you have questions!

@hoi4
Copy link
Contributor Author

hoi4 commented Oct 6, 2023

@liamdebeasi Thank you for your feedback. I understand the issue regarding fixing multiple things simultaneously. I will limit the changes in this PR to the fix for the missing nav animation when using the browser back button.
I will furthermore create another PR with the rest of my changes.

@hoi4 hoi4 mentioned this pull request Oct 6, 2023
2 tasks
Copy link
Contributor

@liamdebeasi liamdebeasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change itself looks good, but I have a question on the test change.

@@ -113,6 +113,7 @@ describe('Router Link', () => {
describe('back', () => {
it('should go back with ion-button[routerLink][routerDirection=back]', () => {
cy.get('#routerLink-back').click();
testBack();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the test change here. The fix modifies whether or not an animation is used, but testBack does not verify this.

Copy link
Contributor Author

@hoi4 hoi4 Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just an issue I fixed where the test executed the back navigation, but did not call testBack like the other test cases. I assumed it was missed originally when writing the test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. Is this change necessary to validate the bug you fixed? If not, could we remove it from the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure :)

Copy link
Contributor

@liamdebeasi liamdebeasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix itself looks good. Thank you very much! One question below on the test changes.

@@ -138,6 +138,7 @@ function testForward() {
cy.get('app-router-link-page #canGoBack').should('have.text', 'true');

cy.go('back');
cy.wait(500);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the timeout changes here necessary? If not, could we revert them? (Sorry, I meant to comment on this with my last round of reviews, but it slipped my mind)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if possible, could you allow me to push changes to your branch? I can resolve the merge conflicts for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it is necessary. Before, the back transition was instant and now it takes the time for the animation which is why we need the additional wait
I invited you to my fork. Thanks for taking care of the merge conflicts.

@liamdebeasi liamdebeasi changed the title fix(angular): fix navigation transition animation when using browser back and forward button fix(angular): transition animation plays when using browser back and forward button Oct 13, 2023
@liamdebeasi liamdebeasi changed the title fix(angular): transition animation plays when using browser back and forward button fix(angular): transition animation plays when using browser back and forward buttons Oct 13, 2023
const id = ev.restoredState ? ev.restoredState.navigationId : ev.id;
this.guessDirection = id < this.lastNavId ? 'back' : 'forward';
this.guessAnimation = !ev.restoredState ? this.guessDirection : undefined;
this.guessDirection = this.guessAnimation = id < this.lastNavId ? 'back' : 'forward';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One bug I noticed is animations now play when using the back/forward buttons in the sidemenu app (clicking items in the menu don't play an animation). I'll see if there's a solution for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen.Recording.2023-10-13.at.12.01.33.PM.mov

Copy link
Contributor Author

@hoi4 hoi4 Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any news regarding this, @liamdebeasi ?
If it helps, I can also take another look at the bug :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have work for this scheduled in our next sprint, but that doesn't start until next week. We'd love it if you could take a look!

For this particular action, the routerDirection is root, so maybe we need to consider that when determining if an animation should play? https://github.com/ionic-team/starters/blob/main/angular/official/sidemenu/src/app/app.component.html#L10C33-L10C33

Copy link
Contributor Author

@hoi4 hoi4 Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liamdebeasi I had a look at the issue. In order to resolve it I needed some of the changes from my other open PR #28297.
I applied the fix to both branches and adapted the test as we don't need the 500ms wait in the root case anymore now. Hope that helps!

I tested the change with the routerlink test page as I didn't know how to get the starter app running with my local changes:
Screenshot 2023-10-31 at 15 38 43

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liamdebeasi Any chance you have some time to take a look at the fix? 😇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not. The ticket has been pulled into our current sprint which just started, so I should be able to look at it soon.

Copy link
Contributor

@liamdebeasi liamdebeasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found one bug, but I'll look into a fix

github-merge-queue bot pushed a commit that referenced this pull request Nov 14, 2023
Issue number: N/A

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

Our Angular E2E tests are brittle because they rely on arbitrary
`cy.wait` calls to account for asynchronous routing. This leads to flaky
tests on CI and seemingly random test failures when we make adjustments
to the Ionic Anguar routing integration (see:
#28188)

Additionally, our test execution for the navigation tests is quite slow
because transitions are enabled. As a result, we need to wait hundreds
of ms per test just for the transitions to finish.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Updated the `testStack` command to use a new `getStack` [Cypress
query](https://docs.cypress.io/api/cypress-api/custom-queries). These
queries come with automatic retrying built-in. By leveraging this query
in the `testStack` command, we can avoid the arbitrary waits.
- Added `ionic:_testing=true` query strings to the navigation tests.
This causes Ionic to disable any transitions so the tests execute
faster.
- Removed most of the arbitrary `cy.wait` calls. I kept the swipe to go
back `cy.wait` -- I wasn't quite sure how to reduce flakiness on that
one.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->
@liamdebeasi
Copy link
Contributor

Hey there!

I spoke with the team, and we would like to proceed with the fix proposed in #28188 (review) to resolve the reported issue. I was also able to avoid the need for increased timeouts in the tests through 093c671.

We looked at the behavior I noted in #28188 (comment) and realized that this was only working by chance. Ionic Angular was never respecting routerDirection when using the browser back/forward buttons. Since there was no page transition when using those buttons, the resulting behavior happened to align with the behavior specified in routerDirection="none" for the side menu starter app.

To mitigate any potential impact that this change has, we'd like to release this fix in Ionic 8. I don't have a timeline for Ionic 8, but we are actively working on it. I opened a new PR in #28530 with this fix based off the Ionic 8 development branch. I have also given you co-author credit on the commit for this fix. There is a dev build on the PR so you can try the fix in your application. Note that this dev build is subject to any Ionic 8 Breaking Changes (there aren't many at the moment).

I am going to close this in favor of the linked PR, but let me know if you have any questions. Thank you again for contributing this fix!

@hoi4
Copy link
Contributor Author

hoi4 commented Nov 15, 2023

We have a big release coming up where this change would be very valuable. But I do understand the decision. Looking forward to Ionic 8! 👍🏻

liamdebeasi added a commit that referenced this pull request Nov 16, 2023
Issue number: resolves #16569

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

Ionic Angular's routing integration disables page transitions when using
the browser back/forward buttons.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Transitions now play when using the back/forward buttons

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

We're not aware of any breaking changes here, though it's possible some
developers were relying on this behavior. As a result, we are targeting
Ionic 8 to minimize any potential negative impact this fix may have on
developers.

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Supersedes #28188

Dev build: `7.5.6-dev.11700068172.15ce9b35`

Co-authored-by: hoi4 <hoi4@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: angular @ionic/angular package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[4.0.0-beta.17] Angular's Location.back() has no navigation animation
3 participants