跳到主内容
智客 ZICQ

技能库 智客分类:Agent 工作流 frontend-a11y

前端 A11y 键

React and Next.js的可访问模式——语义HTML,ARIA属性,形式标签,键盘导航,焦点管理,屏幕阅读器支持. 在构建任何交互式的UI组件或窗体时使用 .

4868 安装量

官方网址:skills.sh

技能介绍

先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。

做什么

React和Next.js的可访问模式——语义HTML,ARIA属性,形式标签,键盘导航,焦点管理,屏幕阅读器支持

何时用

不存在可见标签文本

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:Frontend Accessibility Patterns、When to Activate、Form Accessibility、Label Connection、Required Fields、Error Messages。

文件分析

文件分析:这是一份仅含 SKILL.md 的指令型技能,代理激活后整份正文进入上下文。

官方 description(原文)

Accessibility patterns for React and Next.js — semantic HTML, ARIA attributes, form labeling, keyboard navigation, focus management, and screen reader support. Use when building any interactive UI component or form.

Frontend Accessibility PatternsWhen to ActivateForm AccessibilityLabel ConnectionRequired FieldsError MessagesComplete Accessible FormSemantic HTMLARIA Attributesaria-label vs aria-labelledbyaria-describedbyaria-live for Dynamic Content

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
frontend-a11y
description
Accessibility patterns for React and Next.js — semantic HTML, ARIA attributes, form labeling, keyboard navigation, focus management, and screen reader support. Use when building any interactive UI component or form.
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。

具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗

安装这个技能

Skills CLI ↗

先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。

交给 Agent 安装

复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。

把 Agent Skill「frontend-a11y」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-d73502bddf1c2b57-%E5%89%8D%E7%AB%AF-A11y-%E9%94%AE.html
请存为 .cursor/skills/frontend-a11y/SKILL.md 或 .claude/skills/frontend-a11y/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。

GitHub 完整包 ↗

终端安装 · Skills CLI

需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。

npx skills add 'https://github.com/affaan-m/ecc' --list

npx skills add 'https://github.com/affaan-m/ecc' --skill 'frontend-a11y'

CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。

