Virtual List
React virtual list that renders only rows in view, with fixed or measured row heights, overscan, arrow key and Page Up/Down navigation and scrollToIndex.
Installation
npx shadcn@latest add @minidev/virtual-list
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/virtual-list.json
Usage
import { VirtualList } from "@/components/ui/virtual-list"
export function Example() {
return (
<VirtualList />
)
}
API reference
| Prop | Type | Default |
|---|---|---|
| itemsRows to render. Defaults to 10,000 Lumen invoices. | T[] | SAMPLE_INVOICES as unknown as T[] |
| rowHeightRow height in px, or "auto" to measure every row (estimatedRowHeight is used until a row has been measured). | number | "auto" | 40 |
| estimatedRowHeightHeight assumed for rows that have not been measured yet, in "auto" mode. | number | 40 |
| overscanExtra rows rendered above and below the viewport so fast scrolling never shows a gap. | number | 6 |
| heightViewport height in px. | number | 360 |
| renderRowRenders one row. Defaults to an invoice row. | (item: T, index: number, state: { active: boolean }) => React.ReactNode | defaultRender |
| getKeyStable key per row. Defaults to item.id, then the index. | (item: T, index: number) => React.Key | defaultKey |
| describeRowPlain text for the screen reader announcement of the active row. | (item: T, index: number) => string | defaultDescribe |
| activeIndexControlled active row. | number | - |
| defaultActiveIndex | number | 0 |
| onActiveChange | (index: number) => void | - |
| onSelectCalled on click, Enter or Space on a row. | (item: T, index: number) => void | - |
| titleHeader title. Pass an empty string to hide the header. | string | "Invoices" |
| showJumpShow the "Jump to row" field in the header. | boolean | true |
| showFooterShow the "Rows a to b of n" footer. | boolean | true |
| refImperative handle with scrollToIndex and setActiveIndex. | React.Ref<VirtualListHandle> | - |
| aria-label | string | - |
| className | string | - |
When to use
Use it for thousands of rows (logs, invoices, search results). Generic over the row type: pass items, renderRow and getKey. Set rowHeight="auto" for rows of varying height. A ref exposes scrollToIndex and setActiveIndex. Use VirtualizedList for a bare fixed-height list without header or keyboard model, DataTable for sortable columns under ~1,000 rows, and InfiniteScroll to load pages as the reader scrolls.
Keyboard interactions
| Key | Action |
|---|---|
| Tab | Focuses the list. Rows are not separate Tab stops. |
| ArrowDownorArrowUp | Moves the active row by one and scrolls it into view. |
| PageDownorPageUp | Moves the active row by about one screen. |
| HomeorEnd | Moves to the first or last row. |
| EnterorSpace | Calls onSelect with the active row. |
| Enter | In the Jump to row field, makes that row active, scrolls it to the top and moves focus to the list. |
Accessibility
- The scroller has
role="list"and is named byaria-label, ortitlewhen that is missing. Rows arelistitems witharia-setsizeandaria-posinset, so position is announced even though only a few rows exist in the DOM. The active row hasaria-current="true". - A polite live region announces "Row 513 of 10,000" plus
describeRowfor the active row. PassdescribeRowfor your own data, or it says only "Row n". - The list is described by a hidden hint that explains the arrow, Page Up/Down, Home and End keys.
- The jump field has a hidden label, "Jump to row", and its submit button is labelled "Go to row".
Frequently asked questions
How do I render rows of different heights?
Set rowHeight="auto". One ResizeObserver measures every mounted row, and estimatedRowHeight (40 by default) is used until a row is measured. A fixed number is cheaper, so use it when all rows are the same height.
How do I scroll to a row, for example from a deep link?
Pass a ref and call ref.current?.scrollToIndex(index, "center"), or setActiveIndex(index) to highlight it too. With measured rows it re-scrolls up to four times as rows above the target get measured.
How do I open a row with Enter or a click?
onSelect(item, index) fires on click, and on Enter or Space for the active row. onActiveChange(index) fires as the arrow keys move the active row.
Can I hide the header and footer?
Yes. title="" with showJump={false} removes the header, and showFooter={false} removes the "Rows a to b of n" line. Give the list an aria-label then, since the title also names it.
Dependencies
npm
Built from
Examples
Custom rows
Pass your own items and renderRow; describeRow gives screen readers a plain text version of the active row.
Scroll to index
The ref exposes scrollToIndex with an alignment and setActiveIndex, which also moves the keyboard cursor.
Measured row heights
rowHeight="auto" measures every row as it mounts, so rows with wrapping text keep the scroll position honest.
Select handler
onSelect fires on click, Enter or Space; an empty title plus showJump and showFooter off leaves only the rows.