Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 12

Building Accessible Component Libraries

Recommended resources

Welcome back. In our previous lesson, we designed a comprehensive theming system that gracefully handles light/dark modes, multiple brands, and RTL layouts. By embedding logical properties and a token-based architecture into our design system's core, we made our UI visually and structurally inclusive for a global audience. This lesson builds directly on that foundation, moving from the inclusivity of appearance to the inclusivity of interaction.

Today, we will integrate accessibility into the very DNA of our component library. For a senior engineer, accessibility isn't a checklist to be run before a release; it's a fundamental architectural pillar. Getting it right at the design system level prevents entire classes of bugs, saves hundreds of developer hours, and ensures the products built on top are usable by everyone, including people who rely on assistive technologies. We will cover the core Web Content Accessibility Guidelines (WCAG), the role of ARIA for custom components, and a deep dive into the most critical and often overlooked aspect: focus management.

The Shift-Left Imperative: Accessibility as Architecture

At scale, the most expensive way to handle accessibility is to treat it as an afterthought. Auditing and fixing issues on a finished product is a slow, costly, and often incomplete process. A senior engineer understands that the design system is the highest-leverage place to solve this. By building accessibility into the foundational "atoms" of the UI, we ensure that every "molecule" and "organism" built from them inherits those robust characteristics.

The following presentation from axe-con, a major accessibility conference, provides a compelling argument for this "shift-left" approach, framing it within the context of atomic design.

Auditing Design Systems for Accessibility - axe-con 2021

This talk, "Auditing Design Systems for Accessibility," explains why the design system is the critical point for scaling accessibility and what happens when it's ignored.

First, watch the segment from the beginning, which introduces the core concept: a design system scales accessibility. Pay attention to how the atomic design methodology (atoms -> molecules -> organisms) provides a mental model for this. Next, see a practical example of how seemingly small issues at the atomic level compound into major problems across an entire page. Watch from this segment. The statistic mentioned here is crucial: a 2020 Deque case study found that 67% of accessibility issues originate in the design phase. This is the core justification for our architectural approach.

This "shift-left" mindset is what distinguishes a senior-level approach. It's not about just knowing the rules; it's about building a system that makes it easy for dozens or hundreds of developers to follow them by default.


The Accessibility Tree: A Parallel DOM

To understand how to build accessible components, we first need a clear mental model of how browsers expose information to assistive technologies like screen readers. When a browser parses HTML, it creates the familiar Document Object Model (DOM) Tree. In parallel, it creates an Accessibility Tree.

This diagram shows how HTML, CSS, and JavaScript are processed by the browser. They create the DOM Tree, which leads to the visual rendering for a sighted user. In parallel, they create the Accessibility Tree, which is consumed by assistive technologies to provide an interface for users with disabilities.

The Accessibility Tree is a simplified representation of the UI, containing only the elements that are relevant to an assistive technology user (e.g., text, buttons, links, form controls). It exposes four key pieces of information for each element:

  1. Role: What is this thing? (e.g., a "button", a "slider", a "checkbox").
  2. Name: What is it called? (e.g., the text inside a button, the aria-label attribute).
  3. State: What is its current condition? (e.g., "checked", "disabled", "expanded").
  4. Value: What is its value? (e.g., the text in an input field, the value of a slider).

For standard HTML elements like <button> or <input type="checkbox">, the browser handles this automatically. The role, name, and state are derived directly from the element and its attributes. The challenge arises when we build custom components that have no native HTML equivalent. This is where ARIA comes in.


The "What": Using ARIA to Patch the Accessibility Tree

ARIA (Accessible Rich Internet Applications) is a set of attributes you can add to HTML elements. Its primary purpose is to "patch" the Accessibility Tree, providing the correct role, state, and property information for custom components when native HTML is not sufficient.

The first rule of ARIA is: don't use ARIA. If a native HTML element exists that provides the semantics you need, always use it. For example, never build a button out of a <div> with role="button" when you can just use a <button>.

However, for complex components like modals, custom dropdowns, or tab lists, ARIA is essential. Let's use the example of an accessible modal dialog, a component you've likely built many times.

Building Accessible Modal Components in React: A Complete Guide to Focus Management and WCAG Compliance | AWS Builder Center

This AWS Builder Center guide provides an excellent, production-ready walkthrough of an accessible modal. We will use it to explore both ARIA attributes and focus management.

First, read the section ARIA Screen Reader Excellence. This provides a perfect summary of the essential attributes for a modal dialog. Then, look at the section Tabbed Interface. Notice how it uses role="tab" and aria-selected to communicate the state of a nested component within the modal.

The key ARIA attributes for a modal are:

  • role="dialog": Announces to the screen reader that this container is a dialog window that separates content or interaction from the rest of the page.
  • aria-modal="true": Informs the assistive technology that the content outside the dialog is inert and should be ignored while the dialog is open.
  • aria-labelledby="id_of_title": Connects the dialog to its visible title, which becomes the dialog's accessible name.
  • aria-describedby="id_of_description": (Optional) Connects the dialog to a description element, providing additional context.

By adding these attributes, you are directly manipulating the Accessibility Tree to ensure a screen reader user understands the purpose and context of your custom component, just as a sighted user would from its visual presentation.


The "How": A Deep Dive into Focus Management

This is arguably the most critical part of accessible component design and a common failure point. Proper focus management ensures that keyboard-only users and screen reader users can navigate and interact with your application predictably. For a modal component, there are three non-negotiable rules:

  1. Initial Focus: When the modal opens, focus must move inside it to a logical element (e.g., the first focusable element, or the close button).
  2. Focus Trapping (or "Focus Jail"): While the modal is open, the Tab and Shift+Tab keys must cycle through focusable elements only within the modal. Focus must not escape to the underlying page.
  3. Focus Restoration: When the modal closes, focus must return to the element that originally triggered its opening (e.g., the "Open Modal" button).

