New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn when using onclick on non-clickable elements #59
Comments
I like this idea and I like when you said, "I'm not sure if we should be that aggressive and ban all elements except buttons and anchor tags." Consider the below CodePen for example, it uses CodePen: https://codepen.io/jackdomleo7/pen/yLJLOQr Maybe we could show them as warnings? |
Yes, I agree. Hm...the problem with that approach is that clickable elements still need to be focusable. I think you're right. A warning like you're suggesting would work better. There's too many variables to consider. PS: For this specific example, I'd still use a button as the overlay and style it to not look like a button :) |
I think this may need a little more thought. 😊 I'd strongly argue the overlay doesn't need to be a |
Yep, I was agreeing :) Probably just warning would suit this better. |
Sorry, I misudnerstood 😅 Yes this request has sparked another idea I have, I'll have to think on it. But maybe a warning message like "WARNING: Using a click event here may be inaccessible. Either make this a clickable element such as or or ensure you have provided an alternative accessible means of performing the same fuctionality." That message is probably a bit long. |
What elements should we check for or not check for? Only |
How about
|
This a good question! If we go with a warning, would it be sensible to check for all elements that are not button or anchors? |
#48 is a similar issue. I'll close it as a duplicate of this one because the conversation here goes beyond the other one. From that issue, consider adding more events than [onclick],
[ondblclick],
[onmouseover],
[onmouseenter],
[onmouseleave],
[onmousedown],
[onmouseup] {
/* warning/error */
} Also, the selector from the shared demo would need to be changed slightly. The current one will throw false positives as it targets *:not(a)[onclick]::after,
*:not(button)[onclick]::after {
/* this will give false positives for `<button onclick="doSomething()">text</button>` which is valid */
}
[onclick]:not(a):not(button)::after {
/* this will target elements with inline onclick that are not `a` or `button` */
} |
@alvaromontoro very good point, thank you! I will incorporate those as well. EDIT: Wait...I don't know if this is a go yet :D |
About the overlay exception, I would still show the warning because –and I understand that this is a personal choice and not exactly related to a11y:
|
I would agree with that. 👍 I'll put together a pen to see how it would work, if any other exceptions come to mind. |
Awesome! I'll assign you for the time being 😊 |
That's perfect, thanks @jackdomleo7! This is what I came up with and seems to do the job: https://codepen.io/tricinel/pen/VwjZEPK And then I figured, well, with some work you can make a div be accessible, keyboard and all, so I tried an experiment: https://codepen.io/tricinel/pen/KKMKYya. But I haven't figured out yet why the selector doesn't work. :( If you have any ideas, I'd appreciate them. The experiment might be not worth it in the end, but still figured it was worth the hassle to at least see if it's possible. Thanks! |
It's always good to do some proof of concept! 😊 Hmm... That is odd. I'm determined to get that second one working. Let me take a closer look and see what's going on. If anyone else can figure it out, please shout out! |
Weirdly enough, different combinations of the :not selector will work, but I haven't yet figured out why...Order shouldn't matter as far as I know... Looking forward to seeing what you come up with. |
Based on the previous examples, I tried a slightly different approach (hopefully not too overcomplicated). It has an explanation of the rules: https://codepen.io/alvaromontoro/pen/JjKdXBQ. But it doesn't take into account |
@alvaromontoro I think it works without, as long as the div has an actual text element, or something labelled properly. Maybe we can discount that...as in, that should be a separate issue/warning. Your demo looks good! |
@alvaromontoro @jackdomleo7 what are we thinking on this? I played around a little bit more on a side project with Alvaro's implementation for onclick and it worked fine. Would we use that onclick implementation, combined with the other on* mouse/touch events, showing the same warning? |
I think I'm confident with @alvaromontoro's build on your build @tricinel. I think you've both established something reliable there and from the testing I've been able to do, there are no immediate things that I can see as "wrong". So I'm happy with this 😊 |
Sweet! Sounds great! @alvaromontoro do you want to take it or should I? |
FYI, @alvaromontoro @jackdomleo7, I'm working on this...should be done today. 🚀 |
When HTMLElements use mouse event handlers such as onclick, ondblclick, onmouseup/down, onmouseenter/leave or onmouseover, without providing the proper alternatives (i.e. onkeydown/up/press, no or wrong tabindex and no or wrong role), they could become unreachable via the keyboard. We therefore should warn users of the danger, but it is ultimately up to them if they want to ignore it because they might have, for example, provided some alternative way of doing the same action. This excludes <a> and <button> elements. fixes jackdomleo7#59
When HTMLElements use mouse event handlers such as onclick, ondblclick, onmouseup/down, onmouseenter/leave or onmouseover, without providing the proper alternatives (i.e. onkeydown/up/press, no or wrong tabindex and no or wrong role), they could become unreachable via the keyboard. We therefore should warn users of the danger, but it is ultimately up to them if they want to ignore it because they might have, for example, provided some alternative way of doing the same action. This excludes <a> and <button> elements. fixes #59
Describe the new a11y feature or project enhancement
I see this quite often in code bases, where devs will attach an
onclick
to somediv
orspan
and call it a day. This breaks keyboard navigation. Worse...sometimes there are onclick handlers that will just navigate the user to another page. This specifically fails Success Criterion 1.3.1 and 2.1.1.Describe the solution you'd like
I quickly mocked this up in codepen: https://codepen.io/tricinel/pen/VwjZEPK.
I'm not sure if we should be that aggressive and ban all elements except buttons and anchor tags.
Link(s)
https://www.w3.org/TR/2013/NOTE-WCAG20-TECHS-20130905/F42
The text was updated successfully, but these errors were encountered: