Skip to content

Commit

Permalink
Ensures App Router Link respects scroll-behavior: smooth when only ha…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
coreyleelarson committed Aug 18, 2023
1 parent 0e78798 commit 42abfd6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr

// State is mutated to ensure that the focus and scroll is applied only once.
focusAndScrollRef.apply = false
focusAndScrollRef.onlyHashChange = false
focusAndScrollRef.hashFragment = null
focusAndScrollRef.segmentPaths = []

Expand Down Expand Up @@ -263,6 +262,9 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
}
)

// Mutate after scrolling so that it can be read by `handleSmoothScroll`
focusAndScrollRef.onlyHashChange = false

// Set focus on the element
domNode.focus()
}
Expand Down

0 comments on commit 42abfd6

Please sign in to comment.