Domphy

API Reference

createVirtualizer(options)

import { createVirtualizer } from "@domphy/virtual/domphy"

Returns a reactive virtualizer handle. Call inside a component body (not top-level module).

const list = createVirtualizer<HTMLDivElement, HTMLDivElement>(options)

Handle methods

MethodReturnsDescription
getVirtualItems(l)VirtualItem[]Reactive list of items currently in range. Pass a listener to subscribe.
getTotalSize(l?)numberTotal scroll size in px. Pass listener to subscribe reactively.
setScrollElement(el)voidWire the scroll container. Call from _onMount.
measureElement(el)voidMeasure an item element. Call from item _onMount for variable heights.
scrollToIndex(i, opts?)voidScroll to item at index.
scrollToOffset(px, opts?)voidScroll to an absolute offset.
setOptions(opts)voidUpdate count, estimateSize, or other options reactively.
version(l)numberRaw change counter — subscribe to know when any item changes.
destroy()voidDetach resize observers. Call from _onRemove.

The virtualizer property exposes the underlying Virtualizer instance with additional methods:

MethodDescription
scrollBy(delta, opts?)Scroll by a relative amount.
scrollToEnd(opts?)Scroll to the end of the list.
takeSnapshot()Capture current offsets for later restore.
getDistanceFromEnd()Pixels from current scroll position to the end.
isAtEnd(threshold?)true if within threshold (default 0) px of the end.

VirtualizerOptions

interface VirtualizerOptions<TScrollElement, TItemElement> {
  // Required
  count: number                          // total item count
  estimateSize: (index: number) => number  // item height/width estimate in px

  // Scroll element
  getScrollElement?: () => TScrollElement | null
  observeElementRect?: (instance, callback) => () => void
  observeElementOffset?: (instance, callback) => () => void
  scrollToFn?: (offset, options, instance) => void

  // Layout
  horizontal?: boolean                   // default false (vertical)
  paddingStart?: number                  // px before first item
  paddingEnd?: number                    // px after last item
  gap?: number                           // px gap between items

  // Range control
  overscan?: number                      // extra items beyond visible (default 1)
  rangeExtractor?: (range: Range) => number[]
  initialRect?: { width: number; height: number }
  initialOffset?: number | (() => number)

  // Key extraction
  getItemKey?: (index: number) => Key

  // Dynamic measurement
  measureElement?: (el: TItemElement, entry: ResizeObserverEntry | undefined, instance: Virtualizer<TScrollElement, TItemElement>) => number
  scrollMargin?: number

  // Scroll anchoring
  followOnAppend?: "ancestor" | "anchor"
  scrollAnchor?: "start" | "end"
  isRtl?: boolean

  // Lanes (masonry)
  lanes?: number
  laneAssignmentMode?: "sequential" | "shortest"

  // Performance
  useCachedMeasurements?: boolean

  // Callbacks
  onChange?: (instance: Virtualizer<TScrollElement, TItemElement>, sync: boolean) => void
  debug?: boolean
}

VirtualItem

interface VirtualItem {
  key: Key                 // stable key (getItemKey result or index)
  index: number            // 0-based position in data
  start: number            // px offset from the start of the list (absolute position)
  end: number              // start + size
  size: number             // measured or estimated height/width
  lane: number             // for multi-lane masonry layouts (default: 0)
}

Use start to position: absolute + translateY (vertical) or translateX / left (horizontal).


Range

interface Range {
  startIndex: number      // first visible item index
  endIndex: number        // last visible item index
  overscan: number        // configured overscan value
  count: number           // total item count
}

Passed to rangeExtractor — return an array of indexes to render.


ScrollToOptions

interface ScrollToOptions {
  align?: "start" | "center" | "end" | "auto"   // default "auto"
  behavior?: "auto" | "smooth"                  // default "auto"
}

"auto" alignment moves the viewport the minimum distance needed to bring the item into view.


Virtualizer class (low-level)

Import directly when not using the Domphy adapter:

import { Virtualizer } from "@domphy/virtual"
const virtualizer = new Virtualizer(options)
virtualizer._didMount()

// In your render loop
const items = virtualizer.getVirtualItems()
const totalSize = virtualizer.getTotalSize()

// Cleanup
virtualizer.cleanup()

Additional methods on Virtualizer

MethodDescription
getMeasurements()All measurements (index → Rect).
calculateRange()Recalculate the current visible range.
getVirtualIndexes()Just the indexes (lighter than full VirtualItems).
getVirtualItemForOffset(px)Find the item at a given scroll offset.
getSize()Container viewport size.
getScrollOffset()Current scroll offset.
getMaxScrollOffset()Maximum scrollable offset.
resizeItem(index, size)Programmatically override an item's measured size.
scrollToIndex(i, opts?)Scroll to item.
scrollToOffset(px, opts?)Scroll to absolute offset.
scrollBy(delta, opts?)Relative scroll.
scrollToEnd(opts?)Scroll to the last item.
getDistanceFromEnd()Distance from current position to end.
isAtEnd(threshold?)True if at/near the end.
getOffsetForIndex(i, align?)Compute target scroll offset for an item.
getOffsetForAlignment(align, size)Offset for a given alignment + size.
takeSnapshot()Capture current item offsets.
measure()Force re-measurement of all items.
indexFromElement(el)Get item index from a DOM element.

Low-level helpers

import {
  observeElementRect,
  observeWindowRect,
  observeElementOffset,
  observeWindowOffset,
  measureElement,
  elementScroll,
  windowScroll,
  defaultRangeExtractor,
  defaultKeyExtractor,
} from "@domphy/virtual"
HelperDescription
observeElementRectResizeObserver-based rect tracker for a DOM element.
observeWindowRectTracks window.innerWidth/Height.
observeElementOffsetScroll-event-based offset tracker for a DOM element.
observeWindowOffsetTracks window.scrollX/Y.
measureElementDefault measurement strategy (uses offsetHeight/offsetWidth).
elementScrollDefault scroll function for a DOM element.
windowScrollScroll function for the browser window.
defaultRangeExtractorStandard range calculation. Pass to rangeExtractor as a base.
defaultKeyExtractorReturns item index as the key.