sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
38 lines (31 loc) • 1.04 kB
text/typescript
import {type Rect} from '../overlay/types'
import {isScrollable} from './scrollUtils'
const SCROLL_INTO_VIEW_TOP_PADDING = -15
export function scrollIntoView(field: {element: HTMLElement; rect: Rect; bounds: Rect}): void {
const element = field.element
/*
* Start at current element and check the parent for a scroll
* bar until a scrollable element has been found.
*/
let parentElementWithScroll: HTMLElement | null = element
while (!isScrollable(parentElementWithScroll)) {
parentElementWithScroll = parentElementWithScroll.parentElement
/*
* If the parent element is null it means we are at the root
* element, which has no parent. Since no scroll bar has
* been found so far it does not make sense to scroll.
*/
if (!parentElementWithScroll) {
return
}
}
parentElementWithScroll.scroll({
top:
parentElementWithScroll.scrollTop +
field.rect.top -
field.bounds.top +
SCROLL_INTO_VIEW_TOP_PADDING,
left: 0,
behavior: 'smooth',
})
}