fix: virtualizer initialOffset causes missing items on shift (#582)
This commit is contained in:
1 parent
e3aab10cd0
commit
0de8758996
4 files changed
+40
-28
No files matched your search
@@ -1,27 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import VirtualList from '$lib/components/render/VirtualList.svelte'
|
import VirtualList from '$lib/components/render/VirtualList.svelte'
|
||||||
import type { Post } from 'lemmy-js-client'
|
import type { Post } from 'lemmy-js-client'
|
||||||
import { onMount } from 'svelte'
|
|
||||||
import type { CommentNodeI } from './comments.svelte'
|
import type { CommentNodeI } from './comments.svelte'
|
||||||
import Comments from './Comments.svelte'
|
import Comments from './Comments.svelte'
|
||||||
|
import { onMount } from 'svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
nodes: CommentNodeI[]
|
nodes: CommentNodeI[]
|
||||||
post: Post
|
post: Post
|
||||||
scrollTo: string | undefined
|
scrollTo?: string
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
@@ -35,28 +22,32 @@
|
|||||||
// IntersectionObserver to watch for shifts
|
// IntersectionObserver to watch for shifts
|
||||||
const observer = new IntersectionObserver(
|
const observer = new IntersectionObserver(
|
||||||
entries => {
|
entries => {
|
||||||
entries.forEach(updateOffset)
|
entries.forEach(entry => {
|
||||||
|
if (entry.isIntersecting && offsetEl) {
|
||||||
|
initialOffset = entry.boundingClientRect.top + window.scrollY
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
{ threshold: [0, 1] },
|
{ threshold: [0, 1] },
|
||||||
)
|
)
|
||||||
|
|
||||||
if (offsetEl) {
|
if (offsetEl) observer.observe(offsetEl)
|
||||||
observer.observe(offsetEl)
|
return () => observer.disconnect()
|
||||||
}
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
observer.disconnect()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let { nodes, post, scrollTo }: Props = $props()
|
||||||
|
|
||||||
|
let offsetEl = $state<HTMLElement>()
|
||||||
|
let initialOffset = $derived(offsetEl?.offsetTop)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div bind:this={offsetEl}>
|
<div bind:this={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={500}
|
overscan={500}
|
||||||
items={nodes}
|
items={nodes}
|
||||||
{initialOffset}
|
{initialOffset}
|
||||||
bind:this={list}
|
|
||||||
>
|
>
|
||||||
{#snippet item(index)}
|
{#snippet item(index)}
|
||||||
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
|
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
|
||||||
@@ -68,4 +59,5 @@
|
|||||||
</div>
|
</div>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</VirtualList>
|
</VirtualList>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
@@ -79,7 +79,7 @@
|
|||||||
]
|
]
|
||||||
: []}
|
: []}
|
||||||
bio={community.description}
|
bio={community.description}
|
||||||
class={clazz}
|
class={['tracking-normal', clazz]}
|
||||||
>
|
>
|
||||||
{#snippet nameDetail()}
|
{#snippet nameDetail()}
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -136,6 +136,23 @@
|
|||||||
removedNodes.forEach(unobservePost)
|
removedNodes.forEach(unobservePost)
|
||||||
})
|
})
|
||||||
}).observe(feed, { childList: true, subtree: false })
|
}).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(() => {
|
$effect(() => {
|
||||||
@@ -147,6 +164,8 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let initialOffset = $derived(listEl?.offsetTop)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul class="flex flex-col list-none" bind:this={listEl}>
|
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||||
@@ -171,7 +190,7 @@
|
|||||||
id="feed"
|
id="feed"
|
||||||
class="divide-y -mx-4 sm:-mx-6 divide-slate-200 dark:divide-zinc-900"
|
class="divide-y -mx-4 sm:-mx-6 divide-slate-200 dark:divide-zinc-900"
|
||||||
items={posts}
|
items={posts}
|
||||||
initialOffset={listEl?.offsetTop}
|
{initialOffset}
|
||||||
overscan={500}
|
overscan={500}
|
||||||
bind:restore={postFeeds.value[feedId].clientData}
|
bind:restore={postFeeds.value[feedId].clientData}
|
||||||
bind:this={listComp}
|
bind:this={listComp}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { t } from '$lib/i18n/translations'
|
import { t } from '$lib/i18n/translations'
|
||||||
|
import type { ClassValue } from 'svelte/elements'
|
||||||
import Markdown from '../markdown/Markdown.svelte'
|
import Markdown from '../markdown/Markdown.svelte'
|
||||||
import FormattedNumber from '../util/FormattedNumber.svelte'
|
import FormattedNumber from '../util/FormattedNumber.svelte'
|
||||||
import Avatar from './Avatar.svelte'
|
import Avatar from './Avatar.svelte'
|
||||||
@@ -17,7 +18,7 @@
|
|||||||
format?: boolean
|
format?: boolean
|
||||||
}[]
|
}[]
|
||||||
center?: boolean
|
center?: boolean
|
||||||
class?: string
|
class?: ClassValue
|
||||||
nameDetail?: import('svelte').Snippet
|
nameDetail?: import('svelte').Snippet
|
||||||
actions?: import('svelte').Snippet
|
actions?: import('svelte').Snippet
|
||||||
children?: import('svelte').Snippet
|
children?: import('svelte').Snippet
|
||||||
@@ -39,7 +40,7 @@
|
|||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div {...rest} class="z-10 relative text-sm contents {clazz}">
|
<div {...rest} class={['z-10 relative text-sm contents', clazz]}>
|
||||||
{#if banner}
|
{#if banner}
|
||||||
<img
|
<img
|
||||||
src={banner}
|
src={banner}
|
||||||
|
|||||||
Reference in new issue
Block a user