# 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.

- Kind: Component (Tables)
- Docs: https://ui.minidev.pro/docs/virtual-list
- Source (registry JSON with the full file): https://ui.minidev.pro/r/virtual-list.json
- License: MIT. One file, yours to edit after install.

## 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`](https://ui.minidev.pro/docs/virtualized-list) for a bare fixed-height list without header or keyboard model, [`DataTable`](https://ui.minidev.pro/docs/data-table) for sortable columns under ~1,000 rows, and [`InfiniteScroll`](https://ui.minidev.pro/docs/infinite-scroll) to load pages as the reader scrolls.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/virtual-list
```

Writes `components/ui/virtual-list.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/virtual-list.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

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

From the npm package:

```tsx
import { VirtualList } from "minidev-ui-kit/ui/virtual-list"
```

## Usage

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

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

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `items` | `T[]` | `SAMPLE_INVOICES as unknown as T[]` | no | Rows to render. Defaults to 10,000 Lumen invoices. |
| `rowHeight` | `number \| "auto"` | `40` | no | Row height in px, or "auto" to measure every row (estimatedRowHeight is used until a row has been measured). |
| `estimatedRowHeight` | `number` | `40` | no | Height assumed for rows that have not been measured yet, in "auto" mode. |
| `overscan` | `number` | `6` | no | Extra rows rendered above and below the viewport so fast scrolling never shows a gap. |
| `height` | `number` | `360` | no | Viewport height in px. |
| `renderRow` | `(item: T, index: number, state: { active: boolean }) => React.ReactNode` | `defaultRender` | no | Renders one row. Defaults to an invoice row. |
| `getKey` | `(item: T, index: number) => React.Key` | `defaultKey` | no | Stable key per row. Defaults to item.id, then the index. |
| `describeRow` | `(item: T, index: number) => string` | `defaultDescribe` | no | Plain text for the screen reader announcement of the active row. |
| `activeIndex` | `number` | - | no | Controlled active row. |
| `defaultActiveIndex` | `number` | `0` | no |  |
| `onActiveChange` | `(index: number) => void` | - | no |  |
| `onSelect` | `(item: T, index: number) => void` | - | no | Called on click, Enter or Space on a row. |
| `title` | `string` | `"Invoices"` | no | Header title. Pass an empty string to hide the header. |
| `showJump` | `boolean` | `true` | no | Show the "Jump to row" field in the header. |
| `showFooter` | `boolean` | `true` | no | Show the "Rows a to b of n" footer. |
| `ref` | `React.Ref<VirtualListHandle>` | - | no | Imperative handle with scrollToIndex and setActiveIndex. |
| `aria-label` | `string` | - | no |  |
| `className` | `string` | - | no |  |

## Examples

### Custom rows

Pass your own items and renderRow; describeRow gives screen readers a plain text version of the active row.

```tsx
type Customer = { id: string; name: string; plan: "Free" | "Pro" | "Scale"; mrr: number }

const NAMES = ["Northwind", "Kestrel", "Hollis & Rowe", "Larkspur", "Meridian Health", "Tidewater", "Brightline", "Copperleaf", "Halcyon Freight", "Fernhill"]
const SUFFIXES = ["Labs", "Ltd", "GmbH", "Inc", "Co"]
const PLANS = ["Free", "Pro", "Scale"] as const

const customers: Customer[] = Array.from({ length: 2000 }, (_, i) => ({
  id: "cus_" + (1000 + i),
  name: NAMES[i % NAMES.length] + " " + SUFFIXES[Math.floor(i / NAMES.length) % SUFFIXES.length],
  plan: PLANS[(i * 7) % 3],
  mrr: (((i * 37) % 90) + 10) * 10,
}))

<VirtualList
  items={customers}
  title="Customers"
  height={300}
  renderRow={(c, _i, { active }) => (
    <div className={"flex h-10 items-center gap-3 border-b border-border px-3 text-[0.8125rem] " + (active ? "bg-accent-soft" : "hover:bg-sunken")}>
      <span className="min-w-0 flex-1 truncate text-fg">{c.name}</span>
      <span className="w-12 text-xs text-fg-muted">{c.plan}</span>
      <span className="w-20 text-right text-fg tabular-nums">${c.mrr}/mo</span>
    </div>
  )}
  describeRow={(c) => c.name + ", " + c.plan + ", $" + c.mrr + " per month"}
/>
```

### Scroll to index

The ref exposes scrollToIndex with an alignment and setActiveIndex, which also moves the keyboard cursor.

