MCPcopy Create free account
hub / github.com/adobe/react-spectrum / scrollRectIntoView

Function scrollRectIntoView

packages/react-aria/src/utils/scrollIntoView.ts:46–141  ·  view source on GitHub ↗
(
  scrollView: HTMLElement,
  element: HTMLElement,
  target: DOMRect,
  opts: ScrollIntoViewOpts = {}
)

Source from the content-addressed store, hash-verified

44}
45
46export function scrollRectIntoView(
47 scrollView: HTMLElement,
48 element: HTMLElement,
49 target: DOMRect,
50 opts: ScrollIntoViewOpts = {}
51) {
52 let {block = 'nearest', inline = 'nearest'} = opts;
53 let y = scrollView.scrollTop;
54 let x = scrollView.scrollLeft;
55
56 let view = scrollView.getBoundingClientRect();
57 let itemStyle = window.getComputedStyle(element);
58 let viewStyle = window.getComputedStyle(scrollView);
59 let root = document.scrollingElement || document.documentElement;
60 let isRoot = scrollView === root;
61
62 let viewTop = scrollView === root ? 0 : view.top;
63 let viewBottom = scrollView === root ? scrollView.clientHeight : view.bottom;
64 let viewLeft = scrollView === root ? 0 : view.left;
65 let viewRight = scrollView === root ? scrollView.clientWidth : view.right;
66
67 let scrollMarginTop = parseFloat(itemStyle.scrollMarginTop) || 0;
68 let scrollMarginBottom = parseFloat(itemStyle.scrollMarginBottom) || 0;
69 let scrollMarginLeft = parseFloat(itemStyle.scrollMarginLeft) || 0;
70 let scrollMarginRight = parseFloat(itemStyle.scrollMarginRight) || 0;
71
72 let scrollPaddingTop = parseFloat(viewStyle.scrollPaddingTop) || 0;
73 let scrollPaddingBottom = parseFloat(viewStyle.scrollPaddingBottom) || 0;
74 let scrollPaddingLeft = parseFloat(viewStyle.scrollPaddingLeft) || 0;
75 let scrollPaddingRight = parseFloat(viewStyle.scrollPaddingRight) || 0;
76
77 let borderTopWidth = parseFloat(viewStyle.borderTopWidth) || 0;
78 let borderBottomWidth = parseFloat(viewStyle.borderBottomWidth) || 0;
79 let borderLeftWidth = parseFloat(viewStyle.borderLeftWidth) || 0;
80 let borderRightWidth = parseFloat(viewStyle.borderRightWidth) || 0;
81
82 let scrollAreaTop = target.top - scrollMarginTop;
83 let scrollAreaBottom = target.bottom + scrollMarginBottom;
84 let scrollAreaLeft = target.left - scrollMarginLeft;
85 let scrollAreaRight = target.right + scrollMarginRight;
86
87 let scrollBarOffsetX = scrollView === root ? 0 : borderLeftWidth + borderRightWidth;
88 let scrollBarOffsetY = scrollView === root ? 0 : borderTopWidth + borderBottomWidth;
89 let scrollBarWidth =
90 scrollView === root ? 0 : scrollView.offsetWidth - scrollView.clientWidth - scrollBarOffsetX;
91 let scrollBarHeight =
92 scrollView === root ? 0 : scrollView.offsetHeight - scrollView.clientHeight - scrollBarOffsetY;
93
94 let scrollPortTop = viewTop + (isRoot ? 0 : borderTopWidth) + scrollPaddingTop;
95 let scrollPortBottom =
96 viewBottom - (isRoot ? 0 : borderBottomWidth) - scrollPaddingBottom - scrollBarHeight;
97 let scrollPortLeft = viewLeft + (isRoot ? 0 : borderLeftWidth) + scrollPaddingLeft;
98 let scrollPortRight = viewRight - (isRoot ? 0 : borderRightWidth) - scrollPaddingRight;
99
100 // WebKit on iOS always positions the scrollbar on the right ¯\_(ツ)_/¯
101 if ((isIOS() && isWebKit()) || viewStyle.direction === 'ltr') {
102 scrollPortRight -= scrollBarWidth;
103 } else if (viewStyle.direction === 'rtl') {

Callers 2

scrollCaretIntoViewFunction · 0.90
scrollIntoViewFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected