jsx-a11y/no-noninteractive-element-interactions

Disallow mouse and keyboard handlers on non-interactive elements

Also called jsx_a11y/no-noninteractive-element-interactions

Elements such as <li>, <p>, <img>, <h1> and <main> describe content. A screen reader doesn't announce them as something you can press, and the Tab key skips them, so a click handler on one only works with a mouse.

An element with a non-interactive role, such as <div role="article">, is treated the same way. For <div> and <span> without a role, see jsx-a11y/no-static-element-interactions.

Not reported:

  • components
  • elements with an interactive role, role="presentation" or role="none"
  • elements with aria-hidden or contentEditable="true"
  • handlers set to null or undefined

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"jsx-a11y/no-noninteractive-element-interactions" = "error" # or "warn"

Examples#

example.jsx2 problems
const item = <li onClick={() => select(user)}>{user.name}</li>;
const card = <div role="article" onKeyDown={openCard} />;
example.jsxNo problems
const item = (
  <li>
    <button onClick={() => select(user)}>{user.name}</button>
  </li>
);

const option = <li role="option" tabIndex={0} onClick={() => select(user)} />;

Options#

OptionDefaultWhat it does
handlersmouse, drag and keyboard handlers, onFocus, onBlur, onLoad, onErrorThe handlers to report. Your list replaces the default.
an element namesee belowHandlers that are allowed on that element.

By default onLoad and onError are allowed on <body>, <iframe> and <img>, and onKeyUp, onKeyDown and onKeyPress are allowed on <dialog>. Setting any option drops these defaults, so repeat the ones you want to keep.

example.jsxNo problems
const photo = <img src={url} alt="" onLoad={fadeIn} />;
example.jsxwith { "handlers": ["onClick", "onLoad"] }1 problem
const photo = <img src={url} alt="" onLoad={fadeIn} />;
example.jsxwith { "handlers": ["onClick", "onLoad"], "img": ["onLoad"] }No problems
const photo = <img src={url} alt="" onLoad={fadeIn} />;