Virtualizer

Render large lists, grids, and window-scrolled content by only mounting the items in view.

Loading...

Usage

The virtualizer renders only the items in the scroll viewport, plus a few extra for smooth scrolling. There are three flavors, each a hook plus components:

  • useListVirtualizer + ListVirtualizer for lists that scroll inside their own container.
  • useGridVirtualizer + GridVirtualizer for grids of rows and cells.
  • useWindowVirtualizer + WindowVirtualizer for lists that scroll with the window or nearest scrollable ancestor.

Pass the hook's result to Root as value, render the spacer with Content, and map the virtual items to Item.

import { ListVirtualizer, useListVirtualizer } from '@ark-ui/react/virtualizer'

const virtualizer = useListVirtualizer({
  count: items.length,
  estimatedSize: () => 48,
})

<ListVirtualizer.Root value={virtualizer}>
  <ListVirtualizer.Content>
    {virtualizer.getVirtualItems().map((item) => (
      <ListVirtualizer.Item key={item.key} item={item}>
        {items[item.index]}
      </ListVirtualizer.Item>
    ))}
  </ListVirtualizer.Content>
</ListVirtualizer.Root>

Root is the scroll container. Give it a fixed height (or width when horizontal). Content is a spacer sized to the whole list. Item is positioned with a transform, so give items a height that matches estimatedSize, or measure them as shown below.

The virtualizer re-measures when the container resizes, so it works inside tabs, dialogs and collapsed panels. Set observeScrollElementSize: false to opt out.

List

Dynamic Size

For items of different heights, set measure on Item. Each item is measured after render, so estimatedSize only needs to be a rough guess.

Horizontal

Set orientation="horizontal" to virtualize along the x-axis.

Scroll to Index

scrollToIndex jumps to an item. It takes align (start, center, end, auto) and smooth.

Grid

Map getVirtualRows() to Row and each row's columns to Cell. Cell reads its row from context, so it only needs the column. Rows accept measure.

Window

Scrolling happens on the window or nearest scrollable ancestor, so Root needs no height or overflow. Use it when the list is part of the page flow.

import { WindowVirtualizer, useWindowVirtualizer } from '@ark-ui/react/virtualizer'
import styles from './virtualizer.module.css'

const items = Array.from({ length: 10000 }, (_, index) => `Item ${index + 1}`)

export const Window = () => {
  const virtualizer = useWindowVirtualizer({
    count: items.length,
    estimatedSize: () => 48,
  })

  return (
    <WindowVirtualizer.Root value={virtualizer} className={styles.Window}>
      <WindowVirtualizer.Content>
        {virtualizer.getVirtualItems().map((item) => (
          <WindowVirtualizer.Item
            key={item.key}
            item={item}
            className={`${styles.Item} ${item.index % 2 ? styles.ItemAlt : ''}`}
          >
            {items[item.index]}
          </WindowVirtualizer.Item>
        ))}
      </WindowVirtualizer.Content>
    </WindowVirtualizer.Root>
  )
}

Using the Context

Context exposes the virtualizer to children, for derived state like the visible range.

<ListVirtualizer.Root value={virtualizer}>
  <ListVirtualizer.Context>
    {(virtualizer) => <span>{virtualizer.getVirtualItems().length} items rendered</span>}
  </ListVirtualizer.Context>
</ListVirtualizer.Root>

Composing with other components

Use the render prop to render a component's scroll container as ListVirtualizer.Root and its items as ListVirtualizer.Item. The component keeps its roles, ids and handlers; the virtualizer adds positioning and aria-posinset / aria-setsize.

<Combobox.List render={<ListVirtualizer.Root value={virtualizer} />}>
  <ListVirtualizer.Content>
    {virtualizer.getVirtualItems().map((virtualItem) => {
      const item = collection.items[virtualItem.index]
      return (
        <Combobox.Item key={item.value} item={item} render={<ListVirtualizer.Item item={virtualItem} />}>
          <Combobox.ItemText>{item.label}</Combobox.ItemText>
        </Combobox.Item>
      )
    })}
  </ListVirtualizer.Content>
</Combobox.List>

Forward the component's scrollToIndexFn to virtualizer.scrollToIndex so keyboard navigation reaches unmounted items. See the virtualized examples for Select, Combobox, Listbox, and Tree View.

API Reference

GridCell

Defines the <div> element.

AttributeDefaultType
column
VirtualColumn

The virtual column to render within the current row.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

GridContent

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

GridRoot

Defines the <div> element.

AttributeDefaultType
value
UseGridVirtualizerReturn

The virtualizer instance returned by `useGridVirtualizer`.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

GridRow

Defines the <div> element.

AttributeDefaultType
row
VirtualRow

The virtual row to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
measurefalse
boolean

Whether to measure the rendered height of the row and use it instead of the estimate.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

ListContent

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

ListItem

Defines the <div> element.

AttributeDefaultType
item
VirtualItem

The virtual item to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
measurefalse
boolean

Whether to measure the rendered size of the item and use it instead of the estimate.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

ListRoot

Defines the <div> element.

AttributeDefaultType
value
UseListVirtualizerReturn

The virtualizer instance returned by `useListVirtualizer`.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

WindowContent

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

WindowItem

Defines the <div> element.

AttributeDefaultType
item
VirtualItem

The virtual item to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
measurefalse
boolean

Whether to measure the rendered size of the item and use it instead of the estimate.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

WindowRoot

Defines the <div> element.

AttributeDefaultType
value
UseWindowVirtualizerReturn

The virtualizer instance returned by `useWindowVirtualizer`.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.