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: Link component is not respecting scroll-behavior: smooth when only hash changes. #54240

Closed
1 task done
coreyleelarson opened this issue Aug 18, 2023 · 2 comments · Fixed by #54243
Closed
1 task done
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@coreyleelarson
Copy link
Contributor

Verify canary release

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

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.15.1
      npm: 8.11.0
      Yarn: 1.22.19
      pnpm: 8.6.11
    Relevant Packages:
      next: 13.4.18-canary.0
      eslint-config-next: 13.4.18-canary.0
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

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

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

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

https://github.com/coreyleelarson/scroll-behavior-smooth

To Reproduce

Toggle between the different nav links to observe the difference in behavior between the anchors and Link components.

Describe the Bug

Link component is not respecting scroll-behavior: smooth when only hash changes. If I append !important it works, but that should not be required.

There was a previous bug report for this (#51721) but the linked fix does not appear to resolve the issue in the latest version.

Expected Behavior

Link component should scroll smoothly to the target anchor when scroll-behavior: smooth is set without !important.

Which browser are you using? (if relevant)

Chrome 116.0.5845.96

How are you deploying your application? (if relevant)

No response

@coreyleelarson coreyleelarson added the bug Issue was opened via the bug report template. label Aug 18, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Aug 18, 2023
@coreyleelarson
Copy link
Contributor Author

coreyleelarson commented Aug 18, 2023

I did a little digging into this, and it seems the issue might be that focusAndScrollRef.onlyHashChange = false is being set before calling handleSmoothScroll is called in packages/next/src/client/components/layout-router.tsx.

Screenshot 2023-08-18 at 1 43 29 PM

if (options.onlyHashChange) is therefore always false in handleSmoothScroll sets htmlElement.style.scrollBehavior = 'auto' before scrollIntoView() is called.

Screenshot 2023-08-18 at 1 44 20 PM

If I move focusAndScrollRef.onlyHashChange = false to after the handleSmoothScroll invocation, it fixes the issue. I'd be willing to PR it, but I don't know enough about the code to know if this would cause any other issues.

@kodiakhq kodiakhq bot closed this as completed in #54243 Aug 18, 2023
kodiakhq bot pushed a commit that referenced this issue Aug 18, 2023
…sh is changed. (#54243)

Fixes #54240

The Link component in App Router is not respecting `scroll-behavior: smooth` when only the hash changes.

It appears to be caused by `focusAndScrollRef.onlyHashChange = false` being set before `handleSmoothScroll` is called, causing `htmlElement.style.scrollBehavior` to be overridden to `auto` before `scrollIntoView` is called.

Moving `focusAndScrollRef.onlyHashChange = false` to after `handleSmoothScroll` invocation resolves the issue.
@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2023

This closed issue has been automatically locked because it had no new activity for 2 weeks. 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 added the locked label Sep 2, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 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. 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.

1 participant