Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force reflow when setting scrollBehavior #43673

Merged
merged 4 commits into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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