Skip to content

Commit

Permalink
fix topViewabilityInset
Browse files Browse the repository at this point in the history
  • Loading branch information
yayvery committed Apr 5, 2024
1 parent dd1d770 commit fe284e5
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/viewability/ViewabilityHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,30 @@ class ViewabilityHelper {
return false;
}
const itemTop = (horizontal ? itemLayout.x : itemLayout.y) - scrollOffset;
const itemSize = horizontal ? itemLayout.width : itemLayout.height;
const listMainSize = horizontal
? listSize.width - topViewabilityInset - bottomViewabilityInset
: listSize.height - topViewabilityInset - bottomViewabilityInset;
const pixelsVisible =
Math.min(itemTop + itemSize, listMainSize + topViewabilityInset) -
Math.max(itemTop, 0);
const itemEnd =
itemTop + (horizontal ? itemLayout.width : itemLayout.height);
const listStart = topViewabilityInset;
const listEnd =
(horizontal ? listSize.width : listSize.height) - bottomViewabilityInset;
const visibleStart = Math.max(itemTop, listStart);
const visibleEnd = Math.min(itemEnd, listEnd);
const pixelsVisible = Math.max(0, visibleEnd - visibleStart);

// Always consider item fully viewable if it is fully visible, regardless of the `viewAreaCoveragePercentThreshold`
// Account for floating point imprecision.
const itemSize = horizontal ? itemLayout.width : itemLayout.height;
if (Math.abs(pixelsVisible - itemSize) <= 0.001) {
return true;
}
// Skip checking item if it's not visible at all
if (pixelsVisible === 0) {
if (pixelsVisible <= 0) {
return false;
}
const viewAreaMode =
viewAreaCoveragePercentThreshold !== null &&
viewAreaCoveragePercentThreshold !== undefined;
const percent = viewAreaMode
? pixelsVisible / (listMainSize + topViewabilityInset)
? pixelsVisible / (listEnd - listStart)
: pixelsVisible / itemSize;
const viewableAreaPercentThreshold = viewAreaMode
? viewAreaCoveragePercentThreshold * 0.01
Expand Down

0 comments on commit fe284e5

Please sign in to comment.