Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions packages/virtual/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type VirtualListReturn<T extends readonly any[]> = [
containerHeight: number;
viewerTop: number;
visibleItems: T;
firstIndex: number;
}>,
onScroll: (e: Event) => void,
];
Expand All @@ -41,20 +42,21 @@ export function createVirtualList<T extends readonly any[]>({

const [offset, setOffset] = createSignal(0);

const getFirstIdx = () => Math.max(0, Math.floor(offset() / rowHeight) - overscanCount);

const getLastIdx = () =>
Math.min(
items.length,
Math.floor(offset() / rowHeight) + Math.ceil(rootHeight / rowHeight) + overscanCount,
);

return [
() => ({
containerHeight: items.length * rowHeight,
viewerTop: getFirstIdx() * rowHeight,
visibleItems: items.slice(getFirstIdx(), getLastIdx()) as unknown as T,
}),
() => {
const firstIndex = Math.max(0, Math.floor(offset() / rowHeight) - overscanCount);
const lastIndex = Math.min(
items.length,
Math.floor(offset() / rowHeight) + Math.ceil(rootHeight / rowHeight) + overscanCount,
);

return {
containerHeight: items.length * rowHeight,
viewerTop: firstIndex * rowHeight,
visibleItems: items.slice(firstIndex, lastIndex) as unknown as T,
firstIndex,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you're at it, you can also expose lastIndex.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can. I did originally, but removed it because its a little weird semantically: when a list is empty, should it return -1, or undefined?

firstIndex being zero makes sense, kinda, but I didn't know what to do with last's typing, so I skipped it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, it's more like nextIndex than last because it's an exclusive range.

This comment was marked as duplicate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, too.

};
},
e => {
// @ts-expect-error
if (e.target?.scrollTop !== undefined) setOffset(e.target.scrollTop);
Expand Down
24 changes: 24 additions & 0 deletions packages/virtual/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ describe("createVirtualList", () => {
expect(virtual().visibleItems).toEqual([0, 1, 2]);
});

test("returns firstIndex representing the index of the visibleList", () => {
const [virtual] = createVirtualList({
items: TEST_LIST,
rootHeight: 20,
rowHeight: 10,
});

expect(virtual().firstIndex).toEqual(0);
});

test("returns onScroll which sets viewerTop and visibleItems based on rootElement's scrolltop", () => {
const el = document.createElement("div");

Expand All @@ -62,35 +72,48 @@ describe("createVirtualList", () => {

expect(virtual().visibleItems).toEqual([0, 1, 2]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);

el.scrollTop += 10;

// no change until onScroll is called
expect(virtual().visibleItems).toEqual([0, 1, 2]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);

onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([0, 1, 2, 3]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);

el.scrollTop += 10;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([1, 2, 3, 4]);
expect(virtual().viewerTop).toEqual(10);
expect(virtual().firstIndex).toEqual(1);

el.scrollTop -= 10;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([0, 1, 2, 3]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);

el.scrollTop -= 10;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([0, 1, 2]);
expect(virtual().viewerTop).toEqual(0);
expect(virtual().firstIndex).toEqual(0);

el.scrollTop += 7_000;
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([699, 700, 701, 702]);
expect(virtual().viewerTop).toEqual(6990);
expect(virtual().firstIndex).toEqual(699);
});

test("onScroll handles reaching the bottom of the list", () => {
Expand Down Expand Up @@ -126,6 +149,7 @@ describe("createVirtualList", () => {
onScroll(TARGETED_SCROLL_EVENT(el));

expect(virtual().visibleItems).toEqual([8, 9, 10, 11, 12, 13]);
expect(virtual().firstIndex).toEqual(8);
});

test("overscanCount defaults to 1 if undefined or zero", () => {
Expand Down