Skip to contents

Virtualizer

Renders only the visible items of a large list.

Anatomy

Import the component and place it inside the list of a component that supports virtualization:

Anatomy

<Virtualizer> renders a window of the visible and overscanned items instead of the whole collection, and connects to the surrounding list so that keyboard navigation, scrolling, selection, and accessibility metadata keep working for items that are not mounted.

It can be used inside <Combobox.List> and <Autocomplete.List>, or on its own by passing the collection to its items prop.

Rendering items

Pass the collection to the items prop on the list root, and use the virtualizer as the only item-rendering child of the list. Its children is a function that receives an item and its index in the filtered collection, and must return exactly one item component:

Rendering items

Grid mode is not currently supported.

Use getItemKey to provide stable identity when item values are objects or when TypeScript cannot infer the item type. For primitive values, you can instead declare the item type explicitly, such as <Virtualizer<string>>.

Grouped collections

A grouped collection is an array of groups, each an object with an items array, as for the list components: a collection whose elements carry an items array is taken for a grouped one. Pass it as items together with renderGroupHeader, which renders the element carrying a group’s name. Inside <Combobox.List> that element is <Combobox.GroupLabel>; the virtualizer wraps each group’s rendered items in a role="group" element labeled by it, so <Combobox.Group> is not used. The virtualizer only knows a group by its items, so annotate the group parameter with your own group type to read anything else from it:

Grouped items in a Combobox

In a list of your own, the header renderer receives the group’s name attributes as its third argument, to spread onto the element that names the group:

Grouped items in a custom list

Group headers are rows of their own: they are measured like items, and estimatedGroupHeaderHeight is the height assumed for a header before it is measured. It defaults to the static estimatedItemHeight, and accepts a function receiving the group and its index. Headers never feed the running average that refines the item estimate.

Item indexes count items only, across all groups, so activeIndex, scrollToIndex, and aria-posinset mean the same thing in a grouped collection as in a flat one. getIndexAtOffset answers a header’s offset with the first item of its group; for an empty group, with the item that follows it, or the last item when nothing follows. It returns null only when the collection has no items, however many headers it renders.

Groups are identified by their index in the collection by default. Provide getGroupKey when a collection can be filtered: once a group drops out, the groups behind it would otherwise change identity. A group with no items renders its header.

The wrapper the virtualizer renders around a group spans only the rendered part of that group, so it is not intended for styling; style the header and the items instead.

Styling

The virtualizer is the scroll container, so constrain its height or maximum height. Without a height constraint, every item is rendered and virtualization provides no benefit. In the table layout, the same goes for the element around the table.

The --total-size CSS variable contains the estimated or measured height of the virtual content. It can be combined with --available-height from the positioner to keep the popup within the viewport:

Constraining the scroll container

CSS scroll-padding-top and scroll-padding-bottom on the scroll container are respected when keyboard navigation scrolls an item into view. An inset that depends on the item being scrolled to, rather than being fixed in CSS, travels with the scroll instead — see Leaving room at an edge.

Sizing items

estimatedItemHeight is the height used for items that have not been measured yet. It defaults to 32 pixels, and a static number is automatically refined with the running average of measured items. Pass a function receiving the item and its index to keep full control over per-item estimates.

overscanPx controls the extra pixel buffer rendered before and after the visible range. It defaults to the larger of 150px and the first item’s estimated height. The buffer is at least fifteen estimated rows in total, whatever the prop asks for: half of it on each side while the list is at rest, and all of it ahead of the viewport while the list scrolls, so that native scrolling has rows to move through before the next window is rendered.

Items of a known height

Items are measured by default, which is what lets them vary. When a collection’s items are known to be uniform, itemHeight declares that height instead:

Declaring the item height

Items are then positioned arithmetically: none of them is observed for resizes, none feeds the running average, and the scroll height is exact from the first render rather than converging as measurements arrive. It replaces estimatedItemHeight, which describes a height to assume until one is measured.

