source: frontend/node_modules/eslint-plugin-jsx-a11y/docs/rules/no-noninteractive-element-interactions.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.6 KB
Line 
1# jsx-a11y/no-noninteractive-element-interactions
2
3💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
4
5<!-- end auto-generated rule header -->
6
7Non-interactive HTML elements and non-interactive ARIA roles indicate _content_ and _containers_ in the user interface. A non-interactive element does not support event handlers (mouse and key handlers). Non-interactive elements include `<main>`, `<area>`, `<h1>` (,`<h2>`, etc), `<p>`, `<img>`, `<li>`, `<ul>` and `<ol>`. Non-interactive [WAI-ARIA roles](https://www.w3.org/TR/wai-aria-1.1/#usage_intro) include `article`, `banner`, `complementary`, `img`, `listitem`, `main`, `region` and `tooltip`.
8
9## How do I resolve this error?
10
11### Case: This element acts like a button, link, menuitem, etc
12
13Move the event handler function to an inner element that is either a semantically interactive element (`<button>`, `<a href>`) or that has an interactive role. This leaves the _content_ or _container_ semantic value of this element intact.
14
15Common interactive roles include:
16
17 1. `button`
18 1. `link`
19 1. `checkbox`
20 1. `menuitem`
21 1. `menuitemcheckbox`
22 1. `menuitemradio`
23 1. `option`
24 1. `radio`
25 1. `searchbox`
26 1. `switch`
27 1. `textbox`
28
29Note: Adding a role to your element does **not** add behavior. When a semantic HTML element like `<button>` is used, then it will also respond to Enter key presses when it has focus. The developer is responsible for providing the expected behavior of an element that the role suggests it would have: focusability and key press support.
30see [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex).
31
32### Case: This element is catching bubbled events from elements that it contains
33
34Move the event handler function to an inner element like `<div>` and give that element a role of `presentation`. This leaves the _content_ or _container_ semantic value of this element intact.
35
36```jsx
37<div role="article">
38 <div
39 onClick="onClickHandler"
40 onKeyPress={onKeyPressHandler}
41 role="presentation">
42 {this.props.children}
43 </div>
44</div>
45```
46
47Marking an element with the role `presentation` indicates to assistive technology that this element should be ignored; it exists to support the web application and is not meant for humans to interact with directly.
48
49### Case: This is a heading that expands/collapses content on the package
50
51Headers often double as expand/collapse controls for the content they headline. An accordion component is a common example of this pattern. Rather than assign the interaction handling code to the heading itself, put a button inside the heading instead. This pattern retains the role of the heading and the role of the button.
52
53```jsx
54<h3>
55 <button onClick={this._expandSection}>News</button>
56</h3>
57<ul id="articles-list">
58 <li>...</li>
59</ul>
60```
61
62### Case: This element is a table cell
63
64Table cells (and tables in general) are meant to contain data. ARIA provides us with a construct called a [Grid](https://w3c.github.io/aria-practices/#grid) that is essentially a 2 dimensional logical container for content and interactive elements.
65
66You have two options in this case.
67
68#### Option 1, move the interactive content inside the table cells
69
70For instance, move the button inside the cell:
71
72```jsx
73<table>
74 <tr>
75 <td><button>Sort</button></td>
76 </tr>
77</table>
78```
79
80This preserves the table cell semantics and the button semantics; the two are not conflated on the cell.
81
82#### Option 2, convert the table into an ARIA grid
83
84If your user interface has a table-like layout, but is filled with interactive components in the cells, consider converting the table into a grid.
85
86```jsx
87<table role="grid">
88 <tr>
89 <td role="gridcell" onClick={this.sort}>Sort</td>
90 </tr>
91</table>
92```
93
94You can also put the interactive content inside the grid cell. This maintains the semantic distinction between the cell and the interaction content, although a grid cell can be interactive.
95
96## Rule options
97
98You may configure which handler props should be taken into account when applying this rule. The recommended configuration includes the following 6 handlers.
99
100```javascript
101'jsx-a11y/no-noninteractive-element-interactions': [
102 'error',
103 {
104 handlers: [
105 'onClick',
106 'onMouseDown',
107 'onMouseUp',
108 'onKeyPress',
109 'onKeyDown',
110 'onKeyUp',
111 ],
112 },
113],
114```
115
116Adjust the list of handler prop names in the handlers array to increase or decrease the coverage surface of this rule in your codebase.
117
118### Succeed
119
120```jsx
121<div onClick={() => void 0} role="button" />
122<div onClick={() => void 0} role="presentation" />
123<input type="text" onClick={() => void 0} /> // Interactive element does not require role.
124<button onClick={() => void 0} className="foo" /> // button is interactive.
125<div onClick={() => void 0} role="button" aria-hidden /> // This is hidden from screen reader.
126<Input onClick={() => void 0} type="hidden" /> // This is a higher-level DOM component
127```
128
129### Fail
130
131```jsx
132<li onClick={() => void 0} />
133<div onClick={() => void 0} role="listitem" />
134```
135
136## Accessibility guidelines
137
138- [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)
139
140### Resources
141
142- [WAI-ARIA roles](https://www.w3.org/TR/wai-aria-1.1/#usage_intro)
143- [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
144- [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
145- [Mozilla Developer Network - ARIA Techniques](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus)
Note: See TracBrowser for help on using the repository browser.