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"orrole="none" - elements with
aria-hiddenorcontentEditable="true" - handlers set to
nullorundefined
Configure#
Off by default. To turn it on:
[lint.rules]
"jsx-a11y/no-noninteractive-element-interactions" = "error" # or "warn"Examples#
const item = <liNon-interactive elements should not be assigned mouse or keyboard event listeners. onClick={() => select(user)}>{user.name}</li>;
const card = <divNon-interactive elements should not be assigned mouse or keyboard event listeners. role="article" onKeyDown={openCard} />;const item = (
<li>
<button onClick={() => select(user)}>{user.name}</button>
</li>
);
const option = <li role="option" tabIndex={0} onClick={() => select(user)} />;Options#
| Option | Default | What it does |
|---|---|---|
handlers | mouse, drag and keyboard handlers, onFocus, onBlur, onLoad, onError | The handlers to report. Your list replaces the default. |
| an element name | see below | Handlers 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.
const photo = <img src={url} alt="" onLoad={fadeIn} />;{ "handlers": ["onClick", "onLoad"] }1 problemconst photo = <imgNon-interactive elements should not be assigned mouse or keyboard event listeners. src={url} alt="" onLoad={fadeIn} />;{ "handlers": ["onClick", "onLoad"], "img": ["onLoad"] }No problemsconst photo = <img src={url} alt="" onLoad={fadeIn} />;