英文:
Web components + A11y
问题
- Web components是否与a11y工具完全兼容?
- 如果有多个web组件封装,是否会存在一些限制?
- 是否有一些建议或参考资料?
- 是否有任何可以正确理解web组件的工具(例如wave.webaim)?
英文:
I have a few questions about web components and Accessibility, I read some articles but I never see any examples or case histories that I can test by myself. I did some tests but I'm not sure they were exhaustive.
My questions are:
- Web components are completely compatible with a11y tools?
- Are some limitations if there are many web components encapsulations?
- Are some guidelines or references?
- there are any tools (es. wave.webaim) than understand web components correctly?
答案1
得分: 4
2022年底,我进行了对Web组件的全面分析,以确定其在我的企业组织中的可能用途。我进行了测试,使用了自定义Web组件以及一些预先构建的框架。
以下是我的看法,其他人可能会不同意。
Web组件是否与辅助功能工具兼容?
有点。Web组件与现有的辅助功能工具在某种程度上兼容,前提是工具知道它在寻找什么。
由于Web组件使用自定义元素名称,工具更难识别元素之间的关系(例如父子关系),并且在查找问题时比使用通用HTML元素(如<p>
或<li>
)更困难。
在我的测试中,我使用的所有工具都能够检测到自定义Web组件并识别简单的问题,比如颜色对比,没有问题。这些工具包括Lighthouse、Axe和Wave。
Web组件的辅助功能限制
一个主要问题是跨影子DOM的aria委托,基本上意味着浏览器不能正确地将影子DOM内部的元素与光DOM中的元素关联起来,尤其是在涉及aria-
属性、input
和label
元素等方面。
我目前在新项目中使用自定义元素,但因为这个原因和其他原因,我还没有使用影子DOM。
我遇到的另一个与辅助功能相关的限制是,辅助功能通常没有内置到我所做的事情中,就像如果我使用Bootstrap或者SvelteKit一样。
对于像移动导航菜单、工具提示、模态框或类似的交互式组件,你确实需要知道自己在做什么。你可能需要自己实现所有正确的aria-
属性,并且可能需要编写一些JavaScript代码来确保像选项卡或多级菜单这样的东西的可访问键盘行为。
如果元素之间存在父子关系(例如自定义元素的输入和标签),浏览器将不具备语义HTML或内容模型的优势。一切基本上都是通用元素。这几乎就像ARIA的早期阶段,一切都是div
。
由于封装的原因,还有一些CSS相关的问题在DevTools中无法正常工作。这导致像辅助功能树面板或计算属性面板等工具无法正确显示。
指南或参考资料
你应该能够阅读和理解WCAG和ARIA规范,而不依赖于自动化工具告诉你什么是可以的,什么不可以的。工具应该作为半自动的时间节省工具,而不是知识的来源。
你应该能够阅读HTML和JS规范并理解它们的内容。
你应该能够在关闭显示器的情况下,使用屏幕阅读器对应用程序、页面和组件进行自己的手动辅助功能测试。
了解Web组件的工具
辅助功能对象模型是由Google、Apple和Mozilla在W3C进行孵化的实验性JavaScript API。它提出了几个新功能,旨在解决现有的辅助功能用例。但可能还需要几年时间才能使用。
注意事项
我想关键在于你如何创建你的组件。你可以扩展规范中的现有HTML元素(例如class extends HTMLParagraphElement
),或者你可以创建一个完全自定义的元素(例如class extends HTMLElement
)。我所有没有涉及框架的测试都采用了后一种方法。
我所阅读的所有信息似乎都表明,扩展本机HTML元素是一个不好的做法,如果规范以人们没有预料到的方式发生变化,可能会导致问题。
一般来说,扩展你不拥有的对象是不好的做法。
总结
在评估之后,我的结论是Web组件有很大的潜力,但目前技术的现状使得实际使用变得非常困难。使用Chrome DevTools来调试组件非常令人沮丧。
我认为,目前来说,这可能更麻烦而不值得,至少要等到更好的工具变得更加普及。封装是相当酷的,我喜欢Web组件非常接近于React,可以在浏览器中原生运行的想法。
与此同时,Svelte与Web组件非常相似,但具有更好的开发工具和辅助功能。如果它适用于你的用例,我建议作为未来几年的短期解决方案。
英文:
In late 2022, I did a comprehensive analysis of web components for possible use in my enterprise organization. I conducted testing using my own custom web-components as well as a number of pre-built frameworks available.
Here's my take on the issue. Others will probably disagree with me.
Are Web-Components Compatible with A11y Tools?
Kind of. Web-components are somewhat compatible with existing a11y tools, to the extent that the tool knows what it's looking for.
Because web-components utilize custom element names, it can be more difficult for a tool to identify relationships between elements (e.g. parent-child relationships) and find issues when compared to using generic HTML elements like <p>
or <li>
.
In my testing, all of the tools that I used were able to detect the custom web-components and identify simple issues like color-contrast without a problem. These tools included Lighthouse, Axe, and Wave.
Accessibility Limitations of Web-Components
One major issue is cross-shadow aria delegation, which basically means that browsers can't correctly associate elements inside the shadow DOM with elements in the light DOM, especially when it comes to things like aria-
attributes, input
and label
elements.
I am currently using custom elements in new projects, but for this reason and others, I'm not using shadow DOM just yet.
Another accessibility-related limitation that I encountered was that accessibility wasn't often baked into what I was doing the way it would be if I were using something like Bootstrap or even SvelteKit.
With interactive components like mobile navigation menus, tooltips, modals, or anything similar, you really have to know what you're doing. You'll likely have to implement all the correct aria-
attributes yourself and you'll probably need to write some JS to ensure accessible keyboard behavior for things like tabs or multi-level menus.
If you have any parent-child relationships between elements (e.g input and label that are custom elements), the browser won't have the benefit of semantic HTML or content models. Everything is essentially a generic element. It's almost like the early days of ARIA where everything was a div
.
There are also a number of CSS-based issues that don't work in DevTools due to the encapsulation. This leads to tools like the Accessibility Tree panel or the Computed Properties panel not displaying correctly.
Guidelines or References
You should be able to read and understand the WCAG and ARIA specs without relying on automated tools to tell you what is and isn't okay. The tools should be there as a semi-automated time saver rather than a source of knowledge.
You should be able to read HTML and JS specifications and understand what they say.
You should be able to do your own manual accessibility testing on the application, page, and component levels using a screen reader with your monitor turned off.
Any Tools that Understand Web-Components
The Accessibility Object Model is an experimental JavaScript API being incubated at the W3C by Google, Apple, and Mozilla. It proposes several new features intended to solve existing accessibility use cases. This is probably still a few years away from being useable though.
Caveats
I suppose it matters how you create your components. You can either extend an existing HTML element that's part of the spec (e.g. class extends HTMLParagraphElement
) or you can create a fully custom element (e.g. class extends HTMLElement
). All of my testing that didn't involve frameworks utilized the latter approach.
Everything that I read seemed to indicate that extending native HTML elements was a bad idea and could lead to problems down the road if the spec changes in ways that people didn't anticipate.
Generally speaking, it's bad practice to extend objects that you don't own.
Synopsis
My determination after the evaluation was that there's a lot of potential in web-components, but the current state of the technology makes practical use quite difficult. Using Chrome DevTools to de-bug components was extremely frustrating.
I think that it's probably more trouble than it's worth for right now -- at least until some better tooling becomes more ubiquitous. The encapsulation is pretty cool, and I like the idea that web components are very close to being React that just runs natively in a browser.
In the meantime, Svelte is remarkably similar to web-components, but has better developer tooling and accessibility. If it's a fit for your use-case, I'd recommend that as a short-term solution for the next few years.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论