-
Notifications
You must be signed in to change notification settings - Fork 712
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
Allow setting of scroll position based on browser history state. #5006
Allow setting of scroll position based on browser history state. #5006
Conversation
964ee39
to
4dc6b1f
Compare
Codecov Report
|
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.
Works great for coach pages, but not really for other pages. But hey, some working is better than none working?!
f908b4e
to
64702fe
Compare
CoreBase is not reinitialized on navigation.
64702fe
to
f9ee902
Compare
I updated behaviour after this review was submitted, so probably needs re-review!
Is the scroll position supposed to be unique for each page? The current behavior seems to apply the same position to the previous page even if the position was set at an even earlier page. For example
|
Hrm, good point - need to clear the vuex data when leaving a page, so that it is only set from the history state. |
Or rather, not the vuex state, but the window offset value - if you navigate to pages without ever scrolling, that doesn't get reset. |
Only conditionalize scroll setting on loading state.
// Create a watcher to monitor changes in loading | ||
// to try to set the scrollHeight after the contents | ||
// have loaded. | ||
this.unwatchScrollHeight = this.$watch('loading', () => { |
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 if unwatchScrollHeight
should be defined on data
and initialized to undefined
for explicitness? Seems a little weird to just slap it onto the view model.
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.
Why? It doesn't need to be reactive, putting it on the view itself is exactly the right place for it, the only thing that might be better is prefixing it with an underscore, but I really don't see any benefit to having it be a watched bit of internal state.
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.
Yeah agreed – I don't like the fact that it's reactive either. The thing that bothered me is to add it to the vm
namespace without declaring it anywhere.
There's a lively discussion here so apparently this is something that keeps people up at night :)
Looks like the original strategy you used is likely fine, and another common pattern is to put it inside $options
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 do find it a little odd that the github issue is so long. Either you put it in a namespace, or you put it right on the object. Vue doesn't, and shouldn't care!
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.
added 1a27eb6 to your PR
Summary
Monitors CoreBase scrolling, and uses that information to update window.pageYOffset to update the history state.
Sets a vuex state element to indicate the desired starting scroll position for the core base - this might be better implemented as an event, but this was easier.
CoreBase adds a watcher to this vuex state in the created property to set the scrollTop to this value when the router has set it.
Reviewer guidance
Does it provide the intended scrolling behaviour?
Could this be implemented in a better way? In what way might this be brittle?
References
Fixes #3659
Contributor Checklist
PR process:
Testing:
Reviewer Checklist
yarn
andpip
)