阅读排版
--- name: frontend-a11y description: > Accessibility patterns for React and Next.js — semantic HTML, ARIA attributes, form labeling, keyboard navigation, focus management, and screen reader support. Use when building any interactive UI component or form. metadata: origin: community --- # Frontend Accessibility Patterns Practical accessibility patterns for React and Next.js. Covers the issues most commonly flagged in code review: missing form labels, incorrect ARIA usage, non-semantic interactive elements, and broken keyboard navigation. ## When to Activate - Building or reviewing form components (``, ``, ``) - Creating interactive elements (modals, dropdowns, tooltips, tabs) - Using `<div>` or `<span>` with `onClick` - Adding `aria-*` attributes to any element - Implementing keyboard navigation or focus management - Receiving accessibility feedback from code review tools (CodeRabbit, ESLint a11y) - Building components that must support screen readers ## Form Accessibility Missing `htmlFor` / `id` pairing and disconnected error messages are the most common issues flagged in code review. ### Label Connection ```tsx // BAD: label has no connection to input — screen readers cannot associate them <label>Email</label> <input type="email" /> // GOOD: htmlFor matches input id <label htmlFor="email">Email</label> <input id="email" type="email" /> ``` ### Required Fields ```tsx // BAD: visual-only asterisk conveys nothing to screen readers <label htmlFor="email">Email *</label> <input id="email" type="email" /> // GOOD: required enables native browser validation; aria-required signals it to screen readers <label htmlFor="email"> Email <span aria-hidden="true">*</span> </label> <input id="email" type="email" required aria-required="true" /> ``` ### Error Messages ```tsx // BAD: error text exists visually but is not linked to the input <input id="email" type="email" /> <span className="error">Invalid email address</span> // GOOD: aria-describedby connects input to its error message // aria-invalid signals the invalid state to screen readers <input id="email" type="email" aria-describedby="email-error" aria-invalid={!!error} /> {error && ( <span id="email-error" role="alert"> {error} </span> )} ``` ### Complete Accessible Form ```tsx interface LoginFormProps { onSubmit: (email: string, password: string) => void; } export function LoginForm({ onSubmit }: LoginFormProps) { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [errors, setErrors] = useState<{ email?: string; password?: string }>({}); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const newErrors: typeof errors = {}; if (!email) newErrors.email = 'Email is required'; if (!password) newErrors.password = 'Password is required'; if (Object.keys(newErrors).length) { setErrors(newErrors); return; } onSubmit(email, password); }; return ( <form onSubmit={handleSubmit} noValidate> <div> <label htmlFor="email"> Email <span aria-hidden="true">*</span> </label> <input id="email" type="email" value={email} onChange={e => setEmail(e.target.value)} aria-required="true" aria-describedby={errors.email ? 'email-error' : undefined} aria-invalid={!!errors.email} autoComplete="email" /> {errors.email && ( <span id="email-error" role="alert"> {errors.email} </span> )} </div> <div> <label htmlFor="password"> Password <span aria-hidden="true">*</span> </label> <input id="password" type="password" value={password} onChange={e => setPassword(e.target.value)} aria-required="true" aria-describedby={errors.password ? 'password-error' : undefined} aria-invalid={!!errors.password} autoComplete="current-password" /> {errors.password && ( <span id="password-error" role="alert"> {errors.password} </span> )} </div> <button type="submit">Log in</button> </form> ); } ``` ## Semantic HTML Use the element that matches the intent. Screen readers and keyboard users depend on native semantics. ```tsx // BAD: div has no role, no keyboard support, no accessible name <div onClick={handleClick}>Submit</div> // GOOD: button is focusable, activates on Enter/Space, announces as "button" <button type="button" onClick={handleClick}>Submit</button> ``` ```tsx // BAD: non-semantic navigation <div onClick={() => navigate('/home')}>Home</div> // GOOD: anchor supports right-click, middle-click, and keyboard navigation <a href="/home">Home</a> ``` ```tsx // BAD: heading hierarchy skipped (h1 to h4) <h1>Dashboard</h1> <h4>Recent Activity</h4> // GOOD: sequential heading levels <h1>Dashboard</h1> <h2>Recent Activity</h2> ``` ## ARIA Attributes Use ARIA only when native HTML semantics are insufficient. Wrong ARIA is worse than no ARIA. ### aria-label vs aria-labelledby ```tsx // aria-label: inline string label — use when no visible label text exists <button aria-label="Close modal"> <XIcon /> </button> // aria-labelledby: references another element's text — use when a visible label exists <section aria-labelledby="section-title"> <h2 id="section-title">Recent Orders</h2> {/* content */} </section> ``` ### aria-describedby ```tsx // Provides supplementary description beyond the label <button aria-describedby="delete-warning" onClick={handleDelete} > Delete account </button> <p id="delete-warning">This action cannot be undone.</p> ``` ### aria-live for Dynamic Content ```tsx // Use aria-live to announce content that updates without a page reload // polite: waits for user to finish current action before announcing // assertive: interrupts immediately — use only for urgent errors export function StatusMessage({ message, isError }: { message: string; isError?: boolean }) { return ( <div role="status" aria-live={isError ? 'assertive' : 'polite'} aria-atomic="true"> {message} </div> ); } ``` ### aria-expanded and aria-controls ```tsx export function Accordion({ title, children }: { title: string; children: React.ReactNode }) { const [isOpen, setIsOpen] = useState(false); const contentId = useId(); return ( <div> <button aria-expanded={isOpen} aria-controls={contentId} onClick={() => setIsOpen(prev => !prev)}> {title} </button> <div id={contentId} hidden={!isOpen}> {children} </div> </div> ); } ``` ## Keyboard Navigation Every interactive element must be reachable and operable by keyboard alone. ### Custom Dropdown ```tsx export function Dropdown({ options, onSelect }: { options: string[]; onSelect: (value: string) => void }) { const [isOpen, setIsOpen] = useState(false); const [activeIndex, setActiveIndex] = useState(0); const listId = useId(); if (!options.length) return null; const handleKeyDown = (e: React.KeyboardEvent) => { switch (e.key) { case 'ArrowDown': e.preventDefault(); setActiveIndex(i => Math.min(i + 1, options.length - 1)); break; case 'ArrowUp': e.preventDefault(); setActiveIndex(i => Math.max(i - 1, 0)); break; case 'Enter': case ' ': e.preventDefault(); if (isOpen) onSelect(options[activeIndex]); setIsOpen(prev => !prev); break; case 'Escape': setIsOpen(false); break; } }; return ( <div role="combobox" aria-expanded={isOpen} aria-haspopup="listbox" aria-controls={listId} tabIndex={0} onKeyDown={handleKeyDown} onClick={() => setIsOpen(prev => !prev)} > <span>{options[activeIndex]}</span> {isOpen && ( <ul id={listId} role="listbox"> {options.map((option, index) => ( <li key={option} role="option" aria-selected={index === activeIndex} onClick={() => { onSelect(option); setIsOpen(false); }} > {option} </li> ))} </ul> )} </div> ); } ``` ## Focus Management Focus must move logically when UI state changes — especially for modals and route transitions. ### Modal Focus Restoration > This example covers initial focus and restoration. For a full focus trap (Tab/Shift+Tab cycling within the modal), use a library like [`focus-trap-react`](https://github.com/focus-trap/focus-trap-react) which handles edge cases like dynamic content and nested portals. ```tsx export function Modal({ isOpen, onClose, title, children }: { isOpen: boolean; onClose: () => void; title: string; children: React.ReactNode }) { const modalRef = useRef<HTMLDivElement>(null); const previousFocusRef = useRef<HTMLElement | null>(null); useEffect(() => { if (isOpen) { // Save currently focused element and move focus into modal previousFocusRef.current = document.activeElement as HTMLElement; modalRef.current?.focus(); } else { // Restore focus to the element that opened the modal previousFocusRef.current?.focus(); } }, [isOpen]); if (!isOpen) return null; return ( <div ref={modalRef} role="dialog" aria-modal="true" aria-labelledby="modal-title" tabIndex={-1} onKeyDown={e => e.key === 'Escape' && onClose()}> <h2 id="modal-title">{title}</h2> {children} <button onClick={onClose}>Close</button> </div> ); } ``` ## Images and Icons ```tsx // BAD: decorative icon announced as unlabeled image <img src="/icon.svg" /> // GOOD: decorative image hidden from screen readers <img src="/decoration.png" alt="" aria-hidden="true" /> // GOOD: meaningful image with descriptive alt text <img src="/chart.png" alt="Monthly revenue increased 23% from January to March" /> // GOOD: icon button with accessible label <button aria-label="Delete item"> <TrashIcon aria-hidden="true" /> </button> ``` ## Reduced Motion Respect users who have requested reduced motion in their OS settings. ```tsx export function useReducedMotion(): boolean { const [prefersReduced, setPrefersReduced] = useState(false); useEffect(() => { const mq = window.matchMedia('(prefers-reduced-motion: reduce)'); setPrefersReduced(mq.matches); const handler = (e: MediaQueryListEvent) => setPrefersReduced(e.matches); mq.addEventListener('change', handler); return () => mq.removeEventListener('change', handler); }, []); return prefersReduced; } // Usage export function AnimatedCard({ children }: { children: React.ReactNode }) { const reduceMotion = useReducedMotion(); return ( <div style={{ transition: reduceMotion ? 'none' : 'transform 300ms ease' }} > {children} </div> ); } ``` ## Anti-Patterns ```tsx // BAD: onClick on non-interactive element with no keyboard support <div onClick={handleClick}>Click me</div> // BAD: aria-label on a div that has no role <div aria-label="Navigation">...</div> // BAD: placeholder used as a substitute for label <input placeholder="Enter your email" /> // BAD: positive tabIndex creates unpredictable tab order <button tabIndex={3}>Submit</button> // BAD: aria-hidden on a focusable element — keyboard users get trapped <button aria-hidden="true">Open</button> // BAD: role="button" on div without keyboard handler <div role="button" onClick={handleClick}>Submit</div> // Missing: tabIndex={0}, onKeyDown for Enter/Space ``` ## Checklist Before submitting any interactive component for review: - [ ] Every `<input>`, `<select>`, and `<textarea>` has a connected `<label>` via `htmlFor`/`id` - [ ] Error messages are linked with `aria-describedby` and marked `role="alert"` - [ ] No `onClick` on `<div>` or `<span>` without `role`, `tabIndex`, and `onKeyDown` - [ ] Icon-only buttons have `aria-label` - [ ] Decorative images use `alt=""` and `aria-hidden="true"` - [ ] Modals restore focus on close (for full focus trapping with Tab/Shift+Tab cycling, use a library like `focus-trap-react`) - [ ] Dynamic content updates use `aria-live` - [ ] `prefers-reduced-motion` is respected for animations ## Related Skills - `frontend-patterns` — general React component and state patterns - `design-system` — design token and component consistency - `motion-foundations` and `motion-patterns`: animation patterns with accessibility considerations

