Skip to content

Commit

Permalink
Force reflow when setting scrollBehavior (#43673)
Browse files Browse the repository at this point in the history
fixes #40719
  • Loading branch information
jankaifer committed Dec 7, 2022
1 parent bd9148d commit ba19ce3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/next/client/components/layout-router.tsx
Expand Up @@ -127,6 +127,10 @@ class ScrollAndFocusHandler extends React.Component<{
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
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()
htmlElement.style.scrollBehavior = existing
}
Expand Down
4 changes: 4 additions & 0 deletions packages/next/client/index.tsx
Expand Up @@ -697,6 +697,10 @@ function doRender(input: RenderRouteInfo): Promise<any> {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
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)
htmlElement.style.scrollBehavior = existing
}
Expand Down
4 changes: 4 additions & 0 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -659,6 +659,10 @@ function handleSmoothScroll(fn: () => void) {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
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()
htmlElement.style.scrollBehavior = existing
}
Expand Down

0 comments on commit ba19ce3

Please sign in to comment.