Skip to content

Commit

Permalink
Refine scrolling into view in new router
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jul 15, 2022
1 parent 3430ac1 commit a1b06e8
Showing 1 changed file with 15 additions and 1 deletion.
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

0 comments on commit a1b06e8

Please sign in to comment.