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
Conversation
this.switchTo_(args['id'], NavigationDirection.NEXT); | ||
this.closeOpacityMask_(); | ||
} | ||
} this.switchTo_(args['id'], NavigationDirection.NEXT); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
ca62b59
to
f821798
Compare
|
||
if (caller['offsetParent'].tagName === 'AMP-SIDEBAR') { | ||
this.sidebar_.getImpl() | ||
.then(sidebarImpl => sidebarImpl.close_()); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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:
- Load the story, you're on the cover page
- Open the sidebar and click on a link or action to go to page 2
- You're on page2, you click next, you're on page 3
- Navigate back
- 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.
There was a problem hiding this comment.
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).
} | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
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 tostory.com/2#page=foo
would not navigate tostory.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?
There was a problem hiding this comment.
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
89184d5
to
b8cdbfd
Compare
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 鈩癸笍 Googlers: Go here for more info. |
b8cdbfd
to
ffa60db
Compare
CLAs look good, thanks! 鈩癸笍 Googlers: Go here for more info. |
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Is it always exactly the parent? Or are we really expecting it to be any ancestor?
- Can we use
caller.offsetParent
instead ofcaller['offsetParent']
?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Is this PR still active? |
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. |
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@newmuis is this a candidate for a fixit fix? :) /cc @ampproject/wg-stories |
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. :) |
Closed in favor of #24968 |
#20083
This PR makes the necessary changes to make both
goToPage
actions and fragments work when used withamp-sidebar
inamp-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
amp-story-page
should still workTests have not yet been written; there will be a follow-up PR with e2e tests.