Only declare a height the items genuinely have. An item that turns out to be a different height is still laid out this many pixels from the next one, and nothing corrects it — so leave it out for items whose height depends on their content, on the width available, or on a font that loads later. In a grouped collection it covers the items; group headers are content of your own and are measured as they always are.

Disabled items

The disabled prop only marks a rendered item as disabled, so it is unavailable while the item is outside the rendered window. Declare disabled items in the data instead: create the items with Combobox.createItems() and its getDisabled option, which is what keyboard navigation uses to skip disabled items, including those that are not mounted. Without it, a disabled item can still be highlighted with the keyboard, though not selected.

Scrolling to an item

actionsRef exposes scrollToIndex, which scrolls an item into view by its index in the filtered collection, including when the item is outside the rendered window:

Scrolling to an item

Leaving room at an edge

paddingStart and paddingEnd keep the item clear of an inset at the start or end edge of the scrollport, in place of its scroll-padding-top and scroll-padding-bottom. They are for an inset the caller knows and CSS does not: an overlay whose height depends on the item being scrolled to, such as a tree pinning the ancestors of the row it scrolls to. The edge left out keeps the scrollport’s own scroll-padding.

The same fields belong to an activation, so a scroll that has to leave room for an overlay does not have to become imperative:

Scrolling below an overlay

Standalone lists

Pass the collection to the virtualizer’s own items prop to window a list that Base UI does not provide. Nothing else is required, so the virtualizer can be dropped into any scrolling list:

Windowing a custom list

The third argument of children carries the item’s accessibility and collection metadata — aria-posinset, aria-setsize, and data-index. Spread it onto the element that represents the item, so assistive technology reports the whole collection rather than the mounted window. Items rendered by a list’s own item component, such as <Combobox.Item>, receive it automatically.

Items placed relative to something else

aria-posinset and aria-setsize describe the item’s place in the flat collection, which is the set a listbox’s options belong to. They are wrong for a collection whose items are placed relative to something else: a tree item is the third child of its folder, not the 4,201st row of the flattened list, and a grid cell is placed by its row and column. Set itemAria="none" for those, and state the position yourself:

Declining the collection ARIA

Everything else the metadata carries — data-index, and in the table layout the row’s measurement — is unaffected, so the argument is still spread as it is. totalItems only feeds aria-setsize, so it has nothing to do here.

Set activeIndex to the item the list treats as active. The virtualizer keeps that item mounted even when it falls outside the rendered window, so it can hold focus or be referenced by aria-activedescendant, and scrolls it into view.

Whether an activation should scroll depends on what caused it, not on which item ended up active: a keypress should bring the item into view, while a highlight following the pointer must leave the viewport alone — the cursor is already there, and moving the list would slide the next item under it. So the activation carries that decision, alongside the alignment it wants:

Activating an item

Passing a bare index is shorthand for { index }, so activeIndex={5} activates and scrolls. Because the scroll travels with the index rather than in a prop of its own, the two cannot disagree, and re-rendering with an equal activation does not scroll again.

Everything a list root normally coordinates stays with your list: filtering, keyboard navigation, selection, and clamping activeIndex to the current collection.

Hosting the virtualizer in your own list

A list component can publish its collection to the virtualizer instead of the consumer passing one, which is how <Combobox.List> works: the virtualizer is dropped inside the list, with no items prop, and the list keeps owning filtering, keyboard navigation and selection.

A component of your own does the same by rendering VirtualizerHostProvider around its list element. It takes two values: host carries the stable wiring — the registry and the name diagnostics use — and state carries the collection and the active item, which change as the list does:

Publishing a collection to the virtualizer

The two are separate on purpose: an item part reads host to tell that it is inside a list at all, so anything changing per keystroke must travel through state instead.

