Skip to content
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

App Router: Next/Link does not respect smooth scroll #51721

Closed
1 task done
chrisgms opened this issue Jun 23, 2023 · 8 comments · Fixed by #52915
Closed
1 task done

App Router: Next/Link does not respect smooth scroll #51721

chrisgms opened this issue Jun 23, 2023 · 8 comments · Fixed by #52915
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@chrisgms
Copy link

chrisgms commented Jun 23, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

codesandbox on [email protected]

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/boring-carson-rhrf23?file=%2Fapp%2Flayout.tsx%3A9%2C1

To Reproduce

Use the two different navbars to see smooth scroll difference between Next/Link and normal anchor tag.

Describe the Bug

Next/Link does not scroll smoothly

Expected Behavior

Next/Link should scroll smoothly

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1406

@chrisgms chrisgms added the bug Issue was opened via the bug report template. label Jun 23, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Jun 23, 2023
@azvyae
Copy link

azvyae commented Jun 25, 2023

Possible related to #44295, only current workaround is by define the style inside global.css file with using !important directive

html {
 scroll-behavior: smooth !important;
}

@agrattan0820
Copy link
Contributor

Having the same issue as well, got a test deployment up in case that helps:
Vercel deployment: https://nextjs-scroll-to.vercel.app/
Code: https://github.com/agrattan0820/nextjs-scroll-to

@Manduro
Copy link

Manduro commented Jul 6, 2023

Possible related to #44295, only current workaround is by define the style inside global.css file with using !important directive

html {
 scroll-behavior: smooth !important;
}

Note that this has a possible unwanted side effect: navigating to a different page will not set the scroll position instantly but will also scroll to top smoothly.

@timneutkens
Copy link
Member

To clarify this further, you're expecting <Link href="#somewhere-on-the-same-page"> to be a smooth scroll and <Link href="/blog#somewhere-else"> to not do a smooth scroll right?

As @Manduro said we saw many cases where people added scroll-behavior: smooth and then navigations become a case of rendering the page, then smooth scrolling to the top, which is a very jarring UX. Hence why we override the smooth scrolling before navigating. However, I'm thinking of not overriding scroll-behavior when it's same-page applying a hash.

@timneutkens timneutkens added the linear: next Confirmed issue that is tracked by the Next.js team. label Jul 6, 2023
@chrisgms
Copy link
Author

chrisgms commented Jul 6, 2023

Perhaps a new prop to override the smooth behavior on a per case basis would be ideal to get a wider range of possibility.

<Link href="#somewhere-on-the-same-page"> would move instantly
<Link href="#somewhere-on-the-same-page" smooth> would scroll smoothly if scroll behavior smooth is set in css

and same for different pages:

<Link href="/blog#somewhere-else"> goes to top of page
<Link href="/blog#somewhere-else" smooth> scrolls to section

The last one may be weird depending if you go to the top first or not.

@timneutkens
Copy link
Member

I'd prefer not introducing different behavior / props, just mirroring what <a> does.

@chrisgms
Copy link
Author

chrisgms commented Jul 7, 2023

It would be great if different page hash link could also scroll smoothly, but if this would prove problematic with non-hash links scrolling to top then at least same-page hash smooth scroll as per your original suggestion would be fine. Worst case it should still be possible to achieve different page smooth scroll using normal <a> tags.

@kodiakhq kodiakhq bot closed this as completed in #52915 Jul 20, 2023
kodiakhq bot pushed a commit that referenced this issue Jul 20, 2023
We were preventing smooth scrolling to avoid jarring UX when `scroll-behavior: smooth` was set and the user navigates to another route ([PR](#40642) and [related comment](#51721 (comment))). 

This updates both pages & app router to restore smooth scroll functionality if the only the route hash changes.

Fixes #51721
Closes NEXT-1406
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants