Unverified Commit 916d20b5 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Improve element detection further (#38684)

Follow-up to #38682. Instead of checking if the full element is in the viewport we only check if the top of the element is in the viewport. If it is not we have to scroll back up in cases of navigating between items in the same layout for example.
parent 3ebdf198
...@@ -42,15 +42,9 @@ function createInfinitePromise() { ...@@ -42,15 +42,9 @@ function createInfinitePromise() {
return infinitePromise return infinitePromise
} }
function isInViewport(element: HTMLElement) { function topOfElementInViewport(element: HTMLElement) {
const rect = element.getBoundingClientRect() const rect = element.getBoundingClientRect()
return ( return rect.top >= 0
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
)
} }
export function InnerLayoutRouter({ export function InnerLayoutRouter({
...@@ -84,7 +78,7 @@ export function InnerLayoutRouter({ ...@@ -84,7 +78,7 @@ export function InnerLayoutRouter({
focusRef.focus = false focusRef.focus = false
focusAndScrollRef.current.focus() focusAndScrollRef.current.focus()
// Only scroll into viewport when the layout is not visible currently. // Only scroll into viewport when the layout is not visible currently.
if (!isInViewport(focusAndScrollRef.current)) { if (!topOfElementInViewport(focusAndScrollRef.current)) {
focusAndScrollRef.current.scrollIntoView() focusAndScrollRef.current.scrollIntoView()
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment