add infinite scroll

This commit is contained in:
Pete Matsyburka committed 2026-09-23 10:59:54 +03:00
1 parent d3cc2e1ca8
commit 8cb0a7a020
21 files changed
+179 -10

No files matched your search

@@ -0,0 +1,41 @@
import { renderStreamMessage } from '@hotwired/turbo'
export default class extends HTMLElement {
connectedCallback () {
if (!this.dataset.src) {
this.remove()
return
}
this.observer = new IntersectionObserver((entries) => {
if (entries.some((entry) => entry.isIntersecting)) {
this.load()
}
}, { rootMargin: '400px' })
this.observer.observe(this)
}
disconnectedCallback () {
this.observer?.disconnect()
}
load () {
if (this.loading || !this.dataset.src) return
this.loading = true
fetch(this.dataset.src, {
headers: { 'X-Turbo-Infinite-Scroll': 'true' }
}).then(async (response) => {
if (!response.ok) return
delete this.dataset.src
renderStreamMessage(await response.text())
}).finally(() => {
this.loading = false
})
}
}