Add accessibilityRole=link if Text is pressable (#54490)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/54490

Changelog:
[General][Changed] - Text components with onPress or onLongPress now automatically get accessibilityRole='link' for proper accessibility support

## Background

React Native on Android only generates ClickableSpans when the associated JS element uses `role="link"` or `accessibilityRole="link"`.

Without ClickableSpans, accessibility tools cannot properly identify these interactive text elements.

### This Change

This change makes React Native automatically set `accessibilityRole='link'` on Text components that are pressable, unless an explicit `accessibilityRole`/`role` is already provided.

Reviewed By: javache

Differential Revision: D86657563

fbshipit-source-id: cc1b6f1f91c22d5b6cf7fc4487eeeb612c4a752a
This commit is contained in:
Fabrizio Cucci authored and meta-codesync[bot] committed 2025-11-11 03:04:42 -08:00
1 parent 17d184cbab
commit 4c732b4ccf
4 files changed
+356 -7

No files matched your search

+42 -2
View File
@@ -45,6 +45,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
ref: forwardedRef,
accessible,
accessibilityLabel,
accessibilityRole,
accessibilityState,
allowFontScaling,
'aria-busy': ariaBusy,
@@ -71,6 +72,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
onResponderTerminationRequest,
onStartShouldSetResponder,
pressRetentionOffset,
role,
selectable,
selectionColor,
suppressHighlighting,
@@ -149,6 +151,17 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
onStartShouldSetResponder != null) &&
_disabled !== true;
const shouldUseLinkRole =
ReactNativeFeatureFlags.shouldUseLinkRoleForPressableText() &&
isPressable &&
accessibilityRole == null &&
role == null;
const _accessibilityRole =
accessibilityRole ?? (shouldUseLinkRole ? 'link' : undefined);
const _role = shouldUseLinkRole ? undefined : role;
// TODO: Move this processing to the view configuration.
const _selectionColor =
selectionColor != null ? processColor(selectionColor) : undefined;
@@ -206,6 +219,9 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
if (_accessibilityLabel !== undefined) {
processedProps.accessibilityLabel = _accessibilityLabel;
}
if (_accessibilityRole !== undefined) {
processedProps.accessibilityRole = _accessibilityRole;
}
if (_accessibilityState !== undefined) {
processedProps.accessibilityState = _accessibilityState;
}
@@ -224,6 +240,9 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
if (_selectionColor !== undefined) {
processedProps.selectionColor = _selectionColor;
}
if (_role !== undefined) {
processedProps.role = _role;
}
let textPressabilityProps: ?TextPressabilityProps;
if (isPressable) {
@@ -317,6 +336,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
accessibilityElementsHidden,
importantForAccessibility,
accessibilityLabel,
accessibilityRole,
accessibilityState,
allowFontScaling,
'aria-busy': ariaBusy,
@@ -343,6 +363,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
onResponderTerminationRequest,
onStartShouldSetResponder,
pressRetentionOffset,
role,
selectable,
selectionColor,
suppressHighlighting,
@@ -398,6 +419,17 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
onStartShouldSetResponder != null) &&
_disabled !== true;
const shouldUseLinkRole =
ReactNativeFeatureFlags.shouldUseLinkRoleForPressableText() &&
isPressable &&
accessibilityRole == null &&
role == null;
const _accessibilityRole =
accessibilityRole ?? (shouldUseLinkRole ? 'link' : undefined);
const _role = shouldUseLinkRole ? undefined : role;
// TODO: Move this processing to the view configuration.
const _selectionColor =
selectionColor != null ? processColor(selectionColor) : undefined;
@@ -462,6 +494,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
...restProps,
accessibilityElementsHidden: _accessibilityElementsHidden,
accessibilityLabel: _accessibilityLabel,
accessibilityRole: _accessibilityRole,
accessibilityState: _accessibilityState,
importantForAccessibility: _importantForAccessibility,
nativeID: _nativeID,
@@ -470,6 +503,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
selectionColor: _selectionColor,
style: _style,
disabled: disabled,
role: _role,
children,
}}
textPressabilityProps={{
@@ -495,6 +529,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
{...restProps}
accessibilityElementsHidden={_accessibilityElementsHidden}
accessibilityLabel={_accessibilityLabel}
accessibilityRole={_accessibilityRole}
accessibilityState={_accessibilityState}
importantForAccessibility={_importantForAccessibility}
nativeID={_nativeID}
@@ -503,7 +538,8 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
selectable={_selectable}
selectionColor={_selectionColor}
style={_style}
disabled={disabled}>
disabled={disabled}
role={_role}>
{children}
</NativeVirtualText>
);
@@ -538,6 +574,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
...restProps,
accessibilityElementsHidden: _accessibilityElementsHidden,
accessibilityLabel: _accessibilityLabel,
accessibilityRole: _accessibilityRole,
accessibilityState: _accessibilityState,
accessible: _accessible,
allowFontScaling: allowFontScaling !== false,
@@ -549,6 +586,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
selectable: _selectable,
selectionColor: _selectionColor,
style: _style,
role: _role,
children,
}}
textPressabilityProps={{
@@ -573,6 +611,7 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
{...restProps}
accessibilityElementsHidden={_accessibilityElementsHidden}
accessibilityLabel={_accessibilityLabel}
accessibilityRole={_accessibilityRole}
accessibilityState={_accessibilityState}
accessible={_accessible}
allowFontScaling={allowFontScaling !== false}
@@ -584,7 +623,8 @@ if (ReactNativeFeatureFlags.reduceDefaultPropsInText()) {
ref={forwardedRef}
selectable={_selectable}
selectionColor={_selectionColor}
style={_style}>
style={_style}
role={_role}>
{children}
</NativeText>
);
+296 -4
View File
@@ -8,9 +8,11 @@
* @format
*/
import type {ReactTestRendererJSON} from '../../Utilities/ReactNativeTestTools';
import type {ReactTestRenderer} from 'react-test-renderer';
import {create} from '../../../jest/renderer';
import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
import flattenStyle from '../../StyleSheet/flattenStyle';
import * as React from 'react';
@@ -24,10 +26,36 @@ function omitRefAndFlattenStyle(instance: ReactTestRenderer) {
if (json == null) {
throw new Error('Expected `instance.toJSON()` to be non-null');
}
// Omit `ref` for forward-compatibility with `enableRefAsProp`.
delete json.props.ref;
json.props.style = flattenStyle(json.props.style);
return json;
function processNode(node: ReactTestRendererJSON): ReactTestRendererJSON {
if (node == null || typeof node !== 'object') {
return node;
}
if (node.props) {
// Omit `ref` for forward-compatibility with `enableRefAsProp`.
delete node.props.ref;
// Omit event handlers
delete node.props.onClick;
delete node.props.onResponderGrant;
delete node.props.onResponderMove;
delete node.props.onResponderRelease;
delete node.props.onResponderTerminate;
delete node.props.onResponderTerminationRequest;
delete node.props.onStartShouldSetResponder;
// Flatten style
node.props.style = flattenStyle(node.props.style);
}
// Process children recursively
if (node.children) {
node.children = node.children.map(processNode);
}
return node;
}
return processNode(json);
}
describe('Text', () => {
@@ -46,6 +74,270 @@ describe('Text', () => {
it('has displayName', () => {
expect(Text.displayName).toEqual('Text');
});
describe('accessibilityRole with onPress/onLongPress', () => {
describe('when shouldUseLinkRoleForPressableText flag is OFF (default)', () => {
beforeEach(() => {
jest
.spyOn(ReactNativeFeatureFlags, 'shouldUseLinkRoleForPressableText')
.mockReturnValue(false);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('does not automatically set accessibilityRole when onPress is provided', async () => {
const instance = await create(
<Text onPress={() => {}}>Clickable Text</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Clickable Text
</RCTText>
`);
});
it('does not automatically set accessibilityRole when onLongPress is provided', async () => {
const instance = await create(
<Text onLongPress={() => {}}>Long Press Text</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Long Press Text
</RCTText>
`);
});
it('does not automatically set accessibilityRole when onStartShouldSetResponder is provided', async () => {
const instance = await create(
<Text onStartShouldSetResponder={() => true}>Responder Text</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Responder Text
</RCTText>
`);
});
it('respects explicit accessibilityRole', async () => {
const instance = await create(
<Text accessibilityRole="button" onPress={() => {}}>
Explicit Button Role
</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessibilityRole="button"
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Explicit Button Role
</RCTText>
`);
});
});
describe('when shouldUseLinkRoleForPressableText flag is ON', () => {
beforeEach(() => {
jest
.spyOn(ReactNativeFeatureFlags, 'shouldUseLinkRoleForPressableText')
.mockReturnValue(true);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('automatically sets accessibilityRole="link" when onPress is provided', async () => {
const instance = await create(
<Text onPress={() => {}}>Clickable Text</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessibilityRole="link"
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Clickable Text
</RCTText>
`);
});
it('automatically sets accessibilityRole="link" when onLongPress is provided', async () => {
const instance = await create(
<Text onLongPress={() => {}}>Long Press Text</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessibilityRole="link"
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Long Press Text
</RCTText>
`);
});
it('automatically sets accessibilityRole="link" when onStartShouldSetResponder is provided', async () => {
const instance = await create(
<Text onStartShouldSetResponder={() => true}>Responder Text</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessibilityRole="link"
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Responder Text
</RCTText>
`);
});
it('respects explicit accessibilityRole', async () => {
const instance = await create(
<Text accessibilityRole="button" onPress={() => {}}>
Explicit Button Role
</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessibilityRole="button"
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
>
Explicit Button Role
</RCTText>
`);
});
it('respects explicit role prop', async () => {
// $FlowFixMe[prop-missing]
const instance = await create(
<Text onPress={() => {}} role="button">
Explicit Role Prop
</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
isHighlighted={false}
isPressable={true}
role="button"
>
Explicit Role Prop
</RCTText>
`);
});
it('does not automatically set accessibilityRole when disabled', async () => {
const instance = await create(
<Text disabled onPress={() => {}}>
Disabled Pressable Text
</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessibilityState={
Object {
"disabled": true,
}
}
accessible={true}
allowFontScaling={true}
disabled={true}
ellipsizeMode="tail"
>
Disabled Pressable Text
</RCTText>
`);
});
it('automatically sets accessibilityRole="link" for nested Text with onPress', async () => {
const instance = await create(
<Text>
Parent Text<Text onPress={() => {}}>Nested Clickable Link</Text>
</Text>,
);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
>
Parent Text
<RCTText
accessibilityRole="link"
isHighlighted={false}
isPressable={true}
>
Nested Clickable Link
</RCTText>
</RCTText>
`);
});
});
it('does not set accessibilityRole when no press handlers are provided', async () => {
const instance = await create(<Text>Non-pressable Text</Text>);
expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(`
<RCTText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
>
Non-pressable Text
</RCTText>
`);
});
});
});
describe('Text compat with web', () => {
@@ -1074,6 +1074,17 @@ const definitions: FeatureFlagDefinitions = {
},
ossReleaseStage: 'none',
},
shouldUseLinkRoleForPressableText: {
defaultValue: false,
metadata: {
dateAdded: '2025-11-10',
description:
'Set accessibilityRole to "link" for pressable Text components by default.',
expectedReleaseValue: true,
purpose: 'experimentation',
},
ossReleaseStage: 'none',
},
shouldUseRemoveClippedSubviewsAsDefaultOnIOS: {
defaultValue: false,
metadata: {
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<f1901dffe2f41ee22247bb81f493e62f>>
* @generated SignedSource<<64e8578585baaed88b48703555520f22>>
* @flow strict
* @noformat
*/
@@ -40,6 +40,7 @@ export type ReactNativeFeatureFlagsJsOnly = $ReadOnly<{
reduceDefaultPropsInImage: Getter<boolean>,
reduceDefaultPropsInText: Getter<boolean>,
shouldUseAnimatedObjectForTransform: Getter<boolean>,
shouldUseLinkRoleForPressableText: Getter<boolean>,
shouldUseRemoveClippedSubviewsAsDefaultOnIOS: Getter<boolean>,
shouldUseSetNativePropsInFabric: Getter<boolean>,
virtualViewActivityBehavior: Getter<string>,
@@ -197,6 +198,11 @@ export const reduceDefaultPropsInText: Getter<boolean> = createJavaScriptFlagGet
*/
export const shouldUseAnimatedObjectForTransform: Getter<boolean> = createJavaScriptFlagGetter('shouldUseAnimatedObjectForTransform', false);
/**
* Set accessibilityRole to "link" for pressable Text components by default.
*/
export const shouldUseLinkRoleForPressableText: Getter<boolean> = createJavaScriptFlagGetter('shouldUseLinkRoleForPressableText', false);
/**
* removeClippedSubviews prop will be used as the default in FlatList on iOS to match Android
*/