Skip to content

Commit

Permalink
refactor: move the computation of last right and left indexes to offs…
Browse files Browse the repository at this point in the history
…et function (#157)
  • Loading branch information
JulienIzz authored Sep 30, 2024
1 parent 5c4913e commit af37542
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from './hooks/useVirtualizedListAnimation';
import { NodeOrientation } from '../../types/orientation';
import { typedMemo } from '../../helpers/TypedMemo';
import { getLastLeftItemIndex, getLastRightItemIndex } from './helpers/getLastItemIndex';
import { getSizeInPxFromOneItemToAnother } from './helpers/getSizeInPxFromOneItemToAnother';
import { computeAllScrollOffsets } from './helpers/createScrollOffsetArray';

Expand Down Expand Up @@ -158,9 +157,6 @@ export const VirtualizedList = typedMemo(

const dataSliceToRender = data.slice(range.start, range.end + 1);

const maxPossibleLeftAlignedIndex = getLastLeftItemIndex<T>(data, itemSize, listSizeInPx);
const maxPossibleRightAlignedIndex = getLastRightItemIndex<T>(data, itemSize, listSizeInPx);

const allScrollOffsets = useMemo(
() =>
computeAllScrollOffsets({
Expand All @@ -170,19 +166,8 @@ export const VirtualizedList = typedMemo(
scrollBehavior: scrollBehavior,
data: data,
listSizeInPx: listSizeInPx,
maxPossibleLeftAlignedIndex: maxPossibleLeftAlignedIndex,
maxPossibleRightAlignedIndex: maxPossibleRightAlignedIndex,
}),
[
data,
itemSize,
listSizeInPx,
maxPossibleLeftAlignedIndex,
maxPossibleRightAlignedIndex,
nbMaxOfItems,
numberOfItemsVisibleOnScreen,
scrollBehavior,
],
[data, itemSize, listSizeInPx, nbMaxOfItems, numberOfItemsVisibleOnScreen, scrollBehavior],
);

useOnEndReached({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ScrollBehavior } from '../VirtualizedList';
import { computeTranslation } from './computeTranslation';
import { getLastLeftItemIndex, getLastRightItemIndex } from './getLastItemIndex';

/**
* This function precomputes all scroll offsets
Expand All @@ -12,18 +13,17 @@ export const computeAllScrollOffsets = <T>({
scrollBehavior,
data,
listSizeInPx,
maxPossibleLeftAlignedIndex,
maxPossibleRightAlignedIndex,
}: {
itemSize: number | ((item: T) => number);
nbMaxOfItems: number;
numberOfItemsVisibleOnScreen: number;
scrollBehavior: ScrollBehavior;
data: T[];
listSizeInPx: number;
maxPossibleLeftAlignedIndex: number;
maxPossibleRightAlignedIndex: number;
}) => {
const maxPossibleLeftAlignedIndex = getLastLeftItemIndex<T>(data, itemSize, listSizeInPx);
const maxPossibleRightAlignedIndex = getLastRightItemIndex<T>(data, itemSize, listSizeInPx);

const scrollOffsets = data.map((_, index) =>
computeTranslation({
currentlyFocusedItemIndex: index,
Expand Down

0 comments on commit af37542

Please sign in to comment.