Sub topic 12
Accessibility and Browser Find-in-Page
This chapter assumes you can build the fixed-height list from earlier chapters and know basic ARIA (the attributes that describe roles and states to assistive technology). Virtualization removes rows from the DOM. Screen readers and the browser's search only see what is in the DOM, so removing rows has side effects you need to handle.
What virtualization breaks
Three things stop working as users expect:
- Find-in-page. Ctrl+F searches the rendered DOM. A row that is not mounted cannot be found.
- Screen reader context. A screen reader counts the rows it can see. With 20 rows mounted out of 10,000, it announces a list of 20.
- Keyboard focus. If the focused row scrolls out of range, React unmounts it and focus falls back to the document body.
Tell assistive technology the real size
ARIA lets you state the full list size and each row's real position. Put role="list" on the container and role="listitem" on rows, then add aria-setsize and aria-posinset to each row. Both values are 1-based.
import { useState } from "react";
type AccessibleListProps = {
items: string[];
height: number;
rowHeight: number;
overscan?: number;
};
export function AccessibleList({ items, height, rowHeight, overscan = 3 }: AccessibleListProps) {
const [scrollTop, setScrollTop] = useState(0);
const first = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
const last = Math.min(
items.length - 1,
Math.ceil((scrollTop + height) / rowHeight) + overscan
);
const rows = [];
for (let i = first; i <= last; i++) {
rows.push(
<div
key={i}
role="listitem"
aria-setsize={items.length}
aria-posinset={i + 1}
style={{
position: "absolute",
top: i * rowHeight,
left: 0,
right: 0,
height: rowHeight,
}}
>
{items[i]}
</div>
);
}
return (
<div
role="list"
aria-label="Results"
tabIndex={0}
style={{ height, overflow: "auto" }}
onScroll={(e) => setScrollTop(e.currentTarget.scrollTop)}
>
<div style={{ height: items.length * rowHeight, position: "relative" }}>
{rows}
</div>
</div>
);
}tabIndex={0} makes the scroll container focusable, so keyboard users can scroll it with the arrow keys and Page Down. Without it, some browsers do not let keyboard users scroll an overflow region that has no focusable children.
Keep the DOM order of rows the same as the data order. Libraries render rows in index order, but if you sort mounted rows by anything else, screen reader reading order breaks.
Keeping focus alive
If rows contain buttons or links, a focused row can scroll out of range and unmount. Two common fixes:
- Track the focused index in state and always include it in the rendered range, even when it is outside the visible window.
- Use a single focus target, the container, with
aria-activedescendantpointing at the active row'sid. The row must still be mounted for this to work, so combine it with fix 1.
Find-in-page
You cannot make native Ctrl+F search unmounted rows. Your options are:
| Option | Trade-off |
|---|---|
| Provide your own search box that filters or jumps to a row | Best experience, and you control it. It needs extra UI and code. |
| Use `content-visibility: auto` instead of virtualization | Native find works, but all rows stay in the DOM. |
| Paginate | Find works within a page only. |
Tell users clearly when the browser search will not cover the whole list. A search input that scrolls to the matching index uses the programmatic scrolling from the previous chapter.
Testing
Check these by hand:
- Tab to the list, then scroll with the arrow keys, Page Down, and End.
- Turn on a screen reader and confirm it announces a position such as "item 4,200 of 10,000".
- Focus a button in a row, scroll far away, and confirm focus is not lost.
Exercise
A virtualized list of 5,000 products mounts 25 rows at a time. A screen reader announces "list, 25 items". Which two attributes fix this, and where do they go?
Show answer
Add aria-setsize with the full count (5000) and aria-posinset with the 1-based index (i + 1) to each mounted row. Keep role="list" on the container and role="listitem" on the rows.
Exercise
Why can't Ctrl+F find text in a row that is scrolled far off-screen, and what is one way to still let users search?
Show answer
The row is not in the DOM, because the virtual list unmounts it, and Ctrl+F searches only the DOM. Add your own search input that finds the matching index in the data and scrolls to it.