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

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

Merged
merged 4 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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.client.tsx
Expand Up @@ -164,7 +164,11 @@ export function InnerLayoutRouter({
focusAndScrollElementRef.current.focus()
// Only scroll into viewport when the layout is not visible currently.
if (!topOfElementInViewport(focusAndScrollElementRef.current)) {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
htmlElement.style.scrollBehavior = 'auto'
focusAndScrollElementRef.current.scrollIntoView()
htmlElement.style.scrollBehavior = existing
}
}
}, [focusAndScrollRef])
Expand Down
4 changes: 4 additions & 0 deletions packages/next/client/index.tsx
Expand Up @@ -688,7 +688,11 @@ function doRender(input: RenderRouteInfo): Promise<any> {
}

if (input.scroll) {
const htmlElement = document.documentElement
const existing = htmlElement.style.scrollBehavior
htmlElement.style.scrollBehavior = 'auto'
window.scrollTo(input.scroll.x, input.scroll.y)
htmlElement.style.scrollBehavior = existing
}
}

Expand Down
14 changes: 11 additions & 3 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -655,6 +655,14 @@ interface FetchNextDataParams {
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) {
try {
return JSON.parse(text)
Expand Down Expand Up @@ -2141,7 +2149,7 @@ export default class Router implements BaseRouter {
// Scroll to top if the hash is just `#` with no value or `#top`
// To mirror browsers
if (hash === '' || hash === 'top') {
window.scrollTo(0, 0)
handleSmoothScroll(() => window.scrollTo(0, 0))
return
}

Expand All @@ -2150,14 +2158,14 @@ export default class Router implements BaseRouter {
// First we check if the element by id is found
const idEl = document.getElementById(rawHash)
if (idEl) {
idEl.scrollIntoView()
handleSmoothScroll(() => idEl.scrollIntoView())
return
}
// If there's no element with the id, we check the `name` property
// To mirror browsers
const nameEl = document.getElementsByName(rawHash)[0]
if (nameEl) {
nameEl.scrollIntoView()
handleSmoothScroll(() => nameEl.scrollIntoView())
}
}

Expand Down