Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/@react-aria/utils/src/getScrollParents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ export function getScrollParents(node: Element, checkForOverflow?: boolean): Ele
let parentElements: Element[] = [];
let root = document.scrollingElement || document.documentElement;

do {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old logic wasn't including the root element except if the initial node provided was the root. isScrollable should properly check if scrolling is being prevented on the root so we don't actually need the node ! == root check I believe

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, our test suite was exactly calling with root as the container. Sorry about that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good haha, I didn't catch this either on my original review. So many different scenarios to test too

while (node) {
if (isScrollable(node, checkForOverflow)) {
parentElements.push(node);
}
if (node === root) {
break;
}
node = node.parentElement as Element;
} while (node && node !== root);
}

return parentElements;
}
17 changes: 11 additions & 6 deletions packages/@react-aria/utils/src/scrollIntoView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement, op
let itemStyle = window.getComputedStyle(element);
let viewStyle = window.getComputedStyle(scrollView);
let root = document.scrollingElement || document.documentElement;
let isRoot = scrollView === root;

let viewTop = scrollView === root ? 0 : view.top;
let viewBottom = scrollView === root ? scrollView.clientHeight : view.bottom;
Expand Down Expand Up @@ -72,13 +73,13 @@ export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement, op

let scrollBarOffsetX = scrollView === root ? 0 : borderLeftWidth + borderRightWidth;
let scrollBarOffsetY = scrollView === root ? 0 : borderTopWidth + borderBottomWidth;
let scrollBarWidth = scrollView.offsetWidth - scrollView.clientWidth - scrollBarOffsetX;
let scrollBarHeight = scrollView.offsetHeight - scrollView.clientHeight - scrollBarOffsetY;
let scrollBarWidth = scrollView === root ? 0 : scrollView.offsetWidth - scrollView.clientWidth - scrollBarOffsetX;
let scrollBarHeight = scrollView === root ? 0 : scrollView.offsetHeight - scrollView.clientHeight - scrollBarOffsetY;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already use scrollView.clientHeight when calculating viewBottom as used below. That omits the scroll bar already if scrollView is the root so we can set to 0 here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would want to do that for scrollBarWidth too, right? In case the root is horizontally scrollable.

Also I think we need to adjust the scrollPort for the root element too actually. Since the border may not be in view, it might not make sense to always add/subtract it, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, good points I'll see about trying that soon

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually for the scrollPort do we need to accommodate for that? The scrollPort values should always be the coordinates of the scroll region we are currently positioning the target with respect to so it shouldn't care about the actual viewport and thus should always take the border and scrollPadding into view right? The border of the scroll region/port being not in view should get handled by scrolling all parents up to the root in scrollIntoViewport?

For instance take a table with a border that scrolled partially out of view such that its border isn't in view: Scrolling a table row into view would be to first scroll the table itself into view, then the item into view within the table right so we wouldn't need to accommodate for the border being in view or not?

Copy link
Contributor

@nwidynski nwidynski Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think for normal containers that's right, but not for the root element. I will have to test this more in depth, but here is my thinking:

The Scrollport is defined as the unclipped rect of a scroll container, adjusted by scroll-padding. For the root element, this is always the viewport, hence why we set the view to (0, 0, clientWidth, clientHeight). Here, clientWidth and clientHeight correspond to viewport - scrollbar, while offsetHeight continues to be the "full" rect, including borders.

The client values on the root are unaffected when a border is added/removed on the root. Hence why I think we need to adjust when the border is in view vs not. On normal elements, a border wouldn't affect the inner scroll offset, but on the root it does.

Edit: Confirmed via sandbox.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see, in that case I think we can adjust the scrollPort values to include the border if we are currently scrolling w/ respect to the root?

  let scrollPortTop = viewTop + (isRoot ? 0 : borderTopWidth) + scrollPaddingTop;
  let scrollPortBottom = viewBottom - (isRoot ? 0 : borderBottomWidth) - scrollPaddingBottom - scrollBarHeight;
  let scrollPortLeft = viewLeft + (isRoot ? 0 : borderLeftWidth) + scrollPaddingLeft;
  let scrollPortRight = viewRight - (isRoot ? 0 : borderRightWidth) - scrollPaddingRight;

That way we remain in the same coordinate system w/ regards to the scrollPort values and the scrollArea values.

