Reference

VirtualGrid

Props, events, slots and exposed refs of the VirtualGrid component.

<VirtualGrid> renders the items of a list that are in view and asks pageProvider for the pages they belong to. Style it with CSS grid. Put it inside an element that scrolls.

Example grid

<template>
  <div class="h-[500px] overflow-y-auto">
    <VirtualGrid
      :length="1000"
      :page-size="20"
      :page-provider="pageProvider"
      class="grid grid-cols-[repeat(auto-fill,minmax(200px,1fr))] gap-4"
    >
      <template #default="{ item, style }">
        <div :style="style" class="h-[250px] rounded-lg border">{{ item.title }}</div>
      </template>

      <template #placeholder="{ style }">
        <div :style="style" class="h-[250px] rounded-lg bg-gray-100 animate-pulse" />
      </template>

      <template #probe>
        <div class="h-[250px]" />
      </template>
    </VirtualGrid>
  </div>
</template>
Do not set overflow or a fixed height on <VirtualGrid> itself. After computing, the grid sets its own height, overriding whatever you set, and the page scrolls instead of the grid.

Props

NameTypeDefaultDescription
lengthnumberrequiredTotal number of items in the list.
pageProviderPageProvider<T>requiredReturns the items of one page. See Utilities.
pageSizenumberrequiredItems per page passed to pageProvider.
pageProviderDebounceTimenumber0Milliseconds to wait before calling pageProvider. Pages the user scrolls past during that time are never requested. For a provider that calls a network API, set this first, because it decides how many pages the grid requests. See Performance.
scrollTonumberundefinedIndex of the item to scroll to. Must be less than length.
respectScrollToOnResizebooleantrueScrolls to scrollTo again after the grid is resized.
scrollBehavior'smooth' | 'auto''smooth'Passed to scrollTo() when scrollTo changes.
tagstring'div'Tag of the grid element.
probeTagstring'div'Tag of the hidden probe wrapper.
getItemId(item: T) => string | numberundefinedReturns a unique, stable identifier of an item, for example its database id. When a page is loaded again, the grid does not render items with the same identifier again. Without it, the grid renders every item that is a new object. Leave it out if an item's content can change while its identifier stays the same, or the grid keeps showing the old content.
getKey(item: InternalItem) => string | numberundefinedKey used by Vue to match rendered items between updates.

Events

The grid emits three events. None of them carries a payload. Inside a handler, read positions with the primitives.

render
The set of rendered items changed and the DOM was patched. Call an item tracker here.
viewport-scroll
A scroll parent of the grid scrolled, so every item moved on screen. It fires on every scroll event, so reuse one query object in the handler. See Performance.
layout-change
The grid measured itself again, because the item size, the gap, the tracks or the flow changed.

The grid is the one from Example grid.

<script setup lang="ts">
const onRender = () => console.log("rendered items changed");
const onViewportScroll = () => console.log("scrolled");
const onLayoutChange = () => console.log("measured again");
</script>

<template>
  <div class="h-96 overflow-y-auto">
    <VirtualGrid
      @render="onRender"
      @viewport-scroll="onViewportScroll"
      @layout-change="onLayoutChange"
    />
  </div>
</template>

Slots

default
{ item: T, index: number, style: StyleValue }
Renders a loaded item. Bind style to the root element. It holds the item's position.
placeholder
{ index: number, style: StyleValue }
Renders an item whose page has not arrived. Bind style the same way.
probe
One hidden element with the size of an item. The grid measures it to place every item.
Without a probe slot, set the row height on the grid with grid-template-rows or grid-auto-rows, for example 200px. The grid has no other way to learn the height of an item.
Set :data-index="index" on the root element of both item slots. With the attribute set, you can find an item's element or an event's item without a helper. See Doing it with the DOM.

Exposed values

Read these through a template ref on <VirtualGrid>. They are available on the first render.

The grid is the one from Example grid, with a template ref.

<script setup lang="ts">
import { useTemplateRef } from "vue";

const grid = useTemplateRef("grid");
</script>

<template>
  <div class="h-96 overflow-y-auto">
    <VirtualGrid ref="grid" />
  </div>
</template>
ready
Ref<boolean>
true after the first measurement.
measurement
Ref<ResizeMeasurement | undefined>
The flow, tracks, gaps and item size the grid places items with. Pass it as layout to the primitives. It is undefined until the grid is measured.
Copyright © 2026