RSPEC-1082: Mouse events should have corresponding keyboard events is triggered on custom buttons with only onclick
but not on native buttons.
Consider the following snippet:
<p><button type="button" onclick="pleaseDoIt()">Please do it</button></p>
<p><span type="button" role="button" tabindex="0" onclick="justDoIt()">Just do it</span></p>
<p><custom-button type="button" onclick="doItAgain()">Do it again</custom-button></p>
<p><custom-button type="button" onclick="continueDoinIt()" role="button" tabindex="0">Continue doing it</custom-button></p>
- The native
button
element does not trigger an error message, even though it only appears to have a click even listener and no keyboard event listener. This makes sense; see WCAG Technique SCR35: Making actions keyboard accessible by using the onclick event of anchors and buttons, which says,While âonclickâ sounds like it is tied to the mouse, the onclick event is actually mapped to the default action of a link or button. The default action occurs when the user clicks the element with a mouse, but it also occurs when the user focuses the element and hits enter or space, and when the element is triggered via the accessibility API.
- The
span
-based button triggers the error message âAdd a âonKeyPress|onKeyDown|onKeyUpâ attribute to this tag.â, even though, based on the above, one may think it shouldnât. (It also triggers the warning âUse<button>
or<input>
instead of the button role to ensure accessibility across all devices.â, which is justified but outside the scope of this topic.) - The
custom-button
-based button triggers essentially the same error message as above: âAdd a âonKeyPress|onKeyDown|onKeyUpâ attribute to this<custom-button>
tag.â It does not take into account thatcustom-button
can be a web component that has a native and perfectly accessible HTMLbutton
in its shadow DOM. - The last button generates the same error message as the previous one and the same warning as the second button.
Given the above variations, can the rule RSPEC-1082 reliably judge whether the error message about missing keyboard handlers is justified?
Related topic: False positive for S1082 (Angular keyup.enter event not recognized), posted in April 2024.