svelte-tiny-virtual-list
svelte-tiny-virtual-list copied to clipboard
fix: handle decrease in itemCount cleanly
If itemCount decreases, for example due to filtering, there are two issues:
- SizeAndPositionManager lastMeasuredIndex can point to items that no longer exist, causing "index X is outside of range"
- VirtualList tries to render stale items before refresh() has updated
This fix:
- Clamps lastMeasuredIndex in updateConfig()
- Clamps the scroll offset in refresh()
I introduced a derived validItems to filter out stale items. This can be inlined as {#each items.filter((item) => item.index < itemCount) as item (getKey ? getKey(item.index) : item.index)} but I felt the line was long and hard to follow.