Roles, States and Values

+15 Mana ✨

A custom toggle built from a Pressable and a Text has a label, but a label doesn't say that the control switches something on and off, which position it is in, or how full the progress bar next to it is. Role, state and value props carry that information, and the screen reader speaks it along with the label.

Key terms

  • accessibilityRole: tells assistive technology what kind of control a component is, such as button or adjustable.
  • role: a prop for the same purpose that takes precedence over accessibilityRole.
  • accessibilityState: an object with the component's current state in the optional fields disabled, selected, checked, busy and expanded.
  • accessibilityValue: the component's current value, as min, max and now for a range or as a text description.
  • aria- props*: aliases such as aria-checked and aria-valuenow for the fields of the props above.

What the control is

accessibilityRole communicates the purpose of a component. These values cover most custom controls:

  • button for an element that should be treated as a button.
  • link for an element that opens an external URL or web page. Navigation and actions inside the app use button instead.
  • header for an element that heads a content section.
  • togglebutton for an on/off button, combined with the checked field of accessibilityState to say which position it is in.
  • adjustable for an element whose value can be adjusted, such as a slider.

role takes precedence when both props are set. React Native builds the native traits from role when it is set, and from accessibilityRole only when there is no role. The two lists don't share every name: a section header is heading in the role list and header in the accessibilityRole list.

slider and adjustable on iOS

role="slider" is meant for elements that can be adjusted, but on iOS it maps to no accessibility trait. Only the string adjustable produces the Adjustable trait. Since role wins, writing role="slider" next to accessibilityRole="adjustable" drops the Adjustable trait on iOS. For a control VoiceOver users should adjust, set accessibilityRole="adjustable" and leave role off.

What state it is in

accessibilityState is an object, and each of its fields is optional. disabled and selected say what their names suggest. expanded tells whether an expandable element is expanded or collapsed, and busy whether it is currently busy. checked takes a boolean, or the string "mixed" for a checkbox that is partly checked.

The TypeScript type of checked is boolean | "mixed" | undefined. A count such as checked: quantity fails type checking and isn't a valid state for assistive technology either.

Pressable fills in one field for you: its disabled prop is copied into accessibilityState.disabled, so you don't repeat it in accessibilityState.

Where a value sits

accessibilityValue describes the current value. For a range, such as a slider or a progress bar, give it min, max and now. All three are numbers meant to be integers, and min and max are required as soon as you set now. When a number doesn't describe the value well, set text. It overrides min, now and max when present.

The aria-* spellings

Most of these props have a web-style alias:

  • aria-label for accessibilityLabel
  • aria-busy, aria-checked, aria-disabled, aria-expanded and aria-selected for the matching accessibilityState fields
  • aria-valuemin, aria-valuemax, aria-valuenow and aria-valuetext for the accessibilityValue fields

On a View, the aria version wins when both spellings are set. React Native merges each aria-* value into accessibilityState or accessibilityValue over the matching field, and copies aria-label into accessibilityLabel. A view with aria-checked={true} and accessibilityState={{checked: false}} ends up checked. Both aria-checked and aria-disabled default to false, and aria-disabled marks an element that can still be perceived but is disabled.

Because of that precedence, mixing the two spellings on one component makes its announced state harder to predict. Pick one style per codebase and the question doesn't come up.

A settings block with all three

The OfflineSettings component in the code example below the lesson gives each element what it needs:

  • The title is a Text with role="heading".
  • The toggle pairs accessibilityRole="togglebutton" with accessibilityState={{checked: offlineEnabled, busy: syncing}}, so assistive technology knows whether offline saving is on and whether a sync is running.
  • The storage bar has no text at all. It gets accessible={true}, the label "Storage used" and accessibilityValue={{min: 0, max: 100, now: usedPercent}}.

With aria spellings, the toggle's state would read aria-checked={offlineEnabled} and aria-busy={syncing}, and the bar would take aria-valuemin, aria-valuemax and aria-valuenow.

Code Examples

tsx
import {Pressable, Text, View} from 'react-native';

type Props = {
  offlineEnabled: boolean;
  syncing: boolean;
  usedPercent: number;
  onToggleOffline: () => void;
};

export function OfflineSettings({
  offlineEnabled,
  syncing,
  usedPercent,
  onToggleOffline,
}: Props) {
  return (
    <View>
      <Text role="heading">Offline</Text>
      <Pressable
        onPress={onToggleOffline}
        accessibilityRole="togglebutton"
        accessibilityState={{checked: offlineEnabled, busy: syncing}}>
        <Text>Save episodes for offline listening</Text>
      </Pressable>
      <View
        accessible={true}
        accessibilityLabel="Storage used"
        accessibilityValue={{min: 0, max: 100, now: usedPercent}}
        style={{width: 200, height: 4, backgroundColor: '#d4d4d8'}}>
        <View
          style={{width: usedPercent * 2, height: 4, backgroundColor: '#2563eb'}}
        />
      </View>
    </View>
  );
}
✓ Completed