Skip to content

Fix: DOMLayoutDelegate miscalculates item & visible rects #8696

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('useDroppableCollection', () => {
return this.getBoundingClientRect().top;
});

jest.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(function () {
jest.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(function () {
return this.getBoundingClientRect().height;
});

Expand Down
15 changes: 7 additions & 8 deletions packages/@react-aria/selection/src/DOMLayoutDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ export class DOMLayoutDelegate implements LayoutDelegate {
return null;
}

let containerRect = container.getBoundingClientRect();
let itemRect = item.getBoundingClientRect();
let rect = item.getBoundingClientRect();

return {
x: itemRect.left - containerRect.left + container.scrollLeft,
y: itemRect.top - containerRect.top + container.scrollTop,
Comment on lines -37 to -38
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does anyone know why the item rect was offset by the current scroll point?

width: itemRect.width,
height: itemRect.height
x: rect.left - container.offsetLeft - container.clientLeft,
y: rect.top - container.offsetTop - container.clientTop,
width: rect.width,
height: rect.height
};
}

Expand All @@ -54,8 +53,8 @@ export class DOMLayoutDelegate implements LayoutDelegate {
return {
x: container?.scrollLeft ?? 0,
y: container?.scrollTop ?? 0,
width: container?.offsetWidth ?? 0,
height: container?.offsetHeight ?? 0
width: container?.clientWidth ?? 0,
height: container?.clientHeight ?? 0
};
}
}
6 changes: 3 additions & 3 deletions packages/react-aria-components/test/Table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1633,15 +1633,15 @@ describe('Table', () => {
});

describe('load more spinner', () => {
let offsetHeight, scrollHeight;
let clientHeight, scrollHeight;
let DndTable = stories.DndTable;
let initialItems = [
{id: '1', type: 'file', name: 'Adobe Photoshop'},
{id: '2', type: 'file', name: 'Adobe XD'}
];
beforeAll(function () {
scrollHeight = jest.spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(() => 200);
offsetHeight = jest.spyOn(window.HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(function () {
clientHeight = jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(function () {
if (this.getAttribute('role') === 'grid') {
return 200;
}
Expand All @@ -1651,7 +1651,7 @@ describe('Table', () => {
});

afterAll(function () {
offsetHeight.mockReset();
clientHeight.mockReset();
scrollHeight.mockReset();
});

Expand Down