相关技能

Agent 工作流

技能创建者Skill Creator

创造有效技能指南。 当用户想创造出新的技能(或更新现有的技能),以专业知识,工作流程,或工具集成来扩展克洛德的能力时,应该使用这种技能.

Agent 工作流

克劳德胡布Clawdhub

使用ClawdHub CLI搜索,安装,更新并发布从taladhub.com的代理技能. 需要获取苍蝇上的新技能时使用,将安装的技能同步到最新版本或特定版本,或者发布 npm-instainddhub CLI 的新/更新的技能文件夹.

Agent 工作流

团队指挥Agent Team Orchestration

管弦乐团多代理团队,任务设定周期,交接协议,审查工作流程. 使用时间: (1)建立2+特派员队伍,具有不同专业,(2)确定任务路线和生命周期(收录框_ spec_建设_审查_完成),(3)在特派员之间制定交接协议,(4)建立审查和质量关口,(5)管理特派员之间的交流和文物共享.

Agent 工作流

超级力量Superpowers

Spec-first,TDD,子代理驱动的软件开发工作流程. 当:(1)构建任何新功能或应用——触发脑暴_计划_子代理执行回路,(2)调试出一个bug或测试失败——触发系统性的根起过程,(3)用户说"让我们构建","帮助我计划","我想添加X",或"这个被打破",(4)完成一个功…