Skip to content

Commit 2403f83

Browse files
author
Ruben L
committed
#43 introduces scrollToIndex method
1 parent a05f9c6 commit 2403f83

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

VirtualList.svelte

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// read-only, but visible to consumers via bind:start
1212
export let start = 0;
1313
export let end = 0;
14-
1514
// local state
1615
let height_map = [];
1716
let rows;
@@ -33,6 +32,12 @@
3332
$: if (mounted) refresh(items, viewport_height, itemHeight);
3433
3534
async function refresh(items, viewport_height, itemHeight) {
35+
const isStartOverflow = items.length < start
36+
37+
if (isStartOverflow) {
38+
await scrollToIndex(items.length - 1, {behavior: 'auto'})
39+
}
40+
3641
const { scrollTop } = viewport;
3742
3843
await tick(); // wait until the DOM is up to date
@@ -105,6 +110,7 @@
105110
bottom = remaining * average_height;
106111
107112
// prevent jumping if we scrolled up into unknown territory
113+
return
108114
if (start < old_start) {
109115
await tick();
110116
@@ -127,6 +133,20 @@
127133
// more. maybe we can just call handle_scroll again?
128134
}
129135
136+
export async function scrollToIndex (index, opts) {
137+
const {scrollTop} = viewport;
138+
const itemsDelta = index - start;
139+
const _itemHeight = itemHeight || average_height;
140+
const distance = itemsDelta * _itemHeight;
141+
opts = {
142+
left: 0,
143+
top: scrollTop + distance,
144+
behavior: 'smooth',
145+
...opts
146+
};
147+
viewport.scrollTo(opts);
148+
}
149+
130150
// trigger initial refresh
131151
onMount(() => {
132152
rows = contents.getElementsByTagName('svelte-virtual-list-row');

0 commit comments

Comments
 (0)