Unverified Commit ba19ce37 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Force reflow when setting scrollBehavior (#43673)

fixes https://github.com/vercel/next.js/issues/40719
parent bd9148d4
...@@ -127,6 +127,10 @@ class ScrollAndFocusHandler extends React.Component<{ ...@@ -127,6 +127,10 @@ class ScrollAndFocusHandler extends React.Component<{
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'
// In Chrome-based browsers we need to force reflow before calling `scrollTo`.
// Otherwise it will not pickup the change in scrollBehavior
// More info here: https://github.com/vercel/next.js/issues/40719#issuecomment-1336248042
htmlElement.getClientRects()
domNode.scrollIntoView() domNode.scrollIntoView()
htmlElement.style.scrollBehavior = existing htmlElement.style.scrollBehavior = existing
} }
......
...@@ -697,6 +697,10 @@ function doRender(input: RenderRouteInfo): Promise<any> { ...@@ -697,6 +697,10 @@ function doRender(input: RenderRouteInfo): Promise<any> {
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'
// In Chrome-based browsers we need to force reflow before calling `scrollTo`.
// Otherwise it will not pickup the change in scrollBehavior
// More info here: https://github.com/vercel/next.js/issues/40719#issuecomment-1336248042
htmlElement.getClientRects()
window.scrollTo(input.scroll.x, input.scroll.y) window.scrollTo(input.scroll.x, input.scroll.y)
htmlElement.style.scrollBehavior = existing htmlElement.style.scrollBehavior = existing
} }
......
...@@ -659,6 +659,10 @@ function handleSmoothScroll(fn: () => void) { ...@@ -659,6 +659,10 @@ function handleSmoothScroll(fn: () => void) {
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'
// In Chrome-based browsers we need to force reflow before calling `scrollTo`.
// Otherwise it will not pickup the change in scrollBehavior
// More info here: https://github.com/vercel/next.js/issues/40719#issuecomment-1336248042
htmlElement.getClientRects()
fn() fn()
htmlElement.style.scrollBehavior = existing htmlElement.style.scrollBehavior = existing
} }
......
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