Composables

usePdfKitVirtualScroll

Virtual scrolling for large documents.

Provides virtual scrolling for efficient rendering of large documents.

Usage

const containerRef = ref<HTMLElement | null>(null)
const totalItems = ref(100)
const itemHeight = ref(800)

const {
  visibleItems,
  startIndex,
  endIndex,
  scrollTop,
  onScroll,
  totalHeight,
  offsetTop,
  scrollToItem,
  currentVisiblePage,
} = usePdfKitVirtualScroll({
  totalItems,
  itemHeight,
  containerRef,
  overscan: 2,
})

Options

OptionTypeDefaultDescription
totalItemsRef<number>requiredTotal number of items (pages)
itemHeightRef<number>requiredEstimated height of each item
containerRefRef<HTMLElement | null>requiredReference to the scrollable container
overscannumber2Extra items to render above/below the viewport

totalItems, itemHeight, and containerRef are required. They must be Vue refs, not plain values. :::

Return Values

PropertyTypeDescription
visibleItemsRef<number[]>Indices of items to render
startIndexRef<number>First visible item index
endIndexRef<number>Last visible item index
scrollTopRef<number>Current scroll position
onScroll() => voidContainer scroll handler
totalHeightRef<number>Total scrollable height
offsetTopRef<number>Offset for positioning visible items
scrollToItem(index: number) => voidScroll to a specific item
currentVisiblePageRef<number>Current visible page (1-indexed)

Example

<template>
  <div ref="containerRef" class="h-[600px] overflow-auto" @scroll="onScroll">
    <div :style="{ height: `${totalHeight}px`, position: 'relative' }">
      <div
        v-for="index in visibleItems"
        :key="index"
        :style="{
          position: 'absolute',
          top: `${index * itemHeight - offsetTop}px`,
          height: `${itemHeight}px`,
        }"
      >
        <!-- Render page {{ index + 1 }} -->
      </div>
    </div>
  </div>
</template>

<script setup>
const containerRef = ref(null)
const totalItems = ref(100)
const itemHeight = ref(800)

const {
  visibleItems,
  totalHeight,
  offsetTop,
  onScroll,
  scrollToItem,
} = usePdfKitVirtualScroll({
  totalItems,
  itemHeight,
  containerRef,
})

// Scroll to page 50
scrollToItem(49)
</script>
Copyright © 2026