Reputation: 40814
I used the following jsx to construct an entry in a FAQ table:
<li key={faqKey} className={styles.entry} onClick={handleExpandFn}>
<div>
<span className={`icon-next ${questionClassname}`} />
<span className={styles['question-text']}>{faqEntry.question}</span>
</div>
{faqEntry.answer}
</li>
The idea is that when a user click on the entry, it will toggle the open state of the entry. In the other word, when a user clicks on an open FAQ entry, it will close it. Otherwise it will open it.
However the li
tag triggers this eslint violation: Visible, non-interactive elements should not have mouse or keyboard event listeners jsx-a11y/no-static-element-interactions
Unfortunately I don't think there is alternative way to the above html markup.
Since it is jsx, it also does not allow override such as // eslint-disable-line jsx-a11y/no-static-element-interactions
(The inline comment will be printed out to the web page)
So how I can work around it? Happy to use different jsx markup or jsx eslint override
Upvotes: 33
Views: 71683
Reputation: 31
Div is a non interactive element and it is only used to show content and containers in user interface.
There are few non interactive elements other than <div>
like <main>
, <area>
, all heading tags (like h1, h2, etc.), <p>
, <img>
, <ul>
, <li>
and <ol>
which does not support or say is not appropriate to use events on it.
So add a role
and tabIndex
to make the div interactive and tabbable:
<li key={faqKey} className={styles.entry} role="button" tabIndex={0} onClick={handleExpandFn}>
<div>
<span className={`icon-next ${questionClassname}`} />
<span className={styles['question-text']}>{faqEntry.question}</span>
</div>
{faqEntry.answer}
</li>
Upvotes: 2
Reputation: 1084
To overcome or avoid this error in ES Lint.
You can use three things based on your needs and requirements
aria-hidden="true" - will remove the entire element from the accessibility API
role= "presentation" - The presentation role is used to remove semantic meaning from an element and any of its related child elements.
role= "none" - will remove the semantic meaning of an element while still exposing it to assistive technology.
There are limitations as well:
Upvotes: 9
Reputation: 1848
use
aria-hidden="true"
on non interactive element tag. like
<span aria-hidden="true" onClick={() => void}>Text</span>
Upvotes: 2
Reputation: 149
if you are trying to implement menu using li
then the right solution is using role="menuitem"
in your li
tags.
More details about it: https://w3c.github.io/aria/#menuitem
Upvotes: 6
Reputation: 4656
For those who are looking for a workaround, use role="presentation"
in your tag.
Upvotes: 108
Reputation: 18100
You could put eslint-disable-line
in the jsx
<li // eslint-disable-line jsx-a11y/no-static-element-interactions
key={faqKey}
className={styles.entry}
onClick={handleExpandFn}
>
<div>
<span className={`icon-next ${questionClassname}`} />
<span className={styles['question-text']}>{faqEntry.question}</span>
</div>
{faqEntry.answer}
</li>
Another option, add role='presentation'
<li
key={faqKey}
className={styles.entry}
onClick={handleExpandFn}
role='presentation'
>
<div>
<span className={`icon-next ${questionClassname}`} />
<span className={styles['question-text']}>{faqEntry.question}</span>
</div>
{faqEntry.answer}
</li>
Upvotes: 9
Reputation: 19
You can actually add the eslint override as a comment in JSX. You have to nest the comment inside of braces {}, so that it will be interpreted as JavaScript. Then, of course, since it is a JavaScript comment, it will not be rendered in the JSX.
Depending on your style preference, you can either do it on the line directly before the code
{/* eslint-disable-next-line */}
<li key={faqKey} className={styles.entry} onClick={handleExpandFn}>
or, at the end of the same line
<li key={faqKey} className={styles.entry} onClick={handleExpandFn}> {/* eslint-disable-line */}
Check out the docs for more information on using JavaScript inside of JSX.
Upvotes: 1
Reputation: 765
Here's how you could revise the markup to be semantically appropriate and get the onclick off the <li>
<li key={faqKey} className={styles.entry}>
<h3>
<button type='button' onClick={handleExpandFn}>
<span className={`icon-next ${questionClassname}`} />
<span className={styles['question-text']}>{faqEntry.question}</span>
</button>
</h3>
<div className='add_a_panel_class_name_here'>
{faqEntry.answer}
</div>
</li>
So with the above:
the <h3>
will make the titles of the FAQs easily searchable by screen reader users that are navigating by headings
placing the <button>
inside of the <h3>
gives you the appropriate element to attach a click event to (you want to use a button here because you're toggling state. an <a>
should be used if you were going to a new page. You also don't need to add a tabindex to a button as they are inherently focusable).
There are some additional steps you'd likely want to implement, using ARIA expanded and controls attributes on the button, but the above should get you to a good base for moving beyond your error.
Upvotes: 15
Reputation: 40814
Instead of rolling my implementation of faq table with collapse/expand interactive feature, I replace it with react-collapsible
.
It of course gets rid of the jsx-a11y/no-static-element-interactions
as a result.
Upvotes: -1
Reputation: 1905
One solution I can remember is to use an anchor element with tab-index.
<a style={{display: 'list-item'}} tabIndex={-42} key={faqKey} className={styles.entry} onClick={handleExpandFn}>
<div>
<span className={`icon-next ${questionClassname}`} />
<span className={styles['question-text']}>{faqEntry.question}</span>
</div>
{faqEntry.answer}
</a>
Upvotes: 9