https://codesandbox.io/p/sandbox/2x4wtp?file=%2Findex.html%3A14%2C16 seems to work well now with those changes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what my first thought was too, but I don't think that works fully - scrolling both to red start and red end is broken. I haven't fully grasped why, because I haven't spent time on this today, but I think its because the border falls within the target area, which is what I meant by it being visible or not.


let scrollPortTop = viewTop + borderTopWidth + scrollPaddingTop;
let scrollPortBottom = viewBottom - borderBottomWidth - scrollPaddingBottom - scrollBarHeight;
let scrollPortLeft = viewLeft + borderLeftWidth + scrollPaddingLeft;
let scrollPortRight = viewRight - borderRightWidth - scrollPaddingRight;
let scrollPortTop = viewTop + (isRoot ? 0 : borderTopWidth) + scrollPaddingTop;
let scrollPortBottom = viewBottom - (isRoot ? 0 : borderBottomWidth) - scrollPaddingBottom - scrollBarHeight;
let scrollPortLeft = viewLeft + (isRoot ? 0 : borderLeftWidth) + scrollPaddingLeft;
let scrollPortRight = viewRight - (isRoot ? 0 : borderRightWidth) - scrollPaddingRight;

// IOS always positions the scrollbar on the right ¯\_(ツ)_/¯
if (viewStyle.direction === 'rtl' && !isIOS()) {
Expand Down Expand Up @@ -159,9 +160,13 @@ export function scrollIntoViewport(targetElement: Element | null, opts: ScrollIn
// Account for sub pixel differences from rounding
if ((Math.abs(originalLeft - newLeft) > 1) || (Math.abs(originalTop - newTop) > 1)) {
scrollParents = containingElement ? getScrollParents(containingElement, true) : [];
// scroll containing element into view first, then rescroll target element into view like the non chrome flow above
for (let scrollParent of scrollParents) {
scrollIntoView(scrollParent as HTMLElement, containingElement as HTMLElement, {block: 'center', inline: 'center'});
}
for (let scrollParent of getScrollParents(targetElement, true)) {
scrollIntoView(scrollParent as HTMLElement, targetElement as HTMLElement);
}
Comment on lines +167 to +169
Copy link
Member Author

@LFDanLu LFDanLu Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mirrors the flow above, we need to first scroll the containing element into view if necessary, then re-scroll the target element into view to compensate. There is some logic in useSelectableCollection that revealed that this was a problem because we do two separate scroll in views (one via scrollIntoView and one via scrollIntoViewport ) and the second of which scrolls the containing element into view first

}
}
}
Expand Down
78 changes: 78 additions & 0 deletions packages/@react-aria/utils/test/getScrollParents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import {getScrollParents} from '../src/getScrollParents';

describe('getScrollParents', () => {
let root: Element;

beforeEach(() => {
root = document.documentElement;
});

afterEach(() => {
document.body.innerHTML = '';
jest.restoreAllMocks();
});

it('includes root as a scroll parent for a node in the document', () => {
let div = document.createElement('div');
document.body.appendChild(div);

let parents = getScrollParents(div);
expect(parents).toContain(root);
});

it('does not include root when root has overflow: hidden', () => {
let div = document.createElement('div');
document.body.appendChild(div);

jest.spyOn(window, 'getComputedStyle').mockImplementation((el) => {
if (el === root) {
return {overflow: 'hidden'} as CSSStyleDeclaration;
}
return {overflow: 'visible'} as CSSStyleDeclaration;
});

let parents = getScrollParents(div);
expect(parents).not.toContain(root);
});

it('includes a scrollable intermediate parent', () => {
let scrollable = document.createElement('div');
let child = document.createElement('div');
document.body.appendChild(scrollable);
scrollable.appendChild(child);

jest.spyOn(window, 'getComputedStyle').mockImplementation((el) => {
if (el === scrollable) {
return {overflow: 'auto'} as CSSStyleDeclaration;
}
return {overflow: 'visible'} as CSSStyleDeclaration;
});

let parents = getScrollParents(child);
expect(parents).toContain(scrollable);
expect(parents).toContain(root);
});

it('excludes non-scrollable ancestors', () => {
let plain = document.createElement('div');
let child = document.createElement('div');
document.body.appendChild(plain);
plain.appendChild(child);

let parents = getScrollParents(child);
expect(parents).not.toContain(plain);
expect(parents).not.toContain(document.body);
});
});
Loading