Skip to content

Instantly share code, notes, and snippets.

View sueLan's full-sized avatar
🎯
Focusing

RY Zheng sueLan

🎯
Focusing
View GitHub Profile
// The map storing the cell layout info
{ [cellKey]: {
// offset of the item cell
offset: number,
// length of the item cell. width or height determined by the direction of the VirtualizedList
length: number,
index: number,
inLayout: boolean,
}
}
for (let idx = first; idx <= last; idx++) {
const metrics = getFrameMetrics(idx);
if (!metrics) {
continue;
}
// The top of current item cell, relative to the screen
const top = metrics.offset - scrollOffset;
// The bottom of current item cell
const bottom = top + metrics.length;
if (top < viewportHeight && bottom > 0) {
function _isViewable(
viewAreaMode: boolean,
viewablePercentThreshold: number,
top: number,
bottom: number,
viewportHeight: number,
itemLength: number,
): boolean {
if (_isEntirelyVisible(top, bottom, viewportHeight)) {
// Entirely visible
this._viewableIndices = viewableIndices;
if (this._config.minimumViewTime) {
const handle = setTimeout(() => {
this._timers.delete(handle);
this._onUpdateSync(
viewableIndices,
onViewableItemsChanged,
createViewToken,
);
}, this._config.minimumViewTime);
// Filter out indices that have gone out of view since this call was scheduled.
viewableIndicesToCheck = viewableIndicesToCheck.filter(ii =>
this._viewableIndices.includes(ii),
);
_onUpdateSync(
viewableIndicesToCheck,
onViewableItemsChanged,
createViewToken,
) {
// Filter out indices that have gone out of view since this call was scheduled.
viewableIndicesToCheck = viewableIndicesToCheck.filter(ii =>
this._viewableIndices.includes(ii),
);
const prevItems = this._viewableItems;
export type ViewabilityConfig = {
/**
* Minimum amount of time (in milliseconds) that an item must be physically viewable before the
* viewability callback will be fired. A high number means that scrolling through content without
* stopping will not mark the content as viewable.
*/
minimumViewTime?: number,
/**
* Percent of viewport that must be covered for a partially occluded item to count as
viewabilityConfig = {
waitForInteraction: true,
viewAreaCoveragePercentThreshold: 95,
itemVisiblePercentThreshold: 75
}
onViewableItemsChanged = ({viewableItems, changed}) => {
console.log("Visible items are", viewableItems);
console.log("Changed in this iteration", changed);
};
export type ViewabilityConfig = {
/**
* Minimum amount of time (in milliseconds) that an item must be physically viewable before the
* viewability callback will be fired. A high number means that scrolling through content without
* stopping will not mark the content as viewable.
*/
minimumViewTime?: number,
/**
* Percent of viewport that must be covered for a partially occluded item to count as
const timestamp = e.timeStamp;
let visibleLength = this._selectLength(e.nativeEvent.layoutMeasurement);
let contentLength = this._selectLength(e.nativeEvent.contentSize);
let offset = this._selectOffset(e.nativeEvent.contentOffset);
let dOffset = offset - this._scrollMetrics.offset;
// ...
this._scrollMetrics = {
contentLength,
dt,