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

VirtualizerUse it for
ListVirtualizerA list that scrolls inside its own container: menus, select and combobox lists
GridVirtualizerRows and columns, like a data grid or an icon picker
WindowVirtualizerA 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:

  1. Create a virtualizer whose count is the collection's size.
  2. Render the component's list as ListVirtualizer.Root with the render prop.
  3. Map the virtual items to the component's items, each rendered as ListVirtualizer.Item.
  4. Forward the component's scrollToIndexFn to 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 (default 3) 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 estimatedSize when every item is the same height. Measuring has a cost.