```tsx
function ScrollToDemo() {
  const ref = React.useRef<VirtualListHandle>(null)
  return (
    <div className="flex w-full max-w-2xl flex-col gap-3">
      <div className="flex flex-wrap gap-2">
        <Button variant="outline" size="sm" onClick={() => ref.current?.scrollToIndex(0, "start")}>Top</Button>
        <Button variant="outline" size="sm" onClick={() => ref.current?.setActiveIndex(4999)}>Row 5,000</Button>
        <Button variant="outline" size="sm" onClick={() => ref.current?.scrollToIndex(9999, "end")}>Bottom</Button>
      </div>
      <VirtualList ref={ref} height={280} showJump={false} />
    </div>
  )
}
```

### Measured row heights

rowHeight="auto" measures every row as it mounts, so rows with wrapping text keep the scroll position honest.

```tsx
type Note = { id: string; author: string; text: string }

const AUTHORS = ["Maya Chen", "Theo Okafor", "Priya Raman", "Sam Lowe"]
const TEXTS = [
  "Marked INV-2041 as paid.",
  "Sent a reminder to Northwind about the overdue balance on INV-2038, including the updated bank details.",
  "Deployed lumen-web 4.2.0 to production.",
  "Changed the billing plan to Scale after the finance review. The old plan stays active until the end of the month, then the new seats are billed.",
  "Invited Ada Novak to Finance.",
]

const notes: Note[] = Array.from({ length: 500 }, (_, i) => ({
  id: "note-" + i,
  author: AUTHORS[i % AUTHORS.length],
  text: TEXTS[(i * 3) % TEXTS.length],
}))

<VirtualList
  items={notes}
  title="Activity"
  rowHeight="auto"
  estimatedRowHeight={56}
  height={320}
  showJump={false}
  renderRow={(n, _i, { active }) => (
    <div className={"border-b border-border px-3 py-2.5 text-[0.8125rem] " + (active ? "bg-accent-soft" : "hover:bg-sunken")}>
      <p className="font-medium text-fg">{n.author}</p>
      <p className="text-fg-muted">{n.text}</p>
    </div>
  )}
  describeRow={(n) => n.author + ": " + n.text}
/>
```

### Select handler

onSelect fires on click, Enter or Space; an empty title plus showJump and showFooter off leaves only the rows.

```tsx
function SelectDemo() {
  const [picked, setPicked] = React.useState<string | null>(null)
  return (
    <div className="flex w-full max-w-2xl flex-col gap-3">
      <VirtualList height={240} title="" aria-label="Invoices" showJump={false} showFooter={false} onSelect={(inv) => setPicked(inv.number)} />
      <p className="text-xs text-fg-muted">{picked ? "Selected " + picked : "Click a row, or focus the list and press Enter."}</p>
    </div>
  )
}
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` | Focuses the list. Rows are not separate Tab stops. |
| `ArrowDown` or `ArrowUp` | Moves the active row by one and scrolls it into view. |
| `PageDown` or `PageUp` | Moves the active row by about one screen. |
| `Home` or `End` | Moves to the first or last row. |
| `Enter` or `Space` | 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 by `aria-label`, or `title` when that is missing. Rows are `listitem`s 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".

## FAQ

### 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.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`

## Related

- [Virtualized List](https://ui.minidev.pro/docs/virtualized-list/md): Virtualized list that renders only the visible rows plus a small buffer, using a fixed item height and absolute positioning. Keeps thousands of React rows fast.
- [Data Table](https://ui.minidev.pro/docs/data-table/md): React data table with sortable columns, row selection, a floating bulk actions bar, hover row actions, sticky header, three densities and skeleton loading.
- [Infinite Scroll](https://ui.minidev.pro/docs/infinite-scroll/md): Load the next page when a sentinel scrolls into view, with a loading row, an end of list message and a Load more button as the accessible fallback.
- [Audit Filter Bar](https://ui.minidev.pro/docs/audit-filter-bar/md): Filter toolbar for audit logs combining a search field, actor filter chips for all, admins or system, and a date range picker in one wrapping row.
- [Audit Log Entry](https://ui.minidev.pro/docs/audit-log-entry/md): Single audit log line: actor avatar, actor name, muted action text and the affected target, with a relative timestamp underneath. Stacks into a trail.
- [Audit Trail](https://ui.minidev.pro/docs/audit-trail/md): Bordered audit trail in React that renders a list of actor, action, target and time records as stacked log entries. Useful for security and admin history pages.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
