fix: dynamic initialOffset causes missing comments
This commit is contained in:
1 file changed
+25
-1
@@ -15,6 +15,14 @@
|
|||||||
|
|
||||||
let offsetEl = $state<HTMLElement>()
|
let offsetEl = $state<HTMLElement>()
|
||||||
|
|
||||||
|
let initialOffset = $state(0)
|
||||||
|
|
||||||
|
const updateOffset = (entry: IntersectionObserverEntry) => {
|
||||||
|
if (entry.isIntersecting && offsetEl) {
|
||||||
|
initialOffset = entry.boundingClientRect.top + window.scrollY
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
if (scrollTo) {
|
if (scrollTo) {
|
||||||
const element = document?.getElementById(scrollTo)
|
const element = document?.getElementById(scrollTo)
|
||||||
@@ -22,6 +30,22 @@
|
|||||||
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
||||||
}, 100)
|
}, 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()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -30,7 +54,7 @@
|
|||||||
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={offsetEl?.offsetTop}
|
{initialOffset}
|
||||||
>
|
>
|
||||||
{#snippet item(item, index)}
|
{#snippet item(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">
|
||||||
|
|||||||
Reference in new issue
Block a user