Lesson 6 of 8
Accessibility - WCAG 2.1 in Practice
Accessibility (a11y) is non-negotiable for production applications - it's a legal requirement in many jurisdictions (ADA, WCAG 2.1 AA) and improves UX for all users. The key areas: semantic HTML, ARIA where HTML is insufficient, focus management, color contrast, and testing with automated tools.
WCAG 2.1 AA Implementation Checklist
// ---- 1. Semantic HTML first (zero-cost accessibility) ----
// ❌ div soup
<div onClick={handleSubmit} className="button">Submit</div>
// ✅ semantic button
<button type="submit">Submit</button>
// Why: screen readers announce "Submit, button" - users know to press Enter/Space
// ---- 2. ARIA - use only when HTML is insufficient ----
// Live regions - announce dynamic content changes
<div aria-live="polite" aria-atomic="true">
{status === 'loading' ? 'Loading...' : 'Data loaded'}
</div>
// aria-describedby - link extra description to form field
<label htmlFor="pwd">Password</label>
<input id="pwd" type="password" aria-describedby="pwd-hint pwd-error" />
<p id="pwd-hint">Must be 8+ characters with uppercase and number</p>
<p id="pwd-error" role="alert">{errors.password?.message}</p>
// aria-expanded - for accordion/dropdown toggles
<button aria-expanded={isOpen} aria-controls="menu-list">
Menu ▼
</button>
<ul id="menu-list" hidden={!isOpen}>...</ul>
// ---- 3. Color contrast ----
// WCAG AA: 4.5:1 ratio for normal text, 3:1 for large text
// Tools: WebAIM Contrast Checker, axe DevTools
// ❌ #999 text on #fff ← only 2.85:1
// ✅ #595959 text on #fff ← 7:1
// ---- 4. Focus indicators ----
// Never: outline: none without replacement
button:focus {
outline: 2px solid #229c6b; /* visible blue ring */
outline-offset: 2px;
}
/* Modern approach - only show for keyboard users */
button:focus-visible {
outline: 2px solid #229c6b;
outline-offset: 2px;
}
button:focus:not(:focus-visible) {
outline: none; /* mouse users see no ring */
}
// ---- 5. Skip link - keyboard shortcut to content ----
<a href="#main" className="sr-only focus:not-sr-only">
Skip to main content
</a>
<main id="main">...</main>
// ---- 6. Alt text strategy ----
<img src="hero.jpg" alt="Team celebrating product launch" /> {/* descriptive */}
<img src="decorator.svg" alt="" role="presentation" /> {/* decorative */}
<img src="logo.png" alt="Acme Inc logo" /> {/* functional */}
// ---- 7. Test with automated tools ----
// npm install -D @axe-core/react
import React from 'react';
if (process.env.NODE_ENV !== 'production') {
const axe = require('@axe-core/react');
axe(React, ReactDOM, 1000); // logs a11y violations to console
}
// ESLint plugin
// npm install -D eslint-plugin-jsx-a11y
// .eslintrc: { "extends": ["plugin:jsx-a11y/recommended"] }
// Storybook addon-a11y - see violations per componentTip
Tip
Practice Accessibility WCAG 21 in Practice in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Accessibility - WCAG 2.1 in Practice
Practice Task
Note
Practice Task - (1) Write a working example of Accessibility WCAG 21 in Practice from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Accessibility WCAG 21 in Practice is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?