framework
framework copied to clipboard
feat(pages): default scroll behavior
🔗 Linked issue
❓ Type of change
- [ ] 📖 Documentation (updates to the documentation or readme)
- [ ] 🐞 Bug fix (a non-breaking change that fixes an issue)
- [ ] 👌 Enhancement (improving an existing functionality like performance)
- [x] ✨ New feature (a non-breaking change that adds functionality)
- [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to change)
📚 Description
Added new hook for page:transition:finish emitted from NuxtPage. Hook fires after the vue transition afterLeave event.
Also added default scroll behavior, which can still be overridden by using app/router.options
📝 Checklist
- [x] I have linked an issue or discussion.
- [x] I have updated the documentation accordingly.
Deploy Preview for nuxt3-docs canceled.
| Name | Link |
|---|---|
| Latest commit | 023936fbb14316edc268affc78662b86c53f1542 |
| Latest deploy log | https://app.netlify.com/sites/nuxt3-docs/deploys/634ff01efa0257000931f48e |
@joel-wenzel sorry, your PR got stalled. Can you please consider rebasing it?
@posva Can you please confirm if this changes are looking good to you or any change needed?
Merging this PR would fix the issue nuxt/nuxt.js#13284 btw
@joel-wenzel sorry, your PR got stalled. Can you please consider rebasing it?
@pi0 No problem. Rebased. Should be all good now
@pi0 I merged instead of rebase. If you would rather I make a new branch/PR let me know
When will it be merged? Current Nuxt 3 page link behaviour is counter-intuitive because page sticks on current position of previous page.
Update 2022-06-04: When playing around with Content Wind I noticed that scroll behaviour was as expected. So I checked for a custom router configuration et voila:
import type { RouterConfig } from '@nuxt/schema'
// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
scrollBehavior (to, _, savedPosition) {
const nuxtApp = useNuxtApp()
// If history back
if (savedPosition) {
// Handle Suspense resolution
return new Promise((resolve) => {
nuxtApp.hooks.hookOnce('page:finish', () => {
setTimeout(() => resolve(savedPosition), 50)
})
})
}
// Scroll to heading on click
if (to.hash) {
setTimeout(() => {
const heading = document.querySelector(to.hash) as any
return window.scrollTo({
top: heading.offsetTop,
behavior: 'smooth'
})
})
return
}
// Scroll to top of window
return { top: 0 }
}
}
This might also solve your problems with latest plain Nuxt 3 apps.
bump
When will it be merged? Current Nuxt 3 page link behaviour is counter-intuitive because page sticks on current position of previous page.
Update 2022-06-04: When playing around with Content Wind I noticed that scroll behaviour was as expected. So I checked for a custom router configuration et voila:
import type { RouterConfig } from '@nuxt/schema' // https://router.vuejs.org/api/#routeroptions export default <RouterConfig>{ scrollBehavior (to, _, savedPosition) { const nuxtApp = useNuxtApp() // If history back if (savedPosition) { // Handle Suspense resolution return new Promise((resolve) => { nuxtApp.hooks.hookOnce('page:finish', () => { setTimeout(() => resolve(savedPosition), 50) }) }) } // Scroll to heading on click if (to.hash) { setTimeout(() => { const heading = document.querySelector(to.hash) as any return window.scrollTo({ top: heading.offsetTop, behavior: 'smooth' }) }) return } // Scroll to top of window return { top: 0 } } }This might also solve your problems with latest plain Nuxt 3 apps.
The problem of this, at least in content-wind, is that the scroll starts when the next page has yet to be mounted
Any updates?
Any updates?
I am waiting until I hear from the core team before fixing merge conflicts. I have fixed them 3 or 4 times now only to become outdated again
Hi @joel-wenzel. Sorry, it took long on your pull request. I was mainly waiting for @posva's take about the possibility of moving this to the vue-router core. If you can rebase your PR I can locally test and merge to iterate.
@pi0 No worries. Should be all set now if you want to test.
Nuxt team, please look into this 🙏
Nuxt team, please look into this 🙏
please too
This new hook might help with this issue: https://github.com/vuejs/core/issues/5844
When this will be merged? :O
Waiting for a solution.....
Any new updates?
Hoping that this PR will find itself merged into one of the upcoming rcs soon!
Hoping that this PR will find itself merged into one of the upcoming rcs soon!
You can snag the one from the 'Wind' nuxt content template. Just copy the /app/router.config.ts.
Thank you so much for working on this @joel-wenzel and sorry it took long to land!
I've made a few refactors and fixes on top of your work.
Hello @pi0, I've updated to rc.13 today but the scroll behaviour do not work correctly. When I navigate back, the scroll doesn't go to the save position but it's reset instead.
The problem cause by this line in file: packages/nuxt/src/pages/runtime/router.options.ts
const hasTransition = to.meta.pageTransition !== false && from.meta.pageTransition !== false;
to.meta.pageTransition was undefined so the strict equal not work in here.