HomeBlogGitHubLinkedIn

Improving Accessibility in React Applications

26 March, 2023 | ⏳ 10 min read

Accessibility is an essential consideration for any web application, as it ensures that users with disabilities can access and use your site or app. Accessibility standards and guidelines aim to create an inclusive web experience, ensuring that everyone can access and use digital content regardless of their abilities.

In this post, we'll explore how to improve accessibility in React applications, covering the basics of accessibility in React, best practices for keyboard navigation, ARIA attributes, and semantic HTML markup. We'll also cover common accessibility pitfalls to avoid and tools for testing and auditing your app's accessibility.

Understanding Accessibility in React

Before we dive into specific techniques for improving accessibility in React, let's take a moment to understand what accessibility means in the context of React applications.

At its core, accessibility is about ensuring that your app is usable by as many people as possible, regardless of their abilities. For example, people with visual impairments may use screen readers to navigate the web, while people with mobility impairments may use keyboard navigation instead of a mouse.

In the context of React, accessibility is about ensuring that your app is accessible to these users by providing a user interface that is both understandable and navigable by screen readers, keyboard-only users, and other assistive technologies.

To make your React app accessible, you'll need to focus on several key areas, including keyboard navigation, ARIA attributes, and semantic HTML markup.

Best Practices for Keyboard Navigation

Keyboard navigation is an essential aspect of accessibility in React applications, as many users rely on it to navigate through your app without a mouse. To make your app keyboard-accessible, you'll need to ensure that all interactive elements are focusable and that users can navigate through them using the keyboard.

Here are some best practices for keyboard navigation in React:

  • Use the tabIndex attribute to make elements focusable: By default, not all HTML elements are focusable using the keyboard. However, you can use the tabIndex attribute to make any element focusable, including custom React components. Just be sure to set it to 0 to ensure that it can be focused.

  • Provide a visible focus indicator: When a user navigates through your app using the keyboard, it's essential to provide a visible indication of which element is currently focused. You can do this by applying a CSS style to the focused element, such as a colored border or background.

  • Ensure that keyboard navigation follows a logical order: Users should be able to navigate through your app using the keyboard in a logical order that makes sense. You can ensure this by setting the tabIndex attribute on elements in the correct order and by following the visual layout of your app.

  • Use the aria-label attribute to provide context: For elements that don't have a visible label, such as icons or buttons with only an icon, use the aria-label attribute to provide a context-specific label that screen readers can read aloud.

import React, { useState } from 'react';

function DropdownMenu() {
  const [isOpen, setIsOpen] = useState(false);

  const toggleMenu = () => setIsOpen(!isOpen);

  return (
    <div>
      <button onClick={toggleMenu}>Menu</button>
      {isOpen && (
        <ul>
          <li><a href="#">Option 1</a></li>
          <li><a href="#">Option 2</a></li>
          <li><a href="#">Option 3</a></li>
        </ul>
      )}
    </div>
  );
}

In this example, we create a dropdown menu that can be toggled open and closed using a button. We use the useState hook to keep track of the menu's open/closed state.

To ensure keyboard accessibility, we use the onClick event handler for the button. This makes the menu accessible to users who may not be able to use a mouse.

Using ARIA Attributes

ARIA (Accessible Rich Internet Applications) attributes are a set of attributes that you can use to make your React app more accessible to screen reader users. ARIA attributes provide additional context and semantics to HTML elements that aren't covered by standard HTML attributes.

Here are some common ARIA attributes that you can use to improve accessibility in your React app:

  • aria-label: This attribute provides a text label that screen readers can read aloud to describe the purpose or function of an element. Use it for elements that don't have visible text labels.

  • aria-describedby: Use this attribute to provide a longer description of an element, such as a tooltip or help text, that screen readers can read aloud.

  • aria-expanded: Use this attribute to indicate whether a collapsible element, such as a menu or accordion, is currently expanded or collapsed.

  • aria-haspopup: Use this attribute to indicate that an element, such as a menu or dialog, has additional content or options that are hidden until triggered.

  • aria-hidden: Use this attribute to indicate that an element should be hidden from screen readers and other assistive technologies. This is useful for decorative elements that don't provide any functional value to users.

  • aria-live: Use this attribute to indicate that an element, such as a status message or a live region, should be read aloud by screen readers as soon as it changes.

It's important to note that while ARIA attributes can be helpful in improving accessibility, they should only be used where necessary. Overuse of ARIA attributes can actually make your app less accessible, as it can confuse screen readers and other assistive technologies.

import React from 'react';