What the host publishes:

  • items is the flat, ordered sequence to window. A hierarchy is published as the sequence it is displayed as — a tree publishes its expanded rows in order — and the hierarchy itself stays in your own item metadata. A grouped list publishes groups as a partition of that same sequence, never instead of it.
  • activeIndex is the activation, the same one the activeIndex prop takes: an index alone scrolls, and { index, scroll: false } activates without moving the viewport. align, paddingStart and paddingEnd travel with it.
  • rendersItemPart declares that every row renders your item part. In development, the virtualizer then warns when an item renderer returns none of them, or several, and when an item part is rendered outside the virtualizer. Leave it out if your rows are plain elements that spread the renderer’s third argument.
  • itemAria declares what the host’s items need. Leave it out for a listbox, whose options are placed in the flat collection; publish 'none' for a tree or a grid, whose items are placed relative to something else, and state the position in your own item part.
  • registry lets the host reach the virtualizer’s geometry: getItemMetrics, getIndexAtOffset and scrollToIndex are on registry.virtualizer while one is mounted, so a sticky header of your own can be positioned without an actionsRef.
  • windowingSuspended asks for every item to be mounted for a moment. Clearing it windows the list again, measuring its viewport afresh.

Your item part reads the metadata of the row it is rendered in with useVirtualizerItem(), and spreads it onto the element representing the item, the way <Combobox.Item> does. index is the item’s place in the collection. A group label reads the id the group is named by with useVirtualizerGroupHeader():

Reading the item metadata

A list nested inside one of the rows, rendered through its own VirtualizerHostProvider, does not see the row’s metadata: its item parts belong to it. Item parts rendered outside the list element — a sticky copy of a row, say — must not see the provider either, or they count as items the virtualizer does not own. Render the provider around the list element rather than the root, and where a part can be rendered elsewhere, wrap it in <VirtualizerHostProvider host={undefined} state={undefined}>, as <Combobox.Root> does for a root nested inside another list’s item.

Tables

Set layout="table" to virtualize the rows of a <table>. The virtualizer then renders a <tbody> instead of a scrolling <div>, and the element the item renderer returns is the row itself: a <tr>, with the renderer’s third argument spread onto it. The markup stays a table, so column widths from a <colgroup>, a sticky <thead>, and the table’s own accessibility semantics keep working.

#TitleStatusComments

The scroll container is an element of your own around the table: the nearest ancestor with overflow: auto or overflow: scroll, which is also the element a position: sticky header sticks to. Constrain its height. The section is stuck to the scrollport the way a list’s rows are, with insets that let native scrolling move its rows and that hold them on screen once a scroll outruns them, until the next window is rendered. Two <tbody> elements rendered around it, one before and one after, reserve the space of the rows outside the window. Use border-collapse: collapse on the table, or a zero border-spacing, since spacing would otherwise only be counted for the rendered rows.

Give sticky header cells a z-index: the section is positioned too, and comes later in the table, so without one it paints over them. This layout relies on position: sticky applying to table row groups, which current browsers support.

The third argument of children carries the row’s measurement along with its metadata, so it has to reach the <tr> element. It includes a style for a row kept mounted outside the window; add styles of your own through className, or merge them into the style it carries rather than replacing it.

Rows are identified by their index in the collection, so set aria-rowcount on the table and aria-rowindex on each row from the renderer’s index, counting the header row, for assistive technology to describe the whole table rather than the mounted window.

Everything else works as it does in a list. activeIndex keeps the active row mounted outside the window, so it can hold focus while keyboard navigation moves through the collection; actionsRef scrolls to rows by index; and scroll-padding-top on the scroll container keeps a scrolled row clear of a sticky header. Content passed to trailing is rendered as rows after the section’s reserved space, and a grouped collection renders its group headers as rows among the items, without a wrapper around each group.

Third-party virtualizers

A third-party virtualization library can be used instead when your application already uses one, or needs behavior that <Virtualizer> does not provide. Set the virtualized prop on the list root to opt out of the built-in item indexing, and coordinate filtering, item indexes, scrolling, and accessibility metadata yourself. See virtualized Combobox for a complete example.

API reference

Virtualizer

Renders a window of visible and overscanned items in a flat list. Renders a scrollable <div> element, or a <tbody> element in the table layout.

Pass the collection to the items prop to virtualize any list, or omit it inside a list that supports virtualization to window that list’s own collection. The latter requires the items prop on the list root, and the virtualizer must be the only item-rendering child of the list.

