Skip to content

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.

src/registry/ui/virtual-list.tsx/r/virtual-list.json

Installation

bash
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

tsx
import { VirtualList } from "@/components/ui/virtual-list"

export function Example() {
  return (
    <VirtualList />
  )
}

API reference

VirtualList props
PropTypeDefault
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.number40
overscanExtra rows rendered above and below the viewport so fast scrolling never shows a gap.number6
heightViewport height in px.number360
renderRowRenders one row. Defaults to an invoice row.(item: T, index: number, state: { active: boolean }) => React.ReactNodedefaultRender
getKeyStable key per row. Defaults to item.id, then the index.(item: T, index: number) => React.KeydefaultKey
describeRowPlain text for the screen reader announcement of the active row.(item: T, index: number) => stringdefaultDescribe
activeIndexControlled active row.number-
defaultActiveIndexnumber0
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.booleantrue
showFooterShow the "Rows a to b of n" footer.booleantrue
refImperative handle with scrollToIndex and setActiveIndex.React.Ref<VirtualListHandle>-
aria-labelstring-
classNamestring-

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

Keyboard interactions
KeyAction
TabFocuses the list. Rows are not separate Tab stops.
ArrowDownorArrowUpMoves the active row by one and scrolls it into view.
PageDownorPageUpMoves the active row by about one screen.
HomeorEndMoves to the first or last row.
EnterorSpaceCalls onSelect with the active row.
EnterIn 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 by aria-label, or title when that is missing. Rows are listitems with aria-setsize and aria-posinset, so position is announced even though only a few rows exist in the DOM. The active row has aria-current="true".
  • A polite live region announces "Row 513 of 10,000" plus describeRow for the active row. Pass describeRow for 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

lucide-react

Built from

No other MiniDev files

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.

All data table components

Learn more