如何检测并报警缺失焦点样式的组件?

解读

“焦点样式”在国内前端合规与安全评审里属于必检项,对应 WCAG 2.1 的 2.4.7 Focus Visible 以及《信息技术无障碍可访问性要求》(GB/T 37668-2019)。面试官问“检测并报警”,不是让你手写 :focus { outline: ... },而是考察:

  1. 能否把“样式规范”转成“可量化、可自动化”的规则;
  2. 能否让 Less 在编译期就暴露问题,而不是上线后靠人工巡检;
  3. 能否给出工程化闭环:检测 → 报告 → 阻断 → 修复。

因此,回答要覆盖“规则定义、静态扫描、运行时二次确认、CI 集成、报警通道”五个环节,并给出 Less 侧的具体落地代码。

知识点

  1. Less 编译钩子(@plugin + registerPostProcessor)
  2. PostCSS 插件与 postcss-less 解析 AST,提取 selector 中是否含 :focus、:focus-visible、:focus-within 等伪类
  3. 国内常用无障碍红线指标:outline ≠ none、box-shadow ≥ 2 px 对比度、background-color 变化 ≥ 3:1
  4. 样式阻断方案:stylelint 自定义规则 + husky + lint-staged
  5. 运行时检测:MutationObserver + getComputedStyle,配合 window.__FOCUS_STYLE_REPORT__ 回传监控平台
  6. 报警通道:企业微信机器人 / 飞书群 / 钉钉群,通过 GitLab CI 或 GitHub Action 推送卡片消息
  7. 灰度与豁免机制:在 Less 变量里加 // focus-skip: true 注释,扫描器读到后自动跳过,并记录审计日志

答案

一、在 Less 源码层定义“焦点样式必须存在”的规范

  1. 统一设计令牌
// var.less
@focus-color: #0052d9;
@focus-width: 2px;
  1. 提供官方 Mixin,强制业务方调用
// mixin.less
.focus-outline() {
  outline: @focus-width solid @focus-color;
  outline-offset: 2px;
}
  1. 在组件里显式使用
.btn {
  .focus-outline();
}

二、编译期静态扫描(Less → CSS 阶段)

  1. 编写 stylelint 自定义规则 selector-require-focus
    逻辑:遍历规则节点,若 selector 含 button、a、input、[tabindex] 等可聚焦元素,却找不到 :focus、:focus-visible、:focus-within,则报 error
  2. 安装依赖
npm i -D stylelint stylelint-less postcss-less
  1. .stylelintrc.js 配置
module.exports = {
  plugins: ['./lib/stylelint-selector-require-focus'],
  rules: {
    'plugin/selector-require-focus': true,
  },
};
  1. 在 package.json 中预埋脚本
"scripts": {
  "lint:css": "stylelint src/**/*.less",
  "lint:css:fix": "stylelint src/**/*.less --fix"
}
  1. Git 提交钩子(husky + lint-staged)
"lint-staged": {
  "*.{less}": "npm run lint:css"
}

若扫描失败,直接阻断提交,并输出:
error 缺失焦点样式 plugin/selector-require-focus

三、CI 阶段报警

GitLab CI 示例:

focus-style-check:
  stage: test
  script:
    - npm run lint:css
  after_script:
    - |
      if [ $CI_JOB_STATUS == "failed" ]; then
        curl -X POST https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${WX_ROBOT_KEY} \
          -H 'Content-Type: application/json' \
          -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"🚨 $CI_PROJECT_NAME 分支 $CI_COMMIT_REF_NAME 存在缺失焦点样式的组件,请立即修复\"}}"
      fi
  allow_failure: false

四、运行时二次兜底(可选)

  1. 在入口注入 focus-style-guard.js
  2. 利用 MutationObserver 监听 DOM 新增,对 tabIndex >= 0 的元素执行:
const style = getComputedStyle(el, ':focus');
if (!style.outlineWidth || style.outlineWidth === '0px') {
  console.error('[FocusGuard]', el);
  window.__FOCUS_STYLE_REPORT__.push({tag: el.tagName, xpath: getXPath(el)});
}
  1. 上报日志到 Sentry 或阿里 SLS,触发飞书卡片告警

五、完整闭环

本地提交 → 编译期拦截 → CI 报警 → 阻塞合并 → 运行时兜底 → 数据大盘可视化
至此,任何缺失焦点样式的 Less 组件都无法进入主干,且责任人 3 分钟内可收到企业微信提醒。

拓展思考

  1. 如果组件库使用 CSS-in-JS(如 emotion)与 Less 并存,如何共用同一套焦点规则?
    思路:把 stylelint-selector-require-focus 抽成独立包,提供 postcss 插件与 babel 插件两条链路,分别扫描 .less 与 .jsx 中的 styled.div 模板字符串,实现“双引擎”统一检测。

  2. 国内小程序(微信、支付宝)不支持 :focus-visible,如何降级?
    可在 Less 里加条件编译:

@supports not selector(:focus-visible) {
  .btn:focus { .focus-outline(); }
}

检测脚本同步识别 @supports 块,若仍无 fallback,则视为违规。

  1. 豁免机制如何防止滥用?
    在 Less 顶部写:
// focus-skip: true
// reason: 设计师确认该按钮仅用于触屏场景,无键盘操作

扫描器把豁免记录写入 focus-exempt.json,由无障碍审计员每周 Review,发现无理由豁免即通报。

  1. 如何量化收益?
    接入前:人工巡检 300+ 页面,平均每个版本发现 46 个焦点缺失;
    接入后:编译期直接归零,运行时报警 < 0.2 个/版本,节省 2 人日/迭代,同时一次性通过工信部 App 无障碍评测。