The element must have a constrained height or maximum height for virtualization to limit the number of mounted items. In the table layout, the scroll container around the table must.

Grid mode is not currently supported.

actionsRefReact.RefObject<Virtualizer.Actions | null>—
Description

A ref to imperative actions.

  • getIndexAtOffset: Returns the item a scroll position lands on.
  • getItemMetrics: Returns an item’s logical offset and size, including outside the window.
  • remeasure: Discards measured item heights so they are taken again.
  • scrollToIndex: Scrolls an item into view by its logical collection index.
Type
activeIndexUnion—
Description

The active item in items, kept mounted even when it falls outside the rendered window so it can hold focus or be referenced by aria-activedescendant.

An index alone scrolls the item into view. Pass { index, scroll: false } for activations that must leave the viewport alone, such as a highlight following the pointer, and align to choose where a scrolled item lands. paddingStart and paddingEnd keep the item clear of an inset the activation knows about, in place of the scrollport’s scroll-padding.

Ignored without the items prop: a list that provides the collection tracks its own highlight.

Type
enabledbooleantrue
Description

Whether virtualization is enabled. When false, all items are rendered.

Type
Default
true
endReachedThresholdnumber0
Description

How many items short of the end onEndReached fires. 0 fires once the last item enters the rendered window, which already extends past the visible range by overscanPx.

Type
Default
0
estimatedGroupHeaderHeightUnionthe static `estimatedItemHeight`, or `32` when that is a function
Description

Estimated group header height in CSS pixels used before header elements have been measured. Provide a function to keep full control over per-group estimates.

Type
Default
the static `estimatedItemHeight`, or `32` when that is a function
estimatedItemHeightUnion32
Description

Estimated item height in CSS pixels used before item elements have been measured. A static number is automatically refined with the running average of measured items. Provide a function to keep full control over per-item estimates.

Type
Default
32
getGroupKeyfunction—
Description

Returns a stable key for a group. Defaults to the group’s index in the collection, which is enough while groups keep their order; a filtered collection that can drop a group should provide one so the groups behind it keep their identity.

Type
getItemKeyfunction—
Description

Returns a stable key for the item value.

Primitive item values use the value itself by default. Required when item values are objects or the item type cannot be inferred.

Type
itemAriaVirtualizer.ItemAriawhat the host declares, or `'set'`
Description

Which ARIA the item metadata states for an item’s position in the collection.

  • set: aria-posinset and aria-setsize for the item’s place in the flat collection, which is what the options of a listbox need.
  • none: neither, for a collection whose items are placed relative to something else — the items of a tree, which are placed among their siblings, or the cells of a grid, which are placed by row and column. Everything else the metadata carries stays, so your own ARIA goes next to it.

A host publishing its own collection declares what its items need, and this prop overrides that declaration.

Type
Default
what the host declares, or `'set'`
itemHeightnumber—
Description

Height of every item in CSS pixels, when the collection’s items are known to be uniform.

Items are then positioned arithmetically: no item is measured, and the geometry is exact from the first render rather than converging as measurements arrive. It replaces estimatedItemHeight, which describes a height to assume until one is measured.

Only pass it when the height is genuinely fixed. Items that turn out to be a different height are still laid out as this many pixels apart, and nothing corrects it. Leave it out for items whose height depends on their content, on the width available, or on a font that loads later. In a grouped collection it covers the items; group headers are measured as they always are.

Type
itemsUnion—
Name
Description

The collection to virtualize: a flat array of items, or an array of groups, each an object with an items array. A grouped collection also needs renderGroupHeader.

When omitted, the collection and its highlight state come from a surrounding component that publishes them — a list that supports virtualization, such as <Combobox.List>, or a component of your own that implements the host contract.

Type
layoutVirtualizer.Layout'list'
Name
Description

How the rows are laid out.

  • list: the virtualizer is the scroll container, a <div>, and lays its rows out itself.
  • table: the virtualizer is a table section, a <tbody>, whose rows are the <tr> elements the item renderer returns, inside a scroll container of your own around the table: the nearest ancestor with overflow: auto or scroll. The section is stuck to the scrollport, as a list’s rows are, between two sections of the virtualizer’s own that reserve the space of the rows outside the window, so the rows it holds stay on screen while the next window is rendered. Spread the renderer’s third argument onto each <tr>; it carries the row’s measurement along with its metadata. Group headers are rendered as rows among the items, and trailing content as rows after the reserved space.
