eslint-plugin-vue-a11y icon indicating copy to clipboard operation
eslint-plugin-vue-a11y copied to clipboard

click-events-have-key-events applies to native buttons and links

Open TheAlexLichter opened this issue 5 years ago • 11 comments

Native elements like <button> or <a> (also counting <nuxt-link> / <router-link> into it) don't need additional handlers beside a @click or @click.native listener.

Adding another handler for keydown events will actually lead to bugs and double execution of the function.

Maybe we should add a whitelist to that rule

TheAlexLichter avatar Feb 26 '19 12:02 TheAlexLichter

<router-link> actually render to <a> by default , so you don't need use <a> outside。maybe you can use <!-- eslint-disable-line --> in complex situation @manniL

maranran avatar May 20 '19 01:05 maranran

@maranran Sorry, I don't understand 🤔

TheAlexLichter avatar May 20 '19 07:05 TheAlexLichter

@maranran I'm having this problem even with button elements:

 <button class="button" @click="save">
     {{ buttonText }}
 </button>

Eslint throws: Visible, non-interactive elements with click handlers must have at least one keyboard listener.

I'm using the extends recommended

rafaelmaiach avatar Jun 21 '19 15:06 rafaelmaiach

I'm having this issue too with button elements. @maranran it looks like there's a commented-out line with a todo in https://github.com/maranran/eslint-plugin-vue-a11y/blob/master/lib/rules/click-events-have-key-events.js regarding interactive elements, which I assume pertains to buttons, links etc- I'd be happy to have a go at getting this to work and making a PR.

i-hardy avatar Aug 09 '19 11:08 i-hardy

Can we get some progress on this. Adding the recommended setting causes the event to be triggered twice. As an example

<button @click="toggle">
    <i class="fa fa-angle-down accordion-icon" :class="{ rotate: show }"></i>
</button>

The above code is fully accessible using keyboard and mouse navigation but will fail the linting.

On making the recommended change to <button @click="toggle" @keyPress="toggle"> will trigger the function call twice when using a keyboard.

juliamoses avatar Nov 06 '19 17:11 juliamoses

@juliamoses This seems to work for the button example when fixing the todo that @i-hardy mentioned.

I've done this in my fork here https://github.com/matthewdouglas/eslint-plugin-vue-a11y/commit/f1165191940a8a2a6fee426073123d5e95acb708

For the anchor tags, they need to have a href attribute to meet this rule. <a href="" @click.prevent="something">click me</a>

matthewdouglas avatar Nov 14 '19 21:11 matthewdouglas

Would be cool if this could get fixed.

juliandreas avatar Nov 19 '19 20:11 juliandreas

@maranran What was the reasoning behind commenting out https://github.com/maranran/eslint-plugin-vue-a11y/blob/master/lib/rules/click-events-have-key-events.js#L31? Is @matthewdouglas' fork the solution, or has it side effects? Can contributors help in any way regarding this? I'm eager to support, because this is a critical false negative that could keep developers from using eslint-plugin-vue-a11y altogether.

marcus-herrmann avatar Jan 19 '20 07:01 marcus-herrmann

@marcus-herrmann I'm not aware of any side effects.

This plugin is based on the react version, which has had the isInteractiveElement check since the rule was first added: https://github.com/evcohen/eslint-plugin-jsx-a11y/commit/ec4f193c8117be7f07a8d2054d97fe7a0c1b6473#diff-aeea5a0f9f93bda6f7d13946feb947bf

That said, I'm not entirely sure if the underlying implementation for isInteractiveElement is the same.

matthewdouglas avatar Jan 20 '20 18:01 matthewdouglas

Either way, I forked, removed the comment, and adapted it a little, like this: https://github.com/vue-accessibility/eslint-plugin-vue-a11y/blob/master/lib/rules/click-events-have-key-events.js#L31

In a project using this fork the rule works as intended. Failing at <div @click="" />, but accepting <button @click="" />, same with <a>.

marcus-herrmann avatar Jan 21 '20 10:01 marcus-herrmann

Can this fix be added to this solution via PR instead of a fork?

It makes any tools relying on es-lint-plugin-vue-a11y unusable (e.g. the VS Code extension) in a team setting as it forcing devs to have the knowledge to ignore this one warning

grace-snow avatar Sep 10 '21 09:09 grace-snow