|
| 1 | +<script setup lang="ts"> |
| 2 | +import { connectInfiniteScroll } from '@scrollstackjs/virtual' |
| 3 | +/** |
| 4 | + * Both stores at once: the engine loads pages, the virtualizer renders a window |
| 5 | + * of them. `connectInfiniteScroll` is what replaces the sentinel — a virtual list |
| 6 | + * cannot render the element after the last row, so loading triggers on the |
| 7 | + * *index* the window has reached instead of on geometry. |
| 8 | + * |
| 9 | + * The only thing tying the two together is `count`. The engine never hears about |
| 10 | + * pixels; the virtualizer never hears about pages. |
| 11 | + */ |
| 12 | +import { useInfiniteScroll } from '@scrollstackjs/vue' |
| 13 | +import { useVirtualizer } from '@scrollstackjs/vue/virtual' |
| 14 | +import { computed, onScopeDispose } from 'vue' |
| 15 | +
|
| 16 | +import { fetchPokemon, POKEMON_LIMIT, pokemonSprite, type Pokemon } from './api' |
| 17 | +
|
| 18 | +const { |
| 19 | + state: feed, |
| 20 | + engine, |
| 21 | + reset, |
| 22 | +} = useInfiniteScroll<readonly Pokemon[], number>({ |
| 23 | + initialPageParam: 0, |
| 24 | + fetchPage: ({ pageParam, signal }) => fetchPokemon(pageParam, signal), |
| 25 | + getNextPageParam: (last, all) => |
| 26 | + last.length < POKEMON_LIMIT ? null : all.length * POKEMON_LIMIT, |
| 27 | +}) |
| 28 | +
|
| 29 | +const rows = computed(() => feed.value.pages.flat()) |
| 30 | +
|
| 31 | +const { state, scrollTarget, measure, virtualizer } = useVirtualizer({ |
| 32 | + count: () => rows.value.length, |
| 33 | + estimateSize: () => 56, |
| 34 | + overscan: 5, |
| 35 | +}) |
| 36 | +
|
| 37 | +// Loads the first page too — with no sentinel, nothing else would start it. |
| 38 | +onScopeDispose(connectInfiniteScroll(virtualizer, engine, { threshold: 4 })) |
| 39 | +</script> |
| 40 | + |
| 41 | +<template> |
| 42 | + <div class="ss-demo"> |
| 43 | + <div class="ss-bar"> |
| 44 | + <span class="ss-bar__title"> |
| 45 | + <span class="ss-dot" :class="{ 'ss-dot--warn': feed.isFetching }" /> |
| 46 | + pokeapi.co — offset pagination, virtualized |
| 47 | + </span> |
| 48 | + <div class="ss-bar__controls"> |
| 49 | + <button type="button" class="ss-btn" @click="reset()">Reset</button> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + |
| 53 | + <div :ref="scrollTarget" class="ss-box"> |
| 54 | + <div v-if="feed.isError" class="ss-state" role="alert"> |
| 55 | + <p>Couldn’t reach the API — {{ String(feed.error) }}</p> |
| 56 | + <button type="button" class="ss-btn" @click="engine.retry()">Try again</button> |
| 57 | + </div> |
| 58 | + |
| 59 | + <div v-else :style="{ height: `${state.totalSize}px`, position: 'relative' }"> |
| 60 | + <div |
| 61 | + v-for="item in state.items" |
| 62 | + :key="item.key" |
| 63 | + :ref="measure" |
| 64 | + :data-index="item.index" |
| 65 | + class="ss-row" |
| 66 | + :style="{ |
| 67 | + position: 'absolute', |
| 68 | + top: 0, |
| 69 | + left: 0, |
| 70 | + right: 0, |
| 71 | + transform: `translateY(${item.start}px)`, |
| 72 | + }" |
| 73 | + > |
| 74 | + <img |
| 75 | + class="ss-avatar ss-avatar--sprite" |
| 76 | + :src="pokemonSprite(rows[item.index]!.url)" |
| 77 | + :alt="rows[item.index]!.name" |
| 78 | + loading="lazy" |
| 79 | + /> |
| 80 | + <span class="ss-row__name ss-row__name--caps">{{ rows[item.index]!.name }}</span> |
| 81 | + <!-- `.ss-row__id` carries margin-left:auto, so it belongs last. --> |
| 82 | + <span class="ss-row__id">#{{ item.index + 1 }}</span> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + |
| 86 | + <p v-if="feed.isLoading" class="ss-sentinel">Loading the first page…</p> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div class="ss-counters"> |
| 90 | + <div class="ss-counter"> |
| 91 | + <span class="ss-counter__n">{{ state.items.length }}</span> |
| 92 | + <span class="ss-counter__l">rows in the DOM</span> |
| 93 | + </div> |
| 94 | + <div class="ss-counter"> |
| 95 | + <span class="ss-counter__n">{{ rows.length }}</span> |
| 96 | + <span class="ss-counter__l">rows loaded</span> |
| 97 | + </div> |
| 98 | + <div class="ss-counter"> |
| 99 | + <span class="ss-counter__n">{{ feed.pages.length }}</span> |
| 100 | + <span class="ss-counter__l">pages fetched</span> |
| 101 | + </div> |
| 102 | + <div class="ss-counter"> |
| 103 | + <span class="ss-counter__n">{{ |
| 104 | + feed.isFetchingNextPage ? 'fetching' : feed.fetchStatus |
| 105 | + }}</span> |
| 106 | + <span class="ss-counter__l">engine</span> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + |
| 110 | + <p class="ss-footer__note"> |
| 111 | + Scroll to within four rows of the end and the next page is requested — the virtualizer reports |
| 112 | + where the window is, the engine does the fetching. |
| 113 | + </p> |
| 114 | + </div> |
| 115 | +</template> |
0 commit comments