Type
Default
'list'
onEndReachedfunction—
Description

Called when the rendered window reaches the end of the collection, for loading the next page of a longer list. Fires once per arrival: it does not repeat while the window stays at the end, and arms again when the window moves away or the collection grows past it.

Type
overscanPxnumber—
Description

Pixel buffer rendered before and after the visible range. Defaults to the larger of 150px and the estimated size of the first item. The buffer is at least fifteen estimated rows in total, whatever this prop asks for: half of it on each side while the list is at rest, and all of it ahead while it scrolls.

Type
renderGroupHeaderVirtualizerRenderGroupHeader<Value>—
Description

Renders the header of a group in a grouped collection: exactly one element carrying the group’s name. The third argument holds the id the group is labeled by, to spread onto that element; <Combobox.GroupLabel> applies it automatically. The virtualizer wraps each group’s rendered rows in a role="group" element itself, so no group part is needed.

Type
totalItemsnumberthe number of items in the list
Description

Number of items in the whole collection, when the items rendered are only part of it — a page of a larger result set, say. Rendered items report it as their aria-setsize, so assistive technology describes the collection rather than the part of it currently loaded.

Each item’s aria-posinset is its position in items, counted from the first one, so the items rendered must be the collection from its start: pages loaded so far, appended to the ones before them, rather than a later page on its own.

Pass -1 when the size is not known yet, which is the ARIA convention for it. It has nothing to describe when itemAria is none.

Type
Default
the number of items in the list
trailingReact.ReactNode—
Description

Content rendered after the last item, inside the scroll container, for a loading indicator or an end-of-results note. It scrolls with the items and is measured into the scrollable height, rather than being pinned below the list.

It is not an item: it takes no index, and is left out of aria-setsize and aria-posinset. A list is only allowed to contain options, so this content must not present itself as one — keep it out of the accessibility tree and convey the state it stands for another way, such as aria-busy on the list with a live region outside it. Interactive controls belong outside the list, where they can be reached with the keyboard.

Type
children((item: Value, index: number, itemProps: Virtualizer.ItemProps) => ReactElement)—
Description

Renders exactly one item for the given value and its index in the collection. The third argument carries the item’s accessibility and collection metadata, to spread onto the element representing the item. A list’s own item component applies it automatically.

Type
classNamestring | function—
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function—
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function—
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-empty

Present when the virtualized collection is empty.

Attribute
Description
data-empty

Present when the virtualized collection is empty.

--total-size

The total height of the virtualized content, including the scrollport’s block padding, or, in the table layout, everything the scroll container holds around the table section.

CSS Variable
Description
--total-size

The total height of the virtualized content, including the scrollport’s block padding, or, in the table layout, everything the scroll container holds around the table section.

Virtualizer.PropsHide

Re-Export of Virtualizer props as VirtualizerProps

Virtualizer.StateHide

Virtualizer.ActionsHide

Virtualizer.ItemMetadataHide

Virtualizer.ActiveIndexHide

Virtualizer.ActiveItemHide

Virtualizer.GroupHide

Virtualizer.GroupHeaderElementHide

Virtualizer.GroupHeaderMetadataHide

Virtualizer.GroupHeaderPropsHide

Virtualizer.HandleHide

Virtualizer.HostHide

Virtualizer.HostStateHide

Virtualizer.ItemAriaHide

Virtualizer.ItemPropsHide

Virtualizer.LayoutHide

Virtualizer.RegistrationHide

Virtualizer.RegistryHide

Virtualizer.RowPropsHide

VirtualizerCssVariablesHide

VirtualizerDataAttributesHide

VirtualizerHostProviderPropsHide

VirtualizerItemMetricsHide

VirtualizerScrollAlignmentHide

VirtualizerScrollToIndexOptionsHide