source: frontend/node_modules/eslint-plugin-jsx-a11y/docs/rules/no-noninteractive-tabindex.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.2 KB
Line 
1# jsx-a11y/no-noninteractive-tabindex
2
3💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
4
5<!-- end auto-generated rule header -->
6
7Tab key navigation should be limited to elements on the page that can be interacted with. Thus it is not necessary to add a tabindex to items in an unordered list, for example, to make them navigable through assistive technology. These applications already afford page traversal mechanisms based on the HTML of the page. Generally, we should try to reduce the size of the page's tab ring rather than increasing it.
8
9## How do I resolve this error?
10
11### Case: I am using an `<a>` tag. Isn't that interactive?
12
13The `<a>` tag is tricky. Consider the following:
14
15```jsx
16<a>Edit</a>
17<a href="#">Edit</a>
18<a role="button">Edit</a>
19```
20
21The bare `<a>` tag is an _anchor_. It has no semantic AX API mapping in either ARIA or the AXObject model. It's as meaningful as `<div>`, which is to say it has no meaning. An `<a>` tag with an `href` attribute has an inherent role of `link`. An `<a>` tag with an explicit role obtains the designated role. In the example above, this role is `button`.
22
23### Case: I am using "semantic" HTML. Isn't that interactive?
24
25If we take a step back into the field of linguistics for a moment, let's consider what it means for something to be "semantic". Nothing, in and of itself, has meaning. Meaning is constructed through dialogue. A speaker intends a meaning and a listener/observer interprets a meaning. Each participant constructs their own meaning through dialogue. There is no intrinsic or isolated meaning outside of interaction. Thus, we must ask, given that we have a "speaker" who communicates via "semantic" HTML, who is listening/observing?
26
27In our case, the observer is the Accessibility (AX) API. Browsers interpret HTML (inflected at times by ARIA) to construct a meaning (AX Tree) of the page. Whatever the semantic HTML intends has only the force of suggestion to the AX API. Therefore, we have inconsistencies. For example, there is not yet an ARIA role for `text` or `label` and thus no way to change a `<label>` into plain text or a `<span>` into a label via ARIA. `<div>` has an AXObject correpondant `DivRole`, but no such object maps to `<span>`.
28
29What this lint rule endeavors to do is apply the AX API understanding of the semantics of an HTML document back onto your code. The concept of interactivity boils down to whether a user can do something with the indicated or focused component.
30
31Common interactive roles include:
32
33 1. `button`
34 1. `link`
35 1. `checkbox`
36 1. `menuitem`
37 1. `menuitemcheckbox`
38 1. `menuitemradio`
39 1. `option`
40 1. `radio`
41 1. `searchbox`
42 1. `switch`
43 1. `textbox`
44
45Endeavor to limit tabbable elements to those that a user can act upon.
46
47### Case: Shouldn't I add a tabindex so that users can navigate to this item?
48
49It is not necessary to put a tabindex on an `<article>`, for instance or on `<li>` items; assistive technologies provide affordances to users to find and traverse these containers. Most elements that require a tabindex -- `<a href>`, `<button>`, `<input>`, `<textarea>` -- have it already.
50
51Your application might require an exception to this rule in the case of an element that captures incoming tab traversal for a composite widget. In that case, turn off this rule on a per instance basis. This is an uncommon case.
52
53If you know that a particular element will be scrollable, you might want to add `tabindex="0"` if your website supports browsers that don't make these containers keyboard-focusable. The current status for this platform feature can be tracked in [Chrome Platform Status "Feature: Keyboard-focusable scroll containers"](https://www.chromestatus.com/feature/5231964663578624).
54
55```jsx
56// eslint-disable-next-line no-noninteractive-tabindex
57<pre tabIndex="0">
58 <code>{someLongCode}</code>
59</pre>
60```
61
62## Rule options
63
64The recommended options for this rule allow `tabIndex` on elements with the noninteractive `tabpanel` role. Adding `tabIndex` to a tabpanel is a recommended practice in some instances.
65
66```javascript
67'jsx-a11y/no-noninteractive-tabindex': [
68 'error',
69 {
70 tags: [],
71 roles: ['tabpanel'],
72 allowExpressionValues: true,
73 },
74]
75```
76
77The `allowExpressionValues` option determines whether the `role` attribute is allowed to be assigned using an expression. For example, the following would pass in recommended mode if `allowExpressionValues` is set to be `true`:
78
79```jsx
80<div role={ROLE_BUTTON} onClick={() => {}} tabIndex="0" />;
81// In case of a conditional expression, there should be literals on both sides of ternary operator
82<div role={isButton ? "button" : "link"} onClick={() => {}} tabIndex="0" />;
83```
84
85### Succeed
86
87```jsx
88<div />
89<MyButton tabIndex={0} />
90<button />
91<button tabIndex="0" />
92<button tabIndex={0} />
93<div />
94<div tabIndex="-1" />
95<div role="button" tabIndex="0" />
96<div role="article" tabIndex="-1" />
97<article tabIndex="-1" />
98```
99
100### Fail
101
102```jsx
103<div tabIndex="0" />
104<div role="article" tabIndex="0" />
105<article tabIndex="0" />
106<article tabIndex={0} />
107```
108
109## Accessibility guidelines
110
111- [WCAG 2.1.1](https://www.w3.org/WAI/WCAG21/Understanding/keyboard)
112
113### Resources
114
115- [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
Note: See TracBrowser for help on using the repository browser.