Unverified Commit dc11d77b authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure smooth scroll is disabled for navigation in new and existing router (#40642)

parent c742c038
...@@ -167,7 +167,11 @@ export function InnerLayoutRouter({ ...@@ -167,7 +167,11 @@ export function InnerLayoutRouter({
focusAndScrollElementRef.current.focus() focusAndScrollElementRef.current.focus()
// Only scroll into viewport when the layout is not visible currently. // Only scroll into viewport when the layout is not visible currently.
if (!topOfElementInViewport(focusAndScrollElementRef.current)) { if (!topOfElementInViewport(focusAndScrollElementRef.current)) {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
htmlElement.style.scrollBehavior = 'auto'
focusAndScrollElementRef.current.scrollIntoView() focusAndScrollElementRef.current.scrollIntoView()
htmlElement.style.scrollBehavior = existing
} }
} }
}, [focusAndScrollRef]) }, [focusAndScrollRef])
......
...@@ -688,7 +688,11 @@ function doRender(input: RenderRouteInfo): Promise<any> { ...@@ -688,7 +688,11 @@ function doRender(input: RenderRouteInfo): Promise<any> {
} }
if (input.scroll) { if (input.scroll) {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
htmlElement.style.scrollBehavior = 'auto'
window.scrollTo(input.scroll.x, input.scroll.y) window.scrollTo(input.scroll.x, input.scroll.y)
htmlElement.style.scrollBehavior = existing
} }
} }
......
...@@ -655,6 +655,14 @@ interface FetchNextDataParams { ...@@ -655,6 +655,14 @@ interface FetchNextDataParams {
unstable_skipClientCache?: boolean unstable_skipClientCache?: boolean
} }
function handleSmoothScroll(fn: () => void) {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
htmlElement.style.scrollBehavior = 'auto'
fn()
htmlElement.style.scrollBehavior = existing
}
function tryToParseAsJSON(text: string) { function tryToParseAsJSON(text: string) {
try { try {
return JSON.parse(text) return JSON.parse(text)
...@@ -2141,7 +2149,7 @@ export default class Router implements BaseRouter { ...@@ -2141,7 +2149,7 @@ export default class Router implements BaseRouter {
// Scroll to top if the hash is just `#` with no value or `#top` // Scroll to top if the hash is just `#` with no value or `#top`
// To mirror browsers // To mirror browsers
if (hash === '' || hash === 'top') { if (hash === '' || hash === 'top') {
window.scrollTo(0, 0) handleSmoothScroll(() => window.scrollTo(0, 0))
return return
} }
...@@ -2150,14 +2158,14 @@ export default class Router implements BaseRouter { ...@@ -2150,14 +2158,14 @@ export default class Router implements BaseRouter {
// First we check if the element by id is found // First we check if the element by id is found
const idEl = document.getElementById(rawHash) const idEl = document.getElementById(rawHash)
if (idEl) { if (idEl) {
idEl.scrollIntoView() handleSmoothScroll(() => idEl.scrollIntoView())
return return
} }
// If there's no element with the id, we check the `name` property // If there's no element with the id, we check the `name` property
// To mirror browsers // To mirror browsers
const nameEl = document.getElementsByName(rawHash)[0] const nameEl = document.getElementsByName(rawHash)[0]
if (nameEl) { if (nameEl) {
nameEl.scrollIntoView() handleSmoothScroll(() => nameEl.scrollIntoView())
} }
} }
......
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