(
scrollView: HTMLElement,
element: HTMLElement,
opts: ScrollIntoViewOpts = {}
)
| 31 | * but doesn't affect parents above `scrollView`. |
| 32 | */ |
| 33 | export function scrollIntoView( |
| 34 | scrollView: HTMLElement, |
| 35 | element: HTMLElement, |
| 36 | opts: ScrollIntoViewOpts = {} |
| 37 | ): void { |
| 38 | let {block = 'nearest', inline = 'nearest'} = opts; |
| 39 | |
| 40 | if (scrollView === element) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | let y = scrollView.scrollTop; |
| 45 | let x = scrollView.scrollLeft; |
| 46 | |
| 47 | let target = element.getBoundingClientRect(); |
| 48 | let view = scrollView.getBoundingClientRect(); |
| 49 | let itemStyle = window.getComputedStyle(element); |
| 50 | let viewStyle = window.getComputedStyle(scrollView); |
| 51 | let root = document.scrollingElement || document.documentElement; |
| 52 | let isRoot = scrollView === root; |
| 53 | |
| 54 | let viewTop = scrollView === root ? 0 : view.top; |
| 55 | let viewBottom = scrollView === root ? scrollView.clientHeight : view.bottom; |
| 56 | let viewLeft = scrollView === root ? 0 : view.left; |
| 57 | let viewRight = scrollView === root ? scrollView.clientWidth : view.right; |
| 58 | |
| 59 | let scrollMarginTop = parseFloat(itemStyle.scrollMarginTop) || 0; |
| 60 | let scrollMarginBottom = parseFloat(itemStyle.scrollMarginBottom) || 0; |
| 61 | let scrollMarginLeft = parseFloat(itemStyle.scrollMarginLeft) || 0; |
| 62 | let scrollMarginRight = parseFloat(itemStyle.scrollMarginRight) || 0; |
| 63 | |
| 64 | let scrollPaddingTop = parseFloat(viewStyle.scrollPaddingTop) || 0; |
| 65 | let scrollPaddingBottom = parseFloat(viewStyle.scrollPaddingBottom) || 0; |
| 66 | let scrollPaddingLeft = parseFloat(viewStyle.scrollPaddingLeft) || 0; |
| 67 | let scrollPaddingRight = parseFloat(viewStyle.scrollPaddingRight) || 0; |
| 68 | |
| 69 | let borderTopWidth = parseFloat(viewStyle.borderTopWidth) || 0; |
| 70 | let borderBottomWidth = parseFloat(viewStyle.borderBottomWidth) || 0; |
| 71 | let borderLeftWidth = parseFloat(viewStyle.borderLeftWidth) || 0; |
| 72 | let borderRightWidth = parseFloat(viewStyle.borderRightWidth) || 0; |
| 73 | |
| 74 | let scrollAreaTop = target.top - scrollMarginTop; |
| 75 | let scrollAreaBottom = target.bottom + scrollMarginBottom; |
| 76 | let scrollAreaLeft = target.left - scrollMarginLeft; |
| 77 | let scrollAreaRight = target.right + scrollMarginRight; |
| 78 | |
| 79 | let scrollBarOffsetX = scrollView === root ? 0 : borderLeftWidth + borderRightWidth; |
| 80 | let scrollBarOffsetY = scrollView === root ? 0 : borderTopWidth + borderBottomWidth; |
| 81 | let scrollBarWidth = |
| 82 | scrollView === root ? 0 : scrollView.offsetWidth - scrollView.clientWidth - scrollBarOffsetX; |
| 83 | let scrollBarHeight = |
| 84 | scrollView === root ? 0 : scrollView.offsetHeight - scrollView.clientHeight - scrollBarOffsetY; |
| 85 | |
| 86 | let scrollPortTop = viewTop + (isRoot ? 0 : borderTopWidth) + scrollPaddingTop; |
| 87 | let scrollPortBottom = |
| 88 | viewBottom - (isRoot ? 0 : borderBottomWidth) - scrollPaddingBottom - scrollBarHeight; |
| 89 | let scrollPortLeft = viewLeft + (isRoot ? 0 : borderLeftWidth) + scrollPaddingLeft; |
| 90 | let scrollPortRight = viewRight - (isRoot ? 0 : borderRightWidth) - scrollPaddingRight; |
no outgoing calls
no test coverage detected