-
Notifications
You must be signed in to change notification settings - Fork 145
expose firstIndex in virtual scroller
#823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@solid-primitives/virtual": patch | ||
| --- | ||
|
|
||
| expose `firstIndex` and `lastIndex` in virtual lists, to support counting |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ type VirtualListReturn<T extends readonly any[]> = [ | |
| containerHeight: number; | ||
| viewerTop: number; | ||
| visibleItems: T; | ||
| firstIndex: number; | ||
| lastIndex: number | undefined; | ||
| }>, | ||
| onScroll: (e: Event) => void, | ||
| ]; | ||
|
|
@@ -41,20 +43,23 @@ 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, | ||
| lastIndex: lastIndex > 0 ? lastIndex - 1 : undefined, | ||
| // -1 because slice is an exclusive range | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I'm not sure what you're asking. The
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I read the whole thing in a different order. My bad, I'm currently down with a flu. |
||
| }; | ||
| }, | ||
| e => { | ||
| // @ts-expect-error | ||
| if (e.target?.scrollTop !== undefined) setOffset(e.target.scrollTop); | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?firstIndexbeing zero makes sense, kinda, but I didn't know what to do with last's typing, so I skipped it.There was a problem hiding this comment.
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
nextIndexthan last because it's an exclusive range.This comment was marked as duplicate.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine, too.