Skip to content

Commit

Permalink
Math.floor the top, bottom values of an item in a VirtualizedList (#3…
Browse files Browse the repository at this point in the history
…8962)

Summary:
Pull Request resolved: #38962

Changelog: [General][Changed] Math.floor the top and bottom dimensions of a cell item when determining viewability.

Reviewed By: NickGerleman

Differential Revision: D48212402

fbshipit-source-id: 0ba7d5c218477c257a4504391940d916e4832f91
  • Loading branch information
lunaleaps authored and facebook-github-bot committed Aug 11, 2023
1 parent ed0f60f commit 824c1c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/virtualized-lists/Lists/ViewabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ class ViewabilityHelper {
if (!metrics) {
continue;
}
const top = metrics.offset - scrollOffset;
const bottom = top + metrics.length;
const top = Math.floor(metrics.offset - scrollOffset);
const bottom = Math.floor(top + metrics.length);

if (top < viewportHeight && bottom > 0) {
firstVisible = idx;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,29 @@ describe('onUpdate', function () {
viewableItems: [{isViewable: true, key: 'c'}],
});
});

it('should account for imprecision on measurements of width of viewport and item', () => {
// This test assures we round down the calculations of the item cell layout
// to avoid cases of imprecison when measuring layout
const helper = new ViewabilityHelper({itemVisiblePercentThreshold: 100});
const testProps = {
getItemCount: () => 1,
data: ['Item'],
};
const listMetrics = {
getCellMetrics: () => ({
index: 0,
length: 147.4285888671875,
offset: 1767.6190185546875,
isMounted: true,
}),
};
const viewableIndices = helper.computeViewableItems(
testProps,
1503.61901855, // scrollOffset
411.4285583496094, // viewportHeight (viewportWidth depending on scrolling axis)
listMetrics,
);
expect(viewableIndices).toEqual([0]);
});
});

0 comments on commit 824c1c6

Please sign in to comment.