Skip to content

Commit 30bf55f

Browse files
jorge-cabfacebook-github-bot
authored andcommitted
[skip ci] Add screenReaderFocusable prop (facebook#50850)
Summary: This prop will be used to enable screen reader focusability without allowing keyboard focus. Mostly a quality of life prop for product engineers and maps 1:1 to Android Changelog: [Android][Added] - Expose Android's screenReaderFocusable prop Differential Revision: D73382051
1 parent 43403be commit 30bf55f

File tree

11 files changed

+47
-1
lines changed

11 files changed

+47
-1
lines changed

packages/react-native/Libraries/Components/View/ViewAccessibility.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ export interface AccessibilityPropsAndroid {
273273
| 'no'
274274
| 'no-hide-descendants'
275275
| undefined;
276+
277+
/**
278+
* Enables the view to be screen reader focusable, not keyboard focusable.
279+
*
280+
* @platform android
281+
*/
282+
screenReaderFocusable?: boolean | undefined;
276283
}
277284

278285
export interface AccessibilityPropsIOS {

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

+8
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ export type AccessibilityPropsAndroid = $ReadOnly<{
258258
* See https://reactnative.dev/docs/view#importantforaccessibility
259259
*/
260260
importantForAccessibility?: ?('auto' | 'yes' | 'no' | 'no-hide-descendants'),
261+
262+
/**
263+
* Enables the view to be screen reader focusable, not keyboard focusable. This has lower priority
264+
* than focusable or accessible props.
265+
*
266+
* @platform android
267+
*/
268+
screenReaderFocusable?: boolean,
261269
}>;
262270

263271
export type AccessibilityPropsIOS = $ReadOnly<{

packages/react-native/Libraries/NativeComponent/BaseViewConfig.android.js

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const validAttributesForNonEventProps = {
205205
accessibilityValue: true,
206206
experimental_accessibilityOrder: true,
207207
importantForAccessibility: true,
208+
screenReaderFocusable: true,
208209
role: true,
209210
rotation: true,
210211
scaleX: true,

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,7 @@ export type AccessibilityPropsAndroid = $ReadOnly<{
36423642
accessibilityLiveRegion?: ?(\\"none\\" | \\"polite\\" | \\"assertive\\"),
36433643
\\"aria-live\\"?: ?(\\"polite\\" | \\"assertive\\" | \\"off\\"),
36443644
importantForAccessibility?: ?(\\"auto\\" | \\"yes\\" | \\"no\\" | \\"no-hide-descendants\\"),
3645+
screenReaderFocusable?: boolean,
36453646
}>;
36463647
export type AccessibilityPropsIOS = $ReadOnly<{
36473648
accessibilityIgnoresInvertColors?: ?boolean,

packages/react-native/ReactAndroid/api/ReactAndroid.api

+2
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,7 @@ public abstract class com/facebook/react/uimanager/BaseViewManager : com/faceboo
35243524
public fun setRotation (Landroid/view/View;F)V
35253525
public fun setScaleX (Landroid/view/View;F)V
35263526
public fun setScaleY (Landroid/view/View;F)V
3527+
public fun setScreenReaderFocusable (Landroid/view/View;Ljava/lang/Boolean;)V
35273528
public fun setShadowColor (Landroid/view/View;I)V
35283529
public fun setShouldBlockNativeResponder (Landroid/view/View;Z)V
35293530
public fun setStartShouldSetResponder (Landroid/view/View;Z)V
@@ -4896,6 +4897,7 @@ public final class com/facebook/react/uimanager/ViewProps {
48964897
public static final field ROW_GAP Ljava/lang/String;
48974898
public static final field SCALE_X Ljava/lang/String;
48984899
public static final field SCALE_Y Ljava/lang/String;
4900+
public static final field SCREEN_READER_FOCUSABLE Ljava/lang/String;
48994901
public static final field SCROLL Ljava/lang/String;
49004902
public static final field SHADOW_COLOR Ljava/lang/String;
49014903
public static final field START Ljava/lang/String;

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java

+11
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,17 @@ public void setImportantForAccessibility(
513513
}
514514
}
515515

516+
@ReactProp(name = ViewProps.SCREEN_READER_FOCUSABLE)
517+
public void setScreenReaderFocusable(@NonNull T view, boolean screenReaderFocusable) {
518+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
519+
if (screenReaderFocusable != null) {
520+
view.setScreenReaderFocusable(screenReaderFocusable);
521+
} else {
522+
view.setScreenReaderFocusable(false);
523+
}
524+
}
525+
}
526+
516527
@ReactProp(name = ViewProps.ROLE)
517528
public void setRole(@NonNull T view, @Nullable String role) {
518529
if (role == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManagerDelegate.kt

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public abstract class BaseViewManagerDelegate<
8282
ViewProps.IMPORTANT_FOR_ACCESSIBILITY ->
8383
mViewManager.setImportantForAccessibility(view, value as String?)
8484

85+
ViewProps.SCREEN_READER_FOCUSABLE ->
86+
mViewManager.setScreenReaderFocusable(view, value as Boolean? ?: false)
87+
8588
ViewProps.ROLE -> mViewManager.setRole(view, value as String?)
8689
ViewProps.NATIVE_ID -> mViewManager.setNativeId(view, value as String?)
8790
ViewProps.ACCESSIBILITY_LABELLED_BY -> {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewProps.kt

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public object ViewProps {
159159
public const val ACCESSIBILITY_LABELLED_BY: String = "accessibilityLabelledBy"
160160
public const val ACCESSIBILITY_ORDER: String = "experimental_accessibilityOrder"
161161
public const val IMPORTANT_FOR_ACCESSIBILITY: String = "importantForAccessibility"
162+
public const val SCREEN_READER_FOCUSABLE: String = "screenReaderFocusable"
162163
public const val ROLE: String = "role"
163164
// DEPRECATED
164165
public const val ROTATION: String = "rotation"

packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/HostPlatformViewProps.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ HostPlatformViewProps::HostPlatformViewProps(
8585
rawProps,
8686
"renderToHardwareTextureAndroid",
8787
sourceProps.renderToHardwareTextureAndroid,
88+
{})),
89+
screenReaderFocusable(
90+
ReactNativeFeatureFlags::enableCppPropsIteratorSetter()
91+
? sourceProps.screenReaderFocusable
92+
: convertRawProp(
93+
context,
94+
rawProps,
95+
"screenReaderFocusable",
96+
sourceProps.screenReaderFocusable,
8897
{})) {}
8998

9099
#define VIEW_EVENT_CASE(eventType) \
@@ -119,6 +128,7 @@ void HostPlatformViewProps::setProp(
119128
RAW_SET_PROP_SWITCH_CASE_BASIC(hasTVPreferredFocus);
120129
RAW_SET_PROP_SWITCH_CASE_BASIC(needsOffscreenAlphaCompositing);
121130
RAW_SET_PROP_SWITCH_CASE_BASIC(renderToHardwareTextureAndroid);
131+
RAW_SET_PROP_SWITCH_CASE_BASIC(screenReaderFocusable);
122132
}
123133
}
124134

packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/HostPlatformViewProps.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class HostPlatformViewProps : public BaseViewProps {
4747
bool hasTVPreferredFocus{false};
4848
bool needsOffscreenAlphaCompositing{false};
4949
bool renderToHardwareTextureAndroid{false};
50+
bool screenReaderFocusable{false};
5051

5152
#pragma mark - Convenience Methods
5253

packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/HostPlatformViewTraitsInitializer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ inline bool formsView(const ViewProps& viewProps) {
2121
viewProps.nativeForeground.has_value() || viewProps.focusable ||
2222
viewProps.hasTVPreferredFocus ||
2323
viewProps.needsOffscreenAlphaCompositing ||
24-
viewProps.renderToHardwareTextureAndroid;
24+
viewProps.renderToHardwareTextureAndroid ||
25+
viewProps.screenReaderFocusable;
2526
}
2627

2728
} // namespace facebook::react::HostPlatformViewTraitsInitializer

0 commit comments

Comments
 (0)