fix: virtualizer initialOffset causes missing items on shift (#582)
This commit is contained in:
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>
|
||||
@@ -79,7 +79,7 @@
|
||||
]
|
||||
: []}
|
||||
bio={community.description}
|
||||
class={clazz}
|
||||
class={['tracking-normal', clazz]}
|
||||
>
|
||||
{#snippet nameDetail()}
|
||||
<button
|
||||
|
||||
@@ -136,6 +136,23 @@
|
||||
removedNodes.forEach(unobservePost)
|
||||
})
|
||||
}).observe(feed, { childList: true, subtree: false })
|
||||
|
||||
const offsetObserver = new IntersectionObserver(
|
||||
entries => {
|
||||
entries.forEach(entry => {
|
||||
if (entry.isIntersecting && listEl) {
|
||||
initialOffset = entry.boundingClientRect.top + window.scrollY
|
||||
}
|
||||
})
|
||||
},
|
||||
{ threshold: [0, 1] },
|
||||
)
|
||||
|
||||
if (listEl) offsetObserver.observe(listEl)
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
offsetObserver.disconnect()
|
||||
}
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
@@ -147,6 +164,8 @@
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
let initialOffset = $derived(listEl?.offsetTop)
|
||||
</script>
|
||||
|
||||
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||
@@ -171,7 +190,7 @@
|
||||
id="feed"
|
||||
class="divide-y -mx-4 sm:-mx-6 divide-slate-200 dark:divide-zinc-900"
|
||||
items={posts}
|
||||
initialOffset={listEl?.offsetTop}
|
||||
{initialOffset}
|
||||
overscan={500}
|
||||
bind:restore={postFeeds.value[feedId].clientData}
|
||||
bind:this={listComp}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { t } from '$lib/i18n/translations'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import Markdown from '../markdown/Markdown.svelte'
|
||||
import FormattedNumber from '../util/FormattedNumber.svelte'
|
||||
import Avatar from './Avatar.svelte'
|
||||
@@ -17,7 +18,7 @@
|
||||
format?: boolean
|
||||
}[]
|
||||
center?: boolean
|
||||
class?: string
|
||||
class?: ClassValue
|
||||
nameDetail?: import('svelte').Snippet
|
||||
actions?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
@@ -39,7 +40,7 @@
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<div {...rest} class="z-10 relative text-sm contents {clazz}">
|
||||
<div {...rest} class={['z-10 relative text-sm contents', clazz]}>
|
||||
{#if banner}
|
||||
<img
|
||||
src={banner}
|
||||
|
||||
Reference in new issue
Block a user