Skip to content

Commit d3b5cb0

Browse files
Luna Weifacebook-github-bot
authored andcommitted
Add a pseudo flow declaration for AccessibilityInfo
Summary: Changelog: [Internal] - Create a type declaration for AccsesibilityInfo for clearer signal when our public API types change Reviewed By: yungsters Differential Revision: D38712552 fbshipit-source-id: cc7c727d41fb03ca714cb05fd10dc32038374fd0
1 parent 7ccb292 commit d3b5cb0

File tree

2 files changed

+201
-1
lines changed

2 files changed

+201
-1
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
12+
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
13+
import type {ElementRef} from 'react';
14+
15+
// Events that are only supported on Android.
16+
type AccessibilityEventDefinitionsAndroid = {
17+
accessibilityServiceChanged: [boolean],
18+
};
19+
20+
// Events that are only supported on iOS.
21+
type AccessibilityEventDefinitionsIOS = {
22+
announcementFinished: [{announcement: string, success: boolean}],
23+
boldTextChanged: [boolean],
24+
grayscaleChanged: [boolean],
25+
invertColorsChanged: [boolean],
26+
reduceTransparencyChanged: [boolean],
27+
};
28+
29+
type AccessibilityEventDefinitions = {
30+
...AccessibilityEventDefinitionsAndroid,
31+
...AccessibilityEventDefinitionsIOS,
32+
change: [boolean], // screenReaderChanged
33+
reduceMotionChanged: [boolean],
34+
screenReaderChanged: [boolean],
35+
};
36+
37+
type AccessibilityEventTypes = 'click' | 'focus';
38+
/**
39+
* Sometimes it's useful to know whether or not the device has a screen reader
40+
* that is currently active. The `AccessibilityInfo` API is designed for this
41+
* purpose. You can use it to query the current state of the screen reader as
42+
* well as to register to be notified when the state of the screen reader
43+
* changes.
44+
*
45+
* See https://reactnative.dev/docs/accessibilityinfo
46+
*/
47+
export interface AccessibilityInfo {
48+
/**
49+
* Query whether bold text is currently enabled.
50+
*
51+
* Returns a promise which resolves to a boolean.
52+
* The result is `true` when bold text is enabled and `false` otherwise.
53+
*
54+
* See https://reactnative.dev/docs/accessibilityinfo#isBoldTextEnabled
55+
*/
56+
isBoldTextEnabled: () => Promise<boolean>;
57+
58+
/**
59+
* Query whether grayscale is currently enabled.
60+
*
61+
* Returns a promise which resolves to a boolean.
62+
* The result is `true` when grayscale is enabled and `false` otherwise.
63+
*
64+
* See https://reactnative.dev/docs/accessibilityinfo#isGrayscaleEnabled
65+
*/
66+
isGrayscaleEnabled: () => Promise<boolean>;
67+
68+
/**
69+
* Query whether inverted colors are currently enabled.
70+
*
71+
* Returns a promise which resolves to a boolean.
72+
* The result is `true` when invert color is enabled and `false` otherwise.
73+
*
74+
* See https://reactnative.dev/docs/accessibilityinfo#isInvertColorsEnabled
75+
*/
76+
isInvertColorsEnabled: () => Promise<boolean>;
77+
78+
/**
79+
* Query whether reduced motion is currently enabled.
80+
*
81+
* Returns a promise which resolves to a boolean.
82+
* The result is `true` when a reduce motion is enabled and `false` otherwise.
83+
*
84+
* See https://reactnative.dev/docs/accessibilityinfo#isReduceMotionEnabled
85+
*/
86+
isReduceMotionEnabled: () => Promise<boolean>;
87+
88+
/**
89+
* Query whether reduced transparency is currently enabled.
90+
*
91+
* Returns a promise which resolves to a boolean.
92+
* The result is `true` when a reduce transparency is enabled and `false` otherwise.
93+
*
94+
* See https://reactnative.dev/docs/accessibilityinfo#isReduceTransparencyEnabled
95+
*/
96+
isReduceTransparencyEnabled: () => Promise<boolean>;
97+
98+
/**
99+
* Query whether a screen reader is currently enabled.
100+
*
101+
* Returns a promise which resolves to a boolean.
102+
* The result is `true` when a screen reader is enabled and `false` otherwise.
103+
*
104+
* See https://reactnative.dev/docs/accessibilityinfo#isScreenReaderEnabled
105+
*/
106+
isScreenReaderEnabled: () => Promise<boolean>;
107+
108+
/**
109+
* Query whether Accessibility Service is currently enabled.
110+
*
111+
* Returns a promise which resolves to a boolean.
112+
* The result is `true` when any service is enabled and `false` otherwise.
113+
*
114+
* @platform android
115+
*
116+
* See https://reactnative.dev/docs/accessibilityinfo/#isaccessibilityserviceenabled-android
117+
*/
118+
isAccessibilityServiceEnabled: () => Promise<boolean>;
119+
120+
/**
121+
* Add an event handler. Supported events:
122+
*
123+
* - `reduceMotionChanged`: Fires when the state of the reduce motion toggle changes.
124+
* The argument to the event handler is a boolean. The boolean is `true` when a reduce
125+
* motion is enabled (or when "Transition Animation Scale" in "Developer options" is
126+
* "Animation off") and `false` otherwise.
127+
* - `screenReaderChanged`: Fires when the state of the screen reader changes. The argument
128+
* to the event handler is a boolean. The boolean is `true` when a screen
129+
* reader is enabled and `false` otherwise.
130+
*
131+
* These events are only supported on iOS:
132+
*
133+
* - `boldTextChanged`: iOS-only event. Fires when the state of the bold text toggle changes.
134+
* The argument to the event handler is a boolean. The boolean is `true` when a bold text
135+
* is enabled and `false` otherwise.
136+
* - `grayscaleChanged`: iOS-only event. Fires when the state of the gray scale toggle changes.
137+
* The argument to the event handler is a boolean. The boolean is `true` when a gray scale
138+
* is enabled and `false` otherwise.
139+
* - `invertColorsChanged`: iOS-only event. Fires when the state of the invert colors toggle
140+
* changes. The argument to the event handler is a boolean. The boolean is `true` when a invert
141+
* colors is enabled and `false` otherwise.
142+
* - `reduceTransparencyChanged`: iOS-only event. Fires when the state of the reduce transparency
143+
* toggle changes. The argument to the event handler is a boolean. The boolean is `true`
144+
* when a reduce transparency is enabled and `false` otherwise.
145+
* - `announcementFinished`: iOS-only event. Fires when the screen reader has
146+
* finished making an announcement. The argument to the event handler is a
147+
* dictionary with these keys:
148+
* - `announcement`: The string announced by the screen reader.
149+
* - `success`: A boolean indicating whether the announcement was
150+
* successfully made.
151+
*
152+
* See https://reactnative.dev/docs/accessibilityinfo#addeventlistener
153+
*/
154+
addEventListener<K: $Keys<AccessibilityEventDefinitions>>(
155+
eventName: K,
156+
handler: (...$ElementType<AccessibilityEventDefinitions, K>) => void,
157+
): EventSubscription;
158+
159+
/**
160+
* Set accessibility focus to a React component.
161+
*
162+
* See https://reactnative.dev/docs/accessibilityinfo#setaccessibilityfocus
163+
*/
164+
setAccessibilityFocus: (reactTag: number) => void;
165+
166+
/**
167+
* Send a named accessibility event to a HostComponent.
168+
*/
169+
sendAccessibilityEvent: (
170+
handle: ElementRef<HostComponent<mixed>>,
171+
eventType: AccessibilityEventTypes,
172+
) => void;
173+
174+
/**
175+
* Post a string to be announced by the screen reader.
176+
*
177+
* See https://reactnative.dev/docs/accessibilityinfo#announceforaccessibility
178+
*/
179+
announceForAccessibility: (announcement: string) => void;
180+
181+
/**
182+
* Post a string to be announced by the screen reader.
183+
* - `announcement`: The string announced by the screen reader.
184+
* - `options`: An object that configures the reading options.
185+
* - `queue`: The announcement will be queued behind existing announcements. iOS only.
186+
*/
187+
announceForAccessibilityWithOptions: (
188+
announcement: string,
189+
options: {queue?: boolean},
190+
) => void;
191+
192+
/**
193+
* Get the recommended timeout for changes to the UI needed by this user.
194+
*
195+
* See https://reactnative.dev/docs/accessibilityinfo#getrecommendedtimeoutmillis
196+
*/
197+
getRecommendedTimeoutMillis: (originalTimeout: number) => Promise<number>;
198+
}

