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` and `size` * change 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 and measures the rows drawn, all of // them at once. $effect(() => { void this.#version; this.virtualizer._willUpdate(); untrack(() => { const { list, onUpdate } = options(); for (const row of list?.children ?? []) this.measure(row as HTMLElement); onUpdate(); }); }); } get items(): VirtualItem[] { void this.#version; return this.virtualizer.getVirtualItems(); } get size(): number { void this.#version; return this.virtualizer.getTotalSize(); } /** 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); }; /** Keeps the virtualizer's scroll offset in step with a direct `scrollTop` write. */ syncScroll(offset: number) { this.#reportOffset?.(offset, this.virtualizer.isScrolling); } }