Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # no-static-element-interactions
  2. Static 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>`.
  3. The [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.
  4. In 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.
  5. ## How do I resolve this error?
  6. ### Case: This element acts like a button, link, menuitem, etc.
  7. Indicate the element's role with the `role` attribute:
  8. ```
  9. <div
  10. onClick={onClickHandler}
  11. onKeyPress={onKeyPressHandler}
  12. role="button"
  13. tabIndex="0">
  14. Save
  15. </div>
  16. ```
  17. Common interactive roles include:
  18. 1. `button`
  19. 1. `link`
  20. 1. `checkbox`
  21. 1. `menuitem`
  22. 1. `menuitemcheckbox`
  23. 1. `menuitemradio`
  24. 1. `option`
  25. 1. `radio`
  26. 1. `searchbox`
  27. 1. `switch`
  28. 1. `textbox`
  29. Note: 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.
  30. ### Case: This element is not a button, link, menuitem, etc. It is catching bubbled events from elements that it contains
  31. If your element is catching bubbled click or key events from descendant elements, then the proper role for this element is `presentation`.
  32. ```
  33. <div
  34. onClick="onClickHandler"
  35. role="presentation">
  36. <button>Save</button>
  37. </div>
  38. ```
  39. Marking 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.
  40. ### References
  41. 1. [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro)
  42. 1. [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
  43. 1. [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
  44. 1. [Mozilla Developer Network - ARIA Techniques](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus)
  45. ## Rule details
  46. You may configure which handler props should be taken into account when applying this rule. The recommended configuration includes the following 6 handlers.
  47. ```javascript
  48. 'jsx-a11y/no-static-element-interactions': [
  49. 'error',
  50. {
  51. handlers: [
  52. 'onClick',
  53. 'onMouseDown',
  54. 'onMouseUp',
  55. 'onKeyPress',
  56. 'onKeyDown',
  57. 'onKeyUp',
  58. ],
  59. },
  60. ],
  61. ```
  62. Adjust the list of handler prop names in the handlers array to increase or decrease the coverage surface of this rule in your codebase.
  63. ### Succeed
  64. ```jsx
  65. <button onClick={() => {}} className="foo" />
  66. <div className="foo" onClick={() => {}} role="button" />
  67. <input type="text" onClick={() => {}} />
  68. ```
  69. ### Fail
  70. ```jsx
  71. <div onClick={() => {}} />
  72. ```