diff --git a/packages/react-native/Libraries/Text/Text.js b/packages/react-native/Libraries/Text/Text.js index f30261d8426..1c45116674b 100644 --- a/packages/react-native/Libraries/Text/Text.js +++ b/packages/react-native/Libraries/Text/Text.js @@ -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} ); @@ -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} ); diff --git a/packages/react-native/Libraries/Text/__tests__/Text-test.js b/packages/react-native/Libraries/Text/__tests__/Text-test.js index 0a142e8f767..ee81bb1a806 100644 --- a/packages/react-native/Libraries/Text/__tests__/Text-test.js +++ b/packages/react-native/Libraries/Text/__tests__/Text-test.js @@ -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( + {}}>Clickable Text, + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Clickable Text + + `); + }); + + it('does not automatically set accessibilityRole when onLongPress is provided', async () => { + const instance = await create( + {}}>Long Press Text, + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Long Press Text + + `); + }); + + it('does not automatically set accessibilityRole when onStartShouldSetResponder is provided', async () => { + const instance = await create( + true}>Responder Text, + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Responder Text + + `); + }); + + it('respects explicit accessibilityRole', async () => { + const instance = await create( + {}}> + Explicit Button Role + , + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Explicit Button Role + + `); + }); + }); + + 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( + {}}>Clickable Text, + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Clickable Text + + `); + }); + + it('automatically sets accessibilityRole="link" when onLongPress is provided', async () => { + const instance = await create( + {}}>Long Press Text, + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Long Press Text + + `); + }); + + it('automatically sets accessibilityRole="link" when onStartShouldSetResponder is provided', async () => { + const instance = await create( + true}>Responder Text, + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Responder Text + + `); + }); + + it('respects explicit accessibilityRole', async () => { + const instance = await create( + {}}> + Explicit Button Role + , + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Explicit Button Role + + `); + }); + + it('respects explicit role prop', async () => { + // $FlowFixMe[prop-missing] + const instance = await create( + {}} role="button"> + Explicit Role Prop + , + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Explicit Role Prop + + `); + }); + + it('does not automatically set accessibilityRole when disabled', async () => { + const instance = await create( + {}}> + Disabled Pressable Text + , + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Disabled Pressable Text + + `); + }); + + it('automatically sets accessibilityRole="link" for nested Text with onPress', async () => { + const instance = await create( + + Parent Text {}}>Nested Clickable Link + , + ); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Parent Text + + Nested Clickable Link + + + `); + }); + }); + + it('does not set accessibilityRole when no press handlers are provided', async () => { + const instance = await create(Non-pressable Text); + + expect(omitRefAndFlattenStyle(instance)).toMatchInlineSnapshot(` + + Non-pressable Text + + `); + }); + }); }); describe('Text compat with web', () => { diff --git a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js index 86d965bf673..c38b73062f0 100644 --- a/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js +++ b/packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js @@ -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: { diff --git a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js index faf339cd697..5c01815e035 100644 --- a/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js +++ b/packages/react-native/src/private/featureflags/ReactNativeFeatureFlags.js @@ -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<> + * @generated SignedSource<<64e8578585baaed88b48703555520f22>> * @flow strict * @noformat */ @@ -40,6 +40,7 @@ export type ReactNativeFeatureFlagsJsOnly = $ReadOnly<{ reduceDefaultPropsInImage: Getter, reduceDefaultPropsInText: Getter, shouldUseAnimatedObjectForTransform: Getter, + shouldUseLinkRoleForPressableText: Getter, shouldUseRemoveClippedSubviewsAsDefaultOnIOS: Getter, shouldUseSetNativePropsInFabric: Getter, virtualViewActivityBehavior: Getter, @@ -197,6 +198,11 @@ export const reduceDefaultPropsInText: Getter = createJavaScriptFlagGet */ export const shouldUseAnimatedObjectForTransform: Getter = createJavaScriptFlagGetter('shouldUseAnimatedObjectForTransform', false); +/** + * Set accessibilityRole to "link" for pressable Text components by default. + */ +export const shouldUseLinkRoleForPressableText: Getter = createJavaScriptFlagGetter('shouldUseLinkRoleForPressableText', false); + /** * removeClippedSubviews prop will be used as the default in FlatList on iOS to match Android */