mcpField
A
accessibility-wcag

accessibility-wcag

github.com/rohitg00设计与 UI2.8k225 次安装24 周前更新
代码仓库

关于 accessibility-wcag

为前端开发者提供 WCAG 无障碍实践清单,涵盖语义化 HTML、ARIA、表单和键盘操作检查。

主要功能

  1. 01语义化 HTML 地标
  2. 02ARIA 交互模式
  3. 03键盘可达性检查
  4. 04表单无障碍检查
  5. 05屏幕阅读器测试建议

适用场景

  1. 01检查页面导航结构
  2. 02改进模态框键盘操作
  3. 03审核表单标签与错误提示

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/rohitg00/awesome-claude-code-toolkit --skill accessibility-wcag

安装后在 Claude Code 中输入/accessibility-wcag确认已加载。

详细介绍

无障碍与 WCAG

语义化 HTML


<header>
  <nav aria-label="Main navigation">
    <ul>
      <li><a href="/" aria-current="page">Home</a></li>
      <li><a href="/products">Products</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </nav>
</header>

<main>
  <article>
    <h1>Product Details</h1>
    <section aria-labelledby="specs-heading">
      <h2 id="specs-heading">Specifications</h2>
      <dl>
        <dt>Weight</dt>
        <dd>1.2 kg</dd>
        <dt>Dimensions</dt>
        <dd>30 x 20 x 10 cm</dd>
      </dl>
    </section>
  </article>
</main>

<footer>
  <p>&copy; 2024 Company Name</p>
</footer>

使用 <nav>、<main>、<article>、<section>、<aside> 代替 <div> 作为页面地标。屏幕阅读器会利用这些元素在页面中导航。

ARIA 模式

function Modal({ isOpen, onClose, title, children }) {
  if (!isOpen) return null;

  return (
    <div
      role="dialog"
      aria-modal="true"
      aria-labelledby="modal-title"
      onKeyDown={(e) => e.key === "Escape" && onClose()}
    >
      <h2 id="modal-title">{title}</h2>
      <div>{children}</div>
      <button onClick={onClose} aria-label="Close dialog">
        <XIcon aria-hidden="true" />
      </button>
    </div>
  );
}

function Tabs({ tabs, activeIndex, onChange }) {
  return (
    <div>
      <div role="tablist" aria-label="Settings sections">
        {tabs.map((tab, i) => (
          <button
            key={tab.id}
            role="tab"
            id={`tab-${tab.id}`}
            aria-selected={i === activeIndex}
            aria-controls={`panel-${tab.id}`}
            tabIndex={i === activeIndex ? 0 : -1}
            onClick={() => onChange(i)}
            onKeyDown={(e) => handleArrowKeys(e, i, tabs.length, onChange)}
          >
            {tab.label}
          </button>
        ))}
      </div>
      {tabs.map((tab, i) => (
        <div
          key={tab.id}
          role="tabpanel"
          id={`panel-${tab.id}`}
          aria-labelledby={`tab-${tab.id}`}
          hidden={i !== activeIndex}
          tabIndex={0}
        >
          {tab.content}
        </div>
      ))}
    </div>
  );
}

键盘导航

function handleArrowKeys(
  event: React.KeyboardEvent,
  currentIndex: number,
  totalItems: number,
  onSelect: (index: number) => void
) {
  let newIndex = currentIndex;

  switch (event.key) {
    case "ArrowRight":
    case "ArrowDown":
      newIndex = (currentIndex + 1) % totalItems;
      break;
    case "ArrowLeft":
    case "ArrowUp":
      newIndex = (currentIndex - 1 + totalItems) % totalItems;
      break;
    case "Home":
      newIndex = 0;
      break;
    case "End":
      newIndex = totalItems - 1;
      break;
    default:
      return;
  }

  event.preventDefault();
  onSelect(newIndex);
}

所有交互元素都必须可以通过键盘访问。使用 Tab 键移动焦点,使用 Enter/Space 键激活元素,使用方向键在组件内导航。

表单无障碍

function SignupForm() {
  return (
    <form aria-labelledby="form-title" noValidate>
      <h2 id="form-title">Create Account</h2>

      <div>
        <label htmlFor="email">Email address</label>
        <input
          id="email"
          type="email"
          required
          aria-required="true"
          aria-describedby="email-hint email-error"
          aria-invalid={hasError ? "true" : undefined}
        />
        <p id="email-hint">We will never share your email.</p>
        {hasError && (
          <p id="email-error" role="alert">
            Please enter a valid email address.
          </p>
        )}
      </div>

      <button type="submit">Create Account</button>
    </form>
  );
}

颜色与对比度

:root {
  --text-primary: #1a1a1a;      /* 15.3:1 on white */
  --text-secondary: #595959;    /* 7.0:1 on white */
  --text-on-primary: #ffffff;   /* Ensure 4.5:1 on brand color */
  --border-focus: #0066cc;      /* Visible focus ring */
}

*:focus-visible {
  outline: 3px solid var(--border-focus);
  outline-offset: 2px;
}

.error-message {
  color: #d32f2f;
  /* Don't rely on color alone - add icon or text prefix */
}
.error-message::before {
  content: "Error: ";
  font-weight: bold;
}

WCAG AA 要求普通文本的对比度为 4.5:1,大号文本(18px 粗体或 24px 常规字重)的对比度为 3:1。

反面模式

  • 使用 div 和 span 实现可点击元素,而不使用 button 或 a
  • 移除焦点轮廓,却没有提供替代指示
  • 仅依靠颜色传达信息(错误用红色表示,成功用绿色表示)
  • 元素已有可见文本标签时,仍使用 aria-label
  • 自动播放媒体,却没有暂停机制
  • 缺少供键盘用户使用的跳转导航链接

检查清单

  • 所有交互元素均可通过键盘访问(Tab、Enter、Escape、方向键)
  • 使用语义化 HTML 地标(nav、main、article、section)
  • 图片具有描述性 alt 文本(装饰性图片使用 alt="")
  • 颜色对比度符合 WCAG AA(普通文本为 4.5:1,大号文本为 3:1)
  • 所有交互元素都显示焦点指示
  • 表单输入框具有关联的 <label> 元素
  • 错误消息通过 role="alert" 告知屏幕阅读器
  • 使用屏幕阅读器(VoiceOver、NVDA)和纯键盘进行测试

评论

支持 Markdown