Basic examples
Screen reader announcements example
Show the text that the grid live region reads after a check or a filter, with announceResults turned on.
The grid reads changes to a screen reader through one live region. In the demo, a line above the grid copies the last text of that region, so you can follow the announcements without a screen reader. The grid sets announceResults, so a filter also reads the row count.
Check rows with the markers or Ctrl+Shift+Space, or filter a column. The line below shows the text a screen reader hears from the grid's live region.
pnpm dlx shadcn@latest add @gridcn/data-grid-announcements-demo
What the demo shows
- A check or an uncheck with the row markers reads the number of checked rows, for example "3 rows checked".
- A filter from the header filter reads the number of rows of the view, for example "42 rows". A filter that keeps the same rows stays quiet. A sort that changes the row order reads the count too. A view with zero rows reads "No results".
- The region waits 500 ms after the last change, so a fast series of checks reads one count.
- The live region carries the
data-grid-live-regionattribute. The demo watches it with aMutationObserverand hides its copy from screen readers witharia-hidden.
The grid prop turns on the result counts:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
const rows: Row[] = [{ id: "1", name: "Ada" }];
export function AnnouncedGrid() {
return (
<DataGrid
defaultData={rows}
columns={columns}
rowMarkers="checkbox"
announceResults
/>
);
}Related
- Screen reader support: every change that the live region reads
- Live region messages: the stale view, refused save, and load result messages
- Checked rows: the checked-row count