AccessibilityInfo
Sometimes it's useful to know whether or not the machine has a screen reader that is currently active. The AccessibilityInfo API is designed for this purpose. You can use it to query the current state of the screen reader as well as to register to be notified when the state of the screen reader changes.
The accessibility settings are read from the operating system. On macOS they come from the accessibility preferences. On Windows isScreenReaderEnabled, isReduceMotionEnabled and isDarkerSystemColorsEnabled follow the screen reader flag, the client-area animation setting and the high contrast theme, and isReduceTransparencyEnabled follows the Transparency effects setting. On Linux they follow GNOME's gsettings keys for the screen reader, animations and high contrast; isReduceTransparencyEnabled is always false there. On Windows and Linux the settings are polled every two seconds and the change events fire when a value differs, so a change can take up to that long to show. The remaining settings (isBoldTextEnabled, isGrayscaleEnabled, isInvertColorsEnabled) report false there. announceForAccessibility has no effect on Windows and Linux.
Example
import { useState, useEffect } from 'react';
import { AccessibilityInfo, StyleSheet, Text, View } from 'react-native';
const App = () => {
const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false);
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false);
useEffect(() => {
const reduceMotionChangedSubscription = AccessibilityInfo.addEventListener(
'reduceMotionChanged',
isReduceMotionEnabled => {
setReduceMotionEnabled(isReduceMotionEnabled);
},
);
const screenReaderChangedSubscription = AccessibilityInfo.addEventListener(
'screenReaderChanged',
isScreenReaderEnabled => {
setScreenReaderEnabled(isScreenReaderEnabled);
},
);
AccessibilityInfo.isReduceMotionEnabled().then(isReduceMotionEnabled => {
setReduceMotionEnabled(isReduceMotionEnabled);
});
AccessibilityInfo.isScreenReaderEnabled().then(isScreenReaderEnabled => {
setScreenReaderEnabled(isScreenReaderEnabled);
});
return () => {
reduceMotionChangedSubscription.remove();
screenReaderChangedSubscription.remove();
};
}, []);
return (
<View style={styles.container}>
<Text style={styles.status}>
The reduce motion is {reduceMotionEnabled ? 'enabled' : 'disabled'}.
</Text>
<Text style={styles.status}>
The screen reader is {screenReaderEnabled ? 'enabled' : 'disabled'}.
</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
status: {
margin: 30,
},
});
export default App;
Reference
Methods
addEventListener()
static addEventListener(
eventName: AccessibilityChangeEventName | AccessibilityAnnouncementEventName,
handler: (
event: AccessibilityChangeEvent | AccessibilityAnnouncementFinishedEvent,
) => void,
): EventSubscription;
Add an event handler. Supported events:
| Event name | Description |
|---|---|
announcementFinished | Fires when the screen reader has finished making an announcement. The argument to the event handler is a dictionary with these keys:
|
boldTextChanged | Fires when the state of the bold text toggle changes. The argument to the event handler is a boolean. The boolean is true when bold text is enabled and false otherwise. |
grayscaleChanged | Fires when the state of the gray scale toggle changes. The argument to the event handler is a boolean. The boolean is true when a gray scale is enabled and false otherwise. |
invertColorsChanged | Fires when the state of the invert colors toggle changes. The argument to the event handler is a boolean. The boolean is true when invert colors is enabled and false otherwise. |
reduceMotionChanged | Fires when the state of the reduce motion toggle changes. The argument to the event handler is a boolean. The boolean is true when a reduce motion is enabled (or when "Transition Animation Scale" in "Developer options" is "Animation off") and false otherwise. |
reduceTransparencyChanged | Fires when the state of the reduce transparency toggle changes. The argument to the event handler is a boolean. The boolean is true when reduce transparency is enabled and false otherwise. |
screenReaderChanged | Fires when the state of the screen reader changes. The argument to the event handler is a boolean. The boolean is true when a screen reader is enabled and false otherwise. |
announceForAccessibility()
static announceForAccessibility(announcement: string);
Post a string to be announced by the screen reader.
On macOS the system speaks it. On Windows and Linux the window exposes it as a live region that Narrator or Orca reads when it changes. announceForAccessibilityWithOptions treats the low priority as polite and the others as assertive; queue has no effect there. announcementFinished is not sent on Windows or Linux. These have not been checked against a real screen reader.
announceForAccessibilityWithOptions()
static announceForAccessibilityWithOptions(
announcement: string,
options: {
queue?: boolean;
priority?: 'low' | 'default' | 'high';
},
);
Post a string to be announced by the screen reader with modification options. By default announcements will interrupt any existing speech, but they can be queued behind existing speech by setting queue to true in the options object. An optional priority can also be specified on iOS.
Parameters:
| Name | Type | Description |
|---|---|---|
| announcement Required | string | The string to be announced |
| options Required | object | Options object with the following keys:
|
isInvertColorsEnabled() Desktop
static isInvertColorsEnabled(): Promise<boolean>;
Query whether invert colors is currently enabled. Returns a promise which resolves to a boolean. The result is true when invert colors is enabled and false otherwise.
isBoldTextEnabled() Desktop
static isBoldTextEnabled(): Promise<boolean>:
Query whether a bold text is currently enabled. Returns a promise which resolves to a boolean. The result is true when bold text is enabled and false otherwise.
isDarkerSystemColorsEnabled() Desktop
static isDarkerSystemColorsEnabled(): Promise<boolean>
Query whether dark system colors is currently enabled. Returns a promise which resolves to a boolean. The result is true when dark system colors is enabled and false otherwise.
isGrayscaleEnabled() Desktop
static isGrayscaleEnabled(): Promise<boolean>;
Query whether grayscale is currently enabled. Returns a promise which resolves to a boolean. The result is true when grayscale is enabled and false otherwise.
isReduceTransparencyEnabled() Desktop
static isReduceTransparencyEnabled(): Promise<boolean>;
Query whether reduce transparency is currently enabled. Returns a promise which resolves to a boolean. The result is true when a reduce transparency is enabled and false otherwise.
prefersCrossFadeTransitions() Desktop
static prefersCrossFadeTransitions(): Promise<boolean>;
Query whether reduce motion and prefer cross-fade transitions settings are currently enabled. Returns a promise which resolves to a boolean. The result is true when prefer cross-fade transitions is enabled and false otherwise.
isReduceMotionEnabled()
static isReduceMotionEnabled(): Promise<boolean>;
Query whether reduce motion is currently enabled. Returns a promise which resolves to a boolean. The result is true when reduce motion is enabled and false otherwise.
isScreenReaderEnabled()
static isScreenReaderEnabled(): Promise<boolean>;
Query whether a screen reader is currently enabled. Returns a promise which resolves to a boolean. The result is true when a screen reader is enabled and false otherwise.
isHighTextContrastEnabled() Desktop
static isHighTextContrastEnabled(): Promise<boolean>
Query whether high text contrast is currently enabled. Returns a promise which resolves to a boolean. The result is true when high text contrast is enabled and false otherwise.
🗑️ setAccessibilityFocus()
Prefer using sendAccessibilityEvent with eventType focus instead.
static setAccessibilityFocus(reactTag: number);
Set accessibility focus to a React component.
Make sure that any View you want to receive the accessibility focus has accessible={true}.
sendAccessibilityEvent()
static sendAccessibilityEvent(host: HostInstance, eventType: AccessibilityEventTypes);
Imperatively trigger an accessibility event on a React component, like changing the focused element for a screen reader.
Make sure that any View you want to receive the accessibility focus has accessible={true}.
| Name | Type | Description |
|---|---|---|
| host Required | HostInstance | The component ref to send the event to. |
| eventType Required | AccessibilityEventTypes | 'focus': move the screen reader focus to the component. |