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

Refine scrolling into view in new router #38682

Merged
merged 1 commit into from Jul 15, 2022
Merged
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
16 changes: 15 additions & 1 deletion packages/next/client/components/layout-router.client.tsx
Expand Up @@ -42,6 +42,17 @@ function createInfinitePromise() {
return infinitePromise
}

function isInViewport(element: HTMLElement) {
const rect = element.getBoundingClientRect()
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
)
}

export function InnerLayoutRouter({
parallelRouterKey,
url,
Expand Down Expand Up @@ -72,7 +83,10 @@ export function InnerLayoutRouter({
if (focusRef.focus && focusAndScrollRef.current) {
focusRef.focus = false
focusAndScrollRef.current.focus()
focusAndScrollRef.current.scrollIntoView()
// Only scroll into viewport when the layout is not visible currently.
if (!isInViewport(focusAndScrollRef.current)) {
focusAndScrollRef.current.scrollIntoView()
}
}
}, [focusRef])

Expand Down