---
title: Virtual range
description: 'The offsets, visible range and scroll-to position of a virtualized list, from item sizes.'
order: 12
---

import { HelperResults } from '@/components/primitive-example/helper-results';
import { alignVirtualOffset, findVirtualRange, virtualOffsets } from '@ecopages/radiant-ui/helpers';

export const offsets = virtualOffsets(Array.from({ length: 100 }, () => 20));

# Virtual range

<p class="docs-lede">The offsets, visible range and scroll-to position of a virtualized list, from item sizes.</p>

Reach for it to build a virtualizer of your own, or to work out a range on the server; [VirtualizerController](/docs/controllers/virtualizer) is built on it. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.

## Import

```ts
import { alignVirtualOffset, findVirtualRange, virtualOffsets } from '@ecopages/radiant-ui/helpers';
```

## Offsets

`virtualOffsets(sizes)` lays the items end to end and returns `count + 1` offsets: item `i` starts at `offsets[i]`, and the last offset is the total.

<HelperResults
	rows={[
		{ code: 'virtualOffsets([10, 20, 30])', result: virtualOffsets([10, 20, 30]) },
		{ code: 'virtualOffsets([])', result: virtualOffsets([]) },
	]}
/>

## Range

`findVirtualRange(offsets, view, overscan)` returns the first index to render and one past the last: the items that overlap the view, plus `overscan` on each side, clamped to the list. `view` is `{ start, size }`, the scroll offset and the viewport size. It searches the offsets by halves, so a long list costs a few steps. With 100 items of 20px:

<HelperResults
	rows={[
		{
			code: 'findVirtualRange(offsets, { start: 200, size: 100 })',
			result: findVirtualRange(offsets, { start: 200, size: 100 }),
		},
		{
			code: 'findVirtualRange(offsets, { start: 210, size: 100 }, 2)',
			result: findVirtualRange(offsets, { start: 210, size: 100 }, 2),
		},
		{
			code: 'findVirtualRange(offsets, { start: 1900, size: 100 }, 2)',
			result: findVirtualRange(offsets, { start: 1900, size: 100 }, 2),
		},
	]}
/>

## Align

`alignVirtualOffset(item, align, view, total)` returns the scroll offset that puts an item at the `start`, `center` or `end` of the view, clamped to the scrollable range. `auto` leaves an item that is fully in view where it is, and otherwise takes the nearer edge. For a 20px item at 1000px in a 100px view over 2000px:

<HelperResults
	rows={[
		{
			code: "alignVirtualOffset({ start: 1000, size: 20 }, 'center', { start: 0, size: 100 }, 2000)",
			result: alignVirtualOffset({ start: 1000, size: 20 }, 'center', { start: 0, size: 100 }, 2000),
		},
		{
			code: "alignVirtualOffset({ start: 1000, size: 20 }, 'end', { start: 0, size: 100 }, 2000)",
			result: alignVirtualOffset({ start: 1000, size: 20 }, 'end', { start: 0, size: 100 }, 2000),
		},
		{
			code: "alignVirtualOffset({ start: 1000, size: 20 }, 'auto', { start: 950, size: 100 }, 2000)",
			result: alignVirtualOffset({ start: 1000, size: 20 }, 'auto', { start: 950, size: 100 }, 2000),
		},
		{
			code: "alignVirtualOffset({ start: 1990, size: 20 }, 'start', { start: 0, size: 100 }, 2000)",
			result: alignVirtualOffset({ start: 1990, size: 20 }, 'start', { start: 0, size: 100 }, 2000),
		},
	]}
/>
