Skip to content

noNoninteractiveElementInteractions

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"a11y": {
"noNoninteractiveElementInteractions": "error"
}
}
}
}

¥Description

禁止在非交互式元素上使用事件处理程序。

¥Disallow use event handlers on non-interactive elements.

非交互式 HTML 元素表示用户界面中的内容和容器。非交互式元素包括 <main><area><h1><h2> 等)、<img><li><ul><ol>

¥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

¥Invalid

<div onClick={() => {}}>button</div>
code-block.jsx:1:1 lint/a11y/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

<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

¥Resources

¥Related links