Unverified Commit 8664ffe2 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix: allow smooth scrolling if only hash changes (pages & app) (#52915)

We were preventing smooth scrolling to avoid jarring UX when `scroll-behavior: smooth` was set and the user navigates to another route ([PR](https://github.com/vercel/next.js/pull/40642) and [related comment](https://github.com/vercel/next.js/issues/51721#issuecomment-1623416600)). 

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

Fixes #51721
Closes NEXT-1406
parent 4994a428
...@@ -158,7 +158,7 @@ interface ScrollAndFocusHandlerProps { ...@@ -158,7 +158,7 @@ interface ScrollAndFocusHandlerProps {
segmentPath: FlightSegmentPath segmentPath: FlightSegmentPath
} }
class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerProps> { class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerProps> {
handlePotentialScroll = () => { handlePotentialScroll = (isUpdate?: boolean) => {
// Handle scroll and focus, it's only applied once in the first useEffect that triggers that changed. // Handle scroll and focus, it's only applied once in the first useEffect that triggers that changed.
const { focusAndScrollRef, segmentPath } = this.props const { focusAndScrollRef, segmentPath } = this.props
...@@ -247,6 +247,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr ...@@ -247,6 +247,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
{ {
// We will force layout by querying domNode position // We will force layout by querying domNode position
dontForceLayout: true, dontForceLayout: true,
onlyHashChange: !!isUpdate,
} }
) )
...@@ -262,7 +263,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr ...@@ -262,7 +263,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
componentDidUpdate() { componentDidUpdate() {
// Because this property is overwritten in handlePotentialScroll it's fine to always run it when true as it'll be set to false for subsequent renders. // Because this property is overwritten in handlePotentialScroll it's fine to always run it when true as it'll be set to false for subsequent renders.
if (this.props.focusAndScrollRef.apply) { if (this.props.focusAndScrollRef.apply) {
this.handlePotentialScroll() this.handlePotentialScroll(true)
} }
} }
......
...@@ -2247,27 +2247,35 @@ export default class Router implements BaseRouter { ...@@ -2247,27 +2247,35 @@ export default class Router implements BaseRouter {
scrollToHash(as: string): void { scrollToHash(as: string): void {
const [, hash = ''] = as.split('#') const [, hash = ''] = as.split('#')
// Scroll to top if the hash is just `#` with no value or `#top`
// To mirror browsers
if (hash === '' || hash === 'top') {
handleSmoothScroll(() => window.scrollTo(0, 0))
return
}
// Decode hash to make non-latin anchor works. handleSmoothScroll(
const rawHash = decodeURIComponent(hash) () => {
// First we check if the element by id is found // Scroll to top if the hash is just `#` with no value or `#top`
const idEl = document.getElementById(rawHash) // To mirror browsers
if (idEl) { if (hash === '' || hash === 'top') {
handleSmoothScroll(() => idEl.scrollIntoView()) window.scrollTo(0, 0)
return return
} }
// If there's no element with the id, we check the `name` property
// To mirror browsers // Decode hash to make non-latin anchor works.
const nameEl = document.getElementsByName(rawHash)[0] const rawHash = decodeURIComponent(hash)
if (nameEl) { // First we check if the element by id is found
handleSmoothScroll(() => nameEl.scrollIntoView()) const idEl = document.getElementById(rawHash)
} if (idEl) {
idEl.scrollIntoView()
return
}
// If there's no element with the id, we check the `name` property
// To mirror browsers
const nameEl = document.getElementsByName(rawHash)[0]
if (nameEl) {
nameEl.scrollIntoView()
}
},
{
onlyHashChange: this.onlyAHashChange(as),
}
)
} }
urlIsNew(asPath: string): boolean { urlIsNew(asPath: string): boolean {
......
...@@ -4,8 +4,14 @@ ...@@ -4,8 +4,14 @@
*/ */
export function handleSmoothScroll( export function handleSmoothScroll(
fn: () => void, fn: () => void,
options: { dontForceLayout?: boolean } = {} options: { dontForceLayout?: boolean; onlyHashChange?: boolean } = {}
) { ) {
// if only the hash is changed, we don't need to disable smooth scrolling
// we only care to prevent smooth scrolling when navigating to a new page to avoid jarring UX
if (options.onlyHashChange) {
fn()
return
}
const htmlElement = document.documentElement const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior const existing = htmlElement.style.scrollBehavior
htmlElement.style.scrollBehavior = 'auto' htmlElement.style.scrollBehavior = 'auto'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment