mirror of
https://seed.radicle.dev/rad:z4D5UCArafTzTQpDZNQRuqswh3ury.git
synced 2026-11-21 10:40:41 +08:00
Restore list scroll position on back/forward
Navigating away from a list and back via history (the sidebar back/forward buttons) returns to the same scroll position with the previously loaded pages intact, instead of refetching only the first page at the top.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
<script lang="ts" generics="T">
|
||||
import type { Snippet } from "svelte";
|
||||
import type { VirtualizerHandle } from "virtua/svelte";
|
||||
|
||||
import { Virtualizer } from "virtua/svelte";
|
||||
|
||||
import type { ListCacheSnapshot } from "@app/lib/listState";
|
||||
|
||||
import { getScrollViewport } from "@app/components/ScrollArea.svelte";
|
||||
|
||||
interface Props {
|
||||
@@ -20,6 +23,14 @@
|
||||
startMargin?: number;
|
||||
// Distance from the bottom (in pixels) at which to request the next page.
|
||||
prefetchPx?: number;
|
||||
// Restore a previously-captured scroll position (used on history nav).
|
||||
initialCache?: ListCacheSnapshot;
|
||||
initialScrollOffset?: number;
|
||||
// Reports the current scroll position so callers can persist it.
|
||||
onState?: (state: {
|
||||
scrollOffset: number;
|
||||
cache: ListCacheSnapshot;
|
||||
}) => void;
|
||||
}
|
||||
|
||||
const {
|
||||
@@ -33,6 +44,9 @@
|
||||
bufferSize = undefined,
|
||||
startMargin = undefined,
|
||||
prefetchPx = 800,
|
||||
initialCache = undefined,
|
||||
initialScrollOffset = undefined,
|
||||
onState = undefined,
|
||||
}: Props = $props();
|
||||
|
||||
const getViewport = getScrollViewport();
|
||||
@@ -40,6 +54,27 @@
|
||||
// `initialized`, which is deferred).
|
||||
const viewport = $derived(getViewport());
|
||||
|
||||
let handle = $state<VirtualizerHandle | undefined>(undefined);
|
||||
let restored = false;
|
||||
|
||||
$effect(() => {
|
||||
// Restore the saved scroll offset once, after the virtualizer has mounted
|
||||
// with the restored size cache (so the offset maps to the right item).
|
||||
if (restored || !handle) return;
|
||||
restored = true;
|
||||
if (initialScrollOffset) {
|
||||
handle.scrollTo(initialScrollOffset);
|
||||
}
|
||||
});
|
||||
|
||||
function reportState() {
|
||||
if (!handle || !onState) return;
|
||||
onState({
|
||||
scrollOffset: handle.getScrollOffset(),
|
||||
cache: handle.getCache(),
|
||||
});
|
||||
}
|
||||
|
||||
function maybeLoadMore() {
|
||||
if (!onLoadMore || !hasMore || loadingMore) return;
|
||||
const el = viewport;
|
||||
@@ -62,13 +97,16 @@
|
||||
|
||||
{#if viewport}
|
||||
<Virtualizer
|
||||
bind:this={handle}
|
||||
data={items}
|
||||
scrollRef={viewport}
|
||||
{getKey}
|
||||
itemSize={estimatedItemSize}
|
||||
{bufferSize}
|
||||
{startMargin}
|
||||
onscroll={maybeLoadMore}>
|
||||
cache={initialCache}
|
||||
onscroll={maybeLoadMore}
|
||||
onscrollend={reportState}>
|
||||
{#snippet children(item: T, index: number)}
|
||||
{@render row(item, index)}
|
||||
{/snippet}
|
||||
|
||||
Reference in New Issue
Block a user