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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰Ensure that goToPage actions and fragment Urls work in sidebar for stories #20944

Closed
wants to merge 18 commits into from

Conversation

bramanudom
Copy link
Contributor

#20083

This PR makes the necessary changes to make both goToPage actions and fragments work when used with amp-sidebar in amp-stories, which helps make branching use-cases like table of contents possible.

Previously, clicking on elements with the goToPage action triggers a page change but does not close the sidebar or the sidebar mask. Fragment Urls should be supported as they match with conventions surrounding making ToC in general.

Link to example

  • Open sidebar, click on either links to navigate to the second page.
  • Elements with a goToPage actions in amp-story-page should still work

Tests have not yet been written; there will be a follow-up PR with e2e tests.

this.switchTo_(args['id'], NavigationDirection.NEXT);
this.closeOpacityMask_();
}
} this.switchTo_(args['id'], NavigationDirection.NEXT);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can you put this on its own line, instead of on the same line as the brace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -2216,6 +2239,24 @@ export class AmpStory extends AMP.BaseElement {
if (!this.sidebar_) {
return;
}
if (isExperimentOn(this.win,'amp-story-branching')) {
this.sidebar_.addEventListener('click', e => {
if (e.target.tagName === 'A') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder: is this necessarily sufficient to know that it was a fragment link?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, you're right. I added in a check to make sure that the link contains "#page=" and I think that should be sufficient to make sure that the following steps don't apply to outlinks.

I updated the example linked in the PR description to reflect this change.

@bramanudom bramanudom force-pushed the sidebar_links branch 2 times, most recently from ca62b59 to f821798 Compare February 20, 2019 00:09

if (caller['offsetParent'].tagName === 'AMP-SIDEBAR') {
this.sidebar_.getImpl()
.then(sidebarImpl => sidebarImpl.close_());
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't call a private method, we'd have to make this one public. :(

if (caller['offsetParent'].tagName === 'AMP-SIDEBAR') {
this.sidebar_.getImpl()
.then(sidebarImpl => sidebarImpl.close_());
this.switchTo_(args['id'], NavigationDirection.NEXT);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is introducing a bug. Let's imagine this story has a third page:

  1. Load the story, you're on the cover page
  2. Open the sidebar and click on a link or action to go to page 2
  3. You're on page2, you click next, you're on page 3
  4. Navigate back
  5. Expected: now on page2 / Actual: now on page1

(I haven't tried this exact scenario, but from the history state bug this should happen)

When the sidebar opens, it pushes a new history entry. Because you're using a promise to get the sidebar implementation, the sidebarImpl.close() will happen after the switchTo.
switchTo writes the history state in the entry added by the sidebar, that is then popped by sidebarImpl.close(). So the navigation path actually never got updated, resulting in the navigation bug described above.

An easy fix would be to call switchTo after the sidebar's history entry is popped.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Synched offline but you're right that there was a historyState bug, but it actually corrects itself as you navigate forwards.

With the changes I just pushed, when you navigate using a sidebar link (not action), checking the ampStoryNavigationPath will yield an undefined state, but that also corrects itself when you either navigates forwards or backwards (this is due to manipulating the history state to not show the fragment as the user is navigating via the sidebar links/actions).

}
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A few things about this block:

  • If the link is meant to navigate to another story, this will fail. ie: sidebar in story.com/1 links to story.com/2#page=foo would not navigate to story.com/2
  • This will fail if there is any element within the <a> tag. ie: <a href="#page=foo">Foo</a> will work, but <a href="#page=foo"><span>Foo</span></a> won't
  • Does this piece of code have the same history state issue described above?

Could we do all that from the hashchange handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We took this offline a little, but done! Utilized hashchange handler for fragments, but not for goToPage. I also added a check for the pathname, to handler for href values that look like #page=somePage or story.com/2#page=somePage if the story was on story.com/2

@googlebot
Copy link

So there's good news and bad news.

馃憤 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

馃槙 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

鈩癸笍 Googlers: Go here for more info.

@googlebot
Copy link

CLAs look good, thanks!

鈩癸笍 Googlers: Go here for more info.

@bramanudom
Copy link
Contributor Author

I think this is ready for review! @gmajoulet @newmuis @cvializ

@@ -29,18 +29,19 @@
</head>

<body>
<amp-story standalone publisher="AMP Team" title="Visual Diff Test"
<amp-story id = "story" standalone publisher="AMP Team" title="Visual Diff Test"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove spaces around the = sign

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

<li>Nav item 3</li>
<li><a href="#idFour" on="tap:idFour.scrollTo">Nav item 4</a></li>
<li><span><a href="http://google.com">Out item 1</a></span></li>
<li><span><a href="http://localhost:8000/examples/visual-tests/amp-story/amp-story-sidebar.html#page=page-2">Absolute fragment link</a></span></li>
Copy link
Contributor

Choose a reason for hiding this comment

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

These examples are intended to work regardless of the server. This will break if someone changes the port, or serves their examples off of, for example, Firebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, yes. Thank you for pointing this out! Done.

if (args) {
this.storeService_.dispatch(
Action.SET_ADVANCEMENT_MODE, AdvancementMode.GO_TO_PAGE);
this.switchTo_(args['id'], NavigationDirection.NEXT);

if (caller['offsetParent'].tagName === 'AMP-SIDEBAR') {
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Is it always exactly the parent? Or are we really expecting it to be any ancestor?
  2. Can we use caller.offsetParent instead of caller['offsetParent']?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the code to use closestAncestorElementBySelector since it seems like what we care about is that at some point the callers ancestor is an amp-sidebar.

// Remove the fragment parameter from the URL
const {history} = this.win;
history.pushState(
'', this.win.document.title, this.win.location.pathname);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment to document the first parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and for the other args as well.

@mrjoro
Copy link
Member

mrjoro commented May 15, 2019

Is this PR still active?

@newmuis
Copy link
Contributor

newmuis commented May 21, 2019

Yes, it would be great for us to get this merged. I'll see if I have some time next week; I think it's mostly done, but we just need to rebase and manually test.

@bramanudom
Copy link
Contributor Author

bramanudom commented May 21, 2019

Hello, I can go ahead and rebase today. I'll also manually test but I'll leave it to @newmuis and the team to officially verify.

@@ -444,6 +444,7 @@ export class AmpStory extends AMP.BaseElement {

if (args) {
this.storeService_.dispatch(
<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like there are still change markers here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I think that I already got rid of this in this commit.

I double checked those lines in the Files Changed tab in this PR as well.

@mrjoro
Copy link
Member

mrjoro commented Aug 8, 2019

@newmuis is this a candidate for a fixit fix? :)

/cc @ampproject/wg-stories

@gmajoulet
Copy link
Contributor

I rebased Pet's code and pushed it to a new branch here, I'll double check that everything still works and send a PR next week. :)

@gmajoulet
Copy link
Contributor

Closed in favor of #24968

@gmajoulet gmajoulet closed this Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants