Skip to content

Commit fe284e5

Browse files
committed
fix topViewabilityInset
1 parent dd1d770 commit fe284e5

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/viewability/ViewabilityHelper.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,30 @@ class ViewabilityHelper {
140140
return false;
141141
}
142142
const itemTop = (horizontal ? itemLayout.x : itemLayout.y) - scrollOffset;
143-
const itemSize = horizontal ? itemLayout.width : itemLayout.height;
144-
const listMainSize = horizontal
145-
? listSize.width - topViewabilityInset - bottomViewabilityInset
146-
: listSize.height - topViewabilityInset - bottomViewabilityInset;
147-
const pixelsVisible =
148-
Math.min(itemTop + itemSize, listMainSize + topViewabilityInset) -
149-
Math.max(itemTop, 0);
143+
const itemEnd =
144+
itemTop + (horizontal ? itemLayout.width : itemLayout.height);
145+
const listStart = topViewabilityInset;
146+
const listEnd =
147+
(horizontal ? listSize.width : listSize.height) - bottomViewabilityInset;
148+
const visibleStart = Math.max(itemTop, listStart);
149+
const visibleEnd = Math.min(itemEnd, listEnd);
150+
const pixelsVisible = Math.max(0, visibleEnd - visibleStart);
150151

151152
// Always consider item fully viewable if it is fully visible, regardless of the `viewAreaCoveragePercentThreshold`
152153
// Account for floating point imprecision.
154+
const itemSize = horizontal ? itemLayout.width : itemLayout.height;
153155
if (Math.abs(pixelsVisible - itemSize) <= 0.001) {
154156
return true;
155157
}
156158
// Skip checking item if it's not visible at all
157-
if (pixelsVisible === 0) {
159+
if (pixelsVisible <= 0) {
158160
return false;
159161
}
160162
const viewAreaMode =
161163
viewAreaCoveragePercentThreshold !== null &&
162164
viewAreaCoveragePercentThreshold !== undefined;
163165
const percent = viewAreaMode
164-
? pixelsVisible / (listMainSize + topViewabilityInset)
166+
? pixelsVisible / (listEnd - listStart)
165167
: pixelsVisible / itemSize;
166168
const viewableAreaPercentThreshold = viewAreaMode
167169
? viewAreaCoveragePercentThreshold * 0.01

0 commit comments

Comments
 (0)