---
title: VirtualizerController
description: 'Works out which rows of a long list are in view, so a component renders only those, with measured sizes and scroll to index.'
order: 20
---

# VirtualizerController

<p class="docs-lede">
	Works out which rows of a long list are in view, so a component renders only those, with measured sizes and scroll to
	index.
</p>

Reach for it when a listbox, a table, a log or a feed has thousands of rows and rendering all of them makes the page slow.

## Import

```ts
import { VirtualizerController, createVirtualizerController } from '@ecopages/radiant-ui/controllers';
```

The range math is in `@ecopages/radiant-ui/helpers` for a virtualizer of your own: `virtualOffsets`, `findVirtualRange` and `alignVirtualOffset`.

## Try it

The [Virtual list recipe](/docs/recipes/virtual-list) runs it over 10,000 lines of different heights, with go to line and find in page.

```ts
const virtualizer = new VirtualizerController({
	getScroller: () => scroller,
	getCount: () => lines.length,
	estimate: () => 32,
	onRange: ({ items, total }) => {
		spacer.style.height = `${total}px`;
		const rows = items.map(({ index, start }) => renderRow(index, start));
		list.replaceChildren(...rows);
		rows.forEach((row, position) => virtualizer.measure(items[position].index, row));
	},
});
virtualizer.attach();
```

## Config

| Option        | Description                                                                                                                                         |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `getScroller` | The scrolling element. Offsets count from the start of its scrollable area. Read on `attach()`.                                                     |
| `getCount`    | The number of items. Read on `attach()` and `refresh()`.                                                                                            |
| `estimate`    | `(index) => px`, the size of an item that has not been measured.                                                                                    |
| `overscan`    | Items rendered past each edge of the viewport. Default `4`.                                                                                         |
| `horizontal`  | Scroll along the inline axis instead of the block axis, right to left too. Read on `attach()`.                                                      |
| `onRange`     | Receives `{ start, end, items, total }` on `attach()` and `refresh()`, when the viewport resizes, and each time the range, an offset in it or `total` changes. `end` is exclusive.                       |
| `findable`    | `{ text, getLayer }`: a `hidden="until-found"` stand-in holding `text(index)` for each row that is not rendered, in the positioned `getLayer()`, read on `attach()` and `refresh()`.   |

Each item of `items` is `{ index, start, size }`: where the row starts along the axis and its size, in px. `total` is the size of all items end to end, the length to give the element the rows sit in.

## Methods

| Method                                      | Description                                                                                                                             |
| ------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `measure(index, element)` | Uses the element's size along the axis for the item. Applied in a microtask, then the range is reported with the new offsets. |
| `scrollToIndex(index, { align, behavior })` | Scrolls the item to the `start`, `center` or `end` of the viewport; `auto` (the default) only scrolls an item that is not fully in view. |
| `refresh()`                                 | Reads `getCount` again after rows are added or removed, and reports the range.                                                          |

## Behavior

- When an item that ends above the viewport changes size, the scroll position moves by the difference, so the rows in view stay put. An item the viewport starts inside grows below the part in view.
- A row with no layout box, such as one inside a `display: none` panel, keeps its estimate when measured.
- When the viewport changes size, the range is reported again even when it is the same, so the component renders and measures the rows again, since they may wrap differently. The controller does not observe the rows themselves.
- For two frames after `scrollToIndex`, measurements put the item back where `align` says, so an estimate that was off does not leave it misplaced. A smooth scroll is not corrected.
- Measured sizes are kept by index. After an insert or a reorder, measure the rendered rows again.
- With `findable`, find in page matches the text of a stand-in and fires `beforematch` on it. The controller moves the stand-in to its row's offset, scrolls the row to the center, and hides the stand-in again on the next frame, once the row is rendered. Keep the stand-ins displayed: a rule such as `[hidden] { display: none }` that reaches them hides them from find in page. A stand-in is one element per item, so it suits thousands of rows, not millions.

## Lifecycle

`attach()` listens for scrolls, watches the viewport size, adds the stand-ins and reports the first range; attaching again with the same scroller changes nothing. `destroy()` stops all of it and removes the stand-ins. `measure`, `scrollToIndex` and `refresh` do nothing before `attach()` or after `destroy()`.

With a host, `createVirtualizerController(host, config)` does the same on connect and disconnect; see [bindController](/docs/controllers/bind-controller).

## Accessibility: what the controller does and what you must provide

It renders no rows and writes no ARIA. The rows in the page are not the whole list, so give each `aria-setsize` and `aria-posinset` (a list or listbox), or `aria-rowcount` on the table and `aria-rowindex` on each row (a table or grid). Give the scroller `tabindex="0"` and a name when nothing inside it is focusable, so the keyboard can scroll it. Keyboard navigation across rows that are not rendered is yours: move the active index, call `scrollToIndex` and focus the row once it is rendered. With `findable`, put the stand-in layer inside an `aria-hidden` element: a stand-in the browser reveals shows until the next frame, and a screen reader should not read it beside its row.

## Protected hooks

None. Change behavior through the config callbacks.
