Skip to content

Commit dd1d770

Browse files
committed
add topViewabilityInset
1 parent 73de76d commit dd1d770

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/FlashListProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,5 @@ export interface FlashListProps<TItem> extends ScrollViewProps {
344344
* can consider the visible area of the bottom sheet in its calculations.
345345
*/
346346
bottomViewabilityInsetRef?: React.MutableRefObject<number>;
347+
topViewabilityInsetRef?: React.MutableRefObject<number>;
347348
}

src/__tests__/ViewabilityHelper.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ describe("ViewabilityHelper", () => {
257257
horizontal,
258258
scrollOffset,
259259
bottomViewabilityInset,
260+
topViewabilityInset,
260261
listSize,
261262
getLayout,
262263
runAllTimers,
@@ -265,6 +266,7 @@ describe("ViewabilityHelper", () => {
265266
horizontal?: boolean;
266267
scrollOffset?: number;
267268
bottomViewabilityInset?: number;
269+
topViewabilityInset?: number;
268270
listSize?: Dimension;
269271
getLayout?: (index: number) => Layout | undefined;
270272
runAllTimers?: boolean;
@@ -273,6 +275,7 @@ describe("ViewabilityHelper", () => {
273275
horizontal ?? false,
274276
scrollOffset ?? 0,
275277
bottomViewabilityInset ?? 0,
278+
topViewabilityInset ?? 0,
276279
listSize ?? { height: 300, width: 300 },
277280
getLayout ??
278281
((index) => {

src/viewability/ViewabilityHelper.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ViewabilityHelper {
4949
horizontal: boolean,
5050
scrollOffset: number,
5151
bottomViewabilityInset: number,
52+
topViewabilityInset: number,
5253
listSize: Dimension,
5354
getLayout: (index: number) => Layout | undefined,
5455
viewableIndices?: number[]
@@ -78,6 +79,7 @@ class ViewabilityHelper {
7879
horizontal,
7980
scrollOffset,
8081
bottomViewabilityInset,
82+
topViewabilityInset,
8183
listSize,
8284
this.viewabilityConfig?.viewAreaCoveragePercentThreshold,
8385
this.viewabilityConfig?.itemVisiblePercentThreshold,
@@ -127,6 +129,7 @@ class ViewabilityHelper {
127129
horizontal: boolean,
128130
scrollOffset: number,
129131
bottomViewabilityInset: number,
132+
topViewabilityInset: number,
130133
listSize: Dimension,
131134
viewAreaCoveragePercentThreshold: number | null | undefined,
132135
itemVisiblePercentThreshold: number | null | undefined,
@@ -139,10 +142,11 @@ class ViewabilityHelper {
139142
const itemTop = (horizontal ? itemLayout.x : itemLayout.y) - scrollOffset;
140143
const itemSize = horizontal ? itemLayout.width : itemLayout.height;
141144
const listMainSize = horizontal
142-
? listSize.width
143-
: listSize.height - bottomViewabilityInset;
145+
? listSize.width - topViewabilityInset - bottomViewabilityInset
146+
: listSize.height - topViewabilityInset - bottomViewabilityInset;
144147
const pixelsVisible =
145-
Math.min(itemTop + itemSize, listMainSize) - Math.max(itemTop, 0);
148+
Math.min(itemTop + itemSize, listMainSize + topViewabilityInset) -
149+
Math.max(itemTop, 0);
146150

147151
// Always consider item fully viewable if it is fully visible, regardless of the `viewAreaCoveragePercentThreshold`
148152
// Account for floating point imprecision.
@@ -157,7 +161,7 @@ class ViewabilityHelper {
157161
viewAreaCoveragePercentThreshold !== null &&
158162
viewAreaCoveragePercentThreshold !== undefined;
159163
const percent = viewAreaMode
160-
? pixelsVisible / listMainSize
164+
? pixelsVisible / (listMainSize + topViewabilityInset)
161165
: pixelsVisible / itemSize;
162166
const viewableAreaPercentThreshold = viewAreaMode
163167
? viewAreaCoveragePercentThreshold * 0.01

src/viewability/ViewabilityManager.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ export default class ViewabilityManager<T> {
7979

8080
const bottomViewabilityInset =
8181
this.flashListRef.props.bottomViewabilityInsetRef?.current ?? 0;
82+
const topViewabilityInset =
83+
this.flashListRef.props.topViewabilityInsetRef?.current ?? 0;
8284

8385
this.viewabilityHelpers.forEach((viewabilityHelper) => {
8486
viewabilityHelper.updateViewableItems(
8587
this.flashListRef.props.horizontal ?? false,
8688
scrollOffset,
8789
bottomViewabilityInset,
90+
topViewabilityInset,
8891
listSize,
8992
(index: number) =>
9093
this.flashListRef.recyclerlistview_unsafe?.getLayout(index),

0 commit comments

Comments
 (0)