# Virtualization
Package: @mantine/data-grid
Import: import { Virtualization } from '@mantine/data-grid';
Description: DataGrid row virtualization for large datasets

## virtualize prop

The `virtualize` prop controls which axis is virtualized. The `height` prop is required
in all virtualization modes:

```tsx
<DataGrid store={store} virtualize="rows" height={500} />
<DataGrid store={store} virtualize="columns" height={500} />
<DataGrid store={store} virtualize="rows,columns" height={500} />
```

## Basic virtualization

Set `virtualize="rows"` to render only the rows visible in the viewport:

```tsx
import { useMemo } from 'react';
import { DataGrid } from '@mantine/data-grid';

interface VirtualRow {
  id: string;
  name: string;
  email: string;
  department: string;
  salary: number;
}

const departments = ['Engineering', 'Marketing', 'Sales', 'HR', 'Finance'];

function Demo() {
  const data = useMemo<VirtualRow[]>(
    () =>
      Array.from({ length: 100000 }, (_, i) => ({
        id: String(i + 1),
        name: `Employee ${i + 1}`,
        email: `employee${i + 1}@example.com`,
        department: departments[i % departments.length],
        salary: 50000 + Math.floor(Math.random() * 80000),
      })),
    []
  );

  return (
    <DataGrid
      data={data}
      virtualize="rows"
      height={500}
      columns={[
        { accessorKey: 'id', header: 'ID', size: 80 },
        { accessorKey: 'name', header: 'Name' },
        { accessorKey: 'email', header: 'Email', size: 220 },
        { accessorKey: 'department', header: 'Department' },
        { accessorKey: 'salary', header: 'Salary' },
      ]}
    />
  );
}
```


## Row height estimate

Set `estimateRowHeight` to estimate row height before virtualized rows are measured.
Return `'auto'` from `getRowHeight` to let rows grow to fit wrapped content:

```tsx
<DataGrid
  store={store}
  virtualize="rows"
  height={400}
  estimateRowHeight={60}
  getRowHeight={() => 'auto'}
  textOverflow="wrap"
/>
```

## Virtual scroll config

Fine-tune virtual scroll behavior with the `virtualScrollConfig` prop:

* `overscan` – number of rows rendered above and below the visible area, `10` by default
* `estimateSize` – function that returns the estimated row height in px

```tsx
<DataGrid
  data={data}
  virtualize="rows"
  height={500}
  virtualScrollConfig={{
    overscan: 10,
    estimateSize: () => 48,
  }}
  columns={columns}
/>
```

## Infinite scroll

Use the `onScrollToBottom` callback to load more data when the user scrolls near the bottom of the grid.
It works with or without virtualization. Combine it with `loading` and `loadingVariant="bar"` to show
progress without hiding the rows that are already loaded:

```tsx
import { useCallback, useMemo, useState } from 'react';
import { DataGrid } from '@mantine/data-grid';

interface Row {
  id: string;
  name: string;
  email: string;
  department: string;
}

const departments = ['Engineering', 'Marketing', 'Sales', 'HR', 'Finance'];

function generateRows(start: number, count: number): Row[] {
  return Array.from({ length: count }, (_, i) => {
    const index = start + i;
    return {
      id: String(index + 1),
      name: `Employee ${index + 1}`,
      email: `employee${index + 1}@example.com`,
      department: departments[index % departments.length],
    };
  });
}

function Demo() {
  const [data, setData] = useState(() => generateRows(0, 50));
  const [loading, setLoading] = useState(false);

  const handleScrollToBottom = useCallback(() => {
    if (loading) {
      return;
    }

    setLoading(true);
    setTimeout(() => {
      setData((prev) => [...prev, ...generateRows(prev.length, 50)]);
      setLoading(false);
    }, 500);
  }, [loading]);

  const columns = useMemo(
    () => [
      { accessorKey: 'id' as const, header: 'ID', size: 80 },
      { accessorKey: 'name' as const, header: 'Name' },
      { accessorKey: 'email' as const, header: 'Email', size: 220 },
      { accessorKey: 'department' as const, header: 'Department' },
    ],
    []
  );

  return (
    <DataGrid
      data={data}
      virtualize="rows"
      height={400}
      loading={loading}
      loadingVariant="bar"
      onScrollToBottom={handleScrollToBottom}
      columns={columns}
    />
  );
}
```


## Column virtualization

Set `virtualize="columns"` to render only the columns visible in the horizontal scroll area.
Use it for grids with 50 or more columns. Fine-tune behavior with the `virtualColumnConfig` prop:

```tsx
<DataGrid
  store={store}
  virtualize="columns"
  height={500}
  virtualColumnConfig={{
    overscan: 5,
    estimateSize: (index) => columns[index]?.size ?? 150,
  }}
/>
```

Set `virtualize="rows,columns"` for grids with many rows and many columns.

## Parity with the table body

The virtualized body renders `div` elements with ARIA grid roles instead of a `<table>`.
It looks and behaves like the table body, and its header is always sticky. See the table
below for the features that are not supported.

## Feature compatibility

## Performance tips

Virtualization keeps the number of rendered rows small, the rest of the cost is in your options
and cell renderers. See [Performance](https://alpha.mantine.dev/llms/data-grid-performance.md) for the rendering model, stable
options and large data sets. Adjust `overscan` to the row height: fewer rows for tall rows,
more for short rows.

## Accessibility

The virtualized grid sets `aria-rowcount`, `aria-rowindex` and `aria-colindex`. Screen readers
announce the row position (for example "row 500 of 100,000") even though only a part of the rows
is in the DOM. Keyboard navigation follows the same focus model as the table body, see
[Accessibility](https://alpha.mantine.dev/llms/data-grid-accessibility.md).
