fix: virtual list doesnt update lists with few items
This commit is contained in:
2 files changed
+31
-2
No files matched your search
@@ -42,8 +42,8 @@
|
|||||||
{#if offsetEl}
|
{#if offsetEl}
|
||||||
<VirtualList
|
<VirtualList
|
||||||
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
|
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
|
||||||
overscan={2}
|
overscan={5}
|
||||||
estimatedHeight={2000}
|
estimatedHeight={500}
|
||||||
items={nodes}
|
items={nodes}
|
||||||
{initialOffset}
|
{initialOffset}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
<script lang="ts" generics="T">
|
<script lang="ts" generics="T">
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
|
import { settings } from '$lib/settings.svelte'
|
||||||
import { debounce } from 'mono-svelte/util/time'
|
import { debounce } from 'mono-svelte/util/time'
|
||||||
import { onDestroy, onMount, untrack, type Snippet } from 'svelte'
|
import { onDestroy, onMount, untrack, type Snippet } from 'svelte'
|
||||||
import type { HTMLAttributes } from 'svelte/elements'
|
import type { HTMLAttributes } from 'svelte/elements'
|
||||||
import { innerHeight } from 'svelte/reactivity/window'
|
import { innerHeight } from 'svelte/reactivity/window'
|
||||||
|
import Expandable from '../ui/Expandable.svelte'
|
||||||
|
|
||||||
interface Props extends HTMLAttributes<HTMLDivElement> {
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
||||||
items: T[]
|
items: T[]
|
||||||
@@ -32,6 +34,8 @@
|
|||||||
...rest
|
...rest
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
|
let initialRender = false
|
||||||
|
|
||||||
export function scrollToIndex(index: number, useWindow: boolean = false) {
|
export function scrollToIndex(index: number, useWindow: boolean = false) {
|
||||||
const targetPx = cumulativeItemHeights[index] - (initialOffset || 0)
|
const targetPx = cumulativeItemHeights[index] - (initialOffset || 0)
|
||||||
if (targetPx < (innerHeight.current ?? 0)) return
|
if (targetPx < (innerHeight.current ?? 0)) return
|
||||||
@@ -145,6 +149,7 @@
|
|||||||
const newHeight = entry.contentRect.height
|
const newHeight = entry.contentRect.height
|
||||||
if (itemHeights[index] !== newHeight) {
|
if (itemHeights[index] !== newHeight) {
|
||||||
itemHeights[index] = newHeight
|
itemHeights[index] = newHeight
|
||||||
|
if (!initialRender) visibleItems = updateVisibleItems()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, debounceResize)
|
}, debounceResize)
|
||||||
@@ -193,6 +198,7 @@
|
|||||||
})
|
})
|
||||||
untrack(() => {
|
untrack(() => {
|
||||||
visibleItems = updateVisibleItems()
|
visibleItems = updateVisibleItems()
|
||||||
|
initialRender = false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -231,6 +237,29 @@
|
|||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
|
{#if settings.debugInfo}
|
||||||
|
<Expandable>
|
||||||
|
{#snippet title()}
|
||||||
|
Debug
|
||||||
|
{/snippet}
|
||||||
|
<pre>
|
||||||
|
Virtual list debug info
|
||||||
|
|
||||||
|
List items: {items.length}
|
||||||
|
Rendering items: {visibleItems.length} ({visibleItems[0]
|
||||||
|
?.index} - {visibleItems[visibleItems.length - 1].index})
|
||||||
|
Viewport height: {viewportHeight}
|
||||||
|
Current scroll position: {scrollY}
|
||||||
|
Container height: {cumulativeItemHeights[
|
||||||
|
visibleItems?.[visibleItems.length - 1]?.index
|
||||||
|
]}
|
||||||
|
Overscan: {overscan}
|
||||||
|
Guess item height: {estimatedHeight}
|
||||||
|
Bumpscosity: {Math.floor(Math.random() * 5000)}
|
||||||
|
Restore data: {JSON.stringify(restore)}
|
||||||
|
</pre>
|
||||||
|
</Expandable>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.fix-divide:nth-child(2) {
|
.fix-divide:nth-child(2) {
|
||||||
|
|||||||
Reference in new issue
Block a user