source: frontend/node_modules/eslint-plugin-jsx-a11y/docs/rules/alt-text.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.0 KB
Line 
1# jsx-a11y/alt-text
2
3💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
4
5<!-- end auto-generated rule header -->
6
7Enforce that all elements that require alternative text have meaningful information to relay back to the end user. This is a critical component of accessibility for screen reader users in order for them to understand the content's purpose on the page. By default, this rule checks for alternative text on the following elements: `<img>`, `<area>`, `<input type="image">`, and `<object>`.
8
9## How to resolve
10
11### `<img>`
12
13An `<img>` must have the `alt` prop set with meaningful text or as an empty string to indicate that it is an image for decoration.
14
15For images that are being used as icons for a button or control, the `alt` prop should be set to an empty string (`alt=""`).
16
17```jsx
18<button>
19 <img src="icon.png" alt="" />
20 Save
21
22</button>
23```
24
25The content of an `alt` attribute is used to calculate the accessible label of an element, whereas the text content is used to produce a label for the element. For this reason, adding a label to an icon can produce a confusing or duplicated label on a control that already has appropriate text content.
26
27### `<object>`
28
29Add alternative text to all embedded `<object>` elements using either inner text, setting the `title` prop, or using the `aria-label` or `aria-labelledby` props.
30
31### `<input type="image">`
32
33All `<input type="image">` elements must have a non-empty `alt` prop set with a meaningful description of the image or have the `aria-label` or `aria-labelledby` props set.
34
35### `<area>`
36
37All clickable `<area>` elements within an image map have an `alt`, `aria-label` or `aria-labelledby` prop that describes the purpose of the link.
38
39## Rule options
40
41This rule takes one optional object argument of type object:
42
43```json
44{
45 "rules": {
46 "jsx-a11y/alt-text": [ 2, {
47 "elements": [ "img", "object", "area", "input[type=\"image\"]" ],
48 "img": ["Image"],
49 "object": ["Object"],
50 "area": ["Area"],
51 "input[type=\"image\"]": ["InputImage"]
52 }],
53 }
54}
55```
56
57The `elements` option is a whitelist for DOM elements to check for alternative text. If an element is removed from the default set of elements (noted above), any custom components for that component will also be ignored. In order to indicate any custom wrapper components that should be checked, you can map the DOM element to an array of JSX custom components. This is a good use case when you have a wrapper component that simply renders an `img` element, for instance (like in React):
58
59```jsx
60// Image.js
61const Image = props => {
62 const {
63 alt,
64 ...otherProps
65 } = props;
66
67 return (
68 <img alt={alt} {...otherProps} />
69 );
70}
71
72...
73
74// Header.js (for example)
75...
76return (
77 <header>
78 <Image alt="Logo" src="logo.jpg" />
79 </header>
80
81);
82```
83
84Note that passing props as spread attribute without explicitly the necessary accessibility props defined will cause this rule to fail. Explicitly pass down the set of props needed for rule to pass. Use `Image` component above as a reference for destructuring and applying the prop. **It is a good thing to explicitly pass props that you expect to be passed for self-documentation.** For example:
85
86#### Bad
87
88```jsx
89function Foo(props) {
90 return <img {...props} />
91}
92```
93
94#### Good
95
96```jsx
97function Foo({ alt, ...props}) {
98 return <img alt={alt} {...props} />
99}
100
101// OR
102
103function Foo(props) {
104 const {
105 alt,
106
107 ...otherProps
108 } = props;
109
110 return <img alt={alt} {...otherProps} />
111}
112```
113
114### Succeed
115
116```jsx
117<img src="foo" alt="Foo eating a sandwich." />
118<img src="foo" alt={"Foo eating a sandwich."} />
119<img src="foo" alt={altText} />
120<img src="foo" alt={`${person} smiling`} />
121<img src="foo" alt="" />
122
123<object aria-label="foo" />
124<object aria-labelledby="id1" />
125<object>Meaningful description</object>
126<object title="An object" />
127
128<area aria-label="foo" />
129<area aria-labelledby="id1" />
130
131<area alt="This is descriptive!" />
132
133<input type="image" alt="This is descriptive!" />
134<input type="image" aria-label="foo" />
135<input type="image" aria-labelledby="id1" />
136
137```
138
139### Fail
140
141```jsx
142<img src="foo" />
143<img {...props} />
144<img {...props} alt /> // Has no value
145<img {...props} alt={undefined} /> // Has no value
146<img {...props} alt={`${undefined}`} /> // Has no value
147<img src="foo" role="presentation" /> // Avoid ARIA if it can be achieved without
148
149<img src="foo" role="none" /> // Avoid ARIA if it can be achieved without
150
151<object {...props} />
152
153
154<area {...props} />
155
156<input type="image" {...props} />
157```
158
159## Accessibility guidelines
160
161- [WCAG 1.1.1](https://www.w3.org/WAI/WCAG21/Understanding/non-text-content.html)
162
163### Resources
164
165- [axe-core, object-alt](https://dequeuniversity.com/rules/axe/3.2/object-alt)
166- [axe-core, image-alt](https://dequeuniversity.com/rules/axe/3.2/image-alt)
167- [axe-core, input-image-alt](https://dequeuniversity.com/rules/axe/3.2/input-image-alt)
168- [axe-core, area-alt](https://dequeuniversity.com/rules/axe/3.2/area-alt)
Note: See TracBrowser for help on using the repository browser.