From 61b238541b924f3fbcbbb3299d3127ea89b419eb Mon Sep 17 00:00:00 2001 From: James Xylight Date: Sun, 11 May 2025 10:26:46 -0700 Subject: [PATCH] fix: dynamic initialOffset causes missing comments --- .../comment/CommentListVirtualizer.svelte | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte b/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte index aafdd93c..b4efb098 100644 --- a/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte +++ b/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte @@ -15,6 +15,14 @@ let offsetEl = $state() + let initialOffset = $state(0) + + const updateOffset = (entry: IntersectionObserverEntry) => { + if (entry.isIntersecting && offsetEl) { + initialOffset = entry.boundingClientRect.top + window.scrollY + } + } + onMount(() => { if (scrollTo) { const element = document?.getElementById(scrollTo) @@ -22,6 +30,22 @@ element?.scrollIntoView({ behavior: 'smooth', block: 'start' }) }, 100) } + + // IntersectionObserver to watch for shifts + const observer = new IntersectionObserver( + (entries) => { + entries.forEach(updateOffset) + }, + { threshold: [0, 1] }, + ) + + if (offsetEl) { + observer.observe(offsetEl) + } + + return () => { + observer.disconnect() + } }) @@ -30,7 +54,7 @@ class="divide-y divide-slate-200 dark:divide-zinc-800 w-full" overscan={500} items={nodes} - initialOffset={offsetEl?.offsetTop} + {initialOffset} > {#snippet item(item, index)}