A virtual list component for Svelte apps. Instead of rendering all your data, <VirtuaInfinitelList> just renders the bits that are visible, keeping your page nice and light.
<scriptlang="ts">importVirtualInfiniteListfrom'svelte-virtual-infinite-list'importtype{InfiniteEvent}from'svelte-virtual-infinite-list'import{find}from'./find'letthings=[// these can be any values you like{name: 'one',number: 1},{name: 'two',number: 2},{name: 'three',number: 3},// ...{name: 'six thousand and ninety-two',number: 6092}]letloading=trueletvirtualInfiniteList: VirtualInfiniteListasyncfunctiononInitialize(){awaitvirtualInfiniteList.scrollToBottom()}asyncfunctiononInfinite({ detail }: InfiniteEvent){if(detail.on==='bottom')returnloading=trueconstdata=awaitfind(30)things=[...data, ...things]loading=false}onMount(async()=>{constdata=awaitfind(30)things=[...data]loading=false})asyncfunctionscrollToIndex(item){constindex=things.findIndex((it)=>it===item.number)index&&awaitvirtualInfiniteList.scrollToIndex(index)}</script><VirtualInfiniteListitems={things}{loading}direction="top"
persists={30}uniqueKey={'number'}
on:initialize={onInitialize}
on:infinite={onInfinite}
bind:this={virtualInfiniteList}
let:item
><!-- this will be rendered for each currently visible item --><divslot="item"><p>{item.number}: {item.name}</p></div><!-- option --><divslot="loader">
Loading...
</div><!-- option --><divslot="empty">
Empty
</div></VirtualInfiniteList>
Additional Props
No
Property Name
Type
Note
1
loading
boolean
-
2
direction
'top' or 'bottom' or 'vertical'
Loading direction.
3
maxItemCountPerLoad
number
Deprecated. This valiable removed @2.0.0. Use persists, please.
4
persists
number
[For direction-top infinite scroll user] Maximum number of items loaded per load. The offset after loaded may be significantly shift if the number of items that exceeds this value is loaded. Default value is 30.
5
uniqueKey
string
You need to set specify one unique property like id in the item object if you want to use the scrollToIndex method. Default value is undefined.
Additional Events
No
Property Name
Type
Note
1
on:initialize
() => any
Emit on change items count from 0 to more over.
2
on:infinite
(event: InfiniteEvent) => any
Emit on scrollbar reached top or bottom.
Additional Slots
No
Slot Name
Note
1
item
Displayed item
2
loader
Displayed element if loading is true
3
empty
Displayed element if items is [] and loading is false
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.