Inclusiveness

Improving Accessibility in React

Tushar Kale TK
3 min readDec 12, 2023

Applications Accessibility, often abbreviated as “a11y,” is a crucial aspect of web development. It ensures that web applications are usable by everyone, including those with disabilities. In this blog post, we’ll explore key strategies to enhance accessibility in React applications.

1. Semantic HTML and ARIA:

Use semantic HTML elements to convey meaning and structure. For example, use <button> for buttons, <input> for form inputs, and <nav> for navigation. Additionally, leverage ARIA (Accessible Rich Internet Applications) attributes when necessary to enhance the semantics of custom components.

// Good example
<button onClick={handleClick} aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>

// Bad example (using a <div> instead of a button)
<div onClick={handleClick}>Close</div>

2. Focus Management:

Ensure that users can navigate your application using only a keyboard. Components that receive focus should have clear focus indicators. Use the tabindex attribute judiciously and manage focus changes appropriately, especially in modal dialogs.

// Example with focus management in a modal
const Modal = () => {
useEffect(() => {
const modal = document.getElementById('modal');
modal.focus();
}, []);

return (
<div id="modal" tabIndex={0}>
{/* Modal content */}
</div>
);
};

3. Readable Text and Contrast:

Maintain a readable font size and provide sufficient color contrast to ensure content is accessible to users with visual impairments. Tools like the Web Content Accessibility Guidelines (WCAG) offer guidelines for text contrast ratios.

/* Good example */
body {
font-size: 16px;
color: #333;
background-color: #fff;
}

/* Bad example (insufficient contrast) */
body {
font-size: 12px;
color: #666;
background-color: #f9f9f9;
}

4. Accessible Forms:

Label form elements appropriately, associate labels with form controls, and provide helpful error messages. Use the aria-invalid attribute to indicate invalid input.

// Example of an accessible form
<form>
<label htmlFor="username">Username:</label>
<input type="text" id="username" aria-describedby="username-error" aria-invalid="true" />
<div id="username-error" role="alert">Please enter a valid username.</div>
<button type="submit">Submit</button>
</form>

5. Screen Reader Testing:

Regularly test your application with screen readers to identify and address accessibility issues. Popular screen readers include VoiceOver (iOS), NVDA (Windows), and JAWS (Windows). Familiarize yourself with the screen reader’s navigation and interaction patterns.

6. React Accessibility Libraries:

Consider using React accessibility libraries like react-a11y, react-axe, or eslint-plugin-jsx-a11y to catch common accessibility issues during development. These tools can be integrated into your build process or used as browser extensions.

# Example of installing eslint-plugin-jsx-a11y
npm install eslint-plugin-jsx-a11y --save-dev

Conclusion:

Prioritizing accessibility in React applications is not only a moral imperative but also contributes to a better user experience for everyone. By following these best practices, you can create web applications that are inclusive and accessible to a diverse audience. Remember, accessibility is an ongoing process, and continually testing and refining your application is key to providing an inclusive user experience.

Hit the clap button if you find this post helpful also feel free to comment if you have any doubts.

Happy coding and building accessible React applications!

--

--

Tushar Kale TK
Tushar Kale TK

Written by Tushar Kale TK

Enthusiastic Software Developer. Specialised in both frontend and backend. Ready to help people to solve their problems in all aspects of web development.

No responses yet