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

Function scrollIntoViewport

packages/react-aria/src/utils/scrollIntoView.ts:150–195  ·  view source on GitHub ↗
(
  targetElement: Element | null,
  opts: ScrollIntoViewportOpts = {}
)

Source from the content-addressed store, hash-verified

148 * the body itself.
149 */
150export function scrollIntoViewport(
151 targetElement: Element | null,
152 opts: ScrollIntoViewportOpts = {}
153): void {
154 let {containingElement} = opts;
155 if (targetElement && targetElement.isConnected) {
156 let root = document.scrollingElement || document.documentElement;
157 let isScrollPrevented = window.getComputedStyle(root).overflow === 'hidden';
158 if (!isScrollPrevented) {
159 let {left: originalLeft, top: originalTop} = targetElement.getBoundingClientRect();
160
161 // use scrollIntoView({block: 'nearest'}) instead of .focus to check if the element is fully in view or not since .focus()
162 // won't cause a scroll if the element is already focused and doesn't behave consistently when an element is partially out of view horizontally vs vertically
163 targetElement?.scrollIntoView?.({block: 'nearest'});
164 let {left: newLeft, top: newTop} = targetElement.getBoundingClientRect();
165 // Account for sub pixel differences from rounding
166 if (Math.abs(originalLeft - newLeft) > 1 || Math.abs(originalTop - newTop) > 1) {
167 containingElement?.scrollIntoView?.({block: 'center', inline: 'center'});
168 targetElement.scrollIntoView?.({block: 'nearest'});
169 }
170 } else {
171 let {left: originalLeft, top: originalTop} = targetElement.getBoundingClientRect();
172
173 // If scrolling is prevented, we don't want to scroll the body since it might move the overlay partially offscreen and the user can't scroll it back into view.
174 let scrollParents = getScrollParents(targetElement, true);
175 for (let scrollParent of scrollParents) {
176 scrollIntoView(scrollParent as HTMLElement, targetElement as HTMLElement);
177 }
178 let {left: newLeft, top: newTop} = targetElement.getBoundingClientRect();
179 // Account for sub pixel differences from rounding
180 if (Math.abs(originalLeft - newLeft) > 1 || Math.abs(originalTop - newTop) > 1) {
181 scrollParents = containingElement ? getScrollParents(containingElement, true) : [];
182 // scroll containing element into view first, then rescroll target element into view like the non chrome flow above
183 for (let scrollParent of scrollParents) {
184 scrollIntoView(scrollParent as HTMLElement, containingElement as HTMLElement, {
185 block: 'center',
186 inline: 'center'
187 });
188 }
189 for (let scrollParent of getScrollParents(targetElement, true)) {
190 scrollIntoView(scrollParent as HTMLElement, targetElement as HTMLElement);
191 }
192 }
193 }
194 }
195}

Callers 8

useCalendarCellFunction · 0.90
focusFunction · 0.90
onKeyDownCaptureFunction · 0.90
onFocusFunction · 0.90
useSelectableCollectionFunction · 0.90
onKeyDownCaptureFunction · 0.90
onFocusFunction · 0.90
TableVirtualizerFunction · 0.90

Calls 2

getScrollParentsFunction · 0.90
scrollIntoViewFunction · 0.70

Tested by

no test coverage detected