PXD
v0.2.1-dev

useVirtualList

Manages virtualized rendering of large lists with scroll-to support.

Exports

function useVirtualList<Options extends VirtualListOptions>(
  containerRef: MaybeElementRef<HTMLElement>,
  options: Options,
): {
  virtualItems: ComputedRef<VirtualListItem[]>
  totalSize: ComputedRef<number>
  measureElement: (el: Element | ComponentPublicInstance | null) => void
  scrollToIndex: (index: number, opts?: ScrollToIndexOpts) => number
  scrollToOffset: (offset: number, opts?: ScrollToOffsetOpts) => number
  scrollBy: (delta: number, opts?: ScrollToOffsetOpts) => number
  getVirtualizer: () => Virtualizer
}

Types

interface VirtualListItem extends VirtualItem {
  key: string | number
}

interface VirtualListOptions {
  status?: 'loading' | 'finished' | 'error' | ''
  dataKey?: string
  items?: any[]
  itemSize?: number | ((index: number) => number)
  overScan?: number
  columnGap?: number
  columnCount?: number
  scrollPaddingStart?: number
  scrollPaddingEnd?: number
  onBottom?: () => void | Promise<void>
  bottomThreshold?: number
  onGeometryChange?: () => void
}

Params

NameTypeDescription
containerRefMaybeElementRef<HTMLElement>The scrollable container element
options.status'loading' | 'finished' | 'error' | ''Current list status
options.dataKeystringKey field name for item identification
options.itemsany[]The full list data array
options.itemSizenumber | ((index: number) => number)Height of an unmeasured row, or a per-index guess
options.overScannumberNumber of extra items to render outside viewport
options.columnGapnumberGap between columns in pixels
options.columnCountnumberNumber of columns for grid layout
options.scrollPaddingStartnumberPixels kept free at the start edge when scrolling an item into view
options.scrollPaddingEndnumberPixels kept free at the end edge when scrolling an item into view
options.onBottom() => void | Promise<void>Callback fired when scrolled to bottom
options.bottomThresholdnumberThreshold in pixels for triggering onBottom
options.onGeometryChange() => voidCalled once the content’s size is final for the frame, before it paints

onGeometryChange is the seam for anything that keeps the view pinned to the bottom: the list grows by rewriting its content height, which no observer can attribute to a node, and the resize delivery that follows lands after the paint it should have corrected. Subscribing also measures mounted rows before that paint, so the callback reads a final height.

Source

Source