Skip to content

Commit 71fda5e

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
feat: Add alt prop to Image component (#34550)
Summary: This adds the `alt` prop to the `Image` component as requested on #34424. Using this new `alt` prop enables the `accessibility` prop and passes down the alt text to `accessibilityLabel`. This PR also updates RNTester ImageExample in order to facilitate the manual QA. #### Open questions - ~~On web `alt` text is displayed on the page if the image can't be loaded for some reason, should we implement this same behavior if the `Image` component fails to load `source`?~~ Not for now ## Changelog [General] [Added] - Add alt prop to Image component Pull Request resolved: #34550 Test Plan: 1. Open the RNTester app and navigate to the Image page 2. Test the `alt` prop through the `Accessibility Label via alt prop` section, this can be tested either by enabling Voice Over if you're using a real device or through the Accessibility Inspector if you're using a simulator https://user-images.githubusercontent.com/11707729/187790249-0d851363-c30e-41b6-8c24-73e72467f4ba.mov Reviewed By: lunaleaps Differential Revision: D39618453 Pulled By: cipolleschi fbshipit-source-id: 0e26b2574514e76ce7e98ddb578f587a9cc30ee9
1 parent f85e2ec commit 71fda5e

File tree

5 files changed

+289
-122
lines changed

5 files changed

+289
-122
lines changed

Libraries/Image/Image.android.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
181181
? loadingIndicatorSource.uri
182182
: null,
183183
ref: forwardedRef,
184-
accessibilityLabel: props['aria-label'] ?? props.accessibilityLabel,
184+
accessible: props.alt !== undefined ? true : props.accessible,
185+
accessibilityLabel:
186+
props['aria-label'] ?? props.accessibilityLabel ?? props.alt,
185187
accessibilityState: {
186188
busy: props['aria-busy'] ?? props.accessibilityState?.busy,
187189
checked: props['aria-checked'] ?? props.accessibilityState?.checked,

Libraries/Image/Image.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ export interface ImagePropsBase
245245
* A static image to display while downloading the final image off the network.
246246
*/
247247
defaultSource?: ImageURISource | number | undefined;
248+
249+
/**
250+
* The text that's read by the screen reader when the user interacts with
251+
* the image.
252+
*
253+
* See https://reactnative.dev/docs/image#alt
254+
*/
255+
alt?: string | undefined;
248256
}
249257

250258
export interface ImageProps extends ImagePropsBase {

Libraries/Image/Image.ios.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
173173
<ImageViewNativeComponent
174174
accessibilityState={_accessibilityState}
175175
{...restProps}
176-
accessibilityLabel={accessibilityLabel}
176+
accessible={props.alt !== undefined ? true : props.accessible}
177+
accessibilityLabel={accessibilityLabel ?? props.alt}
177178
ref={forwardedRef}
178179
style={style}
179180
resizeMode={resizeMode}

Libraries/Image/ImageProps.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type {
2121
import type {ViewProps} from '../Components/View/ViewPropTypes';
2222
import type {Node, Ref} from 'react';
2323
import typeof Image from './Image';
24-
import type {DimensionValue} from '../StyleSheet/StyleSheetTypes';
2524

2625
export type ImageLoadEvent = SyntheticEvent<
2726
$ReadOnly<{|
@@ -93,6 +92,14 @@ export type ImageProps = {|
9392
*/
9493
'aria-label'?: ?Stringish,
9594

95+
/**
96+
* The text that's read by the screen reader when the user interacts with
97+
* the image.
98+
*
99+
* See https://reactnative.dev/docs/image#alt
100+
*/
101+
alt?: ?Stringish,
102+
96103
/**
97104
* blurRadius: the blur radius of the blur filter added to the image
98105
*

0 commit comments

Comments
 (0)