PXD
v0.2.1-dev

Virtual List

Display unlimited data lists and maintain page performance

Fixed height items

<script setup>
const items = Array.from({ length: 100 }, (_, i) => ({
  id: i,
  name: `Item ${i}`,
}))
</script>

<template>
  <PVirtualList
    class="!h-32 w-100 max-w-full border border-dashed rounded-lg"
    :items="items"
    data-key="id"
    :item-size="40"
  >
    <template #item="{ item }">
      <div class="px-4 h-10 py-2">
        {{ item.name }}
      </div>
    </template>
  </PVirtualList>
</template>

Dynamic height items

Set an approximate height for each item.

<script setup>
const items = Array.from({ length: 100 }, (_, i) => ({
  key: i,
  name: `Item ${i}`,
  height: Math.random() * 100 + 40,
}))
</script>

<template>
  <PVirtualList
    class="!h-32 w-100 max-w-full border border-dashed rounded-lg"
    :items="items"
    data-key="key"
    :item-size="40"
  >
    <template #item="{ item }">
      <div class="px-4 py-2" :style="{ height: `${item.height}px` }">
        {{ item.name }}
      </div>
    </template>
  </PVirtualList>
</template>

Masonry layout

Set column-count for column count and column-gap for spacing.

<script setup>
const items = Array.from({ length: 50 }, (_, i) => ({
  id: i,
  title: `Card ${i}`,
  height: Math.floor(Math.random() * 120) + 72,
}))
</script>

<template>
  <PVirtualList
    class="!h-64 w-100 max-w-full rounded-lg"
    :items="items"
    :column-count="3"
    :column-gap="12"
    :item-size="80"
  >
    <template #item="{ item }">
      <div
        class="rounded-lg border bg-gray-100 px-3 py-2 text-sm"
        :style="{ height: `${item.height}px` }"
      >
        {{ item.title }}
      </div>
    </template>
  </PVirtualList>
</template>

Infinite scroll

Use the status parameter together with the on-bottom callback and the retry event to achieve infinite scrolling of data loading

<script setup>
import { ref, onMounted } from 'vue'

const items = ref([])
const listStatus = ref() // 'loading' | 'finished' | 'error'

async function onBottom() {
  const startIdx = items.value.length

  if (startIdx >= 100) {
    listStatus.value = 'finished'
    return
  }

  listStatus.value = 'loading'

  // mock async request
  await new Promise((resolve, reject) => {
    setTimeout(() => {
      if (Math.random() < 0.25) {
        listStatus.value = 'error'
      }

      resolve()
    }, 500)
  })

  if (listStatus.value === 'error') {
    return
  }

  items.value.push(
    ...Array.from({ length: 10 }, (_, i) => ({
      id: i,
      name: `Item ${startIdx + i}`,
    }))
  )

  listStatus.value = ''
}

function onRetry() {
  listStatus.value = ''
  onBottom()
}

onMounted(() => {
  onBottom()
})
</script>

<template>
  <PVirtualList
    class="!h-32 w-100 max-w-full border border-dashed rounded-lg"
    data-key="id"
    :item-size="40"
    :status="listStatus"
    :items="items"
    @retry="onRetry"
    @bottom="onBottom"
  >
    <template #item="{ item }">
      <div class="px-4 h-10 py-2">
        {{ item.name }}
      </div>
    </template>
  </PVirtualList>
</template>

Props

NameTypeDefaultDescription
itemsreadonly any[]() => []Rows to render; each row is passed to the item slot
data-keystring'id'Field of an item used as its stable key
item-sizenumber | ((index: number) => number)50Row height in px, or a function returning it per index
status'loading' | 'finished' | 'error' | ''''Renders a message row under the list and, for error, makes it clickable
enabledbooleantrueWhen false, nothing is rendered and totalSize stays 0
over-scannumber2Extra rows rendered outside the viewport
column-gapnumber0Horizontal gap between masonry columns, in px
column-countnumber1Masonry column count; 1 renders a plain vertical list
scroll-padding-startnumber-Space before the first row, in px
scroll-padding-endnumber-Space after the last row, in px
on-bottom() => void | Promise<void>-Called when the last row enters the bottom threshold; the way to load more
bottom-thresholdnumberitem-sizeDistance from the bottom that triggers on-bottom, in px
on-geometry-change() => void-Called once a frame after the container geometry settles, for hosts that size the list
list-classComponentClass-Extra class on the inner content wrapper
item-classComponentClass-Extra class on every item wrapper
error-textComponentText'Request failed. Click to reload'Message shown while status is error
loading-textComponentText'Loading...'Message shown while status is loading
finished-textComponentText'Finished'Message shown while status is finished

Events

NameTypeDescription
retry() => voidEmitted when the message row is clicked while status is error.

Reaching the bottom is reported through the on-bottom prop, not an event.

Slots

NameDescription
itemitem slot
messagemessage slot

Methods

NameTypeDescription
totalSizenumberTotal scrollable size of the list in pixels, 0 while the list is disabled.
virtualItemsVirtualListItem[]Items the virtualizer currently renders, empty while the list is disabled.
getVirtualizer() => Virtualizer<HTMLElement, HTMLElement>Get the underlying TanStack virtualizer instance.
scrollSourceVirtualListScrollSourcePosition and scroll source for PToc’s data-driven outline, tracking this list’s cached offsets.
scrollToIndex(index: number, options?: ScrollToIndexOptions) => voidScroll to the item at the given index.
scrollToOffset(toOffset: number, options?: ScrollToOffsetOptions) => voidScroll to an absolute pixel offset.
scrollBy(delta: number, options?: ScrollToOffsetOptions) => voidScroll by a pixel delta.

Source

Source