Libraries/Components/AccessibilityInfo/AccessibilityInfo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import NativeAccessibilityInfoAndroid from './NativeAccessibilityInfo';
1717
import NativeAccessibilityManagerIOS from './NativeAccessibilityManager';
1818
import legacySendAccessibilityEvent from './legacySendAccessibilityEvent';
1919
import type {ElementRef} from 'react';
20+
import type {AccessibilityInfo as AccessibilityInfoType} from './AccessibilityInfo.flow';
2021

2122
// Events that are only supported on Android.
2223
type AccessibilityEventDefinitionsAndroid = {
@@ -73,7 +74,7 @@ const EventNames: Map<
7374
*
7475
* See https://reactnative.dev/docs/accessibilityinfo
7576
*/
76-
const AccessibilityInfo = {
77+
const AccessibilityInfo: AccessibilityInfoType = {
7778
/**
7879
* Query whether bold text is currently enabled.
7980
*
@@ -295,6 +296,7 @@ const AccessibilityInfo = {
295296
*/
296297
addEventListener<K: $Keys<AccessibilityEventDefinitions>>(
297298
eventName: K,
299+
// $FlowIssue[incompatible-type] - Flow bug with unions and generics (T128099423)
298300
handler: (...$ElementType<AccessibilityEventDefinitions, K>) => void,
299301
): EventSubscription {
300302
const deviceEventName = EventNames.get(eventName);

0 commit comments

Comments
 (0)