Accessibility 辅助功能总结

总结一下接手项目以来Accessibility的一些收获,这个产品要求支持辅助功能,虽然已经是按照最低要求,但做起来即要考虑全键盘可以操作,也要考虑所有显示的内容和需要交互的内容可以被讲述人读出来,还要考虑高对比下的样式兼容,也是有不少的维护开发成本。

css可以通过特定选择器来指定高对比度模式下的样式

js 复制代码
@media screen and (forced-colors: active) {
 color: ButtonText;
 backgroundColor: ButtonFace;
}
// 判断系统主题
@media (prefers-color-scheme: dark) {
  .brand-text {
    color: #ffffff;
  }
}

可用的内置变量:

1. Button
Keyword Meaning
ButtonBorder Border color of native buttons/controls (e.g., the outline of a button).
2. Text & content
Keyword Meaning
Canvas Background color of the main content area (e.g., a window's background).
CanvasText Text color for content on the Canvas background.
LinkText Color of unvisited hyperlinks (matches system's default link color).
VisitedText Color of visited hyperlinks.

3. Highlight & selection

Keyword Meaning
Highlight Background color of selected text/elements (e.g., highlighted text).
HighlightText Text color of selected content (contrasts with Highlight).

4. UI chrome (window/frame elements)

Keyword Meaning
ActiveBorder Border color of active windows/dialogs.
ActiveCaption Background color of the title bar of an active window.
ActiveCaptionText Text color in the title bar of an active window.
InactiveBorder Border color of inactive windows/dialogs.
InactiveCaption Background color of the title bar of an inactive window.

5. Status & feedback

Keyword Meaning
GrayText Color of disabled text (e.g., grayed-out buttons/options).
InfoBackground Background color of tooltip/info panels.
InfoText Text color in tooltip/info panels.

html inert 属性

学到了一个新的且很重要的html属性 inert,这个词表示 "使惰性",它可以让元素中的用户交互事件都失效,适合展示顶层窗口时,把交互trap到当前窗口中。 `

js 复制代码
export function disableApp() {
  document.getElementById('app')?.setAttribute('inert', '')
}
export function enableApp() {
  document.getElementById('app')?.removeAttribute('inert')
}

焦点 管理

如果不是做 accessibility, 我都不知道聚焦是一个这个难以处理的东西。不过也是因为这个项目之前给第三方组件库提了需求,要求弹窗弹出来自动聚焦第一个可聚焦的元素,但是可能没区分好触发方式:

  1. 键盘导航触发 (应该自动聚焦)
  2. 鼠标点击触发(不应该自动聚焦)
  3. 编程式触发 xxx.focus() (不应该自动聚焦,但组件库聚焦了)

为了不牵扯第三方去修改,我们监听了focus事件,发现有自动聚焦但不想聚焦的时候,就去设置blur,其中监听事件有两种:

js 复制代码
parentElement.addEventListener('focus', handlefocus, { capture: true, once: true })
等价于
parentElement.addEventListener('focusin', handlefocus, { once: true }) //childElement也会触发
}

几种关于聚焦元素的伪类选择器

js 复制代码
*:focus
*:focus-within
*:focus-visible  如果没有意外情况,这个选择器本身就只有在键盘导航时才显示样式,已经和鼠标做了区分

判断DOM元素是否可聚焦或者已经聚焦

js 复制代码
// 检查焦点相关状态
function diagnoseFocus(element) {
  return {
    hasFocusVisible: element.matches(':focus-visible'),
    hasFocus: element.matches(':focus'),
    isActive: element.matches(':active'),
    tabIndex: element.tabIndex,
    computedOutline: getComputedStyle(element).outline,
    focusVisibleSupported: CSS.supports('selector(:focus-visible)')
  };
}

// 实时监控
document.addEventListener('focusin', (e) => {
  console.log('焦点诊断:', diagnoseFocus(e.target));
});

其实非预期的行为大多出现在编程式触发,让组件库在调用 focus()方法的时候加一个 flag,使用组件库的控制起来会方便很多。

相关推荐
爱丶不疚2 天前
Electron net 模块你可以没用过,但不能不知道
前端·electron
瑞码空间6 天前
Web应用的多端部署之道:浏览器 · Electron · Docker
前端·docker·electron·浏览器·web
upgrador14 天前
桌面应用开发:Electron 与 NSIS 的关系、打包流程及 Windows 本地构建实战
javascript·windows·electron
盒子里的加菲猫15 天前
什么?不会C语言也可以搭建窗口级应用程序?(Electron)
c语言·开发语言·electron
薛定谔的猫-菜鸟程序员17 天前
基于 Electron 的本地短视频解析与下载工具:架构设计与工程实践
java·electron·音视频
imber17 天前
别把多平台发布当复制粘贴:一套可复用的内容工程工作流
electron
imber17 天前
Visual Muse 三平台真实发布验收
electron
前端逗比逗17 天前
Electron 全维度完整配置手册(最新稳定版,适配 Electron 25+)
前端·electron
寒水馨21 天前
Windows下载、安装electron-v43.2.0(附安装包electron-v43.2.0-win32-x64.zip)
javascript·windows·typescript·electron·跨平台·桌面应用·chromium
寒水馨21 天前
macOS下载、安装electron-v43.2.0(附安装包electron-v43.2.0-darwin-arm64.zip)
javascript·macos·electron·node.js·跨平台·桌面应用开发·开源框架