fix: virtualizer initialOffset causes missing items on shift (#582)

This commit is contained in:
Xylight authored and GitHub committed 2025-06-16 12:10:32 -07:00
1 parent e3aab10cd0
commit 0de8758996
4 files changed
+56 -44

No files matched your search

@@ -1,27 +1,14 @@
<script lang="ts">
import VirtualList from '$lib/components/render/VirtualList.svelte'
import type { Post } from 'lemmy-js-client'
import { onMount } from 'svelte'
import type { CommentNodeI } from './comments.svelte'
import Comments from './Comments.svelte'
import { onMount } from 'svelte'
interface Props {
nodes: CommentNodeI[]
post: Post
scrollTo: string | undefined
}
let { nodes, post, scrollTo }: Props = $props()
let offsetEl = $state<HTMLElement>()
let list = $state()
let initialOffset = $state(0)
const updateOffset = (entry: IntersectionObserverEntry) => {
if (entry.isIntersecting && offsetEl) {
initialOffset = entry.boundingClientRect.top + window.scrollY
}
scrollTo?: string
}
onMount(() => {
@@ -35,37 +22,42 @@
// IntersectionObserver to watch for shifts
const observer = new IntersectionObserver(
entries => {
entries.forEach(updateOffset)
entries.forEach(entry => {
if (entry.isIntersecting && offsetEl) {
initialOffset = entry.boundingClientRect.top + window.scrollY
}
})
},
{ threshold: [0, 1] },
)
if (offsetEl) {
observer.observe(offsetEl)
}
return () => {
observer.disconnect()
}
if (offsetEl) observer.observe(offsetEl)
return () => observer.disconnect()
})
let { nodes, post, scrollTo }: Props = $props()
let offsetEl = $state<HTMLElement>()
let initialOffset = $derived(offsetEl?.offsetTop)
</script>
<div bind:this={offsetEl}>
<VirtualList
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
overscan={500}
items={nodes}
{initialOffset}
bind:this={list}
>
{#snippet item(index)}
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
<Comments
isParent={true}
bind:nodes={() => [nodes[index]], v => (nodes[index] = v[0])}
{post}
/>
</div>
{/snippet}
</VirtualList>
{#if offsetEl}
<VirtualList
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
overscan={500}
items={nodes}
{initialOffset}
>
{#snippet item(index)}
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
<Comments
isParent={true}
bind:nodes={() => [nodes[index]], v => (nodes[index] = v[0])}
{post}
/>
</div>
{/snippet}
</VirtualList>
{/if}
</div>