import { untrack } from 'svelte'; import { Virtualizer, observeElementOffset, observeElementRect, type VirtualItem, type VirtualizerOptions, } from '@tanstack/virtual-core'; type Options = Omit< VirtualizerOptions, | 'getScrollElement' | 'scrollToFn' | 'observeElementRect' | 'observeElementOffset' | 'onChange' > & { scroller: HTMLElement | undefined; /** The rows' parent: each row carries `data-index`. */ list: HTMLElement | undefined; /** Writes `scrollTop` and passes it to `syncScroll`. The virtualizer scrolls through it too. */ scrollTo: (top: number) => void; /** Runs once the drawn rows have committed and been measured, before the page paints. */ onUpdate: () => void; }; /** * A TanStack virtualizer read through runes. `options` is read again * whenever what it reads changes, before the DOM updates, so rows added * above keep the place of the rows drawn before them; `items` changes * with the scroll position and measured heights. */ export class VirtualList { readonly virtualizer: Virtualizer; #version = $state(0); #reportOffset: ((offset: number, isScrolling: boolean) => void) | null = null; constructor(options: () => Options) { const settings = (): VirtualizerOptions => { const { scroller, list, scrollTo, onUpdate, ...rest } = options(); return { ...rest, getScrollElement: () => scroller ?? null, scrollToFn: (offset, { adjustments = 0 }) => scrollTo(offset + adjustments), observeElementRect, observeElementOffset: (instance, cb) => { this.#reportOffset = cb; const stop = observeElementOffset(instance, cb); return () => { this.#reportOffset = null; stop?.(); }; }, onChange: () => this.#version++, }; }; this.virtualizer = new Virtualizer(untrack(settings)); $effect.pre(() => { this.virtualizer.setOptions(settings()); untrack(() => this.#version++); }); $effect(() => this.virtualizer._didMount()); // After the DOM updates: takes the scroll element, applies the scroll a // change of rows held back for it, places the rows drawn and measures // them, all of them at once. Measuring reads the layout, so the rows are // placed first. $effect(() => { void this.#version; this.virtualizer._willUpdate(); untrack(() => { const { list, onUpdate } = options(); if (list) { this.#place(list); for (const row of list.children) this.measure(row as HTMLElement); } onUpdate(); }); }); } get items(): VirtualItem[] { void this.#version; return this.virtualizer.getVirtualItems(); } /** Measures a drawn row, which carries `data-index`. */ measure = (row: HTMLElement) => { this.virtualizer.measureElement(row); // tanstack skips measuring a new row while the reader scrolls and waits for // its resize observer. a row dropped before that fires keeps its estimate, // which can flip the drawn range back and forth every frame. if (row.isConnected) this.virtualizer.resizeItem(Number(row.dataset.index), row.offsetHeight); }; /** * Makes room in `list` for the rows that are not drawn, around the rows it * holds: padding above the first and below the last, and a margin above a * row that follows a gap. Svelte adds and drops rows a flush before it * updates the ones it keeps, so room worked out in the markup can be a * whole chat off when the layout is read. On the list rather than its first * row, the room above is off only by the rows added or dropped since. */ #place(list: HTMLElement) { const { gap, scrollMargin } = this.virtualizer.options; const items = this.virtualizer.getVirtualItems(); const measured = this.virtualizer.measurementsCache; // Drawn items are shifted by a correction the virtualizer holds back. const held = items.length ? measured[items[0].index].start - items[0].start : 0; let above = 0; let end: number | null = null; for (const row of list.children as HTMLCollectionOf) { const item = measured[Number(row.dataset.index)]; if (!item) continue; const start = item.start - held; if (end === null) above = start - scrollMargin; row.style.marginTop = `${end === null ? 0 : start - end - gap}px`; end = item.end - held; } const below = end === null ? 0 : this.virtualizer.getTotalSize() - (end - scrollMargin); list.style.paddingTop = `${above}px`; list.style.paddingBottom = `${below}px`; } /** Keeps the virtualizer's scroll offset in step with a direct `scrollTop` write. */ syncScroll(offset: number) { this.#reportOffset?.(offset, this.virtualizer.isScrolling); } }