- Save scroll position with history
Save scroll position when navigating away from a page, and restore it when navigating back to that page.
Fixes #30.
- Scroll to element position when URL contains a hash
When the URL contains a hash, try to find the corresponding element, and if found, scroll to its position.
Based on darylteo/pjax@4893a2a6574b3490396f83a1863499e8a9ba171e
Fixes #22.
This will only run if state.options.history is falsy, which I don't think is what you intended?
Perhaps something like
elseif(state.options.scrollTo!==false){// Update window scroll position
if(Array.isArray(state.options.scrollTo)){window.scrollTo(state.options.scrollTo[0],state.options.scrollTo[1])}elseif(typeofstate.options.scrollTo==="number"){window.scrollTo(0,state.options.scrollTo)}else{window.scrollTo(state.options.scrollPos[0],state.options.scrollPos[1])}}
instead?
This would require the default value of this.options.scrollTo to change to true as well -- currently it's set to 0 if one is not provided by the user, meaning that the window will always be scrolled back to the top. If we didn't want to break backwards compatibility, then we could accept 'auto' as a value for the option to trigger scroll restoration, but it's probably a behavior that users would expect and prefer to happen -- what do you think?
This will only run if `state.options.history` is falsy, which I don't think is what you intended?
Perhaps something like
```js
else if (state.options.scrollTo !== false) {
// Update window scroll position
if (Array.isArray(state.options.scrollTo)) {
window.scrollTo(state.options.scrollTo[0], state.options.scrollTo[1])
}
else if (typeof state.options.scrollTo === "number") {
window.scrollTo(0, state.options.scrollTo)
}
else {
window.scrollTo(state.options.scrollPos[0], state.options.scrollPos[1])
}
}
```
instead?
This would require the default value of `this.options.scrollTo` to change to `true` as well -- currently it's set to `0` if one is not provided by the user, meaning that the window will always be scrolled back to the top. If we didn't want to break backwards compatibility, then we could accept `'auto'` as a value for the option to trigger scroll restoration, but it's probably a behavior that users would expect and prefer to happen -- what do you think?
No, I believe my code is correct. options.history is true when we're navigating to a page, and false when the initiator was popstate. This is because you don't want to call pushState() when you're in middle of a popstate event.
So we only want to use the scroll position stored in history when we're responding to a popstate event, in which case options.history is false. Otherwise, when it's true, we want to either use the hash, or scrollTo.
See earlier in #46, where we decided the default when clicking a link should be to scroll to the top.
No, I believe my code is correct. `options.history` is true when we're navigating to a page, and false when the initiator was `popstate`. This is because you don't want to call `pushState()` when you're in middle of a `popstate` event.
So we only want to use the scroll position stored in history when we're responding to a `popstate` event, in which case `options.history` is false. Otherwise, when it's true, we want to either use the hash, or `scrollTo`.
See earlier in #46, where we decided the default when clicking a link should be to scroll to the top.
Oh dear, that's a comprehension fail on my part, sorry! You are correct.
One thing that would be good to change though is to only restore the scroll position on popstate if this.options.scrollTo is not false -- another thing from #46 was a decision to let developers to decide on what is the best UX for their app, so we should make sure that this scroll restoration can be opted out of, don't you think? This matches the current behaviour when switching.
Oh dear, that's a comprehension fail on my part, sorry! You are correct.
One thing that would be good to change though is to only restore the scroll position on popstate if `this.options.scrollTo` is not `false` -- another thing from #46 was a decision to let developers to decide on what is the best UX for their app, so we should make sure that this scroll restoration can be opted out of, don't you think? This matches the current behaviour when switching.
options.scrollTo is only documented as taking a number value. From the README:
scrollTo (Integer, default to 0)
I'm not even sure why the check for options.scrollTo !== false exists at all.
I think we should add a scrollRestoration option instead, similar to the HTML spec (see #40).
`options.scrollTo` is only documented as taking a `number` value. From the README:
> `scrollTo` (Integer, default to 0)
I'm not even sure why the check for `options.scrollTo !== false` exists at all.
I think we should add a `scrollRestoration` option instead, similar to the HTML spec (see #40).
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Save scroll position with history
Save scroll position when navigating away from a page, and restore it when navigating back to that page.
Fixes #30.
Scroll to element position when URL contains a hash
When the URL contains a hash, try to find the corresponding element, and if found, scroll to its position.
Based on darylteo/pjax@4893a2a657
Fixes #22.
Wow, nice improvement!
I think you need to reset
scrollPosto be[0, 0]rather than just0, so that you're always storing the same type of data against this key.@@ -220,3 +243,3 @@if (state.options.history) {if (this.firstrun) {if (!window.history.state) {this.lastUid = this.maxUid = newUid()This also should be
[0, 0], per my other comment@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,This will only run if
state.options.historyis falsy, which I don't think is what you intended?Perhaps something like
instead?
This would require the default value of
this.options.scrollToto change totrueas well -- currently it's set to0if one is not provided by the user, meaning that the window will always be scrolled back to the top. If we didn't want to break backwards compatibility, then we could accept'auto'as a value for the option to trigger scroll restoration, but it's probably a behavior that users would expect and prefer to happen -- what do you think?Good point.
@@ -220,3 +243,3 @@if (state.options.history) {if (this.firstrun) {if (!window.history.state) {this.lastUid = this.maxUid = newUid()Good point.
@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,No, I believe my code is correct.
options.historyis true when we're navigating to a page, and false when the initiator waspopstate. This is because you don't want to callpushState()when you're in middle of apopstateevent.So we only want to use the scroll position stored in history when we're responding to a
popstateevent, in which caseoptions.historyis false. Otherwise, when it's true, we want to either use the hash, orscrollTo.See earlier in #46, where we decided the default when clicking a link should be to scroll to the top.
@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,Oh dear, that's a comprehension fail on my part, sorry! You are correct.
One thing that would be good to change though is to only restore the scroll position on popstate if
this.options.scrollTois notfalse-- another thing from #46 was a decision to let developers to decide on what is the best UX for their app, so we should make sure that this scroll restoration can be opted out of, don't you think? This matches the current behaviour when switching.@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,options.scrollTois only documented as taking anumbervalue. From the README:I'm not even sure why the check for
options.scrollTo !== falseexists at all.I think we should add a
scrollRestorationoption instead, similar to the HTML spec (see #40).@@ -236,3 +259,3 @@url: state.href,title: state.options.title,uid: this.maxUiduid: this.maxUid,Yes, I wondered why it hadn't been documented.
Adding a
scrollRestorationoption sounds fine though.@robinnorth Are we good to merge this?
LGTM. Nice one!