Skip to content

Commit 002c1bb

Browse files
nathannewyensaisreelasyaappali
authored andcommitted
feat: Add accessibility collection support to VirtualizedList
This enables Android TalkBack to announce "item X of Y" when navigating through VirtualizedList/FlatList items. Changes: - Add AccessibilityCollectionItem type to VirtualizedListProps - Compute and pass accessibilityCollectionItem to each cell in VirtualizedList - Apply accessibilityCollectionItem prop on cell wrapper View - Add accessibilityCollection (rowCount/columnCount) to ScrollView - Add Flow types for AccessibilityCollection and AccessibilityCollectionItem - Fix FlatList multi-column grid accessibility with proper row/column indices Closes #30975
1 parent 52ff392 commit 002c1bb

7 files changed

Lines changed: 3477 additions & 4 deletions

File tree

packages/react-native/Libraries/Components/View/ViewAccessibility.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,39 @@ export type AccessibilityValue = Readonly<{
211211
text?: Stringish,
212212
}>;
213213

214-
export type AccessibilityPropsAndroid = Readonly<{
214+
/**
215+
* Accessibility collection info for screen readers.
216+
* Used to describe a collection of items (e.g., list, grid).
217+
*
218+
* @platform android
219+
*/
220+
export type AccessibilityCollection = $ReadOnly<{
221+
// The number of rows in the collection
222+
rowCount: number,
223+
// The number of columns in the collection
224+
columnCount: number,
225+
}>;
226+
227+
/**
228+
* Accessibility collection item info for screen readers.
229+
* Used to announce position in collection (e.g., "item 3 of 10").
230+
*
231+
* @platform android
232+
*/
233+
export type AccessibilityCollectionItem = $ReadOnly<{
234+
// The row index of this item in the collection
235+
rowIndex: number,
236+
// The column index of this item in the collection
237+
columnIndex: number,
238+
// The number of rows this item spans
239+
rowSpan: number,
240+
// The number of columns this item spans
241+
columnSpan: number,
242+
// Whether this item is a heading
243+
heading: boolean,
244+
}>;
245+
246+
export type AccessibilityPropsAndroid = $ReadOnly<{
215247
/**
216248
* Identifies the element that labels the element it is applied to. When the assistive technology focuses on the component with this props,
217249
* the text is read aloud. The value should should match the nativeID of the related element.
@@ -220,6 +252,22 @@ export type AccessibilityPropsAndroid = Readonly<{
220252
*/
221253
accessibilityLabelledBy?: ?string | ?Array<string>,
222254

255+
/**
256+
* Describes a collection of items for screen readers.
257+
* Used on container elements like lists and grids.
258+
*
259+
* @platform android
260+
*/
261+
accessibilityCollection?: ?AccessibilityCollection,
262+
263+
/**
264+
* Describes the position of an item within a collection for screen readers.
265+
* Enables TalkBack to announce "item X of Y" when navigating.
266+
*
267+
* @platform android
268+
*/
269+
accessibilityCollectionItem?: ?AccessibilityCollectionItem,
270+
223271
/**
224272
* Identifies the element that labels the element it is applied to. When the assistive technology focuses on the component with this props,
225273
* the text is read aloud. The value should should match the nativeID of the related element.

packages/react-native/Libraries/Lists/FlatList.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,21 @@ class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
646646
return (
647647
<View style={StyleSheet.compose(styles.row, columnWrapperStyle)}>
648648
{item.map((it, kk) => {
649+
// Compute accessibility collection item info for grid layouts
650+
// rowIndex is the row index, columnIndex is position within the row
651+
const accessibilityCollectionItem = {
652+
rowIndex: index,
653+
columnIndex: kk,
654+
rowSpan: 1,
655+
columnSpan: 1,
656+
heading: false,
657+
};
649658
const element = render({
650659
// $FlowFixMe[incompatible-type]
651660
item: it,
652661
index: index * cols + kk,
653662
separators: info.separators,
663+
accessibilityCollectionItem,
654664
});
655665
return element != null ? (
656666
<React.Fragment key={kk}>{element}</React.Fragment>

0 commit comments

Comments
 (0)