Skip to content

Commit dc11d77

Browse files
authored
Ensure smooth scroll is disabled for navigation in new and existing router (#40642)
1 parent c742c03 commit dc11d77

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

packages/next/client/components/layout-router.client.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ export function InnerLayoutRouter({
167167
focusAndScrollElementRef.current.focus()
168168
// Only scroll into viewport when the layout is not visible currently.
169169
if (!topOfElementInViewport(focusAndScrollElementRef.current)) {
170+
const htmlElement = document.documentElement
171+
const existing = htmlElement.style.scrollBehavior
172+
htmlElement.style.scrollBehavior = 'auto'
170173
focusAndScrollElementRef.current.scrollIntoView()
174+
htmlElement.style.scrollBehavior = existing
171175
}
172176
}
173177
}, [focusAndScrollRef])

packages/next/client/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,11 @@ function doRender(input: RenderRouteInfo): Promise<any> {
688688
}
689689

690690
if (input.scroll) {
691+
const htmlElement = document.documentElement
692+
const existing = htmlElement.style.scrollBehavior
693+
htmlElement.style.scrollBehavior = 'auto'
691694
window.scrollTo(input.scroll.x, input.scroll.y)
695+
htmlElement.style.scrollBehavior = existing
692696
}
693697
}
694698

packages/next/shared/lib/router/router.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,14 @@ interface FetchNextDataParams {
655655
unstable_skipClientCache?: boolean
656656
}
657657

658+
function handleSmoothScroll(fn: () => void) {
659+
const htmlElement = document.documentElement
660+
const existing = htmlElement.style.scrollBehavior
661+
htmlElement.style.scrollBehavior = 'auto'
662+
fn()
663+
htmlElement.style.scrollBehavior = existing
664+
}
665+
658666
function tryToParseAsJSON(text: string) {
659667
try {
660668
return JSON.parse(text)
@@ -2141,7 +2149,7 @@ export default class Router implements BaseRouter {
21412149
// Scroll to top if the hash is just `#` with no value or `#top`
21422150
// To mirror browsers
21432151
if (hash === '' || hash === 'top') {
2144-
window.scrollTo(0, 0)
2152+
handleSmoothScroll(() => window.scrollTo(0, 0))
21452153
return
21462154
}
21472155

@@ -2150,14 +2158,14 @@ export default class Router implements BaseRouter {
21502158
// First we check if the element by id is found
21512159
const idEl = document.getElementById(rawHash)
21522160
if (idEl) {
2153-
idEl.scrollIntoView()
2161+
handleSmoothScroll(() => idEl.scrollIntoView())
21542162
return
21552163
}
21562164
// If there's no element with the id, we check the `name` property
21572165
// To mirror browsers
21582166
const nameEl = document.getElementsByName(rawHash)[0]
21592167
if (nameEl) {
2160-
nameEl.scrollIntoView()
2168+
handleSmoothScroll(() => nameEl.scrollIntoView())
21612169
}
21622170
}
21632171

0 commit comments

Comments
 (0)