Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: offset calculation to handle transformed elements with translateY or translateX in scrollIntoView #7717

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 12 additions & 17 deletions packages/@react-aria/utils/src/scrollIntoView.ts
Original file line number Diff line number Diff line change
@@ -73,23 +73,18 @@ export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement) {
* offsetLeft or offsetTop through intervening offsetParents.
*/
function relativeOffset(ancestor: HTMLElement, child: HTMLElement, axis: 'left'|'top') {
const prop = axis === 'left' ? 'offsetLeft' : 'offsetTop';
let sum = 0;
while (child.offsetParent) {
sum += child[prop];
if (child.offsetParent === ancestor) {
// Stop once we have found the ancestor we are interested in.
break;
} else if (child.offsetParent.contains(ancestor)) {
// If the ancestor is not `position:relative`, then we stop at
// _its_ offset parent, and we subtract off _its_ offset, so that
// we end up with the proper offset from child to ancestor.
sum -= ancestor[prop];
break;
}
child = child.offsetParent as HTMLElement;
}
return sum;
let childRect = child.getBoundingClientRect();
Copy link
Member

Choose a reason for hiding this comment

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

We're a little nervous about this change. We think it's missing some cases. Looking at some other libraries, there is a fair amount of complexity in determining this. For example https://www.npmjs.com/package/compute-scroll-into-view

We might be able to use this library, though are reluctant to bring in the size increase. So the thing we need to prove is that this change is "enough" for our use cases or decide we're ok with the size increase.

let ancestorRect = ancestor.getBoundingClientRect();

let viewportOffset = axis === 'left'
? childRect.left - ancestorRect.left
: childRect.top - ancestorRect.top;

let scrollAdjustment = axis === 'left'
? ancestor.scrollLeft
: ancestor.scrollTop;

return viewportOffset + scrollAdjustment;
}

/**
54 changes: 54 additions & 0 deletions packages/react-aria-components/stories/GridList.stories.tsx
Original file line number Diff line number Diff line change
@@ -199,3 +199,57 @@ export function TagGroupInsideGridList() {
</GridList>
);
}

export function GridListScrollIntoView() {
let items: {id: number, name: string}[] = [];
for (let i = 0; i < 100; i++) {
items.push({id: i, name: `Item ${i}`});
}

let list = useListData({
initialItems: items
});

const getElement = (id: number) => document.querySelector(`[data-key="${id}"]`) as HTMLElement;

const rowHeight = 25;

return (
<>
<div style={{height: 500, overflow: 'auto'}}>
<GridList
className={styles.menu}
selectionMode="multiple"
aria-label="virtualized listbox"
items={list.items}
style={{
position: 'relative',
overflow: 'hidden',
width: 100,
height: list.items.length * rowHeight
}}>
{item => (
<GridListItem
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
overflow: 'visible',
transform: `translateY(${item.id * rowHeight}px)`
}}>
{item.name}
</GridListItem>)}
</GridList>
</div>
<button onClick={() => getElement(40)?.scrollIntoView({block: 'start'})}>Scroll to item 40</button>
<button
tabIndex={0}
onKeyDown={() => {
getElement(70)?.focus();
}}>
Click, press ESC key focus item 70
</button>
</>
);
}