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

Function scrollIntoView

packages/react-aria/src/overlays/usePreventScroll.ts:264–307  ·  view source on GitHub ↗
(target: Element)

Source from the content-addressed store, hash-verified

262}
263
264function scrollIntoView(target: Element) {
265 let root = document.scrollingElement || document.documentElement;
266 let nextTarget: Element | null = target;
267 while (nextTarget && nextTarget !== root) {
268 // Find the parent scrollable element and adjust the scroll position if the target is not already in view.
269 let scrollable = getScrollParent(nextTarget);
270 if (
271 scrollable !== document.documentElement &&
272 scrollable !== document.body &&
273 scrollable !== nextTarget
274 ) {
275 let scrollableRect = scrollable.getBoundingClientRect();
276 let targetRect = nextTarget.getBoundingClientRect();
277 if (
278 targetRect.top < scrollableRect.top ||
279 targetRect.bottom > scrollableRect.top + nextTarget.clientHeight
280 ) {
281 let bottom = scrollableRect.bottom;
282 if (visualViewport) {
283 bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height);
284 }
285
286 // Center within the viewport.
287 let adjustment =
288 targetRect.top -
289 scrollableRect.top -
290 ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
291 scrollable.scrollTo({
292 // Clamp to the valid range to prevent over-scrolling.
293 top: Math.max(
294 0,
295 Math.min(
296 scrollable.scrollHeight - scrollable.clientHeight,
297 scrollable.scrollTop + adjustment
298 )
299 ),
300 behavior: 'smooth'
301 });
302 }
303 }
304
305 nextTarget = scrollable.parentElement;
306 }
307}

Callers 1

scrollIntoViewWhenReadyFunction · 0.70

Calls 1

getScrollParentFunction · 0.90

Tested by

no test coverage detected