Render Only the Visible Rows
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Show a list of 10,000 rows without putting 10,000 elements on the page (list virtualisation). Props: items, a list of strings (default "Row 1" to "Row 10000"); rowHeight, each row's height in pixels (default 32); and height, the visible height in pixels (default 320). The scrolling box is an element with role="list" and aria-label="Rows", styled with that height and overflow-y "auto". Inside it, put one element as tall as all the rows together (the number of items times rowHeight), so the scrollbar is right, and place each rendered row at its index times rowHeight from the top. Keep the scroll position in state, read from the box's scroll event as event.currentTarget.scrollTop. Render only the rows at least partly inside the box, from the one at the scroll position to the last one that starts above the box's bottom edge, plus, if you like, up to 3 extra rows above and below. Each rendered row is a role="listitem" element holding just its text, with aria-posinset (its position, counting from 1) and aria-setsize (the number of items), so screen readers know where it is in the whole list.
Your code must pass
- Renders only the rows in view, not all 10,000
- Each row has aria-posinset and aria-setsize
- Scrolling renders the rows now in view
- A row only partly in view is rendered too
- Scrolling to the end renders the last rows