Accessibility
This guide builds a grid that a screen reader announces as "item 4,200 of 10,000", and that keyboard users can move through with the arrow keys.
A list of 10,000 items keeps only a few hundred of them in the DOM, so a screen reader that counts elements reports a few hundred items. The grid does not add ARIA roles or focus handling. Instead, it gives you each item's index and the primitives to compute its row and column, so you can add both yourself.
The ARIA patterns reference explains why this guide uses list and listitem, and which other roles fit the grid.
Add list roles
The samples in this guide use a list of 10,000 items held in memory. The grid uses the scroll container, column tracks and probe from Sizing and direction.
<script setup lang="ts">
import { createPageProvider } from "vvirtual-grid/vue";
const items = Array.from({ length: 10000 }, (_, i) => ({ title: `Item ${i + 1}` }));
const pageProvider = createPageProvider(items);
</script>
Add role="list" to <VirtualGrid>, role="listitem" and :data-index="index" to the root element of the default slot. The data-index attribute lets the focus step find the item's element. The probe slot has no role, because the grid hides it and a screen reader never announces it.
<template>
<div class="h-96 overflow-y-auto">
<VirtualGrid
role="list"
:length="items.length"
:page-size="40"
:page-provider="pageProvider"
class="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-2"
>
<template #default="{ item, index, style }">
<div :style="style" :data-index="index" role="listitem" class="h-16">
{{ item.title }}
</div>
</template>
</VirtualGrid>
</div>
</template>
The scroll container is the div. Do not set overflow or a height on <VirtualGrid> itself. The grid sets its own height when it computes the layout, and that overrides any height you set.
Set the size and position of each item
Add aria-setsize with the number of items in the list and aria-posinset with the item's position to the root element of the default slot. aria-posinset counts from 1 and index counts from 0, so add 1.
<template #default="{ item, index, style }">
<div
:style="style"
:data-index="index"
role="listitem"
:aria-setsize="items.length"
:aria-posinset="index + 1"
class="h-16"
>
{{ item.title }}
</div>
</template>
Mark placeholders as busy
Add the placeholder slot with the same role, aria-setsize and aria-posinset as the default slot, and add aria-busy="true". A placeholder has no content yet, and aria-busy tells the screen reader the content is still loading.
<template #placeholder="{ index, style }">
<div
:style="style"
:data-index="index"
role="listitem"
aria-busy="true"
:aria-setsize="items.length"
:aria-posinset="index + 1"
class="h-16"
/>
</template>
A screen reader now announces the item at index 4199 as position 4,200 of 10,000, although only a few hundred items exist in the DOM. The exact wording varies by screen reader and browser. See ARIA examples for a runnable version.
Keep the focused index in a ref
Store the index of the focused item in a ref, not the focused element. Items leave the DOM when the user scrolls away, so an element you stored would be removed.
Give tabindex="0" to the item whose index equals the ref, and tabindex="-1" to every other item. This pattern is called a roving tabindex. The Tab key reaches the one item with tabindex="0", and your code moves the focus between items.
Add the two refs to the script from the first step, add ref="grid" to <VirtualGrid>, and add the tabindex to the default slot. The placeholder slot gets no tabindex, because a placeholder cannot take focus.
<script setup lang="ts">
import { ref, useTemplateRef } from "vue";
const grid = useTemplateRef("grid");
const focused = ref(0);
</script>
<template #default="{ item, index, style }">
<div
:style="style"
:data-index="index"
role="listitem"
:tabindex="index === focused ? 0 : -1"
:aria-setsize="items.length"
:aria-posinset="index + 1"
class="h-16"
>
{{ item.title }}
</div>
</template>
aria-activedescendant at an item element. The grid removes that element when the item scrolls out of the rendered window, and the attribute then points at nothing. Move real focus with the roving tabindex from Keep the focused index in a ref.tabindex="0", and Tab cannot enter the grid. The same happens while the item at the focused index is still a placeholder, because placeholders get no tabindex. Scroll back to the item, or wait for it to load, and Tab reaches the grid again.Scroll to the new item and focus it
Most arrow presses move to an item the grid has already rendered. Scroll that item into view with scrollIntoView and block: "nearest", which scrolls only as far as needed and does nothing when the item is fully visible. Setting scrollTo on every press would move the item to the top of the grid each time.
Use the grid's scrollTo prop only when the item is not in the DOM. Keep the index for it in a second ref, scrollTarget. The grid scrolls there and renders the item, so focus it after the grid renders it.
moveTo sets the index and marks that a focus is pending. focusFocused runs on the next tick and looks for the item element. If it finds none, it sets scrollTarget and waits for the grid to render. If it finds one, it clears scrollTarget, scrolls the element into view and focuses it. pendingFocus is true from the key press until the item has focus. Without it, focusFocused would also run when the user scrolls with the mouse and pull focus into the grid.
Add :scroll-to="scrollTarget", scroll-behavior="auto" and @render="focusFocused" to <VirtualGrid>. With scroll-behavior="auto", the grid scrolls immediately instead of animating, so focus does not wait for the scroll animation.
import { nextTick, ref } from "vue";
const scrollTarget = ref<number>();
let pendingFocus = false;
function moveTo(index: number) {
focused.value = index;
pendingFocus = true;
nextTick(focusFocused);
}
function focusFocused() {
if (!pendingFocus) return;
const element = grid.value?.$el.querySelector<HTMLElement>(
`:scope > [data-index="${focused.value}"]`,
);
if (!element) {
scrollTarget.value = focused.value;
return;
}
scrollTarget.value = undefined;
element.scrollIntoView({ block: "nearest", inline: "nearest" });
element.focus({ preventScroll: true });
if (document.activeElement === element) pendingFocus = false;
}
preventScroll stops the browser from scrolling a second time, because scrollIntoView already did. Clearing scrollTarget stops the grid from scrolling back to an old index when it is resized. The grid may not emit render when the new item was already rendered, so moveTo also calls focusFocused on the next tick.
Listen for arrow keys on the grid
Add @keydown="onKeydown" to <VirtualGrid>. Key events bubble up from the focused item, so one listener on the grid receives them all.
Map each arrow key to a change of row or column. Convert the focused index to a cell with getItemCell, change the cell, and convert it back with getIndexByCell. Both read a reusable query object, so create it once outside the handler.
import { createCellQuery, getIndexByCell, getItemCell } from "vvirtual-grid/primitives";
const cellQuery = createCellQuery();
function onKeydown(event: KeyboardEvent) {
const layout = grid.value?.measurement;
if (!layout) return;
cellQuery.layout = layout;
cellQuery.index = focused.value;
getItemCell(cellQuery);
const { cell } = cellQuery;
if (event.key === "ArrowDown") cell.row += 1;
else if (event.key === "ArrowUp") cell.row -= 1;
else if (event.key === "ArrowRight") cell.column += 1;
else if (event.key === "ArrowLeft") cell.column -= 1;
else return;
if (cell.row < 0 || cell.column < 0 || cell.column >= layout.columns) return;
const next = getIndexByCell(cellQuery);
if (next < 0 || next >= items.length) return;
event.preventDefault();
moveTo(next);
}
The checks before getIndexByCell stop the focus at the first row, the first and last column, and the end of the list.
cell.column >= layout.columns is correct only for the default grid-auto-flow: row. With grid-auto-flow: column the items fill a fixed number of rows and the columns grow with the list. Replace it with cell.row >= layout.rows there, or read layout.flow and pick the check that matches.The template combines the bindings from this step with those from Keep the focused index in a ref and Scroll to the new item and focus it. The slots stay as they are.
<template>
<div class="h-96 overflow-y-auto">
<VirtualGrid
ref="grid"
role="list"
:length="items.length"
:page-size="40"
:page-provider="pageProvider"
:scroll-to="scrollTarget"
scroll-behavior="auto"
@keydown="onKeydown"
@render="focusFocused"
class="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-2"
>
<!-- the default and placeholder slots -->
</VirtualGrid>
</div>
</template>
Check it with a screen reader
Open the page with VoiceOver on macOS (Cmd + F5) or NVDA on Windows. VoiceOver is the screen reader built into macOS. NVDA is a screen reader for Windows that costs nothing to download and use.
- Press Tab until the focus reaches the grid. The screen reader announces the first item with its role and its position, 1 of 10,000.
- Press the right arrow. The focus moves to the next item and the screen reader reads it. Press the down arrow. The focus moves to the item one row below, in the same column, and the announced position climbs by however many items fit in a row.
- Press the down arrow many times. The grid scrolls, and the announced position keeps climbing, for example to 4,200 of 10,000.
The wording and the order of the announcement vary by screen reader and browser. ARIA examples has runnable versions of each step and instructions for testing without a screen reader.