framework icon indicating copy to clipboard operation
framework copied to clipboard

feat(pages): default scroll behavior

Open joel-wenzel opened this issue 3 years ago • 18 comments

🔗 Linked issue

Discussion #3338

❓ 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.

joel-wenzel avatar Mar 23 '22 01:03 joel-wenzel

Deploy Preview for nuxt3-docs canceled.

Name Link
Latest commit 023936fbb14316edc268affc78662b86c53f1542
Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/634ff01efa0257000931f48e

netlify[bot] avatar Mar 23 '22 01:03 netlify[bot]

@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?

pi0 avatar Apr 22 '22 15:04 pi0

Merging this PR would fix the issue nuxt/nuxt.js#13284 btw

fanckush avatar Apr 30 '22 21:04 fanckush

@joel-wenzel sorry, your PR got stalled. Can you please consider rebasing it?

@pi0 No problem. Rebased. Should be all good now

joel-wenzel avatar May 09 '22 22:05 joel-wenzel

@pi0 I merged instead of rebase. If you would rather I make a new branch/PR let me know

joel-wenzel avatar May 21 '22 14:05 joel-wenzel

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.

bart avatar Jun 01 '22 17:06 bart

bump

fanckush avatar Jun 16 '22 09:06 fanckush

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

Rigo-m avatar Jun 16 '22 09:06 Rigo-m

Any updates?

axelthat avatar Jun 22 '22 07:06 axelthat

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

joel-wenzel avatar Jun 22 '22 11:06 joel-wenzel

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 avatar Jun 22 '22 12:06 pi0

@pi0 No worries. Should be all set now if you want to test.

joel-wenzel avatar Jun 22 '22 22:06 joel-wenzel

Nuxt team, please look into this 🙏

axelthat avatar Jul 18 '22 07:07 axelthat

Nuxt team, please look into this 🙏

please too

jakubM817 avatar Jul 20 '22 11:07 jakubM817

This new hook might help with this issue: https://github.com/vuejs/core/issues/5844

nandi95 avatar Aug 04 '22 16:08 nandi95

When this will be merged? :O

Gyurmatag avatar Aug 17 '22 07:08 Gyurmatag

Waiting for a solution.....

rashidpathiyil avatar Aug 19 '22 19:08 rashidpathiyil

Any new updates?

axelthat avatar Sep 16 '22 12:09 axelthat

Hoping that this PR will find itself merged into one of the upcoming rcs soon!

kemalcany avatar Oct 10 '22 17:10 kemalcany

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.

jiblett1000 avatar Oct 10 '22 18:10 jiblett1000

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.

pi0 avatar Oct 19 '22 12:10 pi0

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.

lamdinh95 avatar Nov 05 '22 13:11 lamdinh95

This should be resolved in the edge channel, or in the next RC.

Let me know if not.

danielroe avatar Nov 05 '22 14:11 danielroe