noNoninteractiveElementInteractions
Summary
Section titled Summary- Diagnostic Category:
lint/nursery/noNoninteractiveElementInteractions - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
Description
Section titled DescriptionDisallow use event handlers on non-interactive elements.
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>.
A Non-interactive element does not support event handlers(mouse and key handlers).
Examples
Section titled ExamplesInvalid
Section titled Invalid<div onClick={() => {}}>button</div>code-block.jsx:1:1 lint/nursery/noNoninteractiveElementInteractions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Non-interactive element should not have event handler.
> 1 │ <div onClick={() => {}}>button</div>
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider replace semantically interactive element like <button/> or <a href/>.
Valid
Section titled Valid<button onClick={() => { }}>button</button>// Adding a role to element does not add behavior.// If not used semantic HTML elements like `button`, developers need to implement the expected behavior for role(like focusability and key press support)// See https://www.w3.org/WAI/ARIA/apg/<div role="button" onClick={() => { }}>button</div>// The role="presentation" attribute removes the semantic meaning of an element, indicating that it should be ignored by assistive technologies.// Therefore, it's acceptable to add event handlers to elements with role="presentation" for visual effects or other purposes,// but users relying on assistive technologies may not be able to interact with these elements.<div role="presentation" onClick={() => { }}>button</div>// Hidden from screen reader.<div onClick={() => {}} aria-hidden />// Custom component is not checked.<SomeComponent onClick={() => {}}>button</SomeComponent>// Spread attributes is not supported.<div {...{"onClick":() => {}}}>button</div>Accessibility guidelines
Section titled Accessibility guidelinesResources
Section titled Resources- WAI-ARIA roles
- WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets
- Fundamental Keyboard Navigation Conventions
- Mozilla Developer Network - ARIA Techniques
How to configure
Section titled How to configure{ "linter": { "rules": { "nursery": { "noNoninteractiveElementInteractions": "error" } } }}