web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6 KiB

  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. ```jsx
  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: The event handler is only being used to capture bubbled events
  31. If 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.
  32. ```jsx
  33. {/* The <div> element has a child <button> element that allows keyboard interaction */}
  34. {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
  35. <div onClick={this.handleButtonClick}>
  36. <button>Save</button>
  37. <button>Cancel</button>
  38. </div>
  39. ```
  40. Do 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.
  41. ## Rule details
  42. You may configure which handler props should be taken into account when applying this rule. The recommended configuration includes the following 6 handlers.
  43. ```javascript
  44. 'jsx-a11y/no-static-element-interactions': [
  45. 'error',
  46. {
  47. handlers: [
  48. 'onClick',
  49. 'onMouseDown',
  50. 'onMouseUp',
  51. 'onKeyPress',
  52. 'onKeyDown',
  53. 'onKeyUp',
  54. ],
  55. },
  56. ],
  57. ```
  58. Adjust the list of handler prop names in the handlers array to increase or decrease the coverage surface of this rule in your codebase.
  59. ### Succeed
  60. ```jsx
  61. <button onClick={() => {}} className="foo" />
  62. <div className="foo" onClick={() => {}} role="button" />
  63. <input type="text" onClick={() => {}} />
  64. ```
  65. ### Fail
  66. ```jsx
  67. <div onClick={() => {}} />
  68. ```
  69. ## Accessibility guidelines
  70. - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)
  71. ### Resources
  72. - [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro)
  73. - [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
  74. - [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
  75. - [Mozilla Developer Network - ARIA Techniques](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus)