-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathColumnVirtualizerDynamic.svelte
More file actions
60 lines (53 loc) · 1.71 KB
/
ColumnVirtualizerDynamic.svelte
File metadata and controls
60 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<script lang="ts">
import { faker } from '@faker-js/faker'
import { createVirtualizer } from '@tanstack/svelte-virtual'
let virtualListEl = $state<HTMLDivElement | null>(null)
let virtualItemEls = $state<HTMLDivElement[]>([])
function randomNumber(min: number, max: number) {
return faker.number.int({ min, max })
}
const sentences = new Array(10000)
.fill(true)
.map(() => faker.lorem.sentence(randomNumber(20, 70)))
let makeGetScrollElement = (scrollElement: HTMLDivElement | null) => () =>
scrollElement
let virtualizer = $derived(
createVirtualizer<HTMLDivElement, HTMLDivElement>({
horizontal: true,
count: sentences.length,
getScrollElement: makeGetScrollElement(virtualListEl),
estimateSize: () => 45,
}),
)
$effect(() => {
if (virtualItemEls.length)
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
})
</script>
<div class="list scroll-container" bind:this={virtualListEl}>
<div
style="position: relative; height: 100%; width: {$virtualizer.getTotalSize()}px;"
>
{#each $virtualizer.getVirtualItems() as col, idx (col.index)}
<div
bind:this={virtualItemEls[idx]}
data-index={col.index}
class:list-item-even={col.index % 2 === 0}
class:list-item-odd={col.index % 2 === 1}
style="position: absolute; top: 0; left: 0; height: 100%; transform: translateX({col.start}px);"
>
<div style="width: {sentences[col.index].length}px">
<div>Column {col.index}</div>
<div>{sentences[col.index]}</div>
</div>
</div>
{/each}
</div>
</div>
<style>
.scroll-container {
height: 400px;
width: 400px;
overflow: auto;
}
</style>