function Button() {
  return (
    <button aria-label="Close" aria-describedby="description">
      <span aria-hidden="true">&times;</span>
      <span id="description">Close</span>
    </button>
  );
}

In this example, we create a close button that uses ARIA attributes to provide additional accessibility information.

We use the aria-label attribute to provide a text alternative for the button that is read by screen readers. We also use the aria-describedby attribute to provide a description of the button that is read by screen readers when the button is focused.

Finally, we use the aria-hidden attribute to hide the close icon from screen readers, as it doesn't provide any additional information.

Semantic HTML Markup

Semantic HTML markup refers to using HTML elements in a way that accurately describes their purpose and meaning. Semantic HTML markup is essential for accessibility, as it provides a clear and understandable structure to your app that can be navigated by screen readers and other assistive technologies.

Here are some best practices for semantic HTML markup in React:

  • Use heading elements to create a hierarchical structure: Use h1 for the main heading of a page or section, h2 for subheadings, and so on. This creates a clear hierarchy that can be navigated by screen readers and keyboard-only users.

  • Use lists for navigation menus: Use ul and li elements to create navigation menus, as this provides a clear structure that can be navigated by screen readers and keyboard-only users.

  • Use form elements for form inputs: Use label, input, and other form elements to create accessible forms that can be navigated by screen readers and keyboard-only users.

  • Use semantic elements for sectioning: Use section, article, aside, and other semantic HTML elements to create a clear structure for your app that can be navigated by screen readers and keyboard-only users.

  • Avoid using non-semantic elements for layout: Avoid using div elements for layout purposes, as this can make your app less accessible by creating a confusing structure that can't be navigated by screen readers and keyboard-only users.

Accessibility Testing and Auditing

Finally, it's essential to test and audit your React app for accessibility issues to ensure that it's accessible to as many users as possible. There are several tools and techniques that you can use to test and audit your app's accessibility, including:

  • Automated testing tools: Use automated testing tools like the axe-core library to automatically test your app for accessibility issues. These tools can identify common accessibility issues like missing alt text, empty links, and invalid ARIA attributes.

  • Manual testing: Conduct manual testing using a screen reader or keyboard-only navigation to identify any issues that automated testing may have missed. This can be time-consuming, but it's an essential part of ensuring that your app is accessible.

  • Accessibility auditing: Conduct an accessibility audit of your app using the Web Content Accessibility Guidelines (WCAG) 2.1 as a reference. This can help you identify areas where your app may fall short of accessibility standards and provide guidance on how to improve.

By following these best practices and testing techniques, you can ensure that your React app is accessible to as many users as possible, regardless of their abilities.

In conclusion, accessibility is an essential consideration for any web application, and it's essential to ensure that your React app is accessible to as many users as possible. By following these best practices for keyboard navigation, ARIA attributes, semantic HTML markup, and accessibility testing and auditing, you can create a more inclusive app that can be used by all users, regardless of their abilities.

One additional consideration for accessibility is providing alternative ways to access content for users who may not be able to use a traditional keyboard or mouse. This includes users with mobility impairments, visual impairments, or other disabilities that may affect their ability to use standard input devices.

One solution is to provide alternative input devices, such as switch devices or sip-and-puff devices, that can be used by users with mobility impairments. Another solution is to provide alternative navigation methods, such as voice recognition or eye-tracking software, that can be used by users with visual impairments or other disabilities.

It's also important to ensure that your app is compatible with assistive technologies, such as screen readers, braille displays, and magnifiers. This means using standard HTML markup and avoiding proprietary technologies that may not be compatible with assistive technologies.

In addition to these technical considerations, it's also important to provide documentation and support for users with disabilities. This includes providing clear instructions on how to use your app with assistive technologies, as well as providing contact information for support and feedback.

By taking a holistic approach to accessibility, you can create a more inclusive and user-friendly React app that can be used by all users, regardless of their abilities. While it may require additional effort and resources, the benefits of accessibility are well worth it, both for your users and for your business.

Automated Testing

import React from 'react';
import { axe } from 'jest-axe';

describe('MyComponent', () => {
  test('it should be accessible', async () => {
    const { container } = render(<MyComponent />);
    const results = await axe(container);
    expect(results).toHaveNoViolations();
  });
});

In this example, we use the axe library to automatically test our React component for accessibility issues.

We use the render function from the @testing-library/react library to render our component. We then pass the container to the axe function to test for accessibility issues.

Finally, we use the toHaveNoViolations matcher from the jest-axe library to ensure that there are no accessibility violations in our component.

© crafted by borzeckid using gatsby