The following diagram perfectly illustrates this flow.

This diagram shows the correct focus management lifecycle for a modal. The flow starts with the "Open Modal" trigger button. When the dialog opens, focus moves to the first input inside. Pressing Tab cycles through the elements within the modal. Pressing Escape closes it. Finally, focus returns to the original trigger button.

Implementing this from scratch is complex. You need to track the previously focused element, handle keydown events to trap the tab key, and find the first and last focusable elements within the modal. Let's look at a practical implementation.

Building Accessible Modal Components in React: A Complete Guide to Focus Management and WCAG Compliance | AWS Builder Center

Continuing with the AWS article, let's now focus on its implementation of focus management.

Start with the section on The Foundation of Accessibility. Note how it uses a useRef to store the previously active element and a useEffect hook to manage focus on open and close. Next, read the section on Focus Trapping. This shows the logic for capturing Tab key presses and manually redirecting focus to create the "focus jail."

Abstraction via Headless Components

As you can see, robust focus management involves a lot of imperative DOM manipulation, which can feel out of place in a declarative React world. This complexity is precisely why a new pattern of UI libraries has become dominant for building design systems: headless components.

A headless component library (like Radix, React Aria, or Headless UI) provides all the accessibility logic, keyboard interactions, and state management for a component, but no styles. It gives you unstyled, but fully accessible, primitives that you can style however you want.

React Aria, from Adobe, is arguably the most comprehensive. It's not just a component library; it's a collection of hooks and components designed to be the foundation of a design system. It handles the immense complexity of interactions and accessibility for you.

Why You Should Use React Aria Components...

This video gives a great overview of the value proposition of React Aria, showing just how much work goes into making even a simple component like a button truly accessible and consistent.

Watch the introduction from the beginning, which frames headless UI libraries as the ideal solution for custom design systems. Pay close attention to the deep dive on the button component, from user experience. The concept of onPress vs. onClick reveals the hidden complexity of normalizing mouse, touch, and keyboard events. Finally, watch the section on focus rings. This demonstrates a very subtle but important senior-level detail: showing focus rings only for keyboard interaction, not for mouse clicks, which improves the experience for all users.

For complex components that require robust focus management (modals, menus, popovers), React Aria provides a FocusScope component that handles all of this for you declaratively.

[PDF] Don't lose your focus; focus management strategies in React

This slide from a presentation by a React Aria engineer provides a concise summary of the FocusScope primitive.

Please review the slide titled FocusScope. Note its key capabilities: containing focus, restoring focus, and supporting autofocus. This is the declarative solution to the imperative code we saw in the AWS article.


Senior-Level Insight

A mid-level developer can add aria-label to an icon button after an accessibility audit flags it. A senior engineer, when tasked with building a design system, makes an architectural decision to adopt a headless component library like React Aria as the foundation. They understand that the complexity of cross-browser/cross-device focus, hover, press, and keyboard interaction management is a specialized domain. By building on top of primitives that have already solved these problems, they abstract this complexity away from product developers, dramatically increasing the quality and consistency of the entire application suite and making the "accessible path" the "easiest path."


Common Mistakes in Interviews

  • No Mention of Accessibility: Failing to bring up accessibility as a non-functional requirement when designing a UI-heavy system is a major red flag for a senior role.
  • The div Button: Proposing <div onClick={...}> for a clickable element. This shows a fundamental misunderstanding of semantics, focusability, and keyboard interaction.
  • Forgetting Focus Management: Describing a modal or popover but failing to mention the need to trap and restore focus. Be prepared to explain how you would do it (imperatively with event listeners or declaratively with a library).
  • Overusing ARIA: Suggesting ARIA attributes for problems that can be solved with semantic HTML. For example, using aria-hidden to hide something when conditional rendering would remove it from the DOM entirely.
  • Ignoring Disabled States: When asked about a disabled button, a junior answer is "add the disabled attribute." A senior answer might be, "The button is disabled because a form field is invalid. Instead of disabling the button, we should keep it enabled and, upon click, move focus to the invalid field with a clear error message. This provides more information to the user."

Conclusion

In this lesson, we embedded accessibility into the core of our component library architecture. We established that building on an accessible foundation is the only scalable way to create inclusive products. You learned about the Accessibility Tree and how ARIA serves as a patch to provide semantics for custom components. Most importantly, we took a deep dive into focus management—trapping, restoration, and initial placement—as a critical requirement for any interactive component. Finally, we identified headless component libraries as a powerful architectural pattern for building a design system that is accessible by default.

Key Takeaways:

  • Shift Left: Build accessibility into your design system "atoms" to ensure it scales.
  • The Accessibility Tree: Understand that you're building for two interfaces: the visual DOM and the semantic Accessibility Tree.
  • ARIA Patches, HTML Builds: Use native HTML first. Use ARIA to add semantics to custom components that have no native equivalent.
  • Master Focus Management: For any component that appears and disappears (modals, menus, popovers), you must handle initial focus, focus trapping, and focus restoration.
  • Leverage Headless Primitives: Don't reinvent the wheel. Use libraries like React Aria to handle the immense complexity of accessible interactions, allowing you to focus on styling and application logic.

In our next and final lesson for this module, we will address a crucial operational aspect of a design system at scale: designing a versioning and deprecation strategy. Now that we have a robust, themeable, and accessible component library, how do we evolve it and roll out changes to dozens of consuming teams without causing chaos?