← Back to Chapters

Accessibility (ARIA Roles in React)

♿ Accessibility (ARIA Roles in React)

? Quick Overview

Accessibility (a11y) ensures that all users — including those with disabilities — can use web applications effectively. In React, accessibility is achieved using semantic HTML, keyboard navigation, and ARIA roles.

? Key Concepts

  • ARIA roles add meaning to non-semantic elements
  • Screen readers rely on roles and labels
  • Keyboard navigation is mandatory for accessibility
  • React supports accessibility but developer intent matters

? Syntax / Theory

ARIA stands for Accessible Rich Internet Applications. It provides additional semantic information for assistive technologies.

? View Code Example
// Basic ARIA role and label examples
<button aria-label="Close Modal">❌</button>
<nav role="navigation">...</nav>
<div role="alert">Error: Please fill all fields.</div>

? Code Example: Accessible Modal

? View Code Example
// React modal with proper ARIA roles and labels
function AccessibleModal({ onClose }) {
return (
<div
role="dialog"
aria-modal="true"
aria-labelledby="modalTitle"
aria-describedby="modalDescription"
className="modal"
>
<h2 id="modalTitle">Delete File</h2>
<p id="modalDescription">Are you sure you want to delete this file?</p>
<button onClick={onClose} aria-label="Close modal">Cancel</button>
<button className="btn btn-danger">Delete</button>
</div>
);
}

? Explanation

ARIA attributes help screen readers identify the modal as a dialog, associate its title and description, and guide user interaction correctly.

? Keyboard Accessibility

? View Code Example
// Keyboard accessible div behaving like a button
<div
role="button"
tabIndex="0"
onClick={handleClick}
onKeyDown={(e) => e.key === "Enter" && handleClick()}
>
Press Enter or Click
</div>

? React Tools for Accessibility

? View Code Example
// Install accessibility linting plugin
npm install eslint-plugin-jsx-a11y --save-dev

? Interactive Concept

Try navigating your app using only the keyboard (Tab, Enter, Space). Notice how ARIA roles improve clarity when screen readers are enabled.

? Live Demo: Custom ARIA Switch

This component is built using a <div> instead of a checkbox. Without ARIA and proper event handling, it would be invisible to screen readers and keyboard users.

 
Dark Mode
Current State: Off

? Click it or focus using Tab and press Space/Enter.

? Use Cases

  • Accessible modals and dialogs
  • Custom buttons and dropdowns
  • Form validation and alerts
  • Navigation menus for screen readers

? Tips & Best Practices

  • Prefer semantic HTML over ARIA when possible
  • Always provide accessible labels
  • Test using keyboard-only navigation
  • Ensure sufficient color contrast
  • Never hide important info without ARIA alternatives

? Try It Yourself

  1. Add role="dialog" to a modal
  2. Use aria-labelledby for headings
  3. Test using NVDA or VoiceOver
  4. Fix issues reported by eslint-plugin-jsx-a11y

Goal: Build inclusive React applications accessible to everyone.