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

Add backButtonClicked event to page component #2316

Merged
merged 2 commits into from
Jun 17, 2022

Conversation

mictro
Copy link
Contributor

@mictro mictro commented Jun 8, 2022

Which issue does this PR close?

This PR closes #2119

What is the new behavior?

Add a back-button event-emitter outpput option to page component. When set the default back-button behaviour is removed.

Does this PR introduce a breaking change?

  • Yes
  • No

Are there any additional context?

This change allows applications to navigate past the routing restrictions present in micro-frontends.

Checklist:

The following tasks should be carried out in sequence in order to follow the process of contributing correctly.

Reminders

  • Make sure you have implemented tests following the guidelines in: "The good: Test".
  • Make sure you have updated the cookbook with examples and showcases (for bug fixes, enhancements & new components).

Review

  • Do a self-review.
  • Request that the changes are code-reviewed
  • Request that the changes are UX reviewed (only necessary if your PR introduces visual changes)

When the pull request has been approved it will be merged to develop by Team Kirby.

@mictro mictro requested a review from MadsBuchmann June 8, 2022 11:16
@github-actions github-actions bot temporarily deployed to pr-2119-add-back-button-click-event-to-page June 8, 2022 11:21 Inactive
Copy link
Contributor

@MadsBuchmann MadsBuchmann left a comment

Choose a reason for hiding this comment

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

It seems like the default behaviour has broken somehow 🤔

Here's how the back button behaves in our showcase without any backButtonClicked event handler registered on the main branch:

main-branch.mp4

On this branch nothing happens however.

@@ -161,6 +161,7 @@ export class PageComponent
@Output() enter = new EventEmitter<void>();
@Output() leave = new EventEmitter<void>();
@Output() refresh = new EventEmitter<PullToRefreshEvent>();
@Output() backButtonClicked = new EventEmitter<Event>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at this again i think we should change the name to be in imperative form, considering other events are named like that as well :D

Suggested change
@Output() backButtonClicked = new EventEmitter<Event>();
@Output() backButtonClick = new EventEmitter<Event>();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, changed

@@ -259,6 +262,16 @@ export class PageComponent
this.windowRef.nativeWindow.addEventListener(selectedTabClickEvent, () => {
this.content.scrollToTop(KirbyAnimation.Duration.LONG);
});

// Intercept back-button click events, defaulting to the built-in click-handler.
if (this.backButtonClicked.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there a specific reason for using the equality - instead of the identity operator?

Personally i prefer the more strict identity operator as it is more predictable how it will behave 😄

Suggested change
if (this.backButtonClicked.length == 0) {
if (this.backButtonClicked.length === 0) {

Copy link
Contributor Author

@mictro mictro Jun 16, 2022

Choose a reason for hiding this comment

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

Doh! Done :-)

Comment on lines 266 to 274
// Intercept back-button click events, defaulting to the built-in click-handler.
if (this.backButtonClicked.length == 0) {
this.backButtonClicked
.pipe(takeUntil(this.ngOnDestroy$))
.subscribe(this.backButtonDelegate.onClick.bind(this.backButtonDelegate));
}
this.backButtonDelegate.onClick = (event: Event) => {
this.backButtonClicked.emit(event);
};
Copy link
Contributor

@MadsBuchmann MadsBuchmann Jun 14, 2022

Choose a reason for hiding this comment

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

We could consider separating all of this into a InterceptBackButtonClicksSetup method (or similar) to make it more clear that this code belongs together and what its responsibility is

Copy link
Contributor Author

@mictro mictro Jun 16, 2022

Choose a reason for hiding this comment

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

Added

Comment on lines 144 to 169
describe('with a back-button-clicked observer', () => {
it('should emit an event when the back button is clicked', () => {
const ionBackButton = spectator.queryHost('ion-toolbar ion-buttons ion-back-button');
const subscriber = jasmine.createSpy();
spectator.output('backButtonClicked').subscribe(subscriber);

spectator.click(ionBackButton);
spectator.detectComponentChanges();

expect(subscriber).toHaveBeenCalledTimes(1);
});
});

Copy link
Contributor

Choose a reason for hiding this comment

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

We could also add a test which checks that the default behaviour is executed whenever no observer is present

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

@mictro mictro force-pushed the enhancement/2119-add-back-button-click-event-to-page branch from de9e84b to e7d9041 Compare June 16, 2022 09:09
@mictro mictro force-pushed the enhancement/2119-add-back-button-click-event-to-page branch from e7d9041 to fc9fa15 Compare June 16, 2022 11:00
@mictro mictro requested review from RasmusKjeldgaard and MadsBuchmann and removed request for MadsBuchmann June 16, 2022 11:03
@github-actions github-actions bot temporarily deployed to pr-2119-add-back-button-click-event-to-page June 16, 2022 11:05 Inactive
@mictro mictro removed the request for review from RasmusKjeldgaard June 17, 2022 07:15
@github-actions github-actions bot temporarily deployed to pr-2119-add-back-button-click-event-to-page June 17, 2022 07:18 Inactive
Copy link
Contributor

@MadsBuchmann MadsBuchmann left a comment

Choose a reason for hiding this comment

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

One minor documentation thing other than that it looks goodie!

@@ -86,6 +86,12 @@ export class PageShowcaseComponent {
description: 'Emitted when leaving the page',
signature: 'func',
},
{
name: 'backButtonClicked',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
name: 'backButtonClicked',
name: 'backButtonClick',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated and regenerated mock

@mictro mictro force-pushed the enhancement/2119-add-back-button-click-event-to-page branch from 4d19f42 to a998ff9 Compare June 17, 2022 09:31
@mictro mictro requested a review from MadsBuchmann June 17, 2022 09:38
Copy link
Contributor

@MadsBuchmann MadsBuchmann left a comment

Choose a reason for hiding this comment

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

LGTM!

@mictro mictro merged commit 6750f45 into develop Jun 17, 2022
@mictro mictro deleted the enhancement/2119-add-back-button-click-event-to-page branch June 17, 2022 10:15
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.

[Enhancement] Add "backButtonClick" event to page
2 participants