Virtualization
Render long lists and menus by mounting only the items on screen.
A list of 10,000 options renders 10,000 elements, and the browser lays out and paints every one of them. A virtualizer mounts only the items in view, plus a few on either side, and swaps them as you scroll. The Virtualizer utility covers the API. This guide covers using it in real components.
When to Virtualize
Reach for it when a list grows into the hundreds of items, or when each item is expensive to render. A few hundred plain rows are usually fine without it.
It has costs worth knowing up front:
- The browser's find-in-page only sees items that are mounted.
- Items you haven't scrolled to don't exist yet, so anything that queries the DOM for them won't find them.
- You need to give the scroll container a fixed height, or a maximum height.
Choosing a Virtualizer
| Virtualizer | Use it for |
|---|---|
ListVirtualizer | A list that scrolls inside its own container: menus, select and combobox lists |
GridVirtualizer | Rows and columns, like a data grid or an icon picker |
WindowVirtualizer | A list that scrolls with the page, like a feed |
Virtualizing a Collection Component
Select, Combobox, Listbox and Tree View keep their items in a collection, so they know every item even when only a few are mounted. Virtualizing one takes four steps:
- Create a virtualizer whose
countis the collection's size. - Render the component's list as
ListVirtualizer.Rootwith therenderprop. - Map the virtual items to the component's items, each rendered as
ListVirtualizer.Item. - Forward the component's
scrollToIndexFnto the virtualizer.
import { Combobox, useListCollection } from '@ark-ui/react/combobox'
import { useFilter } from '@ark-ui/react/locale'
import { Portal } from '@ark-ui/react/portal'
import { ListVirtualizer, useListVirtualizer } from '@ark-ui/react/virtualizer'
import { CheckIcon, ChevronsUpDownIcon } from 'lucide-react'
import { type CSSProperties, useLayoutEffect } from 'react'
import styles from './combobox.module.css'
export const Virtualized = () => {
const { startsWith } = useFilter({ sensitivity: 'base' })
const { collection, filter, reset } = useListCollection({
initialItems: countries,
filter: startsWith,
})
const virtualizer = useListVirtualizer({
count: collection.size,
estimatedSize: () => 36,
overscan: 10,
})
const count = collection.size
useLayoutEffect(() => {
virtualizer.updateOptions({ count })
}, [virtualizer, count])
const handleScrollToIndex: Combobox.RootProps<Country>['scrollToIndexFn'] = (details) => {
virtualizer.scrollToIndex(details.index, { align: 'auto' })
}
const handleInputChange = (details: Combobox.InputValueChangeDetails) => {
filter(details.inputValue)
}
return (
<Combobox.Root
className={styles.Root}
collection={collection}
onInputValueChange={handleInputChange}
scrollToIndexFn={handleScrollToIndex}
>
<Combobox.Label className={styles.Label}>Country</Combobox.Label>
<Combobox.Control className={styles.Control}>
<Combobox.Input className={styles.Input} placeholder="e.g. Germany" />
<div className={styles.Indicators}>
<Combobox.Trigger className={styles.Trigger} onClick={reset}>
<ChevronsUpDownIcon />
</Combobox.Trigger>
</div>
</Combobox.Control>
<Portal>
<Combobox.Positioner className={styles.Positioner}>
<Combobox.Content className={styles.Content}>
<Combobox.List
className={styles.Scroller}
style={{ '--total-size': `${virtualizer.getTotalSize()}px` } as CSSProperties}
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}
className={styles.Item}
render={<ListVirtualizer.Item item={virtualItem} />}
>
<Combobox.ItemText className={styles.ItemText}>
<span aria-hidden style={{ marginRight: 8 }}>
{item.emoji}
</span>
{item.label}
</Combobox.ItemText>
<Combobox.ItemIndicator className={styles.ItemIndicator}>
<CheckIcon />
</Combobox.ItemIndicator>
</Combobox.Item>
)
})}
</ListVirtualizer.Content>
</Combobox.List>
</Combobox.Content>
</Combobox.Positioner>
</Portal>
</Combobox.Root>
)
}
interface Country {
value: string
label: string
emoji: string
}
const countries: Country[] = [
{ value: 'AD', label: 'Andorra', emoji: '๐ฆ๐ฉ' },
{ value: 'AE', label: 'United Arab Emirates', emoji: '๐ฆ๐ช' },
{ value: 'AF', label: 'Afghanistan', emoji: '๐ฆ๐ซ' },
{ value: 'AG', label: 'Antigua and Barbuda', emoji: '๐ฆ๐ฌ' },
{ value: 'AI', label: 'Anguilla', emoji: '๐ฆ๐ฎ' },
{ value: 'AL', label: 'Albania', emoji: '๐ฆ๐ฑ' },
{ value: 'AM', label: 'Armenia', emoji: '๐ฆ๐ฒ' },
{ value: 'AO', label: 'Angola', emoji: '๐ฆ๐ด' },
{ value: 'AQ', label: 'Antarctica', emoji: '๐ฆ๐ถ' },
{ value: 'AR', label: 'Argentina', emoji: '๐ฆ๐ท' },
{ value: 'AS', label: 'American Samoa', emoji: '๐ฆ๐ธ' },
{ value: 'AT', label: 'Austria', emoji: '๐ฆ๐น' },
{ value: 'AU', label: 'Australia', emoji: '๐ฆ๐บ' },
{ value: 'AW', label: 'Aruba', emoji: '๐ฆ๐ผ' },
{ value: 'AX', label: 'ร
land Islands', emoji: '๐ฆ๐ฝ' },
{ value: 'AZ', label: 'Azerbaijan', emoji: '๐ฆ๐ฟ' },
{ value: 'BA', label: 'Bosnia and Herzegovina', emoji: '๐ง๐ฆ' },
{ value: 'BB', label: 'Barbados', emoji: '๐ง๐ง' },
{ value: 'BD', label: 'Bangladesh', emoji: '๐ง๐ฉ' },
{ value: 'BE', label: 'Belgium', emoji: '๐ง๐ช' },
{ value: 'BF', label: 'Burkina Faso', emoji: '๐ง๐ซ' },
{ value: 'BG', label: 'Bulgaria', emoji: '๐ง๐ฌ' },
{ value: 'BH', label: 'Bahrain', emoji: '๐ง๐ญ' },
{ value: 'BI', label: 'Burundi', emoji: '๐ง๐ฎ' },
{ value: 'BJ', label: 'Benin', emoji: '๐ง๐ฏ' },
{ value: 'BL', label: 'Saint Barthรฉlemy', emoji: '๐ง๐ฑ' },
{ value: 'BM', label: 'Bermuda', emoji: '๐ง๐ฒ' },
{ value: 'BN', label: 'Brunei', emoji: '๐ง๐ณ' },
{ value: 'BO', label: 'Bolivia', emoji: '๐ง๐ด' },
{ value: 'BR', label: 'Brazil', emoji: '๐ง๐ท' },
{ value: 'BS', label: 'Bahamas', emoji: '๐ง๐ธ' },
{ value: 'BT', label: 'Bhutan', emoji: '๐ง๐น' },
{ value: 'BW', label: 'Botswana', emoji: '๐ง๐ผ' },
{ value: 'BY', label: 'Belarus', emoji: '๐ง๐พ' },
{ value: 'BZ', label: 'Belize', emoji: '๐ง๐ฟ' },
{ value: 'CA', label: 'Canada', emoji: '๐จ๐ฆ' },
{ value: 'CD', label: 'Congo', emoji: '๐จ๐ฉ' },
{ value: 'CF', label: 'Central African Republic', emoji: '๐จ๐ซ' },
{ value: 'CH', label: 'Switzerland', emoji: '๐จ๐ญ' },
{ value: 'CI', label: "Cรดte d'Ivoire", emoji: '๐จ๐ฎ' },
{ value: 'CK', label: 'Cook Islands', emoji: '๐จ๐ฐ' },
{ value: 'CL', label: 'Chile', emoji: '๐จ๐ฑ' },
{ value: 'CM', label: 'Cameroon', emoji: '๐จ๐ฒ' },
{ value: 'CN', label: 'China', emoji: '๐จ๐ณ' },
{ value: 'CO', label: 'Colombia', emoji: '๐จ๐ด' },
{ value: 'CR', label: 'Costa Rica', emoji: '๐จ๐ท' },
{ value: 'CU', label: 'Cuba', emoji: '๐จ๐บ' },
{ value: 'CV', label: 'Cabo Verde', emoji: '๐จ๐ป' },
{ value: 'CY', label: 'Cyprus', emoji: '๐จ๐พ' },
{ value: 'CZ', label: 'Czech Republic', emoji: '๐จ๐ฟ' },
{ value: 'DE', label: 'Germany', emoji: '๐ฉ๐ช' },
{ value: 'DJ', label: 'Djibouti', emoji: '๐ฉ๐ฏ' },
{ value: 'DK', label: 'Denmark', emoji: '๐ฉ๐ฐ' },
{ value: 'DM', label: 'Dominica', emoji: '๐ฉ๐ฒ' },
{ value: 'DO', label: 'Dominican Republic', emoji: '๐ฉ๐ด' },
{ value: 'DZ', label: 'Algeria', emoji: '๐ฉ๐ฟ' },
{ value: 'EC', label: 'Ecuador', emoji: '๐ช๐จ' },
{ value: 'EE', label: 'Estonia', emoji: '๐ช๐ช' },
{ value: 'EG', label: 'Egypt', emoji: '๐ช๐ฌ' },
{ value: 'ER', label: 'Eritrea', emoji: '๐ช๐ท' },
{ value: 'ES', label: 'Spain', emoji: '๐ช๐ธ' },
{ value: 'ET', label: 'Ethiopia', emoji: '๐ช๐น' },
{ value: 'FI', label: 'Finland', emoji: '๐ซ๐ฎ' },
{ value: 'FJ', label: 'Fiji', emoji: '๐ซ๐ฏ' },
{ value: 'FK', label: 'Falkland Islands', emoji: '๐ซ๐ฐ' },
{ value: 'FM', label: 'Micronesia', emoji: '๐ซ๐ฒ' },
{ value: 'FO', label: 'Faroe Islands', emoji: '๐ซ๐ด' },
{ value: 'FR', label: 'France', emoji: '๐ซ๐ท' },
{ value: 'GA', label: 'Gabon', emoji: '๐ฌ๐ฆ' },
{ value: 'GB', label: 'United Kingdom', emoji: '๐ฌ๐ง' },
{ value: 'GD', label: 'Grenada', emoji: '๐ฌ๐ฉ' },
{ value: 'GE', label: 'Georgia', emoji: '๐ฌ๐ช' },
{ value: 'GH', label: 'Ghana', emoji: '๐ฌ๐ญ' },
{ value: 'GI', label: 'Gibraltar', emoji: '๐ฌ๐ฎ' },
{ value: 'GL', label: 'Greenland', emoji: '๐ฌ๐ฑ' },
{ value: 'GM', label: 'Gambia', emoji: '๐ฌ๐ฒ' },
{ value: 'GN', label: 'Guinea', emoji: '๐ฌ๐ณ' },
{ value: 'GQ', label: 'Equatorial Guinea', emoji: '๐ฌ๐ถ' },
{ value: 'GR', label: 'Greece', emoji: '๐ฌ๐ท' },
{ value: 'GT', label: 'Guatemala', emoji: '๐ฌ๐น' },
{ value: 'GU', label: 'Guam', emoji: '๐ฌ๐บ' },
{ value: 'GW', label: 'Guinea-Bissau', emoji: '๐ฌ๐ผ' },
{ value: 'GY', label: 'Guyana', emoji: '๐ฌ๐พ' },
{ value: 'HK', label: 'Hong Kong', emoji: '๐ญ๐ฐ' },
{ value: 'HN', label: 'Honduras', emoji: '๐ญ๐ณ' },
{ value: 'HR', label: 'Croatia', emoji: '๐ญ๐ท' },
{ value: 'HT', label: 'Haiti', emoji: '๐ญ๐น' },
{ value: 'HU', label: 'Hungary', emoji: '๐ญ๐บ' },
{ value: 'ID', label: 'Indonesia', emoji: '๐ฎ๐ฉ' },
{ value: 'IE', label: 'Ireland', emoji: '๐ฎ๐ช' },
{ value: 'IL', label: 'Israel', emoji: '๐ฎ๐ฑ' },
{ value: 'IM', label: 'Isle of Man', emoji: '๐ฎ๐ฒ' },
{ value: 'IN', label: 'India', emoji: '๐ฎ๐ณ' },
{ value: 'IQ', label: 'Iraq', emoji: '๐ฎ๐ถ' },
{ value: 'IR', label: 'Iran', emoji: '๐ฎ๐ท' },
{ value: 'IS', label: 'Iceland', emoji: '๐ฎ๐ธ' },
{ value: 'IT', label: 'Italy', emoji: '๐ฎ๐น' },
{ value: 'JE', label: 'Jersey', emoji: '๐ฏ๐ช' },
{ value: 'JM', label: 'Jamaica', emoji: '๐ฏ๐ฒ' },
{ value: 'JO', label: 'Jordan', emoji: '๐ฏ๐ด' },
{ value: 'JP', label: 'Japan', emoji: '๐ฏ๐ต' },
{ value: 'KE', label: 'Kenya', emoji: '๐ฐ๐ช' },
{ value: 'KG', label: 'Kyrgyzstan', emoji: '๐ฐ๐ฌ' },
{ value: 'KH', label: 'Cambodia', emoji: '๐ฐ๐ญ' },
{ value: 'KI', label: 'Kiribati', emoji: '๐ฐ๐ฎ' },
{ value: 'KM', label: 'Comoros', emoji: '๐ฐ๐ฒ' },
{ value: 'KN', label: 'Saint Kitts and Nevis', emoji: '๐ฐ๐ณ' },
{ value: 'KP', label: 'North Korea', emoji: '๐ฐ๐ต' },
{ value: 'KR', label: 'South Korea', emoji: '๐ฐ๐ท' },
{ value: 'KW', label: 'Kuwait', emoji: '๐ฐ๐ผ' },
{ value: 'KY', label: 'Cayman Islands', emoji: '๐ฐ๐พ' },
{ value: 'KZ', label: 'Kazakhstan', emoji: '๐ฐ๐ฟ' },
{ value: 'LA', label: 'Laos', emoji: '๐ฑ๐ฆ' },
{ value: 'LB', label: 'Lebanon', emoji: '๐ฑ๐ง' },
{ value: 'LC', label: 'Saint Lucia', emoji: '๐ฑ๐จ' },
{ value: 'LI', label: 'Liechtenstein', emoji: '๐ฑ๐ฎ' },
{ value: 'LK', label: 'Sri Lanka', emoji: '๐ฑ๐ฐ' },
{ value: 'LR', label: 'Liberia', emoji: '๐ฑ๐ท' },
{ value: 'LS', label: 'Lesotho', emoji: '๐ฑ๐ธ' },
{ value: 'LT', label: 'Lithuania', emoji: '๐ฑ๐น' },
{ value: 'LU', label: 'Luxembourg', emoji: '๐ฑ๐บ' },
{ value: 'LV', label: 'Latvia', emoji: '๐ฑ๐ป' },
{ value: 'LY', label: 'Libya', emoji: '๐ฑ๐พ' },
{ value: 'MA', label: 'Morocco', emoji: '๐ฒ๐ฆ' },
{ value: 'MC', label: 'Monaco', emoji: '๐ฒ๐จ' },
{ value: 'MD', label: 'Moldova', emoji: '๐ฒ๐ฉ' },
{ value: 'ME', label: 'Montenegro', emoji: '๐ฒ๐ช' },
{ value: 'MG', label: 'Madagascar', emoji: '๐ฒ๐ฌ' },
{ value: 'MH', label: 'Marshall Islands', emoji: '๐ฒ๐ญ' },
{ value: 'MK', label: 'North Macedonia', emoji: '๐ฒ๐ฐ' },
{ value: 'ML', label: 'Mali', emoji: '๐ฒ๐ฑ' },
{ value: 'MM', label: 'Myanmar', emoji: '๐ฒ๐ฒ' },
{ value: 'MN', label: 'Mongolia', emoji: '๐ฒ๐ณ' },
{ value: 'MO', label: 'Macao', emoji: '๐ฒ๐ด' },
{ value: 'MR', label: 'Mauritania', emoji: '๐ฒ๐ท' },
{ value: 'MS', label: 'Montserrat', emoji: '๐ฒ๐ธ' },
{ value: 'MT', label: 'Malta', emoji: '๐ฒ๐น' },
{ value: 'MU', label: 'Mauritius', emoji: '๐ฒ๐บ' },
{ value: 'MV', label: 'Maldives', emoji: '๐ฒ๐ป' },
{ value: 'MW', label: 'Malawi', emoji: '๐ฒ๐ผ' },
{ value: 'MX', label: 'Mexico', emoji: '๐ฒ๐ฝ' },
{ value: 'MY', label: 'Malaysia', emoji: '๐ฒ๐พ' },
{ value: 'MZ', label: 'Mozambique', emoji: '๐ฒ๐ฟ' },
{ value: 'NA', label: 'Namibia', emoji: '๐ณ๐ฆ' },
{ value: 'NC', label: 'New Caledonia', emoji: '๐ณ๐จ' },
{ value: 'NE', label: 'Niger', emoji: '๐ณ๐ช' },
{ value: 'NF', label: 'Norfolk Island', emoji: '๐ณ๐ซ' },
{ value: 'NG', label: 'Nigeria', emoji: '๐ณ๐ฌ' },
{ value: 'NI', label: 'Nicaragua', emoji: '๐ณ๐ฎ' },
{ value: 'NL', label: 'Netherlands', emoji: '๐ณ๐ฑ' },
{ value: 'NO', label: 'Norway', emoji: '๐ณ๐ด' },
{ value: 'NP', label: 'Nepal', emoji: '๐ณ๐ต' },
{ value: 'NR', label: 'Nauru', emoji: '๐ณ๐ท' },
{ value: 'NU', label: 'Niue', emoji: '๐ณ๐บ' },
{ value: 'NZ', label: 'New Zealand', emoji: '๐ณ๐ฟ' },
{ value: 'OM', label: 'Oman', emoji: '๐ด๐ฒ' },
{ value: 'PA', label: 'Panama', emoji: '๐ต๐ฆ' },
{ value: 'PE', label: 'Peru', emoji: '๐ต๐ช' },
{ value: 'PF', label: 'French Polynesia', emoji: '๐ต๐ซ' },
{ value: 'PG', label: 'Papua New Guinea', emoji: '๐ต๐ฌ' },
{ value: 'PH', label: 'Philippines', emoji: '๐ต๐ญ' },
{ value: 'PK', label: 'Pakistan', emoji: '๐ต๐ฐ' },
{ value: 'PL', label: 'Poland', emoji: '๐ต๐ฑ' },
{ value: 'PR', label: 'Puerto Rico', emoji: '๐ต๐ท' },
{ value: 'PS', label: 'Palestine', emoji: '๐ต๐ธ' },
{ value: 'PT', label: 'Portugal', emoji: '๐ต๐น' },
{ value: 'PW', label: 'Palau', emoji: '๐ต๐ผ' },
{ value: 'PY', label: 'Paraguay', emoji: '๐ต๐พ' },
{ value: 'QA', label: 'Qatar', emoji: '๐ถ๐ฆ' },
{ value: 'RO', label: 'Romania', emoji: '๐ท๐ด' },
{ value: 'RS', label: 'Serbia', emoji: '๐ท๐ธ' },
{ value: 'RU', label: 'Russia', emoji: '๐ท๐บ' },
{ value: 'RW', label: 'Rwanda', emoji: '๐ท๐ผ' },
{ value: 'SA', label: 'Saudi Arabia', emoji: '๐ธ๐ฆ' },
{ value: 'SB', label: 'Solomon Islands', emoji: '๐ธ๐ง' },
{ value: 'SC', label: 'Seychelles', emoji: '๐ธ๐จ' },
{ value: 'SD', label: 'Sudan', emoji: '๐ธ๐ฉ' },
{ value: 'SE', label: 'Sweden', emoji: '๐ธ๐ช' },
{ value: 'SG', label: 'Singapore', emoji: '๐ธ๐ฌ' },
{ value: 'SI', label: 'Slovenia', emoji: '๐ธ๐ฎ' },
{ value: 'SK', label: 'Slovakia', emoji: '๐ธ๐ฐ' },
{ value: 'SL', label: 'Sierra Leone', emoji: '๐ธ๐ฑ' },
{ value: 'SM', label: 'San Marino', emoji: '๐ธ๐ฒ' },
{ value: 'SN', label: 'Senegal', emoji: '๐ธ๐ณ' },
{ value: 'SO', label: 'Somalia', emoji: '๐ธ๐ด' },
{ value: 'SR', label: 'Suriname', emoji: '๐ธ๐ท' },
{ value: 'SS', label: 'South Sudan', emoji: '๐ธ๐ธ' },
{ value: 'ST', label: 'Sao Tome and Principe', emoji: '๐ธ๐น' },
{ value: 'SV', label: 'El Salvador', emoji: '๐ธ๐ป' },
{ value: 'SY', label: 'Syria', emoji: '๐ธ๐พ' },
{ value: 'SZ', label: 'Eswatini', emoji: '๐ธ๐ฟ' },
{ value: 'TC', label: 'Turks and Caicos Islands', emoji: '๐น๐จ' },
{ value: 'TD', label: 'Chad', emoji: '๐น๐ฉ' },
{ value: 'TG', label: 'Togo', emoji: '๐น๐ฌ' },
{ value: 'TH', label: 'Thailand', emoji: '๐น๐ญ' },
{ value: 'TJ', label: 'Tajikistan', emoji: '๐น๐ฏ' },
{ value: 'TK', label: 'Tokelau', emoji: '๐น๐ฐ' },
{ value: 'TL', label: 'Timor-Leste', emoji: '๐น๐ฑ' },
{ value: 'TM', label: 'Turkmenistan', emoji: '๐น๐ฒ' },
{ value: 'TN', label: 'Tunisia', emoji: '๐น๐ณ' },
{ value: 'TO', label: 'Tonga', emoji: '๐น๐ด' },
{ value: 'TR', label: 'Tรผrkiye', emoji: '๐น๐ท' },
{ value: 'TT', label: 'Trinidad and Tobago', emoji: '๐น๐น' },
{ value: 'TV', label: 'Tuvalu', emoji: '๐น๐ป' },
{ value: 'TW', label: 'Taiwan', emoji: '๐น๐ผ' },
{ value: 'TZ', label: 'Tanzania', emoji: '๐น๐ฟ' },
{ value: 'UA', label: 'Ukraine', emoji: '๐บ๐ฆ' },
{ value: 'UG', label: 'Uganda', emoji: '๐บ๐ฌ' },
{ value: 'US', label: 'United States', emoji: '๐บ๐ธ' },
{ value: 'UY', label: 'Uruguay', emoji: '๐บ๐พ' },
{ value: 'UZ', label: 'Uzbekistan', emoji: '๐บ๐ฟ' },
{ value: 'VA', label: 'Vatican City', emoji: '๐ป๐ฆ' },
{ value: 'VC', label: 'Saint Vincent and the Grenadines', emoji: '๐ป๐จ' },
{ value: 'VE', label: 'Venezuela', emoji: '๐ป๐ช' },
{ value: 'VG', label: 'British Virgin Islands', emoji: '๐ป๐ฌ' },
{ value: 'VI', label: 'U.S. Virgin Islands', emoji: '๐ป๐ฎ' },
{ value: 'VN', label: 'Vietnam', emoji: '๐ป๐ณ' },
{ value: 'VU', label: 'Vanuatu', emoji: '๐ป๐บ' },
{ value: 'WF', label: 'Wallis and Futuna', emoji: '๐ผ๐ซ' },
{ value: 'WS', label: 'Samoa', emoji: '๐ผ๐ธ' },
{ value: 'YE', label: 'Yemen', emoji: '๐พ๐ช' },
{ value: 'YT', label: 'Mayotte', emoji: '๐พ๐น' },
{ value: 'ZA', label: 'South Africa', emoji: '๐ฟ๐ฆ' },
{ value: 'ZM', label: 'Zambia', emoji: '๐ฟ๐ฒ' },
{ value: 'ZW', label: 'Zimbabwe', emoji: '๐ฟ๐ผ' },
]
The component keeps its roles, ids and event handlers, and the virtualizer adds positioning. The same pattern works for Select, Listbox and Tree View.
Keyboard Navigation
An item that isn't mounted can't be focused. When arrow keys or typeahead move the highlight to an item off screen, the
component calls scrollToIndexFn first, and the virtualizer scrolls that item into view and mounts it. Without
scrollToIndexFn, the highlight still moves but the list doesn't follow it.
Typeahead searches the collection, not the DOM, so it finds items that haven't been rendered.
Accessibility
ListVirtualizer.Item sets aria-posinset and aria-setsize on each item, so a screen reader announces "12 of 10,000"
even though only a handful of items exist in the DOM. Key items by their value, not their index, so an item keeps its
identity when the list is filtered.
Sizing the List
The list needs a height to scroll within. In a popover, setting --total-size from getTotalSize() lets the list
shrink to fit a short result and stop at a maximum height for a long one:
.list {
height: var(--total-size);
max-height: 300px;
overflow: auto;
}
Filtering
When a filter changes the collection, update the virtualizer's count to match.
useListVirtualizer creates the virtualizer once, so pass the new count with updateOptions from an effect. Calling it
while rendering updates other components mid-render, and React warns about it.
const count = collection.size
useLayoutEffect(() => {
virtualizer.updateOptions({ count })
}, [virtualizer, count])
To start a new search at the top, call virtualizer.scrollToIndex(0) when the filter changes.
Items of Different Heights
The virtualizer positions items using estimatedSize until it has measured them. When items vary in height, set
measure on ListVirtualizer.Item so each one is measured after it renders. Keep estimatedSize close to the average
height so the scrollbar doesn't jump.
Use the virtualizer's gap, paddingStart and paddingEnd options for spacing. Margins on items aren't included in
their measured size, so items drift out of place.
import { ListVirtualizer, useListVirtualizer } from '@ark-ui/react/virtualizer'
import styles from './virtualizer.module.css'
const items = Array.from({ length: 10000 }, (_, index) => ({
title: `Item ${index + 1}`,
lines: Array.from({ length: (index % 4) + 1 }, (_, line) => `Line ${line + 1} of item ${index + 1}`),
}))
export const DynamicSize = () => {
const virtualizer = useListVirtualizer({
count: items.length,
estimatedSize: () => 64,
})
return (
<ListVirtualizer.Root value={virtualizer} className={styles.Root}>
<ListVirtualizer.Content>
{virtualizer.getVirtualItems().map((item) => (
<ListVirtualizer.Item
key={item.key}
item={item}
measure
className={`${styles.ItemDynamic} ${item.index % 2 ? styles.ItemAlt : ''}`}
>
<strong>{items[item.index].title}</strong>
{items[item.index].lines.map((line) => (
<p key={line}>{line}</p>
))}
</ListVirtualizer.Item>
))}
</ListVirtualizer.Content>
</ListVirtualizer.Root>
)
}
Loading More as You Scroll
To load a long list in pages, fetch the next page when the user scrolls near the end. The example pairs the virtualizer with useAsyncList, which tracks the loaded items, the cursor for the next page and whether a request is in flight, so a second request never starts while one is loading.
import { useAsyncList } from '@ark-ui/react/collection'
import { ListVirtualizer, useListVirtualizer } from '@ark-ui/react/virtualizer'
import { useEffect, useLayoutEffect } from 'react'
import styles from './virtualizer.module.css'
const PAGE_SIZE = 50
const TOTAL = 500
const loadPage = async ({ cursor }: { cursor?: string }) => {
const start = Number(cursor ?? 0)
await new Promise((resolve) => setTimeout(resolve, 600))
const items = Array.from({ length: Math.min(PAGE_SIZE, TOTAL - start) }, (_, index) => `Item ${start + index + 1}`)
const next = start + items.length
return { items, cursor: next < TOTAL ? String(next) : undefined }
}
export const InfiniteLoading = () => {
const list = useAsyncList<string>({ autoReload: true, load: loadPage })
const virtualizer = useListVirtualizer({ count: list.items.length, estimatedSize: () => 48 })
const count = list.items.length
useLayoutEffect(() => {
virtualizer.updateOptions({ count })
}, [virtualizer, count])
const virtualItems = virtualizer.getVirtualItems()
const lastIndex = virtualItems[virtualItems.length - 1]?.index ?? 0
const nearEnd = lastIndex >= list.items.length - 10
useEffect(() => {
if (nearEnd && list.cursor !== undefined && !list.isLoading) list.loadMore()
}, [nearEnd, list])
return (
<div className="stack">
<ListVirtualizer.Root value={virtualizer} className={styles.Root}>
<ListVirtualizer.Content>
{virtualItems.map((item) => (
<ListVirtualizer.Item
key={item.key}
item={item}
className={`${styles.Item} ${item.index % 2 ? styles.ItemAlt : ''}`}
>
{list.items[item.index]}
</ListVirtualizer.Item>
))}
</ListVirtualizer.Content>
</ListVirtualizer.Root>
<output className={styles.Meta}>
{list.isLoading ? 'Loading moreโฆ' : `${list.items.length} of ${TOTAL} loaded`}
</output>
</div>
)
}
React reads the last rendered item while rendering and loads the next page from an effect. Loading from onRangeChange
would set state while the virtualizer is updating.
Inside Popovers, Dialogs and Tabs
The virtualizer measures its container, and re-measures when the container resizes. A list inside a closed popover or a
hidden tab measures zero and renders nothing, then fills in as soon as it becomes visible. Set
observeScrollElementSize: false to turn off the resize tracking.
Server Rendering and Tests
The server, jsdom and happy-dom have no layout, so the container measures zero and no items render. Pass initialRect
with the size you expect the container to have, and the virtualizer renders the first window of items before it can
measure:
useListVirtualizer({ count: items.length, estimatedSize: () => 48, initialRect: { width: 320, height: 400 } })
Performance
- Raise
overscan(default3) if fast scrolling shows blank space; lower it if items are expensive to render. - Keep item components light. Every scroll mounts new ones.
- Prefer a fixed
estimatedSizewhen every item is the same height. Measuring has a cost.