source: frontend/node_modules/eslint-plugin-jsx-a11y/docs/rules/no-static-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: 4.3 KB
Line 
1# jsx-a11y/no-static-element-interactions
2
3💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
4
5<!-- end auto-generated rule header -->
6
7Static HTML elements do not have semantic meaning. This is clear in the case of `<div>` and `<span>`. It is less so clear in the case of elements that _seem_ semantic, but that do not have a semantic mapping in the accessibility layer. For example `<a>`, `<big>`, `<blockquote>`, `<footer>`, `<picture>`, `<strike>` and `<time>` -- to name a few -- have no semantic layer mapping. They are as void of meaning as `<div>`.
8
9The [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro) confers a semantic mapping to an element. The semantic value can then be expressed to a user via assistive technology.
10
11In order to add interactivity such as a mouse or key event listener to a static element, that element must be given a role value as well.
12
13## How do I resolve this error?
14
15### Case: This element acts like a button, link, menuitem, etc
16
17Indicate the element's role with the `role` attribute:
18
19```jsx
20<div
21 onClick={onClickHandler}
22 onKeyPress={onKeyPressHandler}
23 role="button"
24 tabindex="0">
25 Save
26</div>
27```
28
29Common interactive roles include:
30
31 1. `button`
32 1. `link`
33 1. `checkbox`
34 1. `menuitem`
35 1. `menuitemcheckbox`
36 1. `menuitemradio`
37 1. `option`
38 1. `radio`
39 1. `searchbox`
40 1. `switch`
41 1. `textbox`
42
43Note: 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.
44
45### Case: The event handler is only being used to capture bubbled events
46
47If your element is catching bubbled click or key events from descendant elements, there are no appropriate roles for your element: you will have to deactivate the rule. Consider explaining the reason for disabling the rule as well.
48
49```jsx
50{/* The <div> element has a child <button> element that allows keyboard interaction */}
51{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
52<div onClick={this.handleButtonClick}>
53 <button>Save</button>
54 <button>Cancel</button>
55</div>
56```
57
58Do not use the role `presentation` on the element: it removes the element's semantics, and may also remove its children's semantics, creating big issues with assistive technology.
59
60## Rule options
61
62You may configure which handler props should be taken into account when applying this rule. The recommended configuration includes the following 6 handlers and the `allowExpressionValues` option.
63
64```javascript
65'jsx-a11y/no-static-element-interactions': [
66 'error',
67 {
68 handlers: [
69 'onClick',
70 'onMouseDown',
71 'onMouseUp',
72 'onKeyPress',
73 'onKeyDown',
74 'onKeyUp',
75 ],
76 allowExpressionValues: true,
77 },
78],
79```
80
81Adjust the list of handler prop names in the handlers array to increase or decrease the coverage surface of this rule in your codebase.
82
83The `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`:
84
85```jsx
86<div role={ROLE_BUTTON} onClick={() => {}} />;
87// In case of a conditional expression, there should be literals on both sides of ternary operator
88<div role={isButton ? "button" : "link"} onClick={() => {}} />;
89```
90
91### Succeed
92
93```jsx
94<button onClick={() => {}} className="foo" />
95<div className="foo" onClick={() => {}} role="button" />
96<input type="text" onClick={() => {}} />
97```
98
99### Fail
100
101```jsx
102<div onClick={() => {}} />
103```
104
105## Accessibility guidelines
106
107- [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)
108
109### Resources
110
111- [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro)
112- [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
113- [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
114- [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.