mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 11:50:42 +08:00
Add VirtualList component backed by virtua
A reusable windowing wrapper that mounts virtua's Virtualizer against the existing OverlayScrollbars viewport, so the app-wide custom scrollbar is preserved.
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { getScrollViewport } from "@app/components/ScrollArea.svelte";
|
||||
|
||||
interface Props {
|
||||
onIntersect: () => void | Promise<void>;
|
||||
disabled?: boolean;
|
||||
rootMargin?: string;
|
||||
}
|
||||
|
||||
const {
|
||||
onIntersect,
|
||||
disabled = false,
|
||||
rootMargin = "500%",
|
||||
}: Props = $props();
|
||||
|
||||
const getViewport = getScrollViewport();
|
||||
let sentinel: HTMLElement | undefined = $state();
|
||||
let firing = false;
|
||||
|
||||
$effect(() => {
|
||||
if (disabled) return;
|
||||
const viewport = getViewport();
|
||||
if (!viewport || !sentinel) return;
|
||||
if (!("IntersectionObserver" in window)) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
entries => {
|
||||
for (const entry of entries) {
|
||||
if (!entry.isIntersecting || firing) continue;
|
||||
firing = true;
|
||||
void Promise.resolve(onIntersect()).finally(() => {
|
||||
firing = false;
|
||||
});
|
||||
}
|
||||
},
|
||||
{ root: viewport, rootMargin },
|
||||
);
|
||||
|
||||
observer.observe(sentinel);
|
||||
return () => observer.disconnect();
|
||||
});
|
||||
</script>
|
||||
|
||||
<div bind:this={sentinel} aria-hidden="true" style="height: 1px;"></div>
|
||||
@@ -0,0 +1,76 @@
|
||||
<script lang="ts" generics="T">
|
||||
import type { Snippet } from "svelte";
|
||||
|
||||
import { Virtualizer } from "virtua/svelte";
|
||||
|
||||
import { getScrollViewport } from "@app/components/ScrollArea.svelte";
|
||||
|
||||
interface Props {
|
||||
items: readonly T[];
|
||||
row: Snippet<[T, number]>;
|
||||
hasMore?: boolean;
|
||||
loadingMore?: boolean;
|
||||
onLoadMore?: () => void;
|
||||
getKey?: (item: T, index: number) => string | number;
|
||||
// Estimated item size in pixels; reduces scrollbar jump before measurement.
|
||||
estimatedItemSize?: number;
|
||||
// Extra pixels rendered before/after the viewport.
|
||||
bufferSize?: number;
|
||||
// Height of any content rendered before this list inside the same scroller.
|
||||
startMargin?: number;
|
||||
// Distance from the bottom (in pixels) at which to request the next page.
|
||||
prefetchPx?: number;
|
||||
}
|
||||
|
||||
const {
|
||||
items,
|
||||
row,
|
||||
hasMore = false,
|
||||
loadingMore = false,
|
||||
onLoadMore = undefined,
|
||||
getKey = undefined,
|
||||
estimatedItemSize = undefined,
|
||||
bufferSize = undefined,
|
||||
startMargin = undefined,
|
||||
prefetchPx = 800,
|
||||
}: Props = $props();
|
||||
|
||||
const getViewport = getScrollViewport();
|
||||
// Resolves once ScrollArea publishes its viewport (set in OverlayScrollbars'
|
||||
// `initialized`, which is deferred).
|
||||
const viewport = $derived(getViewport());
|
||||
|
||||
function maybeLoadMore() {
|
||||
if (!onLoadMore || !hasMore || loadingMore) return;
|
||||
const el = viewport;
|
||||
if (!el) return;
|
||||
if (el.scrollTop + el.clientHeight >= el.scrollHeight - prefetchPx) {
|
||||
onLoadMore();
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
// After the dataset grows (or the viewport first appears), the list may not
|
||||
// fill the viewport yet, so no scroll event would fire. Re-check on the next
|
||||
// frame, once Virtualizer has sized the scroll container.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
(items.length, hasMore, loadingMore, viewport);
|
||||
const id = requestAnimationFrame(() => maybeLoadMore());
|
||||
return () => cancelAnimationFrame(id);
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if viewport}
|
||||
<Virtualizer
|
||||
data={items}
|
||||
scrollRef={viewport}
|
||||
{getKey}
|
||||
itemSize={estimatedItemSize}
|
||||
{bufferSize}
|
||||
{startMargin}
|
||||
onscroll={maybeLoadMore}>
|
||||
{#snippet children(item: T, index: number)}
|
||||
{@render row(item, index)}
|
||||
{/snippet}
|
||||
</Virtualizer>
|
||||
{/if}
|
||||
Reference in New Issue
Block a user