Skip to content

Rewrite bubbled events section in no-static-element-interactions docs #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions docs/rules/no-static-element-interactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In order to add interactivity such as a mouse or key event listener to a static

Indicate the element's role with the `role` attribute:

```
```jsx
<div
onClick={onClickHandler}
onKeyPress={onKeyPressHandler}
Expand All @@ -38,21 +38,23 @@ Common interactive roles include:

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.

### Case: This element is not a button, link, menuitem, etc. It is catching bubbled events from elements that it contains
### Case: The event handler is only being used to capture bubbled events

If your element is catching bubbled click or key events from descendant elements, then the proper role for this element is `presentation`.
If a static element has an event handler for the sole purpose of capturing events from its descendants, you can tell the linter to ignore it by setting `role="presentation"`:

```
```jsx
<div
onClick="onClickHandler"
onClick={this.handleButtonClick}
role="presentation">
<button>Save</button>
<button>Cancel</button>
</div>
```

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.
This `role` has no effect on static elements, but it clarifies your intent.

### References

1. [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro)
1. [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
1. [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
Expand Down Expand Up @@ -81,13 +83,15 @@ You may configure which handler props should be taken into account when applying
Adjust the list of handler prop names in the handlers array to increase or decrease the coverage surface of this rule in your codebase.

### Succeed

```jsx
<button onClick={() => {}} className="foo" />
<div className="foo" onClick={() => {}} role="button" />
<input type="text" onClick={() => {}} />
```

### Fail

```jsx
<div onClick={() => {}} />
```