VoiceOver on iOS and TalkBack on Android move focus from one accessibility element to the next and speak each one. Which of your views become those elements, and what gets spoken for each, depends on a handful of props. Get them wrong and a review card turns into a string of separate stops, or an icon button ends up with no text to announce.
Key terms
- accessible: a prop that, when true, makes a view discoverable by assistive technologies such as screen readers and hardware keyboards.
- accessibilityLabel: the string a screen reader speaks when the user selects the element.
- accessibilityHint: extra text about the result of an action, for when the label alone doesn't make it clear.
- alt: an
Imageprop with a text description of the image for the screen reader. - aria-hidden: a prop that hides an element and its children from assistive technologies.
Accessible views and grouping
accessible={true} marks a view as an accessibility element. The flag doesn't guarantee focus. VoiceOver doesn't allow accessibility elements nested inside each other, and TalkBack can choose to focus a parent element instead. Put the flag on the container you want read as one unit, and keep it off the views inside it.
Several components set it for you. Touchable elements are accessible by default, and Pressable passes accessible as true to its view unless you set it to false. Text differs per platform. On iOS it is accessible unless you turn it off, while on Android a Text with no accessible prop is accessible only when it has onPress or onLongPress.
The flag works at any depth. In <View><View accessible={true} /><View /></View>, accessibility focus goes to the first child only.
Where the spoken label comes from
When the user lands on an element, the screen reader speaks its accessibilityLabel. With no label set, the label is built from the element's children: their labels and text are joined with a comma and a space. A pressable row holding <Text>Lisbon</Text> and <Text>3 nights</Text> is read as "Lisbon, 3 nights".
Set the label yourself when the visible text doesn't say enough: an icon-only button has no text to collect, and a row of short fragments can read better as a sentence. An explicit accessibilityLabel replaces the built text.
accessibilityHint describes what happens when the user acts on the element. The platforms treat it differently. VoiceOver reads the hint after the label, but only if the user has turned hints on in the device's VoiceOver settings. TalkBack reads it after the label, and hints can't currently be turned off on Android. React Native hands it to TalkBack through an accessibility delegate, which it attaches only when the view also has a role (accessibilityRole or role), a state, actions, a testID, links or collection-item info. Pressable always sends a state, so its hint gets through, and a plain View with only accessible and a hint doesn't. Keep the name of the thing in the label and the outcome in the hint, so an iOS user with hints off still gets a label that makes sense.
For forms on Android, accessibilityLabelledBy points at another element's nativeID, which lets a visible Text act as the label of a TextInput.
Images and alt
Image has accessible set to false by default, so a decorative image is skipped with no extra work. An image that carries information gets an alt string. The screen reader reads it, and setting alt marks the image as accessible. On both platforms, an aria-label or accessibilityLabel on the same image takes priority over alt.
Hiding parts of the screen
Some visible content shouldn't be read, such as the screen behind an open menu. Each platform has a prop for that:
accessibilityElementsHidden(iOS) hides an element and every accessibility element inside it from VoiceOver.importantForAccessibility(Android) takesauto,yes,noorno-hide-descendants. The last value makes accessibility services ignore the component and all of its children. The default,auto, lets the system decide.
aria-hidden covers both. On a View, aria-hidden={true} sets accessibilityElementsHidden and also sets importantForAccessibility to no-hide-descendants, so one prop hides the subtree on iOS and Android. It defaults to false. On iOS there is also accessibilityViewIsModal, which works from the other side: set it on the menu and VoiceOver ignores the menu's sibling views.
Reading a review card
The ReviewCard example below puts these props together:
<View accessible={true}>around the author and the date makes them one stop. On iOS it is read as the two strings joined by a comma (for example "Ana, 12 March"). On Android it has none of the props that attach the delegate, so TalkBack builds the reading itself.- The photo has
alt={review.photoAlt}, so it is announced with that description. - The menu icon has no
alt, and its button getsaccessibilityLabel="More options"since there is no text to build a label from. The hint says what a tap does. aria-hidden={menuOpen}on the card's content hides it while the menu is open, and the menu itself, a sibling of that content, stays reachable.
Trying it on a device
Test on the real screen readers. TalkBack is turned on in Settings > Accessibility > TalkBack with the Use service switch, and Android emulators don't have TalkBack installed by default. VoiceOver isn't available in the iOS simulator. Xcode's Accessibility Inspector helps there, but a physical device gives the most accurate result.
Code Examples
import {useState} from 'react';
import {Image, Pressable, Text, View} from 'react-native';
import type {ImageSourcePropType} from 'react-native';
type Review = {
author: string;
date: string;
body: string;
photoUrl: string;
photoAlt: string;
};
type Props = {
review: Review;
menuIcon: ImageSourcePropType;
onReport: () => void;
};
export function ReviewCard({review, menuIcon, onReport}: Props) {
const [menuOpen, setMenuOpen] = useState(false);
return (
<View>
<View aria-hidden={menuOpen}>
<View accessible={true}>
<Text>{review.author}</Text>
<Text>{review.date}</Text>
</View>
<Text>{review.body}</Text>
<Image
source={{uri: review.photoUrl}}
style={{width: 160, height: 120}}
alt={review.photoAlt}
/>
<Pressable
onPress={() => setMenuOpen(true)}
accessibilityLabel="More options"
accessibilityHint="Shows actions for this review">
<Image source={menuIcon} style={{width: 24, height: 24}} />
</Pressable>
</View>
{menuOpen ? (
<Pressable
onPress={() => {
setMenuOpen(false);
onReport();
}}>
<Text>Report review</Text>
</Pressable>
) : null}
</View>
);
}