| 1 | # jsx-a11y/no-noninteractive-element-to-interactive-role
|
|---|
| 2 |
|
|---|
| 3 | 💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Non-interactive HTML elements indicate _content_ and _containers_ in the user interface. Non-interactive elements include `<main>`, `<area>`, `<h1>` (,`<h2>`, etc), `<img>`, `<li>`, `<ul>` and `<ol>`.
|
|---|
| 8 |
|
|---|
| 9 | Interactive HTML elements indicate _controls_ in the user interface. Interactive elements include `<a href>`, `<button>`, `<input>`, `<select>`, `<textarea>`.
|
|---|
| 10 |
|
|---|
| 11 | [WAI-ARIA roles](https://www.w3.org/TR/wai-aria-1.1/#usage_intro) should not be used to convert a non-interactive element to an interactive element. Interactive ARIA roles include `button`, `link`, `checkbox`, `menuitem`, `menuitemcheckbox`, `menuitemradio`, `option`, `radio`, `searchbox`, `switch` and `textbox`.
|
|---|
| 12 |
|
|---|
| 13 | ## How do I resolve this error?
|
|---|
| 14 |
|
|---|
| 15 | ### Case: This element should be a control, like a button
|
|---|
| 16 |
|
|---|
| 17 | Put the control inside the non-interactive container element.
|
|---|
| 18 |
|
|---|
| 19 | ```jsx
|
|---|
| 20 | <li>
|
|---|
| 21 | <div
|
|---|
| 22 | role="button"
|
|---|
| 23 | onClick={() => {}}
|
|---|
| 24 | onKeyPress={() => {}}>
|
|---|
| 25 | Save
|
|---|
| 26 | </div>
|
|---|
| 27 | </li>
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | Or wrap the content inside your interactive element.
|
|---|
| 31 |
|
|---|
| 32 | ```jsx
|
|---|
| 33 | <div
|
|---|
| 34 | role="button"
|
|---|
| 35 | onClick={() => {}}
|
|---|
| 36 | onKeyPress={() => {}}
|
|---|
| 37 | tabIndex="0">
|
|---|
| 38 | <img src="some/file.png" alt="Save" />
|
|---|
| 39 | </div>
|
|---|
| 40 | ```
|
|---|
| 41 |
|
|---|
| 42 | ## Rule options
|
|---|
| 43 |
|
|---|
| 44 | The recommended options for this rule allow several common interactive roles to be applied to a non-interactive element. The options are provided as an object keyed by HTML element name; the value is an array of interactive roles that are allowed on the specified element.
|
|---|
| 45 |
|
|---|
| 46 | ```js
|
|---|
| 47 | {
|
|---|
| 48 | 'no-noninteractive-element-to-interactive-role': [
|
|---|
| 49 | 'error',
|
|---|
| 50 | {
|
|---|
| 51 | ul: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'],
|
|---|
| 52 | ol: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'],
|
|---|
| 53 | li: ['menuitem', 'option', 'row', 'tab', 'treeitem'],
|
|---|
| 54 | table: ['grid'],
|
|---|
| 55 | td: ['gridcell'],
|
|---|
| 56 | },
|
|---|
| 57 | ]
|
|---|
| 58 | }
|
|---|
| 59 | ```
|
|---|
| 60 |
|
|---|
| 61 | Under the recommended options, the following code is valid. It would be invalid under the strict rules.
|
|---|
| 62 |
|
|---|
| 63 | ```jsx
|
|---|
| 64 | <ul role="menu" />
|
|---|
| 65 | ```
|
|---|
| 66 |
|
|---|
| 67 | ## Accessibility guidelines
|
|---|
| 68 |
|
|---|
| 69 | - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)
|
|---|
| 70 |
|
|---|
| 71 | ### Resources
|
|---|
| 72 |
|
|---|
| 73 | - [WAI-ARIA roles](https://www.w3.org/TR/wai-aria-1.1/#usage_intro)
|
|---|
| 74 | - [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
|
|---|
| 75 | - [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
|
|---|
| 76 | - [Mozilla Developer Network - ARIA Techniques](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus)
|
|---|