---
title: "VirtualGrid"
description: "Props, events, slots and exposed refs of the VirtualGrid component."
canonical_url: "https://vvirtual-grid.netlify.app/reference/virtual-grid"
---
# 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

<code-collapse>

```vue
<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>
```

</code-collapse>

<warning>

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.

</warning>

## Props

<table>
<thead>
  <tr>
    <th align="left">
      Name
    </th>
    
    <th align="left">
      Type
    </th>
    
    <th align="left">
      Default
    </th>
    
    <th align="left">
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td align="left">
      <code>
        length
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          number
        </span>
      </code>
    </td>
    
    <td align="left">
      required
    </td>
    
    <td align="left">
      Total number of items in the list.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        pageProvider
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          PageProvider
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          T
        </span>
        
        <span class="sMK4o">
          >
        </span>
      </code>
    </td>
    
    <td align="left">
      required
    </td>
    
    <td align="left">
      Returns the items of one page. See <a href="https://vvirtual-grid.netlify.app/reference/utilities#write-your-own-pageprovider">
        Utilities
      </a>
      
      .
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        pageSize
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          number
        </span>
      </code>
    </td>
    
    <td align="left">
      required
    </td>
    
    <td align="left">
      Items per page passed to <code>
        pageProvider
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        pageProviderDebounceTime
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          number
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sbssI">
          0
        </span>
      </code>
    </td>
    
    <td align="left">
      Milliseconds to wait before calling <code>
        pageProvider
      </code>
      
      . 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 <a href="https://vvirtual-grid.netlify.app/guides/performance">
        Performance
      </a>
      
      .
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        scrollTo
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          number
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          undefined
        </span>
      </code>
    </td>
    
    <td align="left">
      Index of the item to scroll to. Must be less than <code>
        length
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        respectScrollToOnResize
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          boolean
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          true
        </span>
      </code>
    </td>
    
    <td align="left">
      Scrolls to <code>
        scrollTo
      </code>
      
       again after the grid is resized.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        scrollBehavior
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          '
        </span>
        
        <span class="sfazB">
          smooth
        </span>
        
        <span class="sMK4o">
          '
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sMK4o">
          '
        </span>
        
        <span class="sfazB">
          auto
        </span>
        
        <span class="sMK4o">
          '
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          '
        </span>
        
        <span class="sfazB">
          smooth
        </span>
        
        <span class="sMK4o">
          '
        </span>
      </code>
    </td>
    
    <td align="left">
      Passed to <code>
        scrollTo()
      </code>
      
       when <code>
        scrollTo
      </code>
      
       changes.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        tag
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          string
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          '
        </span>
        
        <span class="sfazB">
          div
        </span>
        
        <span class="sMK4o">
          '
        </span>
      </code>
    </td>
    
    <td align="left">
      Tag of the grid element.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        probeTag
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          string
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          '
        </span>
        
        <span class="sfazB">
          div
        </span>
        
        <span class="sMK4o">
          '
        </span>
      </code>
    </td>
    
    <td align="left">
      Tag of the hidden probe wrapper.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        getItemId
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          (
        </span>
        
        <span class="sHdIc">
          item
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          T
        </span>
        
        <span class="sMK4o">
          )
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          number
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          undefined
        </span>
      </code>
    </td>
    
    <td align="left">
      Returns 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.
    </td>
  </tr>
  
  <tr>
    <td align="left">
      <code>
        getKey
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          (
        </span>
        
        <span class="sHdIc">
          item
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          InternalItem
        </span>
        
        <span class="sMK4o">
          )
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          number
        </span>
      </code>
    </td>
    
    <td align="left">
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          undefined
        </span>
      </code>
    </td>
    
    <td align="left">
      Key used by Vue to match rendered items between updates.
    </td>
  </tr>
</tbody>
</table>

## Events

The grid emits three events. None of them carries a payload.
Inside a handler, read positions with the [primitives](https://vvirtual-grid.netlify.app/reference/primitives).

<field-group>
<field name="render">

The set of rendered items changed and the DOM was patched. Call an [item tracker](https://vvirtual-grid.netlify.app/reference/primitives#track-rendered-items) here.

</field>

<field name="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](https://vvirtual-grid.netlify.app/guides/performance#reuse-query-objects-in-viewport-scroll-handlers).

</field>

<field name="layout-change">

The grid measured itself again, because the item size, the gap, the tracks or the flow changed.

</field>
</field-group>

The grid is the one from [Example grid](#example-grid).

```vue
<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

<field-group>
<field name="default" type="{ item: T, index: number, style: StyleValue }">

Renders a loaded item. Bind `style` to the root element. It holds the item's position.

</field>

<field name="placeholder" type="{ index: number, style: StyleValue }">

Renders an item whose page has not arrived. Bind `style` the same way.

</field>

<field name="probe">

One hidden element with the size of an item. The grid measures it to place every item.

</field>
</field-group>

<warning>

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.

</warning>

<tip>

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](https://vvirtual-grid.netlify.app/reference/primitives#doing-it-with-the-dom).

</tip>

## 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](#example-grid), with a template ref.

```vue
<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>
```

<field-group>
<field name="ready" type="Ref<boolean>">

`true` after the first measurement.

</field>

<field name="measurement" type="Ref<ResizeMeasurement | undefined>">

The flow, tracks, gaps and item size the grid places items with. Pass it as `layout` to the [primitives](https://vvirtual-grid.netlify.app/reference/primitives). It is `undefined` until the grid is measured.

</field>
</field-group>


## Sitemap

See the full [sitemap](https://vvirtual-grid.netlify.app/sitemap.md) for all pages.
