Svelte components for efficiently rendering large lists. Instead of rendering all your data, svelte-virtual renders only what's visible
With npm:
npm i -D svelte-virtual@next
With yarn:
yarn add -D svelte-virtual@next
With pnpm:
pnpm add -D svelte-virtual@next
<script>
import { List } from "svelte-virtual";
let items = [...Array(100000).keys()];
</script>
<List itemCount={items.length} itemSize={20} height={500}>
{#snippet item({ index, style })}
<div {style}>
{items[index]}
</div>
{/snippet}
</List>
<script>
import { List } from "svelte-virtual";
let items = [...Array(100000).keys()];
</script>
<List itemCount={items.length} itemSize={60} height={40} layout="horizontal">
{#snippet item({ index, style })}
<div {style}>
{items[index]}
</div>
{/snippet}
</List>
<script>
import { Grid } from "svelte-virtual";
let items = [...Array(100000).keys()];
</script>
<Grid itemCount={items.length} itemHeight={50} itemWidth={60} height={500}>
{#snippet item({ index, style })}
<div {style}>
{items[index]}
</div>
{/snippet}
</Grid>
<script>
import { Grid } from "svelte-virtual";
let itemCount = 100000;
let columnCount = 5;
let items = Array.from({ length: itemCount }, (_, l) =>
Array.from({ length: columnCount }, (_, c) => `${l}-${c}`),
);
</script>
<Grid itemCount={itemCount * columnCount} itemHeight={50} itemWidth={65} height={500} {columnCount}>
{#snippet item({ rowIndex, columnIndex, style })}
<div {style}>
{items[rowIndex][columnIndex]}
</div>
{/snippet}
</Grid>
Property | Type | Default | Required? |
---|---|---|---|
itemCount | number |
✓ | |
itemSize | number |
✓ | |
height | number | string |
"100%" |
|
width | number | string |
"100%" |
|
stickyIndices | number[] |
[] |
|
overScan | number |
1 |
|
marginLeft | number |
0 |
|
marginTop | number |
0 |
|
layout | "vertical" | "horizontal" |
"vertical" |
|
scrollPosition | number |
0 |
|
scrollAlignment | "auto" | "start" | "center" | "end" |
"auto" |
|
scrollBehavior | "auto" | "smooth" |
"auto" |
|
getKey | (index: number) => unknown |
(index: number) => index |
Property | Type | Default | Required? |
---|---|---|---|
itemCount | number |
✓ | |
itemHeight | number |
✓ | |
itemWidth | number |
✓ | |
height | number |
✓ | |
width | string |
"100%" |
|
overScan | number |
1 |
|
marginLeft | number |
0 |
|
marginTop | number |
0 |
|
scrollPosition | number |
0 |
|
scrollBehavior | "auto" | "smooth" |
"auto" |
|
getKey | (index: number) => unknown |
(index: number) => index |
|
columnCount | number |
undefined |
Method | Argument | Type | Default | Required? |
---|---|---|---|---|
scrollToIndex | ||||
index | number |
✓ | ||
alignment | "auto" | "start" | "center" | "end" |
scrollAlignment |
||
behavior | "auto" | "smooth" |
scrollBehavior |
||
scrollToPosition | ||||
position | number |
✓ | ||
behavior | "auto" | "smooth" |
scrollBehavior |
item
- Snippet for each itemindex: number
- Item indexstyle: string
- Item style must be applied to the first child element of the snippet (look above for example)<Grid/>
(demo):rowIndex: number
- Item row indexcolumnIndex: number
- Item column indexplaceholder
(optional) - Snippet for each item (when scrolling fast, replaces item
snippet. if not present, item
snippet is used)index: number
- Item indexstyle: string
- Item style must be applied to the first child element of the snippet (look above for example)<Grid/>
(demo):rowIndex: number
- Item row indexcolumnIndex: number
- Item column indexheader
(optional) - Snippet for the elements that should appear at the top of the componentfooter
(optional) - Snippet for the elements that should